简体   繁体   English

在 NxN 井字游戏(C 程序)中检查获胜者

[英]Check for winner in an NxN tic tac toe game (C program)

I have a homework where I have to create an N x N tic tac toe game in C program.我有一个作业,我必须在 C 程序中创建一个 N x N tic tac toe 游戏。 I've been working on it for two days now and my only problem is to check for winners.我已经为此工作了两天,我唯一的问题是检查获胜者。

Can anyone give me suggestions on how can I check for horizontal win/ diagonal etc.?谁能给我关于如何检查水平赢/对角线等的建议?

The size of the board is N x N where the user determines the size.棋盘的大小是 N x N,用户决定大小。
I have made an array board[i][j] where I save the movements of the players.我制作了一个阵列板[i][j],用于保存玩家的动作。 I am open for any suggestions on how may I do this horizontal check or diagonal etc.我愿意就如何进行水平检查或对角线等提出任何建议。

I tried doing something like this for horizontal check我试着做这样的事情来进行水平检查

void horizontalwin()
{
    for (r=0; r<size; r++){
        for(c=0; c<size; c++){
            for(int n=1; n<=size; n++)
        if(board[r][c]==board[r][c+n]&& board[r][c]!='_')
          win=1;
          break;
        }
    }
}

but this way it stops checking if it finds two columns and it doesn't go to the end of the row to check if all columns in the row have a match.但是这样它会停止检查是否找到两列,并且不会到行的末尾检查行中的所有列是否都匹配。

Depending on how the 2D array board is defined, one potential problem is here:根据 2D 阵列board的定义方式,这里存在一个潜在问题:

if(board[r][c]==board[r][c+n]&& board[r][c]!='_')  
                          ^^    

For example, if board is defined somewhere as:例如,如果 board 在某处被定义为:

int board[size][size] = {0};  

Then:然后:

If c can grow to size-1如果c可以增长到size-1
And if r can grow to size-1如果r可以增长到size-1
Then if n is greater than 0那么如果n大于0
board[r][c+n] will exceed the 2nd index array bounds. board[r][c+n]将超过第二个索引数组边界。 This will cause undefined behavior .这将导致未定义的行为

Also, the phrase:还有一句话:

if(board[r][c]==board[r][c+n]&& board[r][c]!='_')
  win=1;
  break;

will break even if win does not equal 1 .即使win不等于1也会打破。

It should probably be written:大概应该这样写:

if(board[r][c]==board[r][c+n]&& board[r][c]!='_')
{
    win=1;
    break;
}

Three nested loops seems excessive.三个嵌套循环似乎过多。 You should only need two nested loops to check for a horizontal or vertical win.您应该只需要两个嵌套循环来检查水平或垂直赢。 You only need a non-nested loop to check for diagonal wins.您只需要一个非嵌套循环来检查对角线获胜。

For horizontal checking, you can check each row in turn.对于横向检查,您可以依次检查每一行。 If all columns on the row contain the same, non-empty value (ie not the '_' character), then return a win.如果行上的所有列都包含相同的非空值(即不是'_'字符),则返回一个胜利。 After checking all rows, return no win.检查所有行后,返回 no win。

Since this is homework, I will just show how to check a single row for a win.由于这是家庭作业,我将只展示如何检查单行是否获胜。 Basically, it involves checking all elements in the row but one (for example, skip the first element) to see if it is either empty or different to its neighbour (the previous element if the first element was skipped).基本上,它涉及检查行中的所有元素,但有一个(例如,跳过第一个元素)以查看它是否为空或与其相邻元素不同(如果跳过第一个元素,则为前一个元素)。 If any of the tests fail, then there is no win on that row.如果任何测试失败,则该行没有胜利。

        for (c = 1; c < size; c++) {
            if (board[r][c] == '_' || board[r][c] != board[r][c-1]) {
                break; // no winner on this row
            }
        }
        if (c == size) {
            // board[r][0] is the winner!
            // do something here to return the winner.
        }
        // else: no winner on row r
        // continue iterating over the remaining rows.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM