-
Munkey01...
To get back to the first case for a moment if I could... You seem to have missed the point that you are declaring the array wrong (in the case of your original post with strings)...
It needs to be like this
char board[3][3][4]
You have 3 Rows, and 3 columns, each containing a 4 character string (including the 0 terminator).
The way that you had declared it only allowed for one character in each cell of the matrix. You need to allocate space for the 4 characters.
In the case of ints, as Salem has written it, each digit is stored in one int....
To do this with char you would be limited to 1 char per cell...
Code:
char board[3][3] = {{'1','2','3'},{'4','5,''6,'},{'7','8','9'}};
-
or an array of doubles
double board[3][3] = {0.1, 0.234, 0.45, 34.56, 0.9, 8.7, 0.00001, 9.3, 1.0};
int i, j;
for(i = 0; i < 3; ++i)
{
for(j = 0; j < 3; ++j)
{
cout << board[i][j] << ' ';
}
cout << endl;
}
-
Thanks so much for all of your help. I just realized what I had done, lol, I intialized the board in the class declaration (right word?) so I changed it so that the constructor initailized it. I also changed around some other stuff to make it more legible and reasonable. I am not done yet, but I did compile it to see what errors I would get and I had none!!! I ended up using an integer array still. I also, instead of using X and O i used 50 and 70 to substitute the numbers, just whenever I display the board I check to see if I need to display O or X or if I need to just display the number there.
I have also changed it like some of you mentioned, I used 1..9 instead of 01..22 ;)
The only thing left to do is the AI, lol. This will be my first program to include AI, but I might just go ahead and make a sperate version with two player capabilities until I finish my AI for the game, then once the AI is done I will implement them into the same game. Sound good?
If anyone can give me a little example of how I could design the AI? I could do it like I check for a win (search the board for that), but that seems like it would be rather ugly code.
-
Ok, I have it running :) But whenever I call Display() the program gets caught in an infinate loop. I cannot figure out why it is catching :( Any ideas? Plus I am not sure if this would even work, but I cannot tell when I have a whole bunch of numbers flying around.
Code:
for(int j=0; j<3; j++)
{
for(int i=0; i<3; i++)
{
if (board[j][i] == firstP)
cout << "\tX";
else if (board[j][i] == secP)
cout << "\tO";
else
cout << "\t" << board[j][i];
if (i % 3 == 0)
cout << endl;
}
}
-
This piece of code in itself won't create an infinate loop. It must be some other part of the logic. Better post the display function and the function that calls it... if not the whole thing.
-
Code:
void TickTacToe::Display()
{
for(int j=0; j<3; j++) //nested for loops to display board
{
for(int i=0; i<3; i++)
{
if (board[j][i] == firstP)
cout << "\tX";
else if (board[j][i] == secP)
cout << "\tO";
else
cout << "\t" << board[j][i];
if (i % 3 == 0) //checking for the end of the row
cout << endl;
}
}
MakeMove();
}
Function that calls it-
Code:
void TickTacToe::MakeMove()
{
Display();
if (CurPlayer == firstP)
cout << "\n\n\nIt's player one's turn.\n";
else
cout << "\n\n\nIt's player two's turn.\n";
cout << "\n\n\nSelect a location: ";
cin >> location;
if (CheckLocation() == 1) //if CheckLocation() returns a non-zero value
MakeMove(); //restart function
switch(location)
{
case 1: board[0][0] = CurPlayer; break; //CurPlayer claims the selected position
case 2: board[0][1] = CurPlayer; break;
case 3: board[0][2] = CurPlayer; break;
case 4: board[1][0] = CurPlayer; break;
case 5: board[1][1] = CurPlayer; break;
case 6: board[1][2] = CurPlayer; break;
case 7: board[2][0] = CurPlayer; break;
case 8: board[2][1] = CurPlayer; break;
case 9: board[2][2] = CurPlayer; break;
}
CheckWin();
if (CurPlayer == secP) //switch current player
CurPlayer = firstP;
else
CurPlayer = secP;
system("cls");
Display();
}
Oh, and since I posted MakeMove() could I use
Code:
if (!CheckLocation())
MakeMove();
Instead of
Code:
if (CheckLocation() == 1)
MakeMove();
-
I guess one of two things happened. No one can answer my question because of its complexity (very unlikely). Or it fell down a couple of pages so no one has seen it.
I know that there is a rule against bumping threads. But I didn't think creating a new thread would be appropriate.
-
it should be where you are calling MakeMove() at the end of display. since MakeMove() calls Display(), it runs through Display() and then calls MakeMove().
i.e.
MakeMove() -> Display() -> MakeMove() -> Display() -> ...
this will just keep going infinitely.
-
Ahhhh. That fixes that problem. Thanks man.
Now I need to fix the switch current player and to fix the display. But atleast I don't have to fix an infinate loop anymore.