-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
110 lines (95 loc) · 2.37 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
# board = [{0:0,1:0,2:1},{0:1,1:1,2:3},{0:1,1:1,2:3}]
size = int(input('How large do you want the board?'))
print(size)
board = []
for x in xrange(size):
row = {}
k = 0
for x in xrange(size):
row[k] = 0
print(row)
board.append(row)
end = False
players = {1:'X',2:'O'}
turn = 0
def draw(brd):
global start
global players
for l in brd:
row = ''
for i in l:
for p in players:
if l[i] == p:
row = row + players[p]
if l[i] == 0:
row = row + ' '
print(row)
def checkwin(brd):
global end
global players
playerwin = {}
for p in players:
playerwin[p] = 0
#Check if Rows allow for win
rowwin = {}
for p in players:
rowwin[p] = True
for l in brd:
if l[0] == p:
for i in l:
if l[i] != p:
rowwin[p] = False
else:
rowwin[p] = False
playerwin[p] += rowwin[p]
# print(rowwin)
# Check if Column allows for win
colwin = {}
for p in players:
colwin[p] = True
col = 0
while col <= len(brd[0]) - 1:
if brd[0][col] == p:
for l in brd:
if l[col] != p:
colwin[p] = False
else:
colwin[p] = False
playerwin[p] += colwin[p]
col += 1
# print(colwin)
# Check if diangnal allows for win
diwin = {}
for p in players:
diwin[p] = True
if brd[0][0] == p:
di = 0
for l in brd:
if l[di] != p:
diwin[p] = False
di += 1
else:
diwin[p] = False
playerwin[p] += diwin[p]
# print(diwin)
# Check if diangnal allows for win
diwin2 = {}
for p in players:
diwin2[p] = True
if brd[0][len(brd[0]) - 1] == p:
di = 0
for l in brd:
if l[len(brd[0]) - 1 - di] != p:
diwin2[p] = False
di += 1
else:
diwin2[p] = False
playerwin[p] += diwin2[p]
# print(diwin2)
#declare if there was a winner
for p in players:
if playerwin[p] > 0:
print(players[p] + 's Won!')
end = True
draw(board)
checkwin(board)