Bug 410 - When the red king and black king are in the same column, the red king can directly kill the black king at its turn. But it is not checked as valid.
Summary: When the red king and black king are in the same column, the red king can dir...
Status: CONFIRMED
Alias: None
Product: CS3343 2020 Group 9_Chinese Chess
Classification: Unclassified
Component: ChessGame (show other bugs)
Version: V2.0
Hardware: PC Windows
: --- normal
Deadline: 2020-10-03
Assignee: FENG Yi
URL:
: 522 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-11-15 16:38 HKT by guohan2-c
Modified: 2020-12-06 23:09 HKT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description guohan2-c 2020-11-15 16:38:29 HKT
Steps to reproduce:
1. Move red soldier from (4, 3) to (3, 2)
2. Move black soldier from (4, 6) to (4, 5)
3. Move red king from (4, 0) to (4, 9)

Actual result:
The movement is checked as invalid. The program prompt "The move you want to make is invalid!"

Expected result:
The black king should be killed by the red king. And the game is over.
Comment 1 guohan2-c 2020-11-29 18:12:59 HKT
The problem is located in the isValidMove() function under the Rule class. The checking for the situation where the red king and black king are in the same column, one king can kill the other king at its turn is set as a valid move.
Comment 2 guohan2-c 2020-12-06 21:44:50 HKT
(In reply to guohan2-c from comment #1)
> The problem is located in the isValidMove() function under the Rule class.
> The checking for the situation where the red king and black king are in the
> same column, one king can kill the other king at its turn is set as a valid
> move.

The added code part is as follows [line 58-81, Rule class]
// no need to check King, Guard and Pawn
		if( chess instanceof Guard || chess instanceof Pawn) return true;
		// get starting point
		int from_x = chess.getPositionX();
		int from_y = chess.getPositionY();
		// Based on the rule, the middle point cannot have another pieces
		if(chess instanceof King ) {
			if(chess.getType().equals("red")) {
				for(int i=from_y + 1; i<=9;i++) {
					if (board[from_x][i] != null && !board[from_x][i].equals("black King")) {
						return true;
					}
				}
				return false;
			}
			else {
				for(int i=from_y - 1; i>=0;i--) {
					if (board[from_x][i]!=null && !board[from_x][i].equals("red King")) {
						return true;
					}
				}
				return false;
			}
		}
Comment 3 DUAN Longteng 2020-12-06 23:09:45 HKT
*** Bug 522 has been marked as a duplicate of this bug. ***