-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·76 lines (57 loc) · 1.63 KB
/
main.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
#!/usr/bin/python3
from game_backend import *
import minimax_bot
def main():
g = Game()
bot = minimax_bot.Minimax(g, False)
end = False
while not end:
bot_turn = input("Bot goes first? [y/n] : ")[0] == 'y'
bot.setTurn(bot_turn)
g.reset()
while g.state == GameState.NOT_DONE:
bot_turn = next_turn(g, bot, bot_turn)
print_board(g.board)
print_ending(g.state)
end = input("Go again? [y/n] : ")[0] == 'n'
def next_turn(game, bot, bot_turn):
if bot_turn:
row, column = bot.get_move()
print()
input("Press enter")
print()
print("Bot plays ({}, {})".format(row + 1, column + 1))
bot.move(row, column)
else:
print()
str_input = input("Your turn (row column) : ")
row, column = [int(x) for x in str_input.split()]
game.move(row - 1, column - 1)
return not bot_turn
def print_board(board):
print_row(board[0])
print('-+-+-')
print_row(board[1])
print('-+-+-')
print_row(board[2])
def print_row(row):
printing_row = []
for element in row:
if element is None:
printing_row.append(' ')
elif element:
printing_row.append('X')
else:
printing_row.append('O')
print('{}|{}|{}'.format(printing_row[0], printing_row[1], printing_row[2]))
def print_ending(state):
print()
if state == GameState.DRAW:
print("DRAW")
elif state == GameState.X_WIN:
print("X WINS")
elif state == GameState.Y_WIN:
print("O WINS")
print()
if __name__ == '__main__':
main()