-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic_Tac_Toe.py
More file actions
73 lines (66 loc) · 1.96 KB
/
Tic_Tac_Toe.py
File metadata and controls
73 lines (66 loc) · 1.96 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
board = [" " for i in range(9)]
print(board)
def print_board():
row1 = "|{}|{}|{}|".format(board[0], board[1], board[2])
row2 = "|{}|{}|{}|".format(board[3], board[4], board[5])
row3 = "|{}|{}|{}|".format(board[6], board[7], board[8])
print()
print(row1)
print("-------")
print(row2)
print("-------")
print(row3)
print()
#print_board()
#Player Move
def player_move(icon):
if icon == "X":
number = 1
elif icon == "O":
number = 2
print("Your turn player {}".format(number))
#print_board()
choice = int(input("Enter your name(1-9):").strip())
if board[choice - 1] == " ":
board[choice -1] = icon
else:
print("That space is taken!")
def draw():
if " " not in board:
return True
else:
return False
#Winning Rules
def victory(icon):
if (board[0] == icon and board[1] == icon and board[2] == icon) or \
(board[3] == icon and board[4] == icon and board[5] == icon) or \
(board[6] == icon and board[7] == icon and board[8] == icon) or \
(board[0] == icon and board[3] == icon and board[6] == icon) or \
(board[1] == icon and board[4] == icon and board[7] == icon) or \
(board[2] == icon and board[5] == icon and board[8] == icon) or \
(board[0] == icon and board[4] == icon and board[8] == icon) or \
(board[2] == icon and board[4] == icon and board[6] == icon):
return True
else:
return False
#The game loop
while True:
print_board()
player_move("X")
print_board()
if victory("X"):
print("X wins! Congratulations!")
break
elif draw():
print_board()
print("Its a draw :(")
break
player_move("O")
if victory("O"):
print_board()
print("O wins! Congratulations!")
break
elif draw():
print_board()
print("Its a draw :(")
break