Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多维数组中实现Rook逻辑_Java_Multidimensional Array - Fatal编程技术网

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

Java 在多维数组中实现Rook逻辑,java,multidimensional-array,Java,Multidimensional Array,我一直在想一个问题,但我被卡住了。基本上,我一直在尝试在一个不是国际象棋的游戏中实现一辆车的运动逻辑,但我被卡在上面了。我将向您提供详细信息: 棋盘是一个5x5多维数组,每个玩家只有棋子和车 目标是捕获对手的所有棋子,捕获所有棋子的人将赢得比赛 Rooks可以在一个方向上移动任意远,直到它们撞到阻挡其路径的物体为止 我的车现在做的事情是它可以朝一个方向行驶,但它可以在那条线上的任何地方行驶。我需要帮助,试图找出如何添加更多的逻辑,以确保它只能在路径清晰的情况下运行。以下是一个例子: 小的“p

我一直在想一个问题,但我被卡住了。基本上,我一直在尝试在一个不是国际象棋的游戏中实现一辆车的运动逻辑,但我被卡在上面了。我将向您提供详细信息:

  • 棋盘是一个5x5多维数组,每个玩家只有棋子和车
  • 目标是捕获对手的所有棋子,捕获所有棋子的人将赢得比赛
  • Rooks可以在一个方向上移动任意远,直到它们撞到阻挡其路径的物体为止
我的车现在做的事情是它可以朝一个方向行驶,但它可以在那条线上的任何地方行驶。我需要帮助,试图找出如何添加更多的逻辑,以确保它只能在路径清晰的情况下运行。以下是一个例子:

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

* R R R *
* P P P *
* * * * *
* p p p *
* r r r *
以下是我为rook准备的代码:

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; 
}

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


托尔加默斯勋爵:


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


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

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;
}
public boolean isLegalMove(位置从,位置到)
{
//行为X位置(向上/向下)
//列为Y位置(左/右)
int fromRow=from.getXPosition();
int fromColumn=from.getYPosition();
int toRow=to.getXPosition();
int-toColumn=to.getYPosition();
//必须是相同的行或列
if(fromRow!=toRow | | fromColumn!=toColumn)返回false;
//不能移动到同一个正方形
if(fromRow==toRow&&fromColumn==toColumn)返回false;
//行是一样的
if(fromRow-toRow==0){
//这将保留我们下一步要检查的列
int newPos=fromColumn;
//我们应该上还是下?
整数金额=(toColumn-fromColumn<0)?-1:1;
while(newPos!=toColumn){
newPos+=金额;
//如果不是空的,我们找到了另一个片段
if(gameBoard[fromRow][newPos]!=null)返回false;
}
如果(游戏板[toRow][toColumn]!=null){
//如果是您自己的作品,则返回false;如果不是,则返回true
}
//列是相同的
}否则{
//这将保留我们下一步要检查的行
int newPos=fromRow;
//我们应该上还是下?
整数金额=(toRow-fromRow<0)?-1:1;
while(newPos!=toRow){
newPos+=金额;
//如果不是空的,我们找到了另一个片段
if(gameBoard[newPos][fromColumn]!=null)返回false;
}
如果(游戏板[toRow][toColumn]!=null){
//如果是您自己的作品,则返回false;如果不是,则返回true
}
}
返回true;
}


针对您希望能够捕获对手棋子的情况进行编辑。。。但是我没有把最后一段代码放进去,因为您必须再次更改方法签名。请看我的评论。请注意,它现在是一个
循环,不是
do while

,因此如果我检查整个电路板,看看电路板是否为空,那么我可以安全地说,假设有某种东西挡住了方向,在这种情况下返回false?如果不知道电路板的数据结构,我无法回答这个问题。你不会检查电路板是否为空。您必须检查rook的源位置和目标位置之间的各个空格@durron597请在您的答案中添加此内容(或告诉我我遗漏了什么);我想加上我自己的答案,但我觉得它和你的答案太相似了。另外,无关@durron597,嘿,我从Meta认出你了。一个半月内的3k真是令人印象深刻!首先,如果它们不在同一个类中,则将其作为参数传递给该方法。第二次。如果你想从起始位置向上(或向左)移动,则为-1;如果你想向下或向右移动,则为1。
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;
}