我正在制作一个国际象棋程序,目前有运动为典当和骑士完成,但我与主教有点麻烦。我正在使用MVC方法创建我的国际象棋游戏,并创建了一个二维BoardSquare数组,用于存储位置,片段(如果有的话)以及是否为空等信息。就目前而言,我已经让董事会逐行接受来自文本文件的输入。我正在为主教运动工作的目前线路是从c8到a6,因此向下移动并向左移动。要求有人审查主教运动逻辑
我的动作很简单,它从文件中获取输入,将其转换为二维数组坐标,并将其传递到模型的移动方法(以及模型),该方法调用一个块的移动方法。
// In the File I/O Class
public void passinMove(BoardModel bm) {
generateOriginRC(input, START_LOC1, END_LOC1);
generateDestRC(input, START_LOC2, END_LOC2);
System.out.println("Origin C/R: "+oCol+"/"+oRow);
System.out.println("Dest C/R: "+dCol+"/"+dRow);
bm.movePiece(bm.getBoard(), oRow, oCol, dRow, dCol);
}
// In the BoardModel Class
public void movePiece(BoardSquare[][] board, int oRow, int oCol, int dRow, int dCol){
board[oRow][oCol].getPiece().move(board, board[dRow][dCol]);
截至目前为止,我已经将我的作品分别生成了自己的有效动作数组;这是一个BoardSquares的列表,通过移动威盛一次一个的方块,直到它碰到一块或板尾。然后我有它比较它的目标广场到该列表。
@Override
void move(BoardSquare[][] board, BoardSquare target) {
if (!getPossibleMove(board).contains(target)) {
System.out
.println("Sorry. Not a valid move, please enter a new move");
} else {
target.setSquare(board[col][row]);
}
}
对于主教我抓住它的位置,并相应地操纵它来移动它。 (向上移动,减去数组,等等)。
但是,看起来我的逻辑在某个地方是有缺陷的;因为当它开始检查并且向右移动时,它从0/2移动到1/2。然后继续,直到它碰到右侧墙,并直接进入角落。
@Override
Collection<BoardSquare> getPossibleMove(BoardSquare[][] board) {
BoardSquare[][] copy = board;
ArrayList<BoardSquare> validMoves = new ArrayList<BoardSquare>();
// Checks spaces to the Up-Right of the Bishop by moving the Bishop
// until it hits the end of the board or a piece
for (int goUp = getCol(); goUp > -1; goUp--) {
for (int goRight = getRow(); goRight < 8; goRight++) {
System.out.println("Doing Up-Right C/R: "+getCol()+"/"+getRow());
System.out.println("Moving to C/R: "+goUp+"/"+goRight);
tempCol = getCol();
tempRow = getRow();
if (moveValidator(copy[goUp][goRight])) {
validMoves.add(copy[goUp][goRight]);
copy[goUp][goRight].setSquare(copy[tempCol][tempRow]);
} else {
break;
}
}
}
// Checks spaces to the Up-Left of the Bishop by moving the Bishop
// until it hits the end of the board or a piece
for (int goUp = getCol(); goUp > -1; goUp--) {
for (int goLeft = getRow(); goLeft > -1; goLeft--) {
System.out.println("Doing Up-Left C/R: "+getCol()+"/"+getRow());
System.out.println("Moving to C/R: "+goUp+"/"+goLeft);
tempCol = getCol();
tempRow = getRow();
if (moveValidator(copy[goUp][goLeft])) {
validMoves.add(copy[goUp][goLeft]);
copy[goUp][goLeft].setSquare(copy[tempCol][tempRow]);
} else {
break;
}
}
}
// Checks spaces to the Down-Right of the Bishop by moving the Bishop
// until it hits the end of the board or a piece
for (int goDown = getCol(); goDown < 8; goDown++) {
for (int goRight = getRow(); goRight < 8; goRight++) {
System.out.println("Doing Down-Right C/R: "+getCol()+"/"+getRow());
System.out.println("Moving to C/R: "+goDown+"/"+goRight);
tempCol = getCol();
tempRow = getRow();
if (moveValidator(copy[goDown][goRight])) {
validMoves.add(copy[goDown][goRight]);
copy[goDown][goRight].setSquare(copy[tempCol][tempRow]);
} else {
break;
}
}
}
// Checks spaces to the Down-Left of the Bishop by moving the Bishop
// until it hits the end of the board or a piece
for (int goDown = getCol(); goDown < 8; goDown++) {
for (int goLeft = getRow(); goLeft > -1; goLeft--) {
System.out.println("Doing Down-Left C/R: "+getCol()+"/"+getRow());
System.out.println("Moving to C/R: "+goDown+"/"+goLeft);
tempCol = getCol();
tempRow = getRow();
if (moveValidator(copy[goDown][goLeft])) {
validMoves.add(copy[goDown][goLeft]);
copy[goDown][goLeft].setSquare(copy[tempCol][tempRow]);
} else {
break;
}
}
}
return validMoves;
}
一些注意事项: 的tempCol和tempRow变量只是用来获取地点一块是将其移动到“新”的目标。
我仍然试图弄清楚,但是我希望有人审查我的代码,因为我没有注意到或遗忘。任何提示或建议非常感谢。谢谢。
'goLeft = getRow()'你不会使用左/右mouvements的列吗? – 2012-03-09 09:06:13
@ MichaelLaffargueEasysport.fr通常是的,它应该是。然而,当我生成我的作品并开始添加作品时,它们在某处变得混乱起来,我最终重新考虑了你目前看到的作品。我可以去改变它。然而现在它是统一的,因为我的作品被设置为左侧到右侧,与现在,顶部和底部相反。 – DrTran 2012-03-09 09:14:12