Hello,

I have an assignment to make a tic tac toe game using 1-9 in a grid (but without 1 l 2 l 3 and instead, 1 2 3 etc.). I'm having an impossible time figuring out how to redisplay the new x or o (see function comments).

Is there anyone out there that can give me a hint as to what direction I should go to display the updated game grid? I've seen many tic tac toe examples on here but a lot of them are more advanced than necessary or are using attributes I'm not familiar with.

Thanks!

#include <iostream>
using namespace std;

int Change_Board_X(int& Player_1_Move); ////Change from the # ////entered by Player 1 to an X on the board

int main ( )
{
    int Player_1_Move = 0;
    char Repeat = 0;
    int game_grid[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    do
    {
        for(int index1 = 0; index1 < 3; index1++)
        {
            for(int index2 = 0; index2 < 3; index2++)
            {
                cout << " " << game_grid[index1][index2] << " ";
            }
            putchar('\n');
        }
       
cout << endl << "Here is your tic tac toe gameboard. Player 1 will be X and Player 2 will be O.\n" << endl;
cout << "Player 1: Please make your first move by entering a number between 1 and 9: \n";
cin >> Player_1_Move;

Change_Board_X (Player_1_Move);
       
cout << endl << "Would you like to do this again? "
        << "Enter (Y) for Yes and (N) for No and Press     Enter: ";
cin >> Repeat;
cout << endl;
   
    } while ( Repeat == 'y' || Repeat == 'Y' );

    return 0;
}

int Change_Board_X(int& Player_1_Move) //Obviously this function doesn't work yet. I had hoped to do a swap_values function or something to trade out a number entered with an x or o but I can't find a way to tap into my array if I have specific 1-9 values. I also thought about searching the array but I couldn't get it to search properly on a 2d array
{
    return (Player_1_Move);
}

Recommended Answers

All 13 Replies

After assigning a player move to the game_grid[][], in order to display the newly updated board, just use a nested loop to walk through your array, displaying all elements to the screen:

void display(int board[][])
{
     //Display all  contents of the array
     for(int i=0; i<3; i++)
          for(int j=0; j<3; j++)

               cout << board[i][j];
}

So rather than use my Change_Board_X, I would use the function you gave as a way to update the current board each time? Would that make lines 14-20 no longer necessary if I did a function call in that place instead?

I guess my main issue then is where to start to assign the player to the game_grid array. I haven't found a way to assign to the game_grid since I'm passing a int or char x instead of a type [][]. I haven't been able to edit the array or know where to start in regards to editing an array when I gave it integers 1-9.

Thanks for the help!!

You should also make a system("cls"); when recalling the board to clean your screen else it will accumulate. If you prefer not to use the function explained above by our comrade you can recall your main function like this:

void recall()
{
      system("cls"); // Clears your screen
      main(); // recalls your main
}

You can do that in a function or in a if() like..
if(user inputs X || user inputs O)
{ do something
}
Anyways if u prefer this way you need to adapt your code to it.

Thanks for all the tips!

As for assigning X or O to the grid in lieu of the current numbers 1-9...
I ask the user to inter a # 1-9 (based on the integer they want to replace on the grid). I then want to find a way to take that integer given and convert it into an X or O (depending on if it's player 1 or player 2) and then redisplay a new grid with an X or O over the integer they chose. My problem is not knowing how to edit the array to put an X or O over the numbers. Hopefully that made sense. Any suggestions?

Thanks!

Your current code has the user to input the position he/she wants to insert the X or the O now you must make it like this:

- assign the array position to each number e.g(1 is array[0][0], 2 is array[0][1] etc)

- make a simple if to detect wether it is player 1 or player 2 and code each if for each player... e.g(if(num==1) it's player 1 turn else if(num==2) it's player 2 turn)

- at the end of each if() to check the player, you must add or subtract 1 depending which player is it so when you refresh it will know who is next

- then by the number the user digited you should know which array position it is with the 1st rule I said, then all you need to do is array[something][something] = 'x' or 'o' depending in which player digited

- from here you can then make your game rules.. just think for a while and you'll know how. (Tip: The key for this answer is in comparing your different array positions)

Hope it helped I tried to make it as simple as I could :).

