我正在开发一个国际象棋游戏,并且能够使我的Pawn棋子向前移动一个和两个方格,但我很难防止它在第一次移动后向前移动两个方格.如果有人可以帮助我了解如何实施它的逻辑或想法,我将不胜感激.
这是我的代码:
private boolean isValidPawnMove(int sourceRow, int sourceColumn, int targetRow, int
targetColumn) {
boolean isValid = false;
if(isTargetLocationFree()){
if(sourceColumn == targetColumn){
//same column
if(sourcePiece.getColor() == Piece.YELLOW_COLOR){
//yellow
if(sourceRow + 1 == targetRow ){
//move one up
isValid = true;
}else if(sourceRow + 2 == targetRow ){
isValid = true;
}else{
//not moving one up
isValid = false;
}
}else{
//brown
if(sourceRow - 1 == targetRow){
//move one down
isValid = true;
}else if(sourceRow - 2 == targetRow){
isValid = true;
}else{
//not moving one down
isValid = false;
}
}
}else{
//not the same column
isValid = false;
}
}else if(isTargetLocationCaptureable()){
if(sourceColumn + 1 == targetColumn || sourceColumn - 1 == targetColumn ){
//one column to the right or left
if(sourcePiece.getColor() == Piece.YELLOW_COLOR){
//yellow
if(sourceRow + 1 == targetRow){
//move one up
isValid = true;
}else{
//not moving one up
isValid = false;
}
}else{
//brown
if(sourceRow - 1 == targetRow ){
//move one down
isValid = true;
}else{
//not moving one down
isValid = false;
}
}
}else{
//One column to the left or right
isValid = false;
}
}
return isValid;
}
private boolean isTargetLocationCaptureable(){
if(targetPiece == null){
return false;
}else if( targetPiece.getColor() != sourcePiece.getColor()){
return true;
}else{
return false;
}
}
private boolean isTargetLocationFree(){
return targetPiece == null;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
107 次 |
最近记录: |