1
\$\begingroup\$

Below is the code I'm using to check if the board state has a tic tac toe style victory state. The board is always square and is made of an array of objects that keep track of their own completion state (the variable board[x,y].complete is a boolean on if that node is currently complete) The current method is to see if the player can count to the width of the array in a single pass and if so use breaks to skip any future necessary checks.

But at the moment the code either ignores legitimate win states or falsely detects win states.

I'd be appreciative if anyone can give me pointers on what might improve the code or help solve for arbitrarily sized square boards.

///check to see if the player has bingo

var board = global.board;
var score_ = 0;
var row = 0;
var col = 0;
var victory = false;

//check horrozontal complete
for (col = 0; col < array_height_2d(board); col++)
{
    if victory == true
    {
        break;
    }
    for (row=0; row<array_height_2d(board); row++)
    {
        if board[row,col].complete == true
        {
            score_ += 1;    
        }
    }
    if score_ == array_height_2d(board)
    {
        victory = true;
        break;
    }
    score_ = 0;
    score2 = 0;
}

//vertical checks
score_ = 0;
for (row = 0; row < array_height_2d(board); row++)
{
    if victory == true
    {
        break;
    }
    for (col=0; col<array_height_2d(board); col++)
    {
        if board[row,col].complete == true
        {
            score_ += 1;    
        }
    }
    if score_ == array_height_2d(board) 
    {
        victory = true;
        break;
    }
    score_ = 0;
}

//diagonal checks
score_ = 0;
var diag = 0;
for (diag = 0; diag< array_height_2d(board); diag++)
{
    if victory == true
    {
        break;
    }
    if board[diag,diag].complete == true
    {
        score_ += 1;
    }
    if score_ == array_height_2d(board) 
    {
        victory = true;
        break;
    }
}
score_ = 0;
diag = array_height_2d(board);
while diag > -1
{
    if victory == true
    {
        break;
    }
    if board[diag,diag].complete == true
    {
        score_ += 1;
    }
    if score_ == array_height_2d(board) 
    {
        victory = true;
        break;
    }
    diag -= 1;
}
score_ = 0;

return victory;

Examples of arrays that fail to detect a win state:

the basic win states

\$\endgroup\$

closed as off-topic by DMGregory Sep 14 '18 at 0:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem must provide a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog. For more information, see this meta thread." – DMGregory
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I know I can simplify the if statements from: if board[row,col].complete == true to if board[row,col].complete I added the extra clarification when the code didn't appear to work at first \$\endgroup\$ – Robert Tester May 7 '18 at 9:27
  • \$\begingroup\$ "But at the moment the code either ignores legitimate win states or falsely detects win states." - can you provide examples which are failed to be detected and those with false positives? Is it the 3x3 version? \$\endgroup\$ – wondra May 7 '18 at 10:32
  • \$\begingroup\$ It's being tested on a 5x5 grid. Examples of it failing are the top row or far left column are all marked as complete and the code doesn't produce a positive result \$\endgroup\$ – Robert Tester May 7 '18 at 10:36
  • \$\begingroup\$ How about encode those ways to win in a array and verify if any of those arrays match with the game array? \$\endgroup\$ – Rafael Campos Nunes May 7 '18 at 11:47
  • \$\begingroup\$ Probably won't work as the array I'm testing is an array of objects, not just of boolean values \$\endgroup\$ – Robert Tester May 7 '18 at 11:54
0
\$\begingroup\$

The code mysteriously started working again when I added more debug flags to it. Not sure what was wrong but if you need a solution to a similar issue, the above should work as a template.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.