Strange behavior

Nov 16, 2010 at 9:09pm
So, I coded this simple class:
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
#include <iostream>

using namespace std;

class FManager
{
    int field[3][3];
    bool checkWin(int, int);
    bool usedField(int, int);
    public:
    void move(int, int);
};

bool FManager::checkWin(int i, int j)
{
    if (field[i][++j] == 1 && field[i][--j] == 1) return true;
    else if (field[++i][j] == 1 && field[--i][j] == 1) return true;
    else if (field[++i][++j] == 1 && field[--i][--j] == 1) return true;
    else return false;
}

bool FManager::usedField(int s, int v)
{
    int counter1 = 3;
    int counter2 = 3;
    if (field[0][0] != 1 && field[0][0] != 0)
    {
        for(int i, j = 0; i < 2, j < 2; i++, j++)
        {
           field[i][j] = 0;
        }
    }

    if (field[s][v] != 1)
    {
        field[s][v] = 1;
        if (counter1 >= 3 || counter2 >=3)
        {
           checkWin(s, v);
        }
    }
    field[s][v] = 1;
    cout << field[2][2];
    return true;
}

void FManager::move(int m, int n)
{
    usedField(m, n);
    return;
}


It was going smooth, but now everytime I run it a error is generated and Windows asks me if I want to report it. I've read all the code twice but can't find the problem... Can someone help me?
Nov 16, 2010 at 9:38pm
closed account (1yvXoG1T)
My first guess would say that you might be overshooting your field. For example, in your checkWin function, if you passed 2 or higher to it then it will check beyond the scope of the array. I would also suggest running a debugger.

What code are you using to use this class?
Nov 16, 2010 at 9:41pm
*headdesk* Yeah, I was using field.move(3, 3), checking beyond the scope of the array >.< I'm an idiot. Should've checked that after reading the code twice and not noticing anything wrong. Oh well, thanks anyway, pal!
Topic archived. No new replies allowed.