Java 如何在一行中找到三个相同的数字?
我有一个数组,如下所示Java 如何在一行中找到三个相同的数字?,java,arrays,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。两个人不会赢,因为他们不在一排 我想做一些赢的检查,这
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;
}
专用静态布尔校验板组合(int[][]板,int-inputX,int-inputY){
int currentPlayer=board[inputX-1][inputy1];
布尔[][]方向=新的布尔[3][3];
对于(int y=0;y>=-2;y--){
对于(int x=0;x>=-2;x--){
如果(inputX+x>=0&&inputX+x=0&&inputY+y假设已知玩家数量,则可以逐个迭代所有玩家,并检查是否有任何玩家正在形成所需长度的连接
此类代码如下所示:
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;
}
for(int i = MIN_PLAYER_NUMBER; i <= MAX_PLAYER_NUMBER; i++)
{
if(checkGrid(i))
{
// Player i is forming the connection!!!
}
}
但是如果你不想在你的网格上重复这么多次,那么放下你的二维数组,使用一个具有邻接列表表示的图。为它编写一个合适的API,让你可以轻松地更改你的特定表示,然后你就可以发现是否有玩家在图中建立了特定长度的连接或者不,在更少的迭代中。虽然您已经接受了一个答案,但我想也向您提交我的多样性答案:)
publicstaticvoidmain(字符串[]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));
}
/**
*如果没有赢家,返回-1
*/
静态int testForWinner(int[]ar){
对于(int i=0;i应用什么逻辑或粘贴代码会更好?3个相同的值必须是一个接一个的,或者可以分散在行、对角线或列中?这些数字的范围是什么?它们只是一位数吗?或者它们可以范围到任何值吗?或者它们只是0、1、2、3和4吗?@NiksTyagi看到底部的代码。i w考虑创建一个系统,在该系统中,它将扫描最后一个放置的工件以获得相同的数字。它将存储最近的数字与最后一个放置的工件相同的方向。从那里它将继续扫描该方向。最后,它将查看行、列或对角线中是否有三个真。@halex这些数字已被删除触摸,有点,所以是的,它们必须是一个接一个的。什么是用邻接列表表示的图形?@Cammy_the_block:这是一种数据结构。就像数组、列表、地图、集合、树一样,有图形。图形可以用多种方式表示,其中一种表示方式是使用邻接列表。在互联网上搜索了解更多。我不明白整个步骤。你总是用第一步做检查下一步。而且你的解决方案对我来说太混乱了P@Cammy_the_block在checkNext
中,我使用step+1
递归调用该方法。这样我沿着一个方向走,直到我沿着3个相等的值走了3步。我的目标是使用re诅咒我的解决方案:)
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;
}