Tic Tac Toe Help

Hi guys I'm trying to write a simple two player game of Tic Tac Toe that has a board with numbers 1 through 9 on it. when player one hits the 1 key the one turns into an X, and when player 2 hits a key that corresponding number turns into an O. Ive been coding for about a month and I'm on the classes section, which I dont understand at all by the way. People have posted that they have been coding for two weeks and they have a tic tac toe game and the code made no sense to me. Please help me this is killing my confidence level. This is what I have so far.

#include <iostream>
using namespace std;
int main()
{
int slot[3][3]={1,2,3,4,5,6,7,8,9};

cout<<slot[0][0]<<" "<<slot[0][1]<<" "<<slot[0][2]<<endl;
cout<<slot[1][0]<<" "<<slot[1][1]<<" "<<slot[1][2]<<endl;
cout<<slot[2][0]<<" "<<slot[2][1]<<" "<<slot[2][2]<<endl;

system("pause");
}
It sucks that you have such a confidence-killer to have to deal with. :/

How well do you understand pointers? You could keep a 1-D array of 9 pointers to each of the elements of your board, which might help. Alternatively, you could go over each element of your 2-D array and check its value against what the player entered.

Either way, good luck, and please don't hesitate to post with more questions.

-Albatross
I started with a 1-D array with a single pointer but I didnt know how to make it work. Do I really need 9 pointers? and thanks for the help I really appreciate it.
Well, that was just one way of doing it. The advantage of doing that would be that when you get from a user a certain number (like 8), you could just plug it into your 1-D array to get a pointer to the element you want.

I suppose you could also use slot[request/3][request%3] and bypass the 1-D array of pointers and the search, but only if you understand how it works. ;)

-Albatross
P.S.- Wouldn't it be better to have the array made out of chars?
"this doesnt Work, I dont know where to go from here"
#include <iostream>
using namespace std;
char slot[9]={'1','2','3','4','5','6','7','8','9'};
char *P=slot;
char player_turn='1';

void print_board()
{
cout<<slot[0]<<" "<<slot[1]<<" "<<slot[2]<<endl;
cout<<slot[3]<<" "<<slot[4]<<" "<<slot[5]<<endl;
cout<<slot[6]<<" "<<slot[7]<<" "<<slot[8]<<endl;
}
int main()
{
char slot[9]={'1','2','3','4','5','6','7','8','9'};
char *P=slot;
char player_turn='1';

print_board();
cout<<"player "<<player_turn<<"'s turn: ";
cin>>*P;
if (*P=1)
slot[0]='X';
else if (*P=2)
slot[1]='X';
else if (*P=3)
slot[2]='X';
else if (*P=4)
slot[3]='X';
else if (*P=5)
slot[4]='X';
else if (*P=6)
slot[5]='X';
else if (*P=7)
slot[6]='X';
else if (*P=8)
slot[7]='X';
else if (*P=9)
slot[8]='X';

print_board();
system("pause");
}
Last edited on
You're using the wrong equals in your ifs/else ifs, for starters.

Also, what I meant by using pointers was to have a 1-D array of pointers to the elements of a 2-D array. I'd suggest changing the type of P from a pointer to a regular variable.

Fix those and I think you'll be fine for now.

-Albatross
Last edited on
Like this???/
char slot[3][3]={'1','2','3','4','5','6','7','8','9'};
char *pointer[9]={slot[0][0], slot[0][1],slot[0][2], slot[1][0],slot[1][1], slot[1][2],slot[2][0], slot[2][1],slot[2][2];
I think you'll get a type error if you do that. You need to have ampersands (&) before all of the slot[0][0]s and slot[0][1]s and so on. Also, you're missing a bracket. :)

-Albatross
I'm going okay so far, but couldnt I just have two 1D arrays?
"okay stuck again"
#include <iostream>
using namespace std;
char slot[3][3]={'1','2','3','4','5','6','7','8','9'};
char *pointer[9]={&slot[0][0], &slot[0][1], &slot[0][2], &slot[1][0], &slot[1][1], &slot[1][2], &slot[2][0], &slot[2][1], &slot[2][2]};
char player_turn='1';

void print_board()
{
cout<<*pointer[0]<<" "<<*pointer[1]<<" "<<*pointer[2]<<endl;
cout<<*pointer[3]<<" "<<*pointer[4]<<" "<<*pointer[5]<<endl;
cout<<*pointer[6]<<" "<<*pointer[7]<<" "<<*pointer[8]<<endl;
}
int main()
{


print_board();
system("pause");
}
Stuck at what?

See my reply in your other thread:

http://www.cplusplus.com/forum/beginner/61822/#msg334202
Last edited on
Im not sure thats the problem. So I have the board. Now I need to have a player input a number then that number changes to an X or and O. I have no idea where to begin
Start by creating out some way to figure out from the user's input which element he/she wants to change. There are at least three possibilities: one uses a 1D array and pointers, one uses a 2D array and a search, and one uses a 2D array without a search.

You may want another function for doing this. Here's a template:
1
2
3
4
char& get_tile(int pos) {
    //Your code here.
    return /*place a slot[][] or some other char variable here*/;
}


Can you do that?

-Albatross
well I dont really know what happened but I understand it now. I cant use pointers or anything because it never works but I have this so far, and I apoligize for the lack of code tags I dont know how to do them. Though when I run this and enter player_move it just exits. What is wrong??
#include <iostream>
using namespace std;
char slot[3][3]={'1','2','3','4','5','6','7','8','9'};
char *pointer[9]={&slot[0][0], &slot[0][1], &slot[0][2], &slot[1][0], &slot[1][1], &slot[1][2], &slot[2][0], &slot[2][1], &slot[2][2]};
char player_turn='1';
char X_or_O;
char Player_Move;
bool valid_move=true;
bool game_over=false;
void print_board();
void Marker_Set();


