-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
127 lines (117 loc) · 4.06 KB
/
engine.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from tictactoe import TicTacToe
class NotTwoPlayerGameError(Exception):
pass
def get_move(board: TicTacToe) -> tuple[int, int]:
"""
Gets the next move the engine would play in any scenario
:return: the move to be played in the form x,y
:rtype: tuple
"""
if not board.players == 2:
raise NotTwoPlayerGameError("The game must be 2 player if you want to use the engine")
if board.turn == 0:
return 1, 1
elif board.turn == 1:
if (2, 2) == board.moves[1]:
return 1, 1
else:
return 2, 2
elif board.turn == 2:
if (2, 1) == board.moves[1] or (1, 2) == board.moves[1] or (2, 3) == board.moves[1] or (3, 2) == board.moves[1]:
return 2, 2
elif (3, 1) == board.moves[1]:
return 1, 3
elif (1, 3) == board.moves[1] or (3, 3) == board.moves[1]:
return 3, 1
else:
return 3, 3
elif board.turn == 4:
if (1, 2) in board.moves[0]:
if (1, 3) in board.moves[1]:
return 2, 2
else:
return 1, 3
elif (2, 1) in board.moves[0]:
if (3, 1) in board.moves[1]:
if (2, 2) in board.moves[1]:
return 1, 3
else:
return 2, 2
else:
return 3, 1
elif (1, 3) in board.moves[0]:
if (1, 2) in board.moves[1]:
if (2, 3) in board.moves[1]:
return 2, 2
else:
return 1, 3
else:
return 1, 2
elif board.turn == 6:
if (1, 2) in board.moves[0] and (2, 2) in board.moves[0]:
if (3, 2) in board.moves[1]:
return 3, 3
else:
return 3, 2
elif (2, 1) in board.moves[0] and (2, 2) in board.moves[0]:
if (2, 3) in board.moves[1]:
return 3, 3
else:
return 2, 3
elif (2, 1) in board.moves[0] and (1, 3) in board.moves[0]:
if (1, 2) in board.moves[1]:
if (3, 2) in board.moves[1]:
return 1, 3
else:
return 3, 2
else:
return 1, 2
elif (2, 2) in board.moves[0] and (3, 1) in board.moves[0]:
if (2, 1) in board.moves[1]:
return 3, 1
else:
return 2, 1
elif (2, 1) in board.moves[0] and (2, 2) in board.moves[0]:
if (2, 3) in board.moves[1]:
return 3, 3
else:
return 2, 2
elif (3, 1) in board.moves[0] and (1, 3) in board.moves[0]:
if (1, 2) in board.moves[1]:
return 2, 2
else:
return 1, 2
else:
if (3, 3) in board.moves[1]:
return 2, 3
else:
return 3, 3
def play_x(board: TicTacToe) -> int:
"""
Plays the game until a player wins based on the win function
:return: The player that won
:rtype: int
"""
while not board.winner and not board.cats:
board.make_move(position=get_move(board))
print(f"Player: {board.get_symbol(board.turn % board.players + 1)}")
print(board.print_board())
try:
move = list(map(lambda x: int(x), input(">> ").replace("(", "").replace(")", "").split(",")))
except (TypeError, ValueError):
print("Please use numbers in the form (x,y)")
else:
if len(move) != 2:
print("Please make a move in the form (x,y)")
else:
while not board.make_move(position=tuple(move)):
move = list(map(lambda x: int(x), input(">> ").replace("(", "").replace(")", "").split(",")))
print("")
print(f"Player {board.get_symbol(player=board.winner)} wins!!")
print(board.print_board())
return (board.turn - 1) % board.players + 1 if not board.cats else 0
def main():
game = TicTacToe()
play_x(game)
if __name__ == '__main__':
main()