首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在多维数组中实现Rook逻辑

在多维数组中实现Rook逻辑
EN

Stack Overflow用户
提问于 2012-12-05 18:02:24
回答 1查看 738关注 0票数 3

我有一个问题,我一直想弄清楚,但我被困住了。基本上,我一直在尝试在一种不是国际象棋的游戏中实现鲁克运动的逻辑,但我被困在了它上。我会告诉你细节:

  • 董事会是一个5x5多维数组,其中只有每个玩家的棋子和钩子。
  • 目标是捕捉你对手的所有棋子,而捕获他们的人将赢得这场比赛。
  • 鱼钩可以向一个方向移动到他们想要的地方,直到他们撞到什么东西挡住了他们的路。

我的车现在做的事情是它可以走一个方向,但它可以在这条线上的任何地方。我需要帮助,试图找出如何添加更多的逻辑,以确保它只能走下去,只要道路是明确的。下面是一个示例:

小的"p“和"r”是玩家2的棋子,大的"P“和"R”是玩家的棋子。现在,右上角R(车)只能向右移动,但如果你这样做,它将超越棋子,然后可以走到它想要的地方。

代码语言:javascript
复制
* R R R *
* P P P *
* * * * *
* p p p *
* r r r *

下面是我对这辆车的代码:

代码语言:javascript
复制
public boolean isLegalMove(Location from,Location to)
{
  // Row is XPosition (Up/Down)
  // Column is YPosition(Left/Right)

  int fromRow = from.getXPosition();
  int fromColumn = from.getYPosition();
  int toRow = to.getXPosition();
  int toColumn = to.getYPosition();

  // higher row or column or both
  if(((fromColumn >= toColumn) || (fromColumn <= toColumn)) && ((fromRow == toRow))) {
    return true;
  }
  if(((fromRow >= toRow) || (fromRow <= toRow)) && ((fromColumn == toColumn))) {
    return true;
  }
  return false; 
}

我想我要做另一种方法来检查逻辑,如果路径中有什么东西,叫它isPathClear()

编辑:

下面是其余的代码:

