问题描述
我正在开发具有7x7场,水平和垂直检查的Connect 4游戏,但是我没有得到对角线检查的工作
I am developing a Connect 4 game with 7x7 fields, horizontal and vertical checks but I dont get diagonal checks works
只要其中一个标记不在最后一列,我就可以赢.
I can win as long as one of the tokens is not in the last column.
所有这些检查:
private static int getWinningInDiagonals() {
// Top-Left -> Bottom-Right
for (int column = 0; column < 7; column++) {
int count = 0;
for (int row = 0; row < 7; row++) {
if (field[row][column] != 0 && field[row+1][column + row - 1] == field[row][column])
count++;
else
count = 1;
if (count >= 4) {
return field[row][column];
}
}
}
// Top-Right -> Bottom-Left
for (int column = 0; column < 7; column++) {
int count = 0;
for (int row = 0; row < 7; row++) {
if (field[row][column] != 0 && field[row+1][column - row + 1] == field[row][column])
count++;
else
count = 1;
if (count >= 4) {
return field[row][column];
}
}
}
return 0;
}
推荐答案
对于每次要同时右移和右移的人,您也只需要转到3、3,因为对角线不会再出现比没有它离开数组边界的情况更是如此.
For one you want to move down and right at the same time every time, you also only need to go to 3, 3 as a diagonal cannot occur any farther than that without it leaving the bounds of the array.
如果我正确地假设您的左上角是您的0,0,那么这应该适用于您的左上角到底部右角.从右上到左下是更改列和行循环以及更改偏移量的方法.
This should work for your top left to bottom rights if I'm right in assuming that your top left is your 0,0.Doing top right to bottom left is a matter of changing the column and row loops and changing how the offset works.
// Top-Left -> Bottom-Right
for (int column = 0; column < 4; column++) {
for (int row = 0; row < 4; row++) {
player = 0;
if (field[row][column] != 0){
player=field[row][column];
offset = 1;
}
while (player != 0){
if (field[row + offset][column + offset] == player){
offset += 1;
}else{
player = 0;
}
}
if(offset >= 4){
return field[row][column];
}
}
}
这篇关于Connect 4中的对角线赢钱支票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!