Skip to content

Commit 1966e43

Browse files
committed
Adds redo logic to app
1 parent a4ce065 commit 1966e43

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

app.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ def console_demo():
153153

154154
def run_game():
155155
global board
156+
global undo_moves_stack = []
156157
board = chess.Board()
157158
Human = Player1(board)
158159
Human2 = Player2(board)
159160

160-
app = Flask(__name__, static_url_path='/static')
161+
app = Flask(__name__, root_path='./', static_url_path="")
161162
@app.route('/', methods=['GET'])
162163
def index():
163164
global board
@@ -220,12 +221,16 @@ def reset():
220221
@app.route("/undo", methods=["GET"])
221222
def undo():
222223
global board
224+
global undo_moves_stack
225+
canundo = True
223226
try:
224-
board.pop()
225-
except Exception:
226-
pass
227+
undo_moves_stack.append(board.pop())
228+
except IndexError:
229+
canundo = False
227230

228-
resp = {"fen": board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves())}
231+
resp = {"fen": board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves()), 'canundo': 'true'}
232+
if canundo == False:
233+
resp['canundo'] = 'false'
229234
response = app.response_class(
230235
response=json.dumps(resp),
231236
status=200,
@@ -234,6 +239,28 @@ def undo():
234239
return response
235240

236241

242+
@app.route("/redo", methods=["GET"])
243+
def redo():
244+
global board
245+
global undo_moves_stack
246+
if len(undo_moves_stack) != 0:
247+
board.push(undo_last_move.pop())
248+
else:
249+
pass
250+
251+
resp = {'fen': board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves()), 'canredo': 'true'}
252+
if len(undo_moves_stack) == 0:
253+
resp['canredo'] = 'false'
254+
255+
response = app.response_class(
256+
response=json.dumps(resp),
257+
status=200,
258+
mimeype='application/json'
259+
)
260+
261+
return response
262+
263+
237264
http_server = WSGIServer(('', 1337), app)
238265
http_server.serve_forever()
239266

0 commit comments

Comments
 (0)