void print_board()
{
cout<<slot[0][0]<<" "<<slot[0][1]<<" "<<slot[0][2]<<endl;
cout<<slot[1][0]<<" "<<slot[1][1]<<" "<<slot[1][2]<<endl;
cout<<slot[2][0]<<" "<<slot[2][1]<<" "<<slot[2][2]<<endl;
}
void Marker_Set()
{
if (X_or_O==player_turn)
X_or_O='X';
else
X_or_O='O';
}
int main()
{
do
{
print_board();
Marker_Set();
cout<<"player "<<player_turn<<"'s turn: ";
do
{
cin>>Player_Move;
if (Player_Move=='1' && slot[0][0]=='1')
slot[0][0]=X_or_O;
else if (Player_Move=='2' && slot[0][1]=='2')
slot[0][1]=X_or_O;
else if (Player_Move=='3' && slot[0][2]=='3')
slot[0][2]=X_or_O;
else if (Player_Move=='4' && slot[1][0]=='4')
slot[1][0]=X_or_O;
else if (Player_Move=='5' && slot[1][1]=='5')
slot[1][1]=X_or_O;
else if (Player_Move=='6' && slot[1][2]=='6')
slot[1][2]=X_or_O;
else if (Player_Move=='7' && slot[2][0]=='7')
slot[2][0]=X_or_O;
else if (Player_Move=='8' && slot[2][1]=='8')
slot[2][1]=X_or_O;
else if (Player_Move=='9' && slot[2][2]=='9')
slot[2][2]=X_or_O;
else
{ cout<<"Invalid move, have another go.";
valid_move=false; }
}
while (valid_move=false);

if (slot[0][0]==slot[0][1] && slot[0][1]==slot[0][2])
game_over=true;
if (slot[1][0]==slot[1][1] && slot[1][1]==slot[1][2])
game_over=true;
if (slot[2][0]==slot[2][1] && slot[2][1]==slot[2][2])
game_over=true;
if (slot[0][0]==slot[1][0] && slot[1][0]==slot[2][0])
game_over=true;
if (slot[0][1]==slot[1][1] && slot[1][1]==slot[2][1])
game_over=true;
if (slot[0][2]==slot[1][2] && slot[1][2]==slot[2][2])
game_over=true;
if (slot[0][0]==slot[1][1] && slot[1][1]==slot[2][0])
game_over=true;
if (slot[0][2]==slot[1][1] && slot[1][1]==slot[0][2])
game_over=true;

}
while (game_over=false);
}
I think you're not really understanding the concept of the board.

char board[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' } ;

You now have a blank board. You don't need to do anything with the 1-9 as far as the contents of the board go. 1-9 are simply the positions the user can specify.

1
2
3
4
5
 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9


Note that position 1 corresponds to board[0].

You might print the board like:

1
2
3
4
5
6
7
8
9
10
11
void print_board(char*brd)
{
    char const* horizontal = "---+---+---\n" ;

     cout << "\n\n" ;
     cout << ' ' << brd[0] << " | " << brd[1] << " | " << brd[2] << '\n' ; 
     cout << horizontal ;
     cout << ' ' << brd[3] << " | " << brd[4] << " | " << brd[5] << '\n' ;
     cout << horizontal ;
     cout << ' ' << brd[6] << " | " << brd[7] << " | " << brd[8] << '\n' ;
}


I think you'll find it much easier to implement than the 2D array since user input corresponds so closely to the 1D representation of the board.

1
2
3
4
5
6
    cin >> Player_Move ;

    if ( Player_Move < 1 || Player_Move > 9 || board[Player_Move-1] != ' ')
          // inform player the move is invalid
    else
         board[Player_Move-1] = X_or_O ;


Note: I didn't take the time to test any of this code.

No I understand the board just fine. But I dont want to use yours because I'm trying to stay away from pointers they just make it more difficult
I think cire was suggesting making your actual board a 1D array. Although it might make some tasks more complex (like checking for 3-in-a-rows), it'd simplify others (like user input).

-Albatross
Yeah I agree but I already made all of the multidimesional arrays in the code I dont really want to change them. But albatross can you put this code into your compiler and try it. I dont know whats wrong with it it doesnt loop or anything it just exits.
For my implementation of print_board you could simply make it take no parameters and, in the function body, use the global board variable. Then you wouldn't be explicitly using a pointer.

I have to disagree with Albatross on one point though, checking for 3-in-a-rows is the same level of complexity for a 1D array as a built-in 2D array (which actually is a 1D array under the hood.) In your current implementation the board (slots) has each slot initialized to unique values. That does make looking for 3's a little easier. You don't have to worry about 3 spaces being interpreted as a winner.

But aside from that:

while (valid_move=false);

should be

while (valid move == false) ;

and

while (game_over=false);

should be

while (game_over==false) ;

If your compiler's warning level is set properly, your compiler should warn you of this.




Okay so I basically have the whole program now, but I have a few issues. One, during the game whenever a player inputs X or O in the corner, the middle, and another corner, it says Player___wins, and it is not actually three in a row. and two how do you delete all previous memory? It says play again y/n and if they press y I want it to delete all the previous memory so the board is clear
Topic archived. No new replies allowed.