如何连续找到三个相同的数字?

时间:2014-03-01 17:51:07

标签: java arrays

我有一个如下所示的数组。

int[][] myArray = 
        {{1, 2, 3, 0, 0, 1}
         {1, 0, 4, 4, 0, 1}
         {1, 2, 4, 3, 4, 0}
         {2, 2, 0, 0, 2, 2}
         {3, 0, 0, 3, 0, 0}
         {4, 2, 3, 0, 0, 0}}

可以说一个人因为第一栏中的三个1中的1而赢了。两个人不会赢,因为他们不在“排”。

我想做某种胜利检查,以便在一行,对角线或列中找到三个相同的数字。有点像tic-tac-toe但有更大的网格。在我使用一组凌乱的if语句和goto语句之前。 (它是用Basic编写的。)我尝试过使用一个系统,它从最后放置的部分找到方向,其中有许多相同,但它没有正常工作。我怎样才能以简单易维的方式做到这一点?

尝试过的代码:

private static boolean checkBoardCombinations(int[][] board, int inputX, int inputY) {
        int currentPlayer = board[inputX-1][inputY-1];
        boolean[][] directions = new boolean[3][3];
        for(int y = 0; y >= -2; y--){
            for(int x = 0; x >= -2; x--){
                if(inputX+x >= 0 && inputX+x <= 7 && inputY+y >= 0 && inputY+y <= 7 
                        && (board[inputX+x][inputY+y] == currentPlayer)){
                    //System.out.printf("X: %s Y: %s", inputX+x, inputY+y);
                    directions[x+2][y+2] = true;
                }
                else{
                    directions[x+2][y+2] = false;
                }
                //System.out.printf("X: %s Y: %s B: %s,", inputX+x, inputY+y, directions[x+2][y+2]);
            }
            //System.out.println();
        }
        /*
         for(int x = 0; x <= 2; x++){
            for(int y = 0; y <= 2; y++){
                System.out.print(directions[x][y] + " ");
            }
                System.out.println();
        }
        */
        return false;

    }

2 个答案:

答案 0 :(得分:1)

假设已知玩家数量,您可以逐个遍历所有玩家,并检查是否有任何玩家正在形成所需长度的连接。

此类代码如下所示:

private int[][] grid; // Your array of size ROWS x COLUMNS
private final int ROWS = 6, COLUMNS = 6;
private final int CONSECUTIVE_CONNECTION_REQUIRED = 3;

// Returns true if given playerType is forming a connection, else false.
public boolean checkGrid(int playerType)
{
  // Check downward
  for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
  {
    for (int j = 0; j < COLUMNS; j++)
    {
      int counter = 0;
      for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++)
      {
        if (grid[k][j] == playerType)
          counter++;
      }

      if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
        return true;
    }
  }

  // Check across
  for (int i = 0; i <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
  {
    for (int j = 0; j < ROWS; j++)
    {
      int counter = 0;
      for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++)
      {
        if (grid[j][k] == playerType)
          counter++;
      }

      if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
        return true;
    }
  }

  // Check left to right diagonally
  for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
  {
    for (int j = 0; j <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j++)
    {
      int counter = 0;
      for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m++)
      {
        if (grid[k][m] == playerType)
          counter++;
      }

      if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
        return true;
    }
  }

  // Check right to left diagonally
  for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
  {
    for (int j = COLUMNS - 1; j >= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j--)
    {
      int counter = 0;
      for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m--)
      {
        if (grid[k][m] == playerType)
          counter++;
      }

      if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
        return true;
    }
  }

  return false;
}

其中playerType为0,1,2,3等......

您可以使用以下checkGrid()方法:

for(int i = MIN_PLAYER_NUMBER; i <= MAX_PLAYER_NUMBER; i++)
{
  if(checkGrid(i))
  {
    // Player i is forming the connection!!!
  }
}

但是如果您不想多次遍历网格,则删除二维数组,并使用带有邻接列表表示的图形。为此编写一个合适的API,使您可以轻松地更改特定的表示形式,然后您可以在较少的迭代中查找是否有任何玩家在图表中进行特定长度的连接。

答案 1 :(得分:1)

虽然你已经接受了一个答案,但我也想提交给你多样性的答案:)

public static void main (String[] args)
{
    int[][] myArray = 
    {{1, 2, 3, 0, 0, 1},
     {1, 0, 4, 4, 0, 1},
     {1, 2, 4, 3, 4, 0},
     {2, 2, 0, 0, 2, 2},
     {3, 0, 0, 3, 0, 0},
     {4, 2, 3, 0, 0, 0}};
     System.out.println(testForWinner(myArray));
}

/**
 * Returns -1 if no winner
 */
static int testForWinner(int[][] ar) {
    for(int i=0; i<ar.length; i++) {
        for(int j=0; j<ar[i].length; j++) {
            if(checkNext(ar, i, j, 0, 1, 1)) { //Check the element in the next column
                return ar[i][j];
            }
            for(int k=-1; k<=1; k++) { //Check the three adjacent elements in the next row
                if(checkNext(ar, i, j, 1, k, 1)) {
                    return ar[i][j];
                }
            }
        }
    }
    return -1;
}

/**
 * Step number `step` starting at `ar[i][j]` in direction `(di, dj)`.
 * If we made 3 steps we have a winner
 */
static boolean checkNext(int[][] ar, int i, int j, int di, int dj, int step) {
    if(step==3) {
        return true;
    }
    if(i+di<0 || i+di>ar.length-1 || j+dj<0 || j+dj>ar[i].length-1) {
        return false;
    }
    if(ar[i+di][j+dj]==ar[i][j]) {
        return checkNext(ar, i+di, j+dj, di, dj, step+1);
    }
    return false;
}

查看实际操作:http://ideone.com/Ou2sRh