检查2D数组中的4个连续的相同对角线元素(连接4个游戏) [英] Check 4 consecutive identical diagonal elements in 2D Array (Connect 4 game)
问题描述
我正在致力于在Java上实现connect 4 Game.我几乎完成了模拟游戏的程序.
I am working on implementing connect 4 Game on Java. I have almost finished the program that simulates the game.
我使用2D字符数组char [][] board = new char[6][7];
表示游戏的网格.
I use a 2D character array char [][] board = new char[6][7];
to represent the grid of the game.
我已经实现了checkHorizontal
方法来查找是否有4个连续的相同水平元素来检查获胜条件.我还实现了checkVertical
方法来查找是否有4个连续的相同垂直元素来检查获胜条件.
I have implemented checkHorizontal
method to find if there are 4 consecutive identical horizontal elements to check win condition. I have also implemented checkVertical
method to find if there are 4 consecutive identical vertical elements to check win condition.
我对checkDiagonal
方法的编写算法有点困惑,该算法检查2D数组中4个连续的相同对角线元素的所有可能性.
I am a little confused in writing algorithm of checkDiagonal
method that check all possibilities of 4 consecutive identical diagonal elements in 2D array.
以下是2个游戏对角线获胜案例的示例
Below are 2 examples for diagonal win case on game
情况1:
* * * * * * *
* * * * * * *
Y * * * * * *
R Y * * Y * *
Y R Y R Y R R
R Y R Y R Y R
情况2:
* * * * * * *
* * * * * * *
* * * * * R *
* * * * R Y *
* * * R Y R *
Y Y R Y R Y R
如何检查rows
和columns
以解决这些情况?
How can I check rows
and columns
to solve these cases?
推荐答案
您只需要检查放置新的type
类型棋子的位置,因为其余的游戏场保持不变.在这里,您可以执行以下操作:
You only need to check where a new piece of type type
has been placed, as the rest of the game field stays the same. There, you can do something like this:
/**
* Counts pieces of the given type, starting at (y, x),
* in the direction denoted by (dy, dx).
* Stops at field boundaries or when a different field type is encountered.
*/
int count(char type, int x, int y, int dx, int dy) {
int count = 0;
x += dx; // Skip the piece at (y, x) to avoid counting it twice
y += dy; // when looking in both directions on a line.
while (x >= 0 && x < 7 && y >= 0 && y < 6 && board[x][y] == type) {
count++;
x += dx; // Move in the direction denoted by (dy, dx)
y += dy;
}
return count;
}
/**
* Main entry point after a new piece of type `type` was added at (y, x).
* Returns true if this connects 4 or more in any direction.
*/
boolean check(char type, int x, int y) {
return count(type, x, y, -1, 0) + 1 + count(type, x, y, 1, 0) >= 4 // horizontal
|| count(type, x, y, 0, -1) + 1 + count(type, x, y, 0, 1) >= 4 // vertical
|| count(type, x, y, -1, -1) + 1 + count(type, x, y, 1, 1) >= 4 // diagonal
|| count(type, x, y, -1, 1) + 1 + count(type, x, y, 1, -1) >= 4);
}
dx和dy检查参数用于在不同方向上移动,而不必为每个方向使用单独的方法.
The dx and dy check parameters are used to move in different directions without having a separate method for each direction.
在水平检查代码中,您可以通过在循环中将x加1来移动到下一段(保持y不变,即将y加0).在垂直检查代码中,将y加1(将x加0)将移至下一个代码.要沿对角线移动,需要在x和y坐标上都加1.
In your horizontal check code, you probably move to the next piece by adding 1 to x in a loop (keeping y constant, i.e. add 0 to y). In your vertical check code, you move to the next piece by adding 1 to y (and 0 to x). To move diagonally, you need to add 1 to both, the x and y coordinates.
为了能够使用一种方法检查所有方向,check()使用了运动方向的参数:dx = 1和dy = 0在每一步中加1到x和0到y,因此您执行水平检查.在dx = 0且dy = 1的情况下,您将进行垂直检查.
To be able to check all directions with a single method, check() uses parameters for the direction of the movement: dx = 1 and dy = 0 adds 1 to x and 0 to y in each step, so you do a horizontal check. With dx = 0 and dy = 1, you do a vertical check.
编辑:省去了检查助手,因为它仅在一个地方才真正需要
Edit: Got rid of the check helper, as it was only really needed in one place
这篇关于检查2D数组中的4个连续的相同对角线元素(连接4个游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!