-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.py
94 lines (82 loc) · 2.57 KB
/
tic-tac-toe.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
import random
L = []
def display_board(board):
# The function accepts one parameter containing the board's current status
# and prints it out to the console.
for i in board:
for j in i:
print(j,end=' ')
print()
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.
while True:
um = int(input('Enter your move 1-9: '))
if um in range(1,10) and um not in L:
break
else:
print('Wrong move, try again!')
for i in range(len(board)):
for j in range(len(board[i])):
if um == board[i][j]:
board[i][j] = 'O'
L.append(um)
return display_board(board)
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
vl = []
global victory
for i in board:
for j in i:
if i[0] == i[1] == i[2]:
victory = True
for i in board:
vl.append(i[0])
if vl[0] == vl[1] == vl[2]:
victory = True
vl2 = [[board[0][0],board[1][1],board[2][2]],[board[2][0],board[1][1],board[0][2]]]
if vl2[0][0] == vl2[0][1] == vl2[0][2] or vl2[1][0] == vl2[1][1] == vl2[1][2]:
victory = True
if victory:
if sign == 'X':
print("Computer won!\nBetter luck next time.")
elif sign == 'O':
print("Congratulations! You won!")
elif len(L) == 9:
print("It's a tie!")
victory = 'tie'
def draw_move(board):
# The function draws the computer's move and updates the board.
while True:
cm = random.randint(1,9)
if cm not in L:
break
for i in range(len(board)):
for j in range(len(board[i])):
if cm == board[i][j]:
board[i][j] = 'X'
L.append(cm)
return display_board(board)
board = [[1,2,3],[4,5,6],[7,8,9]]
victory = False
while True:
print("***TIC-TAC-TOE***")
ans = input("Shall we play? (Y/N): ")
if ans not in 'Yy':
break
else:
display_board(board)
print("------")
while not victory or victory == 'tie':
draw_move(board)
print("------")
victory_for(board,'X')
if victory or victory == 'tie':
break
enter_move(board)
print("------")
victory_for(board,'O')
end = int(input("Enter 0 to exit: "))
if end == 0:
break