-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac-toe.py
More file actions
159 lines (140 loc) · 5.55 KB
/
tic-tac-toe.py
File metadata and controls
159 lines (140 loc) · 5.55 KB
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
from time import sleep
from random import choice
def display_board(board):
# The function accepts one parameter containing the board's current status
# and prints it out to the console.
return '''
+--------+--------+--------+
| | | |
| {} | {} | {} |
| | | |
+--------+--------+--------+
| | | |
| {} | {} | {} |
| | | |
+--------+--------+--------+
| | | |
| {} | {} | {} |
| | | |
+--------+--------+--------+
'''.format(board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2])
def enter_move(board):
# The function accepts the board's current status, asks the user about their move,
# checks the input, and updates the board according to the user's decision.
valid = []
for x in range(len(board)):
for y in board[x]:
if y != 'x' and y != 'o':
valid.append(y)
print('valid moves ', valid)
try:
p_move = int(input('Enter your move: '))
for x in range(len(board)):
for y in board[x]:
if (p_move in valid) and (y == p_move):
row = x
col = board[x].index(y)
board[row][col] = 'o'
print(display_board(board))
return True
except:
print('Invalid Entry, ', p_move,' Miss a turn')
sleep(3)
print(display_board(board))
return False
def com_move(board):
valid = []
for x in range(len(board)):
for y in board[x]:
if y != 'x' and y != 'o':
valid.append(y)
print('valid moves ', valid)
c_move = choice(valid)
print('Computer moves: ',c_move)
sleep(2)
try:
for x in range(len(board)):
for y in board[x]:
if (c_move in valid) and (y == c_move):
row = x
col = board[x].index(y)
board[row][col] = 'x'
print(display_board(board))
return True
except:
print('Invalid Entry, ', c_move,' Miss a turn')
sleep(3)
print(display_board(board))
return True
def make_list_of_free_fields(board):
# The function browses the board and builds a list of all the free squares;
# the list consists of tuples, while each tuple is a pair of row and column numbers.
#valid = []
valid_t = []
for x in range(len(board)):
for y in board[x]:
if y != 'x' and y != 'o':
#valid.append(y)
row = x
col = board[x].index(y)
valid_t.append((row,col))
return valid_t
def victory_for(board, sign):
# The function analyzes the board's status in order to check if
# the player using 'O's or 'X's has won the game
if len(make_list_of_free_fields(board)) > 6:
return False
if sign == "o":
if board[0][0] == 'o' and board[1][0] == 'o' and board[2][0] == 'o': return True
if board[0][1] == 'o' and board[1][1] == 'o' and board[2][1] == 'o': return True
if board[0][2] == 'o' and board[1][2] == 'o' and board[2][2] == 'o': return True
if board[0][0] == 'o' and board[0][1] == 'o' and board[0][2] == 'o': return True
if board[1][0] == 'o' and board[1][1] == 'o' and board[1][2] == 'o': return True
if board[2][0] == 'o' and board[2][1] == 'o' and board[2][2] == 'o': return True
if board[0][0] == 'o' and board[1][1] == 'o' and board[2][2] == 'o': return True
if board[0][2] == 'o' and board[1][1] == 'o' and board[2][0] == 'o': return True
if sign == "x":
if board[0][0] == 'x' and board[1][0] == 'x' and board[2][0] == 'x': return True
if board[0][1] == 'x' and board[1][1] == 'x' and board[2][1] == 'x': return True
if board[0][2] == 'x' and board[1][2] == 'x' and board[2][2] == 'x': return True
if board[0][0] == 'x' and board[0][1] == 'x' and board[0][2] == 'x': return True
if board[1][0] == 'x' and board[1][1] == 'x' and board[1][2] == 'x': return True
if board[2][0] == 'x' and board[2][1] == 'x' and board[2][2] == 'x': return True
if board[0][0] == 'x' and board[1][1] == 'x' and board[2][2] == 'x': return True
if board[0][2] == 'x' and board[1][1] == 'x' and board[2][0] == 'x': return True
return False
def draw_move(board):
# The function draws the computer's move and updates the board.
if len(make_list_of_free_fields(board)) == 0:
return True
else: return False
def play_ttt():
board = [[1,2,3],[4,5,6],[7,8,9]]
status = True
print(display_board(board))
while status == True:
user = enter_move(board)
if user == True:
u_vic = victory_for(board, 'o')
if u_vic == True:
print('You Won')
status = False
continue
draw = draw_move(board)
if draw == True:
print('The game ended in a draw')
status = False
continue
com = com_move(board)
if com == True:
c_vic = victory_for(board, 'x')
if c_vic == True:
print('Computer Won')
status = False
continue
draw = draw_move(board)
if draw == True:
print('The game ended in a draw')
status = False
continue
play_ttt()