如何在c中的行中检查n个连续的相同字符
How to check for n consecutive same characters in row in c
所以我的任务是为 Connect-N 编写一个程序,这基本上是游戏 Connect-4,但用户指定行数和列数(不必是正方形)并指定数字连续获胜的棋子数(这不一定要放在棋盘上)。例如,如果用户指定一个 3x3 棋盘,他们也可以说需要连续 4 个棋子才能获胜。
我在编写一个程序来检查连续获胜时遇到了问题,在这种情况下,玩家完全水平地赢得了比赛。这是我目前拥有的:
bool horizontalWin(char **board, const int numRows,
const int numCols, const char blankSpace, const int numToWin) {
if((numCols - numToWin) < 0) {
return false;
}
else {
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col <= numCols-numToWin; ++col) {
///This is where I need help
}
}
}
return false;
}
仅供参考:变量 blankSpace 是一个'*',用于表示板上的空白space。
我的想法是有一个嵌套的 for 循环,它从第 0 列开始,然后向前检查足够远以查看它们是否都是相同的字符,但我似乎无法弄清楚如何实现这一点。有人能指出我正确的方向吗?
您可以设置两个计数器,一个用于红色圆盘,另一个用于黄色圆盘。然后在嵌套循环体中,每当遇到 r
时,红色圆盘计数器加一,黄色圆盘也是如此。
然后您可以在每一行迭代后检查这些计数器中的任何一个是否等于赢得游戏所需的给定 N
。
假设您用 r
表示红色磁盘,用 y
表示黄色磁盘:
else
{
int red_c, yellow_c;
for (int row = 0; row < numRows; ++row)
{
red_c=0; yellow_c=0;
for (int col = 0; col < numCols; ++col)
{
if(board[r][c]=='r') red_c++;
else if (board[r][c]=='y') yellow_c++;
if(red_c == numToWin )
{
//Red wins
}
else if(yellow_c == numToWin )
{
//Yellow wins
}
}
}
}
代码将取决于您是否想知道是否有人在棋盘中获胜,或者您是否在特定动作后获胜。
这是一个常见且简单的代码,您可以在每次移动后或对棋盘中的每个位置执行:
想法是从中心(棋子的新位置)向各个方向移动,使之像星号“*”(如星号)
consecutivesInHorizontal =0
consecutivesInVertical = 0
consecutivesDiagLeft =0
consecutiveDiagRight = 0
for i =1 ; i < height && i<width && i<= 4 ; i++
if(board[centerX][centerY+i] == myPieceColor)
consecutivesInVertical++;
if(board[centerX][centerY-i] == myPieceColor)
consecutivesInVertical++;
if(board[centerX+i][centerY] == myPieceColor)
consecutivesInHorizontal++;
if(board[centerX-i][centerY] == myPieceColor)
consecutivesInHorizontal++;
if(board[centerX+i][centerY-i] == myPieceColor)
consecutivesDiagLeft++;
if(board[centerX-i][centerY+i] == myPieceColor)
consecutivesDiagLeft++;
if(board[centerX-i][centerY+i] == myPieceColor)
consecutiveDiagRight++;
if(board[centerX+i][centerY-i] == myPieceColor)
consecutiveDiagRight
if any of the consecutive variables == 4
return true
计算匹配的次数,或在没有匹配时重置计数。
假设board[row][col]
,你检查的颜色是user1
for (int row = 0; row < numRows; ++row) {
int match = 0;
for (int col = 0; col <= numCols-numToWin; ++col) {
if (board[row][col] != user1) match = 0;
else {
match++;
if (match >= numToWin) return true;
}
}
}
请注意,您可以 if (++match >= numToWin) return true;
保存一行(如果您觉得它可读)。
这在问题描述中没有,但是如果你有两个玩家,应该有3种颜色,比如blankSpace
、user1
和user2
。上面的程序检查 user1
是否获胜。
所以你可以向函数添加一个参数来告诉你正在测试哪种颜色获胜(比如 colorCheck
),整个函数变为
bool horizontalWin(char **board, const int numRows,
const int numCols, const char blankSpace, const int numToWin, const in colorCheck) {
if((numCols - numToWin) < 0) {
return false;
}
else {
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col <= numCols-numToWin; ++col) {
if (board[row][col] != colorCheck) match = 0;
else {
match++;
if (match >= numToWin) return true;
}
}
}
}
return false;
}
所以我的任务是为 Connect-N 编写一个程序,这基本上是游戏 Connect-4,但用户指定行数和列数(不必是正方形)并指定数字连续获胜的棋子数(这不一定要放在棋盘上)。例如,如果用户指定一个 3x3 棋盘,他们也可以说需要连续 4 个棋子才能获胜。
我在编写一个程序来检查连续获胜时遇到了问题,在这种情况下,玩家完全水平地赢得了比赛。这是我目前拥有的:
bool horizontalWin(char **board, const int numRows,
const int numCols, const char blankSpace, const int numToWin) {
if((numCols - numToWin) < 0) {
return false;
}
else {
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col <= numCols-numToWin; ++col) {
///This is where I need help
}
}
}
return false;
}
仅供参考:变量 blankSpace 是一个'*',用于表示板上的空白space。
我的想法是有一个嵌套的 for 循环,它从第 0 列开始,然后向前检查足够远以查看它们是否都是相同的字符,但我似乎无法弄清楚如何实现这一点。有人能指出我正确的方向吗?
您可以设置两个计数器,一个用于红色圆盘,另一个用于黄色圆盘。然后在嵌套循环体中,每当遇到 r
时,红色圆盘计数器加一,黄色圆盘也是如此。
然后您可以在每一行迭代后检查这些计数器中的任何一个是否等于赢得游戏所需的给定 N
。
假设您用 r
表示红色磁盘,用 y
表示黄色磁盘:
else
{
int red_c, yellow_c;
for (int row = 0; row < numRows; ++row)
{
red_c=0; yellow_c=0;
for (int col = 0; col < numCols; ++col)
{
if(board[r][c]=='r') red_c++;
else if (board[r][c]=='y') yellow_c++;
if(red_c == numToWin )
{
//Red wins
}
else if(yellow_c == numToWin )
{
//Yellow wins
}
}
}
}
代码将取决于您是否想知道是否有人在棋盘中获胜,或者您是否在特定动作后获胜。
这是一个常见且简单的代码,您可以在每次移动后或对棋盘中的每个位置执行:
想法是从中心(棋子的新位置)向各个方向移动,使之像星号“*”(如星号)
consecutivesInHorizontal =0
consecutivesInVertical = 0
consecutivesDiagLeft =0
consecutiveDiagRight = 0
for i =1 ; i < height && i<width && i<= 4 ; i++
if(board[centerX][centerY+i] == myPieceColor)
consecutivesInVertical++;
if(board[centerX][centerY-i] == myPieceColor)
consecutivesInVertical++;
if(board[centerX+i][centerY] == myPieceColor)
consecutivesInHorizontal++;
if(board[centerX-i][centerY] == myPieceColor)
consecutivesInHorizontal++;
if(board[centerX+i][centerY-i] == myPieceColor)
consecutivesDiagLeft++;
if(board[centerX-i][centerY+i] == myPieceColor)
consecutivesDiagLeft++;
if(board[centerX-i][centerY+i] == myPieceColor)
consecutiveDiagRight++;
if(board[centerX+i][centerY-i] == myPieceColor)
consecutiveDiagRight
if any of the consecutive variables == 4
return true
计算匹配的次数,或在没有匹配时重置计数。
假设board[row][col]
,你检查的颜色是user1
for (int row = 0; row < numRows; ++row) {
int match = 0;
for (int col = 0; col <= numCols-numToWin; ++col) {
if (board[row][col] != user1) match = 0;
else {
match++;
if (match >= numToWin) return true;
}
}
}
请注意,您可以 if (++match >= numToWin) return true;
保存一行(如果您觉得它可读)。
这在问题描述中没有,但是如果你有两个玩家,应该有3种颜色,比如blankSpace
、user1
和user2
。上面的程序检查 user1
是否获胜。
所以你可以向函数添加一个参数来告诉你正在测试哪种颜色获胜(比如 colorCheck
),整个函数变为
bool horizontalWin(char **board, const int numRows,
const int numCols, const char blankSpace, const int numToWin, const in colorCheck) {
if((numCols - numToWin) < 0) {
return false;
}
else {
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col <= numCols-numToWin; ++col) {
if (board[row][col] != colorCheck) match = 0;
else {
match++;
if (match >= numToWin) return true;
}
}
}
}
return false;
}