-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py
More file actions
226 lines (195 loc) · 8.14 KB
/
app.py
File metadata and controls
226 lines (195 loc) · 8.14 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import queue
import threading
import time
from flask import Flask, request
from flask_cors import CORS, cross_origin
from src.board import Board
from src.colour import Colour
from src.compare_all_moves_strategy import CompareAllMovesSimple, \
CompareAllMovesWeightingDistanceAndSingles, \
CompareAllMovesWeightingDistanceAndSinglesWithEndGame, \
CompareAllMovesWeightingDistanceAndSinglesWithEndGame2
from src.strategies import MoveRandomPiece, MoveFurthestBackStrategy
from src.game import Game
from random import randint
from src.strategies import Strategy
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
moves_to_make = queue.Queue()
move_results = queue.Queue()
current_board = []
current_roll = []
used_die_rolls = []
def set_current_move(dice_roll):
current_roll.insert(0, dice_roll)
del current_roll[1:]
used_die_rolls.insert(0, [])
del used_die_rolls[1:]
def game_thread(difficulty):
class ApiStrategy(Strategy):
def __init__(self) -> None:
self.board_after_your_last_turn = Board.create_starting_board()
def move(self, board, colour, dice_roll, make_move, opponents_activity):
set_current_move(dice_roll.copy())
board_json_before_opp_move = self.board_after_your_last_turn.to_json()
def map_move(move):
self.board_after_your_last_turn.move_piece(
self.board_after_your_last_turn.get_piece_at(move['start_location']),
move['die_roll']
)
move['board_after_move'] = self.board_after_your_last_turn.to_json()
return move
print('[Game]: Sending opponents activity (end of previous turn, start of new turn)')
move_results.put({
'result': 'success',
'opponents_activity': {
'opponents_move': [map_move(move) for move in opponents_activity['opponents_move']],
'dice_roll': opponents_activity['dice_roll'],
},
'board_after_your_last_turn': board_json_before_opp_move,
})
while len(dice_roll) > 0:
print('[Game]: Waiting for moves_to_make...')
move = moves_to_make.get()
if move == 'end_game':
print('[Game]: ...got end_game, so crashing')
raise Exception("Game ended")
elif move == 'end_turn':
print('[Game]: ...got end_turn')
break
print('[Game]: ...got move')
try:
rolls_moved = make_move(move['location'], move['die_roll'])
for roll in rolls_moved:
dice_roll.remove(roll)
used_die_rolls[0].append(roll)
if len(dice_roll) > 0:
print('[Game]: Sending move success (middle of go)')
move_results.put({
'result': 'success'
})
except:
print('[Game]: Sending move failed')
move_results.put({
'result': 'move_failed'
})
self.board_after_your_last_turn = board.create_copy()
print('[Game]: Done last move of turn. Going to wait for opponent information')
def game_over(self, opponents_activity):
board_json_before_opp_move = self.board_after_your_last_turn.to_json()
def map_move(move):
self.board_after_your_last_turn.move_piece(
self.board_after_your_last_turn.get_piece_at(move['start_location']),
move['die_roll']
)
move['board_after_move'] = self.board_after_your_last_turn.to_json()
return move
print('[Game]: Sending opponents activity (end of game)')
move_results.put({
'result': 'success',
'opponents_activity': {
'opponents_move': [map_move(move) for move in opponents_activity['opponents_move']],
'dice_roll': opponents_activity['dice_roll'],
},
'board_after_your_last_turn': board_json_before_opp_move,
})
print(difficulty)
if difficulty == 'veryeasy':
opponent_strategy = MoveFurthestBackStrategy()
elif difficulty == 'easy':
opponent_strategy = CompareAllMovesSimple()
elif difficulty == 'medium':
opponent_strategy = CompareAllMovesWeightingDistanceAndSingles()
elif difficulty == 'hard':
opponent_strategy = CompareAllMovesWeightingDistanceAndSinglesWithEndGame()
elif difficulty == 'veryhard':
opponent_strategy = CompareAllMovesWeightingDistanceAndSinglesWithEndGame2()
else:
raise Exception('Not a valid strategy')
print('[Game]: Starting game with strategy %s' % opponent_strategy.__class__.__name__)
game = Game(
white_strategy=ApiStrategy(),
black_strategy=opponent_strategy,
first_player=Colour(randint(0, 1))
)
current_board.append(game.board)
game.run_game(verbose=False)
# Thread is only ended by an 'end_game' move
while True:
print('[Game]: run_game has completed, waiting for moves_to_make...')
if moves_to_make.get() == 'end_game':
print('[Game] ... got end_game (in final bit)')
break
else:
print('[Game] ... got non-end_game (in final bit)')
move_results.put({
'result': 'move_failed'
})
def get_state(response={}):
if len(current_board) == 0:
return {'board': "{}", 'dice_roll': [], 'used_rolls': []}
board = current_board[0]
move = current_roll[0]
moves_left = move.copy()
for used_move in used_die_rolls[0]:
moves_left.remove(used_move)
state = {'board': board.to_json(),
'dice_roll': move,
'used_rolls': used_die_rolls[0],
'player_can_move': not board.no_moves_possible(Colour.WHITE, moves_left)}
if board.has_game_ended():
state['winner'] = str(board.who_won())
if 'opponents_activity' in response:
# dict, keys: start_location, die_roll, end_location
opponents_activity = response['opponents_activity']
state['opp_move'] = opponents_activity['opponents_move']
state['opp_roll'] = opponents_activity['dice_roll']
if 'board_after_your_last_turn' in response:
state['board_after_your_last_turn'] = response['board_after_your_last_turn']
if 'result' in response:
state['result'] = response['result']
return state
@app.route('/start-game')
@cross_origin()
def start_game():
return get_state()
@app.route('/move-piece')
@cross_origin()
def move_piece():
print('[API]: move-piece called')
location = request.args.get('location', default=1, type=int)
die_roll = request.args.get('die-roll', default=1, type=int)
end_turn = request.args.get('end-turn', default='', type=str)
print(end_turn)
if end_turn == 'true':
print('[API]: Sending end_turn...')
moves_to_make.put('end_turn')
else:
print('[API]: Sending moves_to_make...')
moves_to_make.put({
'location': location,
'die_roll': die_roll
})
print('[API]: Waiting for move_results...')
response = move_results.get()
print('[API]: ...got result, responding to frontend')
return get_state(response)
@app.route('/new-game')
@cross_origin()
def new_game():
difficulty = request.args.get('difficulty', default='hard', type=str)
print(difficulty)
print('[API]: new-game called')
if len(current_board) != 0:
print('[API]: Sending end_game')
moves_to_make.put('end_game')
current_board.clear()
current_roll.clear()
time.sleep(1)
print('[API]: Starting new game thread')
threading.Thread(target=game_thread, args=[difficulty]).start()
print('[API]: Waiting for move_results...')
response = move_results.get()
print('[API]: ...got result, responding to frontend')
return get_state(response)