scope errors

Hi guys I am writing a new Tic Tac Toe game. I have created a function called computer_move. It suppose to do as the name says find a move for the computer. It does three basic checks. One it looks to find if there is a single move it can take to win its always O second it looks for a single move that the player X can do to win and if found take that spot and finally if it cant find winners it chose the best open spot. My problem is that when found it true all my while loops should break and the computer_move takes a spot. But It is getting stuck in loop and I think it has to do with the scope of a while loop but I am not sure! Any help and suggestion would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  void computer_move()
{
    cls();
    int comp_col[9] = {1,0,0,2,2,0,2,1,2};
    int comp_row[9] = {1,0,2,0,2,1,1,2,1};
    int cols = 0;
    int rows = 0;
    int up = 0;
    bool found = false;
    string temp;

    //check for O winner
    while(!found && up<8)
    {
        cols = comp_col[up];
        rows = comp_row[up];
        if(validate(rows,cols))
        {
            TicTac[rows][cols] = "O";
            temp = Check("O",2);
            if(temp == "winner")
            {
                found = true;
                player ="X";

            }

        }
        else
        {
            TicTac[rows][cols] = " ";
            up++;
        }
    }

    //reset
      cols = 0;
      rows = 0;
      up = 0;



    //check for X winner

    while(!found && up<8)
    {
        cols = comp_col[up];
        rows = comp_row[up];
        if(validate(rows,cols))
        {
            TicTac[rows][cols] = "X";
            temp = Check("X",2);
            if(temp == "winner")
            {
                TicTac[rows][cols] = "O";
                player ="X";
                found = true;
            }

        }
        else
        {
            TicTac[rows][cols] = " ";
            up++;
        }
    }
    cols = 0; 
    rows = 0;
    up = 0;


    //best move if no win can be found

    while(!found)
    {
       cols = comp_col[up];
       rows = comp_row[up];

       if(validate(rows,cols))
       {
           TicTac[rows][cols] = "O";
           found = true;
       }
       else{up++;}
    }
}
What does validate(rows, cols) do? My guess is that validate() is returning true which enters the if-block then temp != "winner" so found never gets set to true, up never gets incremented and the loop just runs forever.
Last edited on
this is the code for validate:

bool validate( int x, int y)
{
if(TicTac[x][y] != "X" && TicTac[x][y] != "O") {return true;}
else { return false;}
}

in a nut shell this code returns true if the TicTac space is empty and false if it has X or O in it.
Yeah, so what I believe is happening is that you call validate() and it returns true because the space is empty so you enter the if-block. Inside the if-block you check to see if temp equals "winner". If temp does not equal "winner" then found never gets set to true and you never increment the up variable.
Last edited on
You could always self debug by adding print outs.

First thing i'd do is print out "true" or "false", depending on whether or not <b>found</b> was true or false.

In your program, if it was false, it would increment up by one and test once again - never exiting unless a spot was found?

What happens if no spots are left?

When does it infinite loop? During first move? Last move?

Is there a possibility that it is infinite looping at a false?

=D
Topic archived. No new replies allowed.