Skip to content

Commit a32a28d

Browse files
committed
added backend logic to check every move for game status. when white wins, black wins, draw, stalemate, etc. currently shows update on bottom of screen, but will add big win/lose/draw UI in the future
1 parent 24a437b commit a32a28d

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

app/src/main/java/com/example/chaiss/PlayActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ private void handleTouch(float x, float y) {
8989
try {
9090
PyObject result = chessLogic.callAttr("make_move", currentFEN, move);
9191
currentFEN = result.toString();
92+
93+
// Check for game result (win loss or draw
94+
PyObject gameResult = chessLogic.callAttr("check_game_result", currentFEN);
95+
String resultMessage = gameResult.toString();
96+
97+
// Display the results
98+
if (!resultMessage.equals("Game still ongoing")) {
99+
Toast.makeText(this, resultMessage, Toast.LENGTH_LONG).show();
100+
}
101+
92102
selectedSquare = null;
93103
this.legalMoves = null;
94104
updateBoard();

app/src/main/python/chess_logic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ def render_board(fen, selected_square=None, legal_moves=None):
4343

4444
svg_data = chess.svg.board(board, squares=squares_to_highlight)
4545
return svg_data
46+
47+
def check_game_result(fen):
48+
board = chess.Board(fen)
49+
if board.is_checkmate():
50+
if board.turn == chess.WHITE:
51+
return "Black wins!"
52+
else:
53+
return "White wins!"
54+
elif board.is_stalemate():
55+
return "Stalemate!"
56+
elif board.is_insufficient_material():
57+
return "Draw (Insufficient Material)!"
58+
elif board.is_game_over():
59+
return "Game Over!"
60+
else:
61+
return "Game still in progress"

0 commit comments

Comments
 (0)