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
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
, 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
|