phantom values in 2D array

using Win 7 64 bit enterprise, sp1.

I'm getting unwanted values assigned to a 2D array.


more info:
I'm working on a tic tac toe program as a method of building my array skills.
It's just text/ascii based command prompt output, in case you are wondering.

I have a 3x3 array for the actual grid values (to determine if there is a blank, an X or an O) based on x,y position as determined by GetAsyncKeyState to use arrows for square selection.

these x,y values I use to determine the array slot, square[xX][xY]
the xX and xY values range from 0-2 respectively depending on the game square the cursor is currently in.

The problem is, some of the array values get assigned to multiple array slots, and some don't get assigned at all, although 6 of the 9 squares function correctly. It is always the same squares.

(I've tried it on multiple PC's and get the same error)
I reset the array to 0's when the program starts.

I have a debugging output of this array's values that show on the screen so I can keep an eye on what it is doing.

Intended operation is to select a square and assign a value of 1 indicating an X. For example, select the center square and hit the enter key it will place an X in the center square and assign a value of 1 to square[1][1].
My debugging output shows
square[1][1]=1
as shown by this line of code from below
curPos(31,4); cout<<"square["<<xX<<"]["<<xY<<"]="<<square[xX][xY];
if I hit the up arrow, xY changes to 0, xX remains 1, and the cursor moves up one. If I hit enter, it will place an X in the upper center square. Debugging output shows
square[0][1]=1
, but my grid tracking the values square[][] will change to square [0][0]=1, square [0][1]=1, and square[0][2]=1. function square[xX][xY]=1 get's exercised only once that I can see, yet 3 elements in the array get assigned the value of 1.
square[0][0] when selected gets no value assigned.


Here is the code in question:
I think the offend variables are the xX and xY, and this is the only location in the entire code where they appear, aside from declaring them.

Hopefully my question and explanation are not too long winded and confusing. Better than yelling
HELP I have a problem!!!!!!
and give no further info.

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
    for (a=0; a<3; a++){//reset square[][] to 0
        for (b=0; b<3; b++)
        square[a][b]=0;
    }
    drawboard();//draw large # gameboard
    cX=9; cY=9; xX=1; xY=1; //start in center square
    drawcursor(cX,cY,cNum);//draw square selecting cursor with ascii char cNum

do {//gameplay and keystroke reading
            
        if(GetAsyncKeyState(VK_RETURN)==-32767){
            keydir='E';
            curPos(cX,cY);//move cursor to current selected square
            tokenX(cX,cY);//write 5x5 char X in selected square based on cursor position
            square[xX][xY]=1;//assign value of 1 to array square[xX][xY]
            }
        if(GetAsyncKeyState(VK_SPACE)==-32767){
            keydir='E';
            curPos(cX,cY);
            tokenO(cX,cY);
            square[xX][xY]=4;
            }
        if(GetAsyncKeyState(VK_UP)==-32767){
            keydir='U';
            drawcursor(cX,cY,255);//erase cursor
            cY=cY-8;//change cursor draw start location
            xY--;//change value for array location
            if (cY<0) cY=17;
            if (xY<0) xY=2;
            drawcursor(cX,cY,cNum);//draws the cursor in a new square
            }
        if(GetAsyncKeyState(VK_DOWN)==-32767){
            keydir='D';
            drawcursor(cX,cY,255);
            cY=cY+8;
            xY++;
            if (cY>17) cY=1;
            if (xY>2) xY=0;
            drawcursor(cX,cY,cNum);
            }
        if(GetAsyncKeyState(VK_LEFT)==-32767){
            keydir='L';
            drawcursor(cX,cY,255);
            cX=cX-8;
            xX--;
            if (cX<1) cX=17;
            if (xX<0) xX=2;
            drawcursor(cX,cY,cNum);
            }
        if(GetAsyncKeyState(VK_RIGHT)==-32767){
            keydir='R';
            drawcursor(cX,cY,255);
            cX=cX+8;
            xX++;
            if (cX>17) cX=1;
            if (xX>2) xX=0;
            drawcursor(cX,cY,cNum);
            }
        if(GetAsyncKeyState(VK_ESCAPE)==-32767){
            win=true;
            }//programming esc sequence. 
        else {
            //this block for debugging. This shows the xX, xY, and square[][] values
            curPos(31,3); cout<<"xX="<<xX<<"\txY="<<xY;
            curPos(31,4); cout<<"square["<<xX<<"]["<<xY<<"]="<<square[xX][xY];
            curPos(31,8); cout<<square[0][0]<<"\t"<<square[1][0]<<"\t"<<square[2][0];
            curPos(31,9); cout<<square[0][1]<<"\t"<<square[1][1]<<"\t"<<square[2][1];
            curPos(31,10); cout<<square[0][2]<<"\t"<<square[1][2]<<"\t"<<square[2][2];
            //end debugging block

}while (win==false);//end gameplay and keystroke 

Please provide a testcase. Try to make it minimal, removing all the code that's related to the interface.

Also, consider using a debugger to debug. You can watch your variable and it would interrupt the execution when it changes.
in what form would you like the test case, like a screen shot? I left all the code in that is relevant to the error, otherwise I could just omit the the 'else' statement.
as for the debugger, I'm still trying to figure out how it works. It was all 'greek' when I tried to use it, hence my own built in debugger.
fwiw, I use codeblocks currently.
> in what form would you like the test case, like a screen shot?
please no.
http://www.eelis.net/iso-c++/testcase.xhtml (points 6 and 7)
Something that can be built and tested.

Also, I would appreciate if you could omit the windows specific code.
darn power went out at my house just after I posted...still out. I'll get the code adjusted for you when I can. I realized just before you posted what you meant by a test case--been a long day.
Anyway, I have no idea what part is windows code and what is not. That said, what is the problem with windows specific code, unless it runs funny on Linux or OS10?
It wouldn't even compile

¿are you sure that you are using `GetAsyncKeyState()' correctly?
http://www.cplusplus.com/forum/windows/6632/#msg30578
works for me...but not all of the code is here. That said, I'm working on a test case right now...We'll see if it compiles then.
Ahh....Stripping the program down to bare bones to build a test case I discovered I declared my array incorrectly.
I wanted a 3x3 but I declared it int squares [2][2]

I was thinking it would be 3 numbers-- 0,1,2, so I got hung up on the 2. Works fine now.

I can still post the code if you are interested.

As for the `GetAsyncKeyState()', I don't know if I am doing it the best way or not. I did a forum search when I needed a keystroke function and that is what I dug up.
1
2
3
if(GetAsyncKeyState(VK_UP)==-32767){
do something
}


Thanks for slapping me around enough to scrutinize it more closely.
Topic archived. No new replies allowed.