现在我陷入对角线检查,我不知道该怎么做。 这是我的检查功能:
public boolean checkWinRows() {
int sameDiscs;
Player lastPlayerField = gameField[0][0].getPlayer();
for(int i = 0; i < gameField.length; i++) {
sameDiscs = 0;
for(int j = 0; j < gameField[i].length; j++) {
sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
lastPlayerField = gameField[i][j].getPlayer();
if(sameDiscs == 4) {
// set winner
lastPlayerField.setWon(true);
return true;
}
}
}
return false;
}
public boolean checkWinCols() {
int sameDiscs;
Player lastPlayerField = gameField[0][0].getPlayer();
for(int j = 0; j < gameField[0].length; j++) {
sameDiscs = 0;
for(int i = 0; i < gameField.length; i++) {
sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
lastPlayerField = gameField[i][j].getPlayer();
if(sameDiscs == 4) {
// set winner
lastPlayerField.setWon(true);
return true;
}
}
}
return false;
}
private int countSameDiscs(int sameDiscs, boolean samePlayer, Field field) {
if(field.getPlayer() != null) {
if(samePlayer)
sameDiscs ++;
else
sameDiscs = 1;
} else
sameDiscs = 0;
return sameDiscs;
}
现在我不确定如何进行对角线检查。我知道我可以查看所有列和行,但是如何计算对角放置的标记?
问候
修改
以下是我的两个对角线检查功能:
public boolean checkWinDiagonal1(int row, int column) {
int i=0, j=0;
int iterateCount= 4;
int sameDiscs;
Player lastPlayerField = gameField[0][0].getPlayer();
for( i=row, j=column; iterateCount>0; i++, j--, iterateCount--) {
sameDiscs = 0;
sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
lastPlayerField = gameField[i][j].getPlayer();
if(sameDiscs == 4) {
// set winner
lastPlayerField.setWon(true);
return true;
}
}
return false;
}
public boolean checkWinDiagonal2(int row, int column) {
int i=0, j=0;
int iterateCount= 4;
int sameDiscs;
Player lastPlayerField = gameField[0][0].getPlayer();
for( i=row, j=column; iterateCount>0; i++, j++, iterateCount--) {
sameDiscs = 0;
sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
lastPlayerField = gameField[i][j].getPlayer();
if(sameDiscs == 4) {
// set winner
lastPlayerField.setWon(true);
return true;
}
}
return false;
}
我收到此错误消息:线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException
答案 0 :(得分:0)
连接4游戏是一个7 * 6矩阵,因此我们可以解决它如下
矩阵条目
00 01 02 03 04 05 06
10 11 12 13 14 15 16
20 21 22 23 24 25 26
30 31 32 33 34 35 36
40 41 42 43 44 45 46
50 51 52 53 54 55 56
对角线中连接4的匹配可能发生在前后对角线上。
前进对角线
正对斜线的对角线&#34; /&#34;在这里被称为前对角线。
示例匹配 03,12,21,30或23,32,41,50或26,35,44,53等等
让我们想出一个数组,其中包含前对角线的起始行和列条目
应该是03,04,05,06,16,26。
在这个数组中,我添加了计数,因此很容易迭代并找到匹配
前向对角线:
[(0,3,4),(0,4,5),(0,5,6),(0,6,6),(1,6,5),(2,6,4) )] 读为行,列和计数
迭代forward diagonals数组并调用forwardDiagonals方法
public boolean forwardDiagonals(int row, int column, int iterateCount){
int i=0, j=0;
for( i=row, j=column; iterateCount>0; i++, j--, iterateCount--) {
// Matching logic goes here
}
}
向后对角线
反斜杠后面的对角线&#34; \&#34;在这里被命名为向后对角线。
示例匹配 03,14,25,36或20,31,42,53或11,22,33,44等
让我们想出一个数组,其中包含向后对角线的起始行和列条目
应该是20,10,00,01,02,03。
此数组遵循相同的方法,我们有迭代计数,因此很容易迭代并找到匹配
向后对角线:
[ (2,0,4), (1,0,5), (0,0,6), (0,1,6), (0,2,5), (0,3,4)]
read as row, column and count
迭代向后对角线数组并调用backwardDiagonals方法
public boolean backwardDiagonals(int row, int column, int iterateCount){
int i=0, j=0;
for( i=row, j=column; iterateCount>0; i++, j++, iterateCount--) {
// Matching logic goes here
}
}