第一个基于文本的Java程序
我正在学习Java,这是我的第一个大项目。我正在使用Java设计一个基于文本的国际象棋游戏。我成功地完成了铸造工作,但由于某种原因,我无法让兵移动。我想分享其余的代码,但我不能,因为字符的限制,但我想通过DM发送整个程序给你第一个基于文本的Java程序,java,Java,我正在学习Java,这是我的第一个大项目。我正在使用Java设计一个基于文本的国际象棋游戏。我成功地完成了铸造工作,但由于某种原因,我无法让兵移动。我想分享其余的代码,但我不能,因为字符的限制,但我想通过DM发送整个程序给你 package il.co.Pawn; import il.co.ChessInterface.ChessPiece; public class Pawn extends ChessPiece{ int columnEnd; int rowEnd;
package il.co.Pawn;
import il.co.ChessInterface.ChessPiece;
public class Pawn extends ChessPiece{
int columnEnd;
int rowEnd;
int columnStart;
int rowStart;
String name;
boolean simpleMove;
boolean twoSquareMove;
boolean devourMove;
boolean valid;
String chessPiece;
boolean pieceDevour;
char firstLetter;
char secondLetter;
boolean secondLetterNotNull;
public Pawn(String name){
this.name=name;
}
@Override
public String toString() {
return "Pawn [name=" + name + "]";
}
//pieces, columnStart,rowStart,columnEnd,rowEnd
public boolean isMoveValid(ChessPiece[][] pieces, int columnStart, int rowStart, int columnEnd, int rowEnd) {
int deltaX = rowEnd - rowStart;
if (deltaX == 1 || deltaX == -1)
simpleMove = true;
boolean pieceMove1 = false;
boolean pieceMove2 = false;
boolean pieceMove3 = false;
boolean pieceMove4 = false;
pieceMove1 = true;
pieceMove2 = true;
pieceMove3 = true;
pieceMove4 = true;
return pieceMove1 || pieceMove2 || pieceMove3 || pieceMove4;
}
//This part displays the Pawns at their respective locations and it is used to differentiate between the White pieces and black pieces.
String pawn="";
public String print() {
if(name.charAt(0)=='B'){
pawn= "|"+ name + "|";
}
else{
pawn="|"+ name + "|";
}
return pawn;
}
@Override
//This part finds the piece in the board.
public String FindPiece() {
return this.name;
}
//pieces,rowStart,columnStart ,columnEnd,rowEnd
//this method is the part where the program checks if the piece is moving in accordance to the rules or not.
public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
boolean isPawnMovingUP=false;
boolean isPawnMovingUP2=false;
boolean isPawnMovingRIGHT=false;
boolean isPawnMovingDOWN=false;
boolean isPawnMovingDOWN2=false;
boolean isPawnMovingLEFT=false;
if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='W'){
if(rowStart==7 && (rowEnd==6 || rowEnd==5) && columnStart==columnEnd){
isPawnMovingUP = true;
}
else if((rowStart-1)==rowEnd && pieces[rowEnd][columnEnd]==null && columnStart==columnEnd){
isPawnMovingUP2 = true;
//isPawnMovingUP = true;
}
else{
//isPawnMovingUP = false;
isPawnMovingUP2 = false;
}
}
if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='B'){
if(rowStart==2 && (rowEnd==3 || rowEnd==4)&& columnStart==columnEnd){
isPawnMovingDOWN = true;
}
else if((rowStart+1)==rowEnd && pieces[rowEnd][columnEnd]==null && columnStart==columnEnd){
//isPawnMovingDOWN2 = true;
//============================
isPawnMovingDOWN = true;
//============================
}
else{
isPawnMovingDOWN = false;
//isPawnMovingDOWN2 = false;
}
}
if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='W'){
if(isPawnMovingDOWN == false && (rowStart-1)==rowEnd && (columnStart-1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
//if(isPawnMovingDOWN2 == false && isPawnMovingDOWN == false && (rowStart-1)==rowEnd && (columnStart-1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
isPawnMovingLEFT=true;
}
else if(isPawnMovingDOWN == false &&(rowStart-1)==rowEnd && (columnStart+1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true&& columnStart!=columnEnd){
//else if(isPawnMovingDOWN2 == false && isPawnMovingDOWN == false &&(rowStart-1)==rowEnd && (columnStart+1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true&& columnStart!=columnEnd){
isPawnMovingRIGHT=true;
}
else{
isPawnMovingLEFT=false;
isPawnMovingRIGHT=false;
}
}
if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='B'){
if(isPawnMovingUP==false && isPawnMovingUP2==false && (rowStart+1)==rowEnd && (columnStart-1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
isPawnMovingLEFT=true;
}
else if(isPawnMovingUP==false && isPawnMovingUP2==false && (rowStart+1)==rowEnd && (columnStart+1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
isPawnMovingRIGHT=true;
}
else{
isPawnMovingLEFT=false;
isPawnMovingRIGHT=false;
}
}
return isPawnMovingUP || isPawnMovingRIGHT|| isPawnMovingDOWN || isPawnMovingLEFT || isPawnMovingUP2 || isPawnMovingDOWN2;
}
public boolean piecesDevour(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd){
firstLetter = pieces[rowStart][columnStart].FindPiece().charAt(0);
if (pieces[rowEnd][columnEnd] != null) {
secondLetterNotNull = true;
secondLetter = pieces[rowEnd][columnEnd].FindPiece().charAt(0);
} else {
secondLetterNotNull = false;
}
if (secondLetter != firstLetter && secondLetterNotNull) {
pieceDevour = true;
} else {
pieceDevour = false;
}
return pieceDevour;
}
}
isMoveValid将始终返回true
boolean pieceMove1 = false;
boolean pieceMove2 = false;
boolean pieceMove3 = false;
boolean pieceMove4 = false;
pieceMove1 = true;
pieceMove2 = true;
pieceMove3 = true;
pieceMove4 = true;
return pieceMove1 || pieceMove2 || pieceMove3 || pieceMove4;
计算最后一条语句时,pieceMove1到4都将为true,且true或true或true或true等于true。我甚至不知道您想在这里完成什么,所以我无法告诉您如何修复它,但一般来说,您应该重新考虑如何构造数据类型,以及哪个类实际上需要哪些信息。您有问题吗?那么您的问题是什么?您将国际象棋设计为基于文本的Java程序?请分享代码。我会很惊讶的。哇,下棋了。你使用JavaFX吗?或者说基于文本的方法……国际象棋很难,但对语言学习者来说也是一项很好的任务。但是,除非您展示代码的相关部分,否则我们无法真正帮助您。另外,我建议您尽快学习如何使用调试器。