gpt4 book ai didi

java - 如何找出连续三个相同的数字?

转载 作者:行者123 更新时间:2023-12-02 06:08:01 25 4
gpt4 key购买 nike

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

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,所以会说一方获胜。两个人不会获胜,因为他们不在“排”中。

我想做某种获胜检查,以便它在行、对角线或列中找到三个相同的数字。有点像井字棋,但网格更大。之前我使用了一组凌乱的 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;

}

最佳答案

假设玩家数量已知,您可以一一迭代所有玩家,并检查是否有玩家正在形成所需长度的连接。

这样的代码如下所示:

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,让您可以轻松地更改特定的表示形式,然后您可以以更少的迭代次数找到是否有任何玩家在图表中建立特定长度的连接。

关于java - 如何找出连续三个相同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com