2012-03-09 23 views
0

我正在制作一个国际象棋程序,目前有运动为典当和骑士完成,但我与主教有点麻烦。我正在使用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变量只是用来获取地点一块是将其移动到“新”的目标。

我仍然试图弄清楚,但是我希望有人审查我的代码,因为我没有注意到或遗忘。任何提示或建议非常感谢。谢谢。

+0

'goLeft = getRow()'你不会使用左/右mouvements的列吗? – 2012-03-09 09:06:13

+0

@ MichaelLaffargueEasysport.fr通常是的,它应该是。然而,当我生成我的作品并开始添加作品时,它们在某处变得混乱起来,我最终重新考虑了你目前看到的作品。我可以去改变它。然而现在它是统一的,因为我的作品被设置为左侧到右侧,与现在,顶部和底部相反。 – DrTran 2012-03-09 09:14:12

回答

1

我认为你描述的问题会发生,如果moveValidator函数总是返回false,直到你的作品到达墙壁。

You'll have 
For1 --> goDown=0 
For2 --> goRight=2 --> moveValidator is false so break; 
For1 --> goDown=1 
For2 --> goRight=2 --> moveValidator is false so break; 
... 
And when goDown = 8 
For2 --> goRight=2 --> moveValidator is true 
For2 --> goRight=3 --> moveValidator is true 
... 

所以,你可能会想验证我的猜测调试,并试图验证moveValidator功能(你没有给它在你的代码)

+0

我回到了整个项目,修复了混合行和列,发现了一些破坏代码的错误。再次感谢。 – DrTran 2012-03-09 17:57:50

0

我觉得逻辑可能更简单的用一个计数器。此外,由于您无论如何都会发生问题,您可以通过计数到8来进一步简化逻辑。当我读取副本[goUp] [goRight]时,我想知道您是否意指复制[goRight] [goUp],因为通常我设置了游戏板像x,y和row,col这样的变量。如果我误解了任何或所有问题,我自己都是新人,并且事先道歉。祝你好运与你的国际象棋游戏!

// 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 go = 1; go < 8; go++){ 
     int testRow = getRow(); 
     int testCol = getCol(); 
     testRow-=go; //looking upwards 
     testCol+=go; //looking rightwards