-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.py
More file actions
121 lines (89 loc) · 2.37 KB
/
TicTacToe.py
File metadata and controls
121 lines (89 loc) · 2.37 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
def checkwin(player):
if diagcheck(player):
return True
if rowcheck(player):
print('comes here')
return True
if colcheck(player):
print('comes here')
return True
def diagcheck(player):
leftright = None
rightleft = None
for i in range(0,size):
if board[i][i] != player:
leftright = False
for row in range(0,size):
for j in range(size-1, -1, -1):
if board[row][j] != player:
rightleft = False
if rightleft or leftright:
return True
else:
return False
def rowcheck(player):
row_check_result = [True]*size
for row in range(size):
for col in range(size):
if board[row][col] != player:
row_check_result[row] = False
break
row_check_result[row] = True
for item in row_check_result:
if item:
return True
return False
def colcheck(player):
for col in range(0,size):
for row in range(0,size):
if baord[row][col] != player:
break
return True
return False
def checkFinish():
for i in range(0, size):
for j in range(0,size):
if board[i][j] == 0:
return False
return True
size = int(input('What size of the board do you want?'))
board = [[0 for i in range(0,size)]for j in range(size)]
print(board)
for i in range(0,size):
for j in range(0,size):
board[i][j] = 0
p1 = input('Player1 enter your name')
p2= input('Player2 enter your name')
gameDone = False
p1flag = 1
winner = None
drawgame = False
while not gameDone:
row = int(input('Enter your row:'))
col = int(input('Enter your col:'))
if board[row-1][col-1] == 0:
if p1flag:
board[row-1][col-1] = 1
p1flag = 0
print(board)
if checkwin(1):
winner = p1
break
else:
board[row-1][col-1] = 5
p1flag = 1
print(board)
if checkwin(5):
winner = p2
break
else:
print('Slot already filled, can\'t take this move')
if checkFinish():
gameDone = True
if drawgame:
print('draw game')
else:
if winner == p1:
print('winner is {}'.format(p1))
else:
print('winner is {}'.format(p2))