-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
188 lines (113 loc) · 3.99 KB
/
tictactoe.py
File metadata and controls
188 lines (113 loc) · 3.99 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import itertools
def state_board(game_map, player = 0, row = 0, col = 0, just_display = False):
try:
header = " " + " ".join([str(x) for x in range(len(game_map))])
if(game_map[row][col]) == 0:
game_map[row][col] = player
print(header)
for count, row in enumerate(board):
print(count, row)
global play_count
play_count += 1
if(play_count-1 == len(game_map)**2):
win_status = win_condition(game_map)
if not win_status:
print("Draw")
restart()
return game_map;
if(game_map[row][col] != 0):
current_player = next(player_cycle)
print(header)
for count, row in enumerate(board):
print(count, row)
print("This space is occupied")
return game_map;
return False;
except IndexError as e:
print(f"Error: Row/Column value must be between (0-{board_size - 1})", e)
current_player = next(player_cycle)
return False;
except Exception as e:
print("Something went wrong", e)
return False;
def check_win(row):
if row.count(row[0]) == len(row) and row[0] != 0:
# os.system('cls' if os.name == 'nt' else 'clear')
print(f"Winner! is player {row[0]}")
global player_cycle
player_cycle = itertools.cycle(players)
return True;
return False;
def win_condition(current_board):
#diagonal win
diag = []
for index in range(len(board)):
diag.append(board[index][index])
win = check_win(diag)
if(win):
return True;
diag = []
for col, row in enumerate(reversed(range(len(board)))):
diag.append(board[row][col])
win = check_win(diag)
if(win):
return True;
#horizantal win
for row in board:
win = check_win(row)
if(win):
return True;
#vertical win
for col in range(len(board)):
check = []
for row in board:
check.append(row[col])
win = check_win(check)
if(win):
return True;
return False;
def restart():
global play_count
play_count = 0
print(f"Do you want to play again? (y/n)")
response = input()
if(response == "y"):
global play
global game_won
global board
global board_size
play = True
game_won = False
board = [[0 for row in range(board_size)] for col in range(board_size)]
state_board(board, just_display = True)
player_cycle = itertools.cycle(players)
else:
print("Thanks for playing")
play = True
game_won = False
players = [1, 2]
play_count = 0
# os.system('cls' if os.name == 'nt' else 'clear')
board_size = int(input("Enter the size of the board: "))
if(board_size <=0):
print("Board size must be greater than 0")
play = False
board_size = int(input("Enter the size of the board: "))
board = [[0 for row in range(board_size)] for col in range(board_size)]
play = True
state_board(board, just_display = True)
player_cycle = itertools.cycle(players)
while play:
while game_won == False:
current_player = next(player_cycle)
print(f"Current Player is: {current_player}")
row_choice = int(input(f"Enter row choice between (0-{board_size-1}): "))
column_choice = int(input(f"Enter your column choice (0-{board_size-1}): "))
# os.system('cls' if os.name == 'nt' else 'clear')
state = state_board(board, current_player, row_choice, column_choice)
win_state = win_condition(state)
if(win_state):
play = False
game_won = True
restart()