Hi! Total c++ newbie here. I'm trying to make a tic-tac-toe game for my c++ class and I've made pretty good progress, but I think I'm finally stuck. Here's what I've got:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void recordMove(int currentTurn, int &xloc, int &yloc, char boardLoc, char boardContents[][3])
{
convert1d2d(boardLoc, xloc, yloc, boardContents);
while ((boardLoc<49) || (boardLoc>57) || (yloc==3))
{
cout << "\nError, please enter a number designating a valid open space. ";
cin >> boardLoc;
convert1d2d(boardLoc, xloc, yloc, boardContents);
cout << endl;
}
if (currentTurn%2==0)
boardContents[xloc][yloc]='X';
else
boardContents[xloc][yloc]='O';
}
My code compiles and the program runs fine past calls of this function. The valid entry check works as expected. The problem is that the array boardContents[][3] is unchanged after it is called (judging by a simple print function that executes afterwards):
It seems that, failing all else, array position 0,0 should be updated to 'O', but instead nothing is changing, and I can't figure out why. Aren't arrays passed by reference by default?
Arrays decay to pointers when passed to functions so you're right in thinking their values should be modified.
What are these convert1d2d calls doing? Converting the user choice (which appears to be 1-9) to array indices? Have you tried checking the value of xloc and yloc after this call?
void convert1d2d(char boardLocation, int &xlocation, int &ylocation, char boardContents[][3])
{
ylocation=0;
while ((boardContents[xlocation][ylocation]!=boardLocation) && (ylocation<3))
{
xlocation=0;
while ((xlocation<3) && (boardContents[xlocation][ylocation]!=boardLocation))
xlocation++;
ylocation++;
}
}
It's supposed to accept 1-9 (the array is initialized to those char values) and figure out where in the array the program needs to be manipulating. If it fails to find a match (i.e. there is already an 'X' or 'O' in that slot, the flag is set to 1. I realized I screwed up my flagging method (fixed here and above). Now whenever I select 1 on the first turn, it puts an 'X' there. Everything else triggers the invalid entry message. Progress!
edit: also, I don't really know how to check values of things :S
The best way to check the value of things is to put a breakpoint at that point in the code and run it through a debugger. It'll halt when it gets to your break and you'll be able to see the current state of the variables.
Another way of doing this - some programmers might curse me for it - is to just spew something to stdout. So before your check to see if current turn is even or not you could have:
std::cout << "xloc is " << xloc << ", yloc is " << yloc << std::endl;
For what it's worth, this seems more complicated than it could be. I'm not usually an advocate of 2D arrays - most of the time a simple 1D array would suffice. I find a one dimension array of nine elements easier to traverse and manipulate than two arrays of three.
Yeah, the assignment specifically said to use a 2d array :/ Although it did make it a lot easier to write the function that indicates when the win condition is met (I didn't have to check every possible 3-in-a-row for every turn):
bool checkWin(int currentTurn, int xloc, int yloc, char boardContents[][3])
{
int counter=0,sum=0, diag1, diag2;
bool win=0;
if (currentTurn>4)
{
while (counter<3)
{
if (boardContents[xloc][counter]<60)
break;
sum+=boardContents[xloc][counter];
counter++;
}
if ((sum==237)||(sum==264))
win=1;
else
{
sum=0;
counter=0;
while (counter<3)
{
if (boardContents[counter][yloc]<60)
break;
sum+=boardContents[counter][yloc];
counter++;
}
}
if ((sum==237)||(sum==264))
win=1;
elseif (((xloc+yloc)%2)==0 && boardContents[2][2]>60)
{
diag1=boardContents[0][0]+boardContents[2][2]+boardContents[3][3];
diag2=boardContents[0][3]+boardContents[2][2]+boardContents[3][0];
if ((diag1==237)||(diag1==264)||(diag2==237)||(diag2==264))
win=1;
}
return win;
}
}
I dunno if it works yet, but I guess I'll find out once I've got the basic game working. I'll try your cout debugging method and see what happens. I've gotten to the point where entries in the first row all work, so maybe that'll help me get the rest of them up and running. Thanks for your help!
edit: Ha! Got it working. Now to wade through that mess up there ^^
edit 2: success!