代码语言:javascript
复制
 public class Board
  {
    // The depth and width of the field.
    public static final int ROW = 5;
    public static final int COLUMN = 5;
    public static final String EMPTYPIECE = " * ";

     //Storage for the game pieces
     private GamePiece [] [] gameBoard;
     //Makes the balls and torches for player1
     private Pawn1 p1Pawn1,p1Pawn2,p1Pawn3;
     private Rook1 p1Rook1,p1Rook2,p1Rook3;

     //Makes the ball and torchers for player2
     private Pawn2 p2Pawn1,p2Pawn2,p2Pawn3;
     private Rook2 p2Rook1,p2Rook2,p1Rook3;

/**
 * Makes a 5x5 Gameboard
 */
public Board()
{
    // initialise instance variables
    gameBoard = new GamePiece [ROW][COLUMN];

    //Makes pieces for player1
    p1Pawn1 = new Pawn1();
    p1Pawn2 = new Pawn1();
    p1Pawn3 = new Pawn1();
    p1Rook1 = new Rook1();
    p1Rook2 = new Rook1();
    p1Rook3 = new Rook1();

    //Makes pieces for player2
    p2Pawn1 = new Pawn2();
    p2Pawn2 = new Pawn2();
    p2Pawn3 = new Pawn2();
    p2Rook1 = new Rook2();
    p2Rook2 = new Rook2();
    p2Rook3 = new Rook2();
}

/**
 * Makes new games
 */
public void newGame()
{  
    // Assigns the piece of the board for player1
    gameBoard[0][1] = p1Rook1;
    gameBoard[0][2] = p1Rook2;
    gameBoard[0][3] = p1Rook3;
    gameBoard[1][1] = p1Pawn1;
    gameBoard[1][2] = p1Pawn2;
    gameBoard[1][3] = p1Pawn3;

    // Assigns the pieces of the board for player2
    gameBoard[4][1] = p2Rook1;
    gameBoard[4][2] = p2Rook2;
    gameBoard[4][3] = p2Rook3;
    gameBoard[3][1] = p2Pawn1;
    gameBoard[3][2] = p2Pawn2;
    gameBoard[3][3] = p2Pawn3;
}

/**
 * Displays the content of the board
 */
public void displayBoard()
{
    System.out.println("  a  b  c  d  e");
    int counter = 1;
    for (int i = 0; i < gameBoard.length; i++){
       System.out.print(counter);
       for (int j = 0; j < gameBoard[i].length; j++) {
           if (gameBoard[i][j] == null) {
              System.out.print(EMPTYPIECE);
           } else {
               System.out.print(" " + gameBoard[i][j] + " ");
           }
       }
       counter++;
       System.out.println();
    }
}

/**
 * Moves the movepiece from one locatin to another
 * @param from - where the location was from
 * @param to - Where the location is going to
 */
public void movePiece(Location from,Location to) throws InvalidMoveException
{
    int fromRow     = from.getXPosition();
    int fromColumn  = from.getYPosition();
    int toRow       = to.getXPosition();
    int toColumn    = to.getYPosition();

    if (gameBoard[fromRow][fromColumn] == null) {
        throw new InvalidMoveException("Invalid input for source location.");
    }
    if (! checkBounds(from, to)) {
        throw new InvalidMoveException("Invalid input for destination location.");
    }
    if (isSameLocation(from, to)){
        throw new InvalidMoveException("Invalid move, source and destination cannot    bethe same.");
    }
    if (! gameBoard[fromRow][fromColumn].isLegalMove(from, to)) {
        throw new InvalidMoveException("Invalid move for this piece.");
    }
       gameBoard[toRow][toColumn] = gameBoard[fromRow][fromColumn];
       gameBoard[fromRow][fromColumn] = null;

   displayBoard();
}

/**
 * Checks a proposed move to ensure it is within the bounds of the board.
 * @param source location, destination location
 * @return true if both source and destination are within bounds
 */
private boolean checkBounds(Location from, Location to)
{
    int fromRow     = from.getXPosition();
    int fromColumn  = from.getYPosition();
    int toRow       = to.getXPosition();
    int toColumn    = to.getYPosition();

    boolean testFrom = (fromRow >= 0) && (fromColumn >= 0) && (fromRow < gameBoard.length) && (fromColumn < gameBoard[0].length);
    boolean testTo = (toRow >= 0) && (toColumn >= 0) && (toRow < gameBoard.length) && (toColumn < gameBoard[0].length);
    return testFrom && testTo;
}

/**
 * Checks a proposed move to ensure source and destination are different.
 * @param source location, destination location
 * @return true if source and destination are the same
 */
private boolean isSameLocation(Location from, Location to)
{
    int fromRow     = from.getXPosition();
    int fromColumn  = from.getYPosition();
    int toRow       = to.getXPosition();
    int toColumn    = to.getYPosition();
    return fromRow == toRow && fromColumn == toColumn;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 18:07:43

如果你不知道董事会上还有什么,你就不知道这条路是否畅通。但是,您的方法签名不允许此函数访问板的布局。如果您通过整个板,您可以使用一个循环检查所有正方形之间的其他部分。

托加默斯勋爵:

你不会检查板是否为空。您必须检查rook的源和目标位置之间的各个空间。

现在我知道了董事会的样子,下面是一些代码:

代码语言:javascript
复制
public boolean isLegalMove(Location from,Location to)
{
  // Row is XPosition (Up/Down)
  // Column is YPosition(Left/Right)

  int fromRow = from.getXPosition();
  int fromColumn = from.getYPosition();
  int toRow = to.getXPosition();
  int toColumn = to.getYPosition();

  // Has to be same row or column
  if(fromRow != toRow || fromColumn != toColumn) return false;
  // Can't move to the same square
  if(fromRow == toRow && fromColumn == toColumn) return false;

  // Rows are the same
  if(fromRow - toRow == 0) {
    // this will hold the column of the we're going to check next
    int newPos = fromColumn;
    // Should we go up or down?
    int amount = (toColumn - fromColumn < 0) ? -1 : 1;
    while(newPos != toColumn) {
      newPos += amount;
      // if it's not null, we found a different piece
      if(gameBoard[fromRow][newPos] != null) return false;
    }
    if(gameBoard[toRow][toColumn] != null) {
      // return false if it's your own piece, true if it's not
    }
  // Columns are the same
  } else {
    // this will hold the row of the we're going to check next
    int newPos = fromRow;
    // Should we go up or down?
    int amount = (toRow - fromRow < 0) ? -1 : 1;
    while(newPos != toRow) {
      newPos += amount;
      // if it's not null, we found a different piece
      if(gameBoard[newPos][fromColumn] != null) return false;
    }
    if(gameBoard[toRow][toColumn] != null) {
      // return false if it's your own piece, true if it's not
    }
  }

  return true;
}

因为你想要捕捉对手的作品.但我没有将最后一段代码放入其中,因为您必须再次更改方法签名。请听我的评论。注意,它现在是一个while循环,而不是do-while

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13729546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档