Skip to content

Commit fd6ba48

Browse files
committed
Fixed feed bug
1 parent 8e0cf7d commit fd6ba48

3 files changed

Lines changed: 37 additions & 26 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,21 @@ private void updateCapturedPiecesUI() {
319319

320320
private void makeMove(String move) {
321321
try {
322+
char turnBefore = currentFEN.split(" ")[1].charAt(0);
322323
PyObject result = chessLogic.callAttr("make_move", currentFEN, move);
323324
List<PyObject> tuple = result.asList();
324325
String newFen = tuple.get(0).toString();
325326
String capturedSymbol = tuple.get(1).toString();
327+
char turnAfter = newFen.split(" ")[1].charAt(0);
328+
if (turnBefore == turnAfter) {
329+
Toast.makeText(this, "Invalid move: Move did not change the turn", Toast.LENGTH_SHORT).show();
330+
selectedSquare = null;
331+
legalMoves = null;
332+
updateBoard();
333+
return;
334+
}
326335
currentFEN = newFen;
327-
Log.d("ChessApp", "After move, FEN = " + currentFEN
328-
+ ", next turn = " + currentFEN.split(" ")[1]);
336+
Log.d("ChessApp", "After move, FEN = " + currentFEN + ", next turn = " + turnAfter);
329337
if (!capturedSymbol.isEmpty()) {
330338
handleCapturedPiece(capturedSymbol);
331339
}
@@ -337,21 +345,19 @@ private void makeMove(String move) {
337345
showGameOverDialog(gameResult.toString());
338346
return;
339347
}
340-
// ADDED FOR MOVE FEED: Add move to feed and auto-scroll
341348
String currentPlayer = (moveCounter % 2 == 1) ? "White" : "Black";
342349
if (moveFeedAdapter != null) {
343350
MoveItem moveItem = new MoveItem(moveCounter, currentPlayer, move);
344351
moveFeedAdapter.addMove(moveItem);
345-
moveFeedRecyclerView.smoothScrollToPosition(moveFeedAdapter.getItemCount() - 1); // AUTO-SCROLL
352+
moveFeedRecyclerView.smoothScrollToPosition(moveFeedAdapter.getItemCount() - 1);
346353
}
347354
moveCounter++;
348355
selectedSquare = null;
349356
legalMoves = null;
350357
updateBoard();
351358
if (isBotTurn()) {
352359
new Handler(Looper.getMainLooper()).postDelayed(this::makeBestAIMove, 2000);
353-
}
354-
else {
360+
} else {
355361
checkAndPlayPremove();
356362
}
357363
} catch (Exception e) {

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,38 +318,46 @@ private void updateCapturedPiecesUI() {
318318

319319
private void makeMove(String move) {
320320
try {
321+
char turnBefore = currentFEN.split(" ")[1].charAt(0);
321322
PyObject result = chessLogic.callAttr("make_move", currentFEN, move);
322323
List<PyObject> tuple = result.asList();
323324
String newFen = tuple.get(0).toString();
324325
String capturedSymbol = tuple.get(1).toString();
326+
char turnAfter = newFen.split(" ")[1].charAt(0);
327+
if (turnBefore == turnAfter) {
328+
Toast.makeText(this, "Invalid move: Move did not change the turn", Toast.LENGTH_SHORT).show();
329+
selectedSquare = null;
330+
legalMoves = null;
331+
updateBoard();
332+
return;
333+
}
325334
currentFEN = newFen;
326-
Log.d("ChessApp", "After move, FEN = " + currentFEN
327-
+ ", next turn = " + currentFEN.split(" ")[1]);
335+
Log.d("ChessApp", "After move, FEN = " + currentFEN + ", next turn = " + turnAfter);
328336
if (!capturedSymbol.isEmpty()) {
329337
handleCapturedPiece(capturedSymbol);
330338
}
331339

332-
soundPool.play(moveSoundId, 1, 1, 0, 0, 1);
340+
if (moveSoundId != 0) {
341+
soundPool.play(moveSoundId, 1, 1, 0, 0, 1);
342+
}
333343
PyObject gameResult = chessLogic.callAttr("check_game_result", currentFEN);
334344
if (!gameResult.toString().equals("Game still in progress")) {
335345
showGameOverDialog(gameResult.toString());
336346
return;
337347
}
338-
// ADDED FOR MOVE FEED: Add move and auto-scroll
339348
String currentPlayer = (moveCounter % 2 == 1) ? "White" : "Black";
340349
if (moveFeedAdapter != null) {
341350
MoveItem moveItem = new MoveItem(moveCounter, currentPlayer, move);
342351
moveFeedAdapter.addMove(moveItem);
343-
moveFeedRecyclerView.smoothScrollToPosition(moveFeedAdapter.getItemCount() - 1); // AUTO-SCROLL
352+
moveFeedRecyclerView.smoothScrollToPosition(moveFeedAdapter.getItemCount() - 1);
344353
}
345354
moveCounter++;
346355
selectedSquare = null;
347356
legalMoves = null;
348357
updateBoard();
349358
if (isBotTurn()) {
350359
new Handler(Looper.getMainLooper()).postDelayed(this::makeMediumAIMove, 2000);
351-
}
352-
else {
360+
} else {
353361
checkAndPlayPremove();
354362
}
355363
} catch (Exception e) {

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,45 +326,42 @@ private void updateCapturedPiecesUI() {
326326

327327
private void makeMove(String move) {
328328
try {
329-
//PyObject result = chessLogic.callAttr("make_move", currentFEN, move);
330-
//currentFEN = result.toString();
329+
char turnBefore = currentFEN.split(" ")[1].charAt(0);
331330
PyObject result = chessLogic.callAttr("make_move", currentFEN, move);
332331
List<PyObject> tuple = result.asList();
333332
String newFen = tuple.get(0).toString();
334333
String capturedSymbol = tuple.get(1).toString();
335-
334+
char turnAfter = newFen.split(" ")[1].charAt(0);
335+
if (turnBefore == turnAfter) {
336+
Toast.makeText(this, "Invalid move: Move didn't change turn", Toast.LENGTH_SHORT).show();
337+
selectedSquare = null;
338+
legalMoves = null;
339+
updateBoard();
340+
return;
341+
}
336342
currentFEN = newFen;
337-
Log.d("ChessApp", "After move, FEN = " + currentFEN
338-
+ ", next turn = " + currentFEN.split(" ")[1]);
339-
343+
Log.d("ChessApp", "After move, FEN = " + currentFEN + ", next turn = " + turnAfter);
340344
if (!capturedSymbol.isEmpty()) {
341345
handleCapturedPiece(capturedSymbol);
342346
}
343-
344347
if (isSoundEnabled && moveSoundId != 0) {
345348
soundPool.play(moveSoundId, 1, 1, 0, 0, 1);
346349
}
347-
348350
PyObject gameResult = chessLogic.callAttr("check_game_result", currentFEN);
349351
if (!gameResult.toString().equals("Game still in progress")) {
350352
showGameOverDialog(gameResult.toString());
351353
return;
352354
}
353-
354-
// ADDED FOR MOVE FEED: Add the move to the feed and auto-scroll
355355
String currentPlayer = (moveCounter % 2 == 1) ? "White" : "Black";
356356
if (moveFeedAdapter != null) {
357357
MoveItem moveItem = new MoveItem(moveCounter, currentPlayer, move);
358358
moveFeedAdapter.addMove(moveItem);
359-
// ADDED FOR AUTO-SCROLL: Scroll to the bottom of the RecyclerView
360359
moveFeedRecyclerView.smoothScrollToPosition(moveFeedAdapter.getItemCount() - 1);
361360
}
362361
moveCounter++;
363-
364362
selectedSquare = null;
365363
legalMoves = null;
366364
updateBoard();
367-
368365
if (isBotTurn()) {
369366
new Handler(Looper.getMainLooper()).postDelayed(this::makeRandomAIMove, 2000);
370367
}

0 commit comments

Comments
 (0)