Is this what you meant? I can't check with my compiler right now so I'm not sure if that would be the way to code it. I'm still working on getting that to reflect the last 3 steps you mentioned. Thank you!!!

#include <iostream>
using namespace std;

int main ( )
{
    char Repeat = 0;
    int Player_Move(1);
    char Player_Symbol = 0;
    int Game_Grid[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
board[0][0] = "1";
board[0][1] = "2";
board[0][2] = "3";
board[0][3] = "4";
board[0][4] = "5";
board[0][5] = "6";
board[0][6] = "7";
board[0][7] = "8";
board[0][8] = "9";

    do
    {
        for(int index1 = 0; index1 < 3; index1++)
        {
            for(int index2 = 0; index2 < 3; index2++)
            {
                cout << " " << Game_Grid[index1][index2] << " ";
            }
            putchar('\n');
        }

cout << endl << "Here is your tic tac toe gameboard. Player 1 will be X and Player 2 will be O.\n" << endl;

if (Player_Move == 1)
{
Player_Symbol = 'X';
Player_Move--
}
else 
{
Player_Symbol = 'O';
Player_Move++
}

cout << "Player, please mark your " << Player_Symbol << " by entering a number between 1 and 9: \n";
       
cout << endl << "Would you like to do this again? "
        << "Enter (Y) for Yes and (N) for No and Press     Enter: ";
cin >> Repeat;
cout << endl;
   
    } while ( Repeat == 'y' || Repeat == 'Y' );

    return 0;
}
board[0][0] = "1";
board[0][1] = "2";
board[0][2] = "3";
board[0][3] = "4";
board[0][4] = "5";
board[0][5] = "6";
board[0][6] = "7";
board[0][7] = "8";
board[0][8] = "9";

No no, by "assigning" a number to the position I meant theoretically speaking and your positions are totally wrong I'll make an simpler example.

// position is the variable you ask the player to input
if(position==1 && Player == 1)
{
      board[0][0] = 'X';
}

else if(position==1 && Player == 2)
{
      board[0][0] = 'O';
}

if(position==2 && Player == 1)
{
      board[0][1] = 'X';
}

else if(position==2 && Player == 2)
{
      board[0][1] = 'O';
}

etc..
So you see you have to do two if() for each position because of the players... and about the coordinates, remember the table? It is a 3x3 so the positions are like this:

// First Horizontal Line
board[0][0]
board[0][1]
board[0][2]
// Second Horizontal Line
board[1][0]

and so on...
So you see the first [] is the line the second [] is the column. I hope you understood it now :).

Ahhh! I see!!

As soon as I get off work I'll go home and duke this out with my compiler and see if I can't come up with a fuller program.

You've been immensely helpful, thank you so much.

No problem! If this solved your problem please mark this thread as solved :). You can always reply again here if you need more help.

Okay your tips helped me get a lot farther in the program. My only issue now that I can't seem to solve is instead of the program reading the X or O in my if/else statements, it just fills that specific array slot with old memory garble.

So I'm finally getting it to go from
1 2 3
4 5 6
7 8 9

to
1 2 3
4 88 6
7 8 9

but I can't figure out how to have it read my if/else and not output the leftover stored memory.

My other issue is starting the game rules, but I think I'll have that down pat once I can get the X and O to show up in the places the players have designated.

Any idea what I'm doing wrong?

Thanks

#include <iostream>
using namespace std;

void Display(int board[3][3]);
int Player_Decision (int& Player);
int Position = 0;

int main ( )
{
    
    char Repeat = 0;
    int board[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int Player = 0;
	int Count = 0;
	char X = 'X';
	char O = 'O';

    do
    {
        for(int index1 = 0; index1 < 3; index1++)
        {
            for(int index2 = 0; index2 < 3; index2++)
            {
                cout << " " << board[index1][index2] << " ";
            }
            putchar('\n');
        }
       
	cout << endl << "Here is your tic tac toe gameboard. Player 1 will be X and Player 2 will be O.\n" << endl;

	cout << "Enter 1 if you would like to be Player 1 and will play as X for this game: \n";
	cin >> Player;

	for(Count = 0; Count < 9; Count++)
	{
		cout << "Player " << Player << ", please make a move by entering a number between 1 and 9: \n";
		cin >> Position;

		if(Position== 1 && Player == 1)
		{
		board[0][0] = 'X';
		}
		else if(Position == 1 && Player == 2)
		{
		board[0][0] = 'O';
		}

		if(Position ==2 && Player == 1)
		{
		board[0][1] = 'X';
		}
		else if(Position == 2 && Player == 2)
		{
		board[0][1] = 'O';
		}

		if(Position == 3 && Player == 1)
		{
		board[0][2] = 'X';
		}
		else if(Position == 3 && Player == 2)
		{
		 board[0][2] = 'O';
		}

		if(Position == 4 && Player == 1)
		{
		board[1][0] = 'X';
		}
		else if(Position == 4 && Player == 2)
		{
		 board[1][0] = 'O';
		}

		if(Position == 5 && Player == 1)
		{
		 board[1][1] = 'X';
		}
		else if(Position == 5 && Player == 2)
		{
		 board[1][1] = 'O';
		}
		if(Position== 6 && Player == 1)
		{
		  board[1][2] = 'X';
		}
		else if(Position== 6 && Player == 2)
		{
		 board[1][2] = 'O';
		}

		if(Position== 7 && Player == 1)
		{
		 board[2][0] = 'X';
		}
		else if(Position== 7 && Player == 2)
		{
		board[2][0] = 'O';
		}

		if(Position== 8 && Player == 1)
		{
		 board[2][1] = 'X';
		}
		else if(Position== 8 && Player == 2)
		{
		  board[2][1] = 'O';
		}

		if(Position== 9 && Player == 1)
		{
		 board[2][2] = 'X';
		}
		else if(Position== 9 && Player == 2)
		{
		 board[2][2] = 'O';
		}
	

		Display(board);
		Player_Decision (Player);
	}

	cout << endl << "Would you like to do this again? "
         << "Enter (Y) for Yes and (N) for No and Press     Enter: ";
	cin >> Repeat;
	cout << endl;
   
    } while ( Repeat == 'y' || Repeat == 'Y' );

    return 0;
}

int Player_Decision (int& Player)
{
	if (Player != 1)
	{
		Player--;
		return (Player);
	}
	else
	{
			Player++;
			return (Player);
	}
}



void Display(int board[][3])
{
	for(int index1 = 0; index1 < 3; index1++)
        {
            for(int index2 = 0; index2 < 3; index2++)
            {
                cout << " " << board[index1][index2] << " ";
            }
            putchar('\n');
        }
}

Heya I've changed your code and cleaned it a bit. I deleted all unnecessary variables and other things.. so here it is:

NOTE: First read this new code and try to understand it then try to change your code above with your new knowledge instead of copying it :).

#include <iostream>

using namespace std;

// Functions
void Display();
int Player_Decision (int& Player);
int Position;

// Variables
char board[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int Player = 1;
	
int main ( )
{
    for(int index1 = 0; index1 < 3; index1++)
    {
            for(int index2 = 0; index2 < 3; index2++)
            {
                cout << " " << board[index1][index2] << " ";
            }
            putchar('\n');
    }
       
	cout << endl << "Here is your tic tac toe gameboard. Player 1 will be X and Player 2 will be O.\n" << endl;

    cout << "Player " << Player << ", please make a move by entering a number between 1 and 9: \n";
    cin >> Position;

    if(Position== 1 && Player == 1)
    {
        board[0][0] = 'X';
    }
    else if(Position == 1 && Player == 2)
    {
         board[0][0] = 'O';
    }

    if(Position ==2 && Player == 1)
    {
         board[0][1] = 'X';
    }
    else if(Position == 2 && Player == 2)
    {
         board[0][1] = 'O';
    }

    if(Position == 3 && Player == 1)
    {
         board[0][2] = 'X';
    }
    else if(Position == 3 && Player == 2)
    {
         board[0][2] = 'O';
    }

    if(Position == 4 && Player == 1)
    {
         board[1][0] = 'X';
    }
    else if(Position == 4 && Player == 2)
    {
		 board[1][0] = 'O';
    }

    if(Position == 5 && Player == 1)
    {
		 board[1][1] = 'X';
    }
    else if(Position == 5 && Player == 2)
    {
		 board[1][1] = 'O';
    }
    if(Position== 6 && Player == 1)
    {
         board[1][2] = 'X';
    }
    else if(Position== 6 && Player == 2)
    {
		 board[1][2] = 'O';
    }

    if(Position== 7 && Player == 1)
    {
         board[2][0] = 'X';
    }
    else if(Position== 7 && Player == 2)
    {
         board[2][0] = 'O';
    }

    if(Position== 8 && Player == 1)
    {
		 board[2][1] = 'X';
    }
    else if(Position== 8 && Player == 2)
    {
         board[2][1] = 'O';
    }

    if(Position== 9 && Player == 1)
    {
         board[2][2] = 'X';
    }
    else if(Position== 9 && Player == 2)
    {
         board[2][2] = 'O';
    }
	
    Player_Decision (Player);
    Display();

    return 0;
}

int Player_Decision (int& Player)
{
	if (Player == 1)
	{
         Player++;
         return 0;
	}
	else if (Player == 2)
	{
         Player--;
         return 0;
	}
}

void Display()
{
     system("cls"); // Cleans your screen
     main(); // Recursive
}

I see! I think I was getting stuck with knowing that the array should be of type char but having trouble converting because of putting the for statement under the display and the compiler getting confused over my char/int issues. I also didn't think to declare Player as 1 from the start so that I could go with the == instead of saying != to, which I've been told is better to use ! as sparingly as possible.

Here is my code with the added game decision (my decision just declares a winner. I didn't get fancy with it).

#include <iostream>
using namespace std;

void Display();
int Player_Decision (int& Player);
int Position = 0;
int Winner = 0;

char board[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int Player = 1;

int main ( )
{
    char Repeat = 0;
    
    do
    {
 	for(int index1 = 0; index1 < 3; index1++)
        	{
            	for(int index2 = 0; index2 < 3; index2++)
           		 {
                		cout << " " << board[index1][index2] << " ";
            	}
           		 putchar('\n');
        	}

              
	cout << endl << "Here is your tic tac toe gameboard. Player 1 will be X and 	Player 2 will be O.\n" << endl;

	cout << "Enter (1) if you would like to be Player 1 and play as X for this 		game: ";
	cin >> Player;

	cout << "Player " << Player << ", please make a move by picking a number 	between 1 and 9 and then Press Enter: ";
	cin >> Position;

	if(Position== 1 && Player == 1)
	{
	board[0][0] = 'X';
	}
	else if(Position == 1 && Player == 2)
	{
	board[0][0] = 'O';
	}

	if(Position ==2 && Player == 1)
	{
	board[0][1] = 'X';
	}
	else if(Position == 2 && Player == 2)
	{
	board[0][1] = 'O';
	}

	if(Position == 3 && Player == 1)
	{
	board[0][2] = 'X';
	}
	else if(Position == 3 && Player == 2)
	{
	 board[0][2] = 'O';
	}

	if(Position == 4 && Player == 1)
	{
	board[1][0] = 'X';
	}
	else if(Position == 4 && Player == 2)
	{
	 board[1][0] = 'O';
	}

	if(Position == 5 && Player == 1)
	{
	 board[1][1] = 'X';
	}
	else if(Position == 5 && Player == 2)
	{
	 board[1][1] = 'O';
	}
	if(Position== 6 && Player == 1)
	{
	board[1][2] = 'X';
	}
	else if(Position== 6 && Player == 2)
	{
	board[1][2] = 'O';
	}

	if(Position== 7 && Player == 1)
	{
	 board[2][0] = 'X';
	}
	else if(Position== 7 && Player == 2)
	{
	board[2][0] = 'O';
	}

	if(Position== 8 && Player == 1)
	{
	 board[2][1] = 'X';
	}
	else if(Position== 8 && Player == 2)
	{
	board[2][1] = 'O';
	}

	if(Position== 9 && Player == 1)
	{
	 board[2][2] = 'X';
	}
	else if(Position== 9 && Player == 2)
	{
	board[2][2] = 'O';
	}
	
	Player_Decision (Player);
	Display();
	Check_Winner (Winner);

	cout << endl << "Would you like to do this again? "
          	        << "Enter (Y) for Yes and (N) for No and Press     Enter: ";
	cin >> Repeat;
	cout << endl;
   
    } while ( Repeat == 'y' || Repeat == 'Y' );

    return 0;
}

int Player_Decision (int& Player)
{
	if (Player == 1)
	{
		Player++;
		return 0;
	}
	else
	{
		Player--;
		return 0;
	}
}


void Display()
{
     system("cls"); 
     return; 
}

int Check_Winner (int& Winner)
{
	if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || //checks winning 	conditions for diagonals
         	(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
	{
		cout << "Congrats to the winner!! This game is now over.\n";
        		Winner = 1;
		return (Winner);
	}
	else if ((board[0][0] == board[0][1] && board[0][0] == board[0][2]) || //checks 	winning conditions for rows
         	(board[2][0] == board[2][1] && board[2][0] == board[2][2]) || 
	(board[1][0] == board[1][1] && board[1][0] == board[1][2]))
	{
		cout << "Congrats to the winner!! This game is now over.\n";
        		Winner = 1;
		return (Winner);
	}
	else if ((board[0][0] == board[1][0] && board[0][0] == board[2][0]) || //checks 	winning conditions for columns
            (board[0][1] == board[1][1] && board[0][1] == board[1][2]) || 
	(board[0][2] == board[1][2] && board[0][2] == board[2][2]))
	{
		cout << endl << "Congrats to the winner!! This game is now over.\n";
        		Winner = 1;
		return (Winner);
	}
	else
	{
		return (Winner);
	}
}

Is it possibly to modify the display function to just return to main instead of putting the recursive 'main ()' in there? I put in what I mean to see if that also works the same way.

I love those eureka moments where I realize I was just being an idiot the entire time and could have done it in a much more straightforward and concise way. Although, the more I practice, the better I'll get at adapting code that way. It's just hard starting out ;)

Thanks so much for sticking with me! I was so lost about arrays and the way you explained the board set-up helped tremendously. I had seen the board[0][0] and so on in the book but it didn't explain what it was doing so I didn't realize that was how to define array positions. Thanks!

You can only return back to main doing a recursive call it's how it is.. and the way your doing the winning checks are cool to tell which player won all you need to do is cout << Player; because you check everytime after a player's turn.

By the I think a recursive call is much better then a (do while) statement but I wont force you to use it.

Last but not least you don't need that question asking if the player wants to be number 1 it's just is.. the game starts with player 1 and the last question asking if he wants to play again should be after the game has ended, it has no sence to ask a player if he wants to play again after a turn...

Anything else just ask.. :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.