-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
62 lines (51 loc) · 1.87 KB
/
tic_tac_toe.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
class TicTacToe:
def __init__(self, n):
self.rows = [0]*n
self.cols = [0]*n
self.diagonal = 0
self.anti_diagonal = 0
self.size = n
self.winner = None
self.moves_left = n*n
def move(self, row, col, player):
self.moves_left -= 1
player_value = 1 if player == 'X' else -1
self.rows[row] += player_value
self.cols[col] += player_value
if row == col:
self.diagonal += player_value
if row + col == self.size - 1:
self.anti_diagonal += player_value
if (abs(self.rows[row]) == self.size or abs(self.cols[col]) == self.size or abs(self.diagonal) == self.size or abs(self.anti_diagonal) == self.size):
self.winner = player
return player
return -1
def is_game_over(self):
return self.winner is not None or self.moves_left == 0
n = 3
game = TicTacToe(n)
players = ['X', 'O']
current_player = 0
visited = set()
while not game.is_game_over():
while True:
try:
row = int(input(f"Player {players[current_player]}, enter row (0 to {n - 1}): "))
col = int(input(f"Player {players[current_player]}, enter column (0 to {n - 1}): "))
if 0 <= row < n and 0 <= col < n:
if (row, col) not in visited:
visited.add((row, col))
break
else:
print("This position is already chosen. Try again.")
else:
print(f"Invalid input! Row and column must be between 0 and {n - 1}. Try again.")
except ValueError:
print("Invalid input! Please enter numbers.")
win = game.move(row, col, players[current_player])
if win != -1:
print(f"Player {win} wins!")
break
current_player = 1 - current_player
else:
print("It's a draw!")