连接-4类游戏的赢得条件 [英] Win conditions for a connect-4 like game
问题描述
我有一个5x10的数组,填充随机值1-5。我想要能够检查3个数字,水平或垂直匹配。
I have an 5x10 array that is populated with random values 1-5. I want to be able to check when 3 numbers, either horizontally, or vertically, match. I can't figure out a way to do this without writing a ton of if statements.
这里是随机填充的数组的代码
Here is the code for the randomly populated array
int i;
int rowincrement = 10;
int row = 0;
int col = 5;
int board[10][5];
int randomnum = 5;
int main(int argc, char * argv[])
{
srand(time(NULL));
cout << "============\n";
while(row < rowincrement)
{
for(i = 0; i < 5; i++)
{
board[row][col] = rand()%5 + 1;
cout << board[row][col] << " ";
}
cout << endl;
cout << "============\n";
row++;
}
cout << endl;
return 0;
}
推荐答案
假设你有一些特定的起点(x,y),你很好奇,如果有一个行中有三个相等的数字从这一点开始。让我们考虑一下你在水平方向看的情况。然后,一个方法(忽略边界检查)将是这样:
Suppose that you have some particular starting point (x, y) and you're curious if there's three equal numbers in a row that start at this point. Let's consider just the case where you're looking in the horizontal direction. Then one way to do this (ignoring bounds-checking) would be like this:
bool IsHorizontalMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
你可以像这样写一个垂直检查的函数:
You could similarly write a function like this for checking vertically:
bool IsVerticalMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x][y + i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
最后,一个对角线:
bool IsDiagonalDownMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y + i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
bool IsDiagonalUpMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y - i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
所有这三个功能看起来非常相似!幸运的是,您可以根据单个统一函数重写所有这些。想法是这 - 如果你会注意到,所有三个功能工作通过一些步长定义指示你移动的方向。在水平情况下,步长是(+1,+0),在垂直情况下是(+0,+1),在对角线上是(+1,+1)或(+1,-1)。给定这个,你可以写一个函数来检查三个值是否匹配在一行:
This works, but it's just not very elegant; all three of these functions look very similar! Fortunately, you can rewrite all of them in terms of a single unifying function. The idea is this - if you'll notice, all three functions work by having some "step size" defined indicating what direction you move. In the horizontal case, the step is (+1, +0), in the vertical case it's (+0, +1), and in the diagonal it's (+1, +1) or (+1, -1). Given this, you can write one function to check if three values match in a line:
bool IsLinearMatch(int x, int y, int stepX, int stepY) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i * stepX][y + i * stepY] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
然后您可以写
bool IsLineStartingAt(int x, int y) {
return IsLinearMatch(x, y, 1, 0) || // Horizontal
IsLinearMatch(x, y, 0, 1) || // Vertical
IsLinearMatch(x, y, 1, 1) || // Diagonal Down
IsLinearMatch(x, y, 1, -1) || // Diagonal Up
}
给定这个原语,你可以检查所有可能的匹配只要迭代所有可能的起点。
Given this primitive, you can check for all possible matches by just iterating over all possible starting points.
希望这有助于!
编辑:感谢评论者帮助修复我的愚蠢的bug。 : - )
Thanks to commenters for helping to fix my silly bugs. :-)
这篇关于连接-4类游戏的赢得条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!