-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
129 lines (114 loc) · 3.76 KB
/
tictactoe.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
dictDactDoe = {
'tl': ' ', 't': ' ', 'tr': ' ',
'l': ' ', 'm': ' ', 'r': ' ',
'bl': ' ', 'b': ' ', 'br': ' '
}
def mark(dictDactDoe, pos, sign):
"""
assigns a mark on dictDactDoe
:param dictDactDoe: dict
:param pos: str
:param sign: str
:return: bool
"""
if dictDactDoe[pos] == ' ':
dictDactDoe[pos] = sign
return True
else:
print("You can't replace another mark. Please pick another position.")
return False
# testMark
# print(dictDactDoe)
# mark(dictDactDoe, 'l', 'o')
# print(dictDactDoe)
def showGrid(dictDactDoe):
"""
prints the tic-tac-toe grid
:param dictDactDoe: dict
:return: None
"""
count = 0
for val in dictDactDoe.values():
count += 1
print(val, end='')
if count == 3 or count == 6 or count == 9:
print()
if count == 3 or count == 6:
print('---------')
else:
print(' | ', end='')
print()
def isOver(dictDactDoe):
"""
returns win, draw or not over
:param dictDactDoe: dict
:return: str
"""
# shortcuts
tl = dictDactDoe['tl']
t = dictDactDoe['t']
tr = dictDactDoe['tr']
l = dictDactDoe['l']
m = dictDactDoe['m']
r = dictDactDoe['r']
bl = dictDactDoe['bl']
b = dictDactDoe['b']
br = dictDactDoe['br']
# boolean variables
row1 = (tl == 'X' and t == 'X' and tr == 'X') or (tl == 'O' and t == 'O' and tr == 'O')
row2 = (l == 'X' and m == 'X' and r == 'X') or (l == 'O' and m == 'O' and r == 'O')
row3 = (bl == 'X' and b == 'X' and br == 'X') or (bl == 'O' and b == 'O' and br == 'O')
col1 = (tl == 'X' and l == 'X' and bl == 'X') or (tl == 'O' and l == 'O' and bl == 'O')
col2 = (t == 'X' and m == 'X' and b == 'X') or (t == 'O' and m == 'O' and b == 'O')
col3 = (tr == 'X' and r == 'X' and br == 'X') or (tr == 'O' and r == 'O' and br == 'O')
diag1 = (tl == 'X' and m == 'X' and br == 'X') or (tl == 'O' and m == 'O' and br == 'O')
diag2 = (tr == 'X' and m == 'X' and bl == 'X') or (tr == 'O' and m == 'O' and bl == 'O')
draw = tl != ' ' and t != ' ' and tr != ' ' and l != ' ' and m != ' ' and r != ' ' and bl != ' ' and b != ' ' and br != ' '
# final statement
if row1 or row2 or row3 or col1 or col2 or col3 or diag1 or diag2:
return 'win'
elif draw:
return 'draw'
else:
return 'notOver'
def isDraw(dictDactDoe):
draw = tl != ' ' and t != ' ' and tr != ' ' and l != ' ' and m != ' ' and r != ' ' and bl != ' ' and b != ' ' and br != ' '
return draw
def playGame(dictDactDoe):
playerTurn = 1
while isOver(dictDactDoe) == 'notOver':
# assigning sign to players
if playerTurn == 1:
sign = 'X'
else:
sign = 'O'
showGrid(dictDactDoe)
print("Player " + str(playerTurn) + "'s turn.")
while True:
pos = input("Enter the position in the grid: ")
if mark(dictDactDoe, pos, sign) == True:
break
# change playerTurn
if playerTurn == 1:
playerTurn = 2
else:
playerTurn = 1
# change playerTurn after game ends
showGrid(dictDactDoe)
if isOver(dictDactDoe) == 'win':
if playerTurn == 1:
playerTurn = 2
else:
playerTurn = 1
print("Player " + str(playerTurn) + " wins!")
else:
print("It's a draw.")
# gotta make sure no one replaces
# showGrid(dictDactDoe)
# mark(dictDactDoe, 'l', 'X')
# mark(dictDactDoe, 'm', 'X')
# mark(dictDactDoe, 'r', 'X')
#
# showGrid(dictDactDoe)
# print(isOver(dictDactDoe))
playGame(dictDactDoe)