-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe2.py
175 lines (135 loc) · 4.37 KB
/
tic_tac_toe2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from IPython.display import clear_output
def display_board(board):
x=1
for i in range(1,6):
print('')
for k in range(0,11):
if i%2==0:
print('-',end='')
else :
if k in [0,2,4,6,8,10]:
print('-',end='')
elif k in [3,7]:
print('|',end='')
elif k in [1,5,9]:
print(board[x],end='')
x=x+1
def player_input():
x=True
while x :
player=input("Please input a marker 'X' or '0' :")
if player !='X' and player !='0' :
print('Wrong Input ,Enter again:')
else :
x=False
if player=='0':
p='X'
else :
p='0'
return (player,p)
pass
def place_marker(board, marker, position):
position=int(position)
board[position]=marker
pass
def win_check(board, mark):
for i in range(1,6):
list=[mark,mark,mark]
if board[1:4]==list or board[4:7]==list or board[7:] ==list or [board[1],board[4],board[7]]==list or [board[2],board[5],board[8]]==list or [board[3],board[6],board[9]]==list or [board[1],board[5],board[9]]==list or [board[3],board[5],board[7]]==list:
return True
pass
import random
def choose_first():
x= random.randint(0,100)
x=int(x)
if x%2==0:
return 'Player 1'
else:
return 'Player 2'
pass
def space_check(board, position):
position=int(position)
if board[position] == '0'or board[position] == 'X':
return False
else :
return True
pass
def full_board_check(board):
c=0
for position in range(1,10):
if board[position] == '0' or board[position] == 'X':
c=c+1
if c==9 :
return True
else:
return False
pass
def player_choice(board):
x=input('Enter next move:')
x=int(x)
y=space_check(board,x)
if y== True:
return x
pass
def replay():
x=input("Want to play again??? \n Press 1 if Yes:")
if x=='1':
return True
return False
pass
print('Welcome to Tic Tac Toe!')
#while True:
# Set the game up here
#pass
#while game_on:
#Player 1 Turn
# Player2's turn.
#pass
#if not replay():
#break
while True:
# Reset the board
theBoard = [' '] * 10
player1_marker, player2_marker = player_input()
turn = choose_first()
print(turn + ' will go first.')
play_game = input('Are you ready to play? Enter Yes or No.')
if play_game.lower()[0] == 'y':
game_on = True
else:
game_on = False
while game_on:
if turn == 'Player 1':
# Player1's turn.
display_board(theBoard)
position = player_choice(theBoard)
place_marker(theBoard, player1_marker, position)
if win_check(theBoard, player1_marker):
display_board(theBoard)
print('\n Congratulations! You have won the game!')
game_on = False
else:
if full_board_check(theBoard):
display_board(theBoard)
print('The game is a draw!')
break
else:
turn = 'Player 2'
else:
# Player2's turn.
display_board(theBoard)
position = player_choice(theBoard)
place_marker(theBoard, player2_marker, position)
if win_check(theBoard, player2_marker):
display_board(theBoard)
print('Player 2 has won!')
game_on = False
else:
if full_board_check(theBoard):
display_board(theBoard)
print('The game is a draw!')
break
else:
turn = 'Player 1'
if not replay():
break