Hey guys, I just learned the very basics of arrays and I have to write this program of a tic-tac-toe game. The requirements are this:

Write a modular program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk(*). The program should display the initial board configuration and then start a loop that does the following:

Allow player 1 to select a location on the board for an X by entering a row and column number. Then redisplay the board with an X replacing the * in the chosen location

If there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an O by entering a row and column number. Then redisplay the board with an O replacing the * in the chosen location.


The Loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie has occurred.

Player 1 wins when there are three Xs in a row, a column, or diagonal on the game board
Player 2 wins when there are three Os in a row, a column, or diagonal on the game board
A tie occurs when all locations on the board are full, but there is no winner.

Input Validation: Only allow legal moves to be entered. The row must be 1, 2, or 3. The column must be 1, 2, or 3. The(row,column) position entered must currently be empty(i.e., still have an asterisk in it).

Okay, that is all. My problem is that I barely know how to use arrays. So what I was thinking is that maybe I should make an array as a function and in that function, make a loop for when I call it in my main function. That is where I am starting to get at. Are there any suggestions as to how else I can start this program? I just need guidance, not the code itself. I want to make the code myself, but I feel like I will need help. Any suggestion will be appreciated. Thank you.

i think it is easy:
1- you declare 2d array
2- fill it with *
3- declare player1Turn =true
4- declare player2Turn =false
5- make while loop where not finished
BS:not Finished would be function return bool that checks the condtions
6-if player1Turn
6.1- player1Turn =false
6.2- player2Turn =true
6.3- call function called play with "player1","X" as args
6-else if player2Turn
6.1- player2Turn =false
6.2- player1Turn =true
6.3- call function called play with "player2","O" as args
5- end while loop

How would I put out the layout of the tic-tac-toe board?

will I have to manually do that by making lines such as -------- |||||||?

#include <iostream>
using namespace std;

char a[9] = { '*', '*', '*', '*', '*', '*', '*', '*', '*'};
char b = '0';

void draw();
bool getinput();
bool checkwin();


int main()
{
	for(;;)
	{
		draw();
		if(getinput () == 1)
		{
			break;
		}
		if(checkwin() == 1)
		{
			cout << "You win!!!!!!" << endl;
			break;
		}
	}

	return 0;
}


	void draw()
	{
		cout << a[0] << "|" << a[1] << "|" << a[2] << endl;
		cout << a[3] << "|" << a[4] << "|" << a[5] << endl;
		cout << a[6] << "|" << a[7] << "|" << a[8] << endl;

	}

	bool getinput()
	{
		cout << "Enter your move" << endl;
		cin >> b;
		switch(b)
		{
		case '1' : a[0] = 'X'; break;
		case '2' : a[1] = 'X'; break;
		case '3' : a[2] = 'X'; break;
		case '4' : a[3] = 'X'; break;
		case '5' : a[4] = 'X'; break;
		case '6' : a[5] = 'X'; break;
		case '7' : a[6] = 'X'; break;
		case '8' : a[7] = 'X'; break;
		case '9' : a[8] = 'X'; break;
		default: cout << "Hey!! You have to play by the rules!" << endl; return 1;
		}
		return 0;
	}

	bool checkwin()
	{
		if(a[0] == 'X' && a[1] == 'X' && a[2] == 'X') return 1;
		if(a[0] == 'X' && a[3] == 'X' && a[6] == 'X') return 1;
		if(a[0] == 'X' && a[4] == 'X' && a[9] == 'X') return 1;
	}

here is what I have so far


I have trouble trying to make the it switch to the O and I have trouble trying to make all the combinations work. I also have trouble so that you can't overlap. Any suggestions?

Your array is not two-dimensional.


I also suggest using a variable for input as opposed to just defining every possible outcome. You're setting yourself for one tedious and hard to follow jumble of code.

An example:

unsigned short x,y;
cin >> x >> y;
myArray[x][y]= 'X'

This takes the code that was 13 lines long and makes it three long.

getInput i think it can be like this:

bool getinput(char playerSympol)	
{
		cout << "Enter your move" << endl;
		cin >> b;
		if(b<'0'||b>'9')//cause the ascii is sorted for single digit //will not work if index allow 2 or more digits (10 and beyond)
		{
		      cout << "Hey!! You have to play by the rules!" << endl;
    		      return 0;
		}
		else
		{
		      //Since '0'-'0'=0
		      //and digits ascii are sorted
		      //so '3' - '0'=3
		      //in other words
		      //'0' ascii is 48
		      //so 48-48=0
		      //'3' ascii is 51
		      //51-48=3
		      a[b-'0'] = playerSympole;
		      return 1;
		}		
}

Here is what I have now

#include <iostream>
#include <iomanip>
using namespace std;

const int SIZE = 3;

char a[3][3] = {
	{'*','*','*'},
	{'*','*','*'},
	{'*','*','*'}
				};
				
char b = '0';

void draw();
bool getinputP1();
bool getinputP2();
bool checkwinP1();
bool checkwinP2();

int main()
{
	draw();
	cout << "Player one make your move" << endl;
	getinputP1();
	draw();
	cout << "Player two make your move" << endl;
	getinputP2();
	draw();
	for (;;)
	{
		cout << "Player one make your move" << endl;
		getinputP1();
		if(checkwinP1() == 1)
		{
			draw();
			cout << "Player one, you win!" << endl;
			break;
		}
		draw();
		cout << "Player two make your move" << endl;
		getinputP2();
		if(checkwinP2() == 1)
		{
			draw();
			cout << "Player two, you win!" << endl;
			break;
		}
		else 
		{
			draw();
			cout << "It's a tie" << endl;
			break;
		}
	}
	
	return 0;
}


void draw()
{
	for (int i = 0; i < SIZE; i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			cout << setw(5) << a[i][j];
		}
		cout << endl;
	}
}

bool getinputP1()
{
	cout << "Enter your move" << endl;
	cin >> b;
	switch(b)
	{
		case '1' : a[0][0] = 'X'; break;
		case '2' : a[0][1] = 'X'; break;
		case '3' : a[0][2] = 'X'; break;
		case '4' : a[1][0] = 'X'; break;
		case '5' : a[1][1] = 'X'; break;
		case '6' : a[1][2] = 'X'; break;
		case '7' : a[2][0] = 'X'; break;
		case '8' : a[2][1] = 'X'; break;
		case '9' : a[2][2] = 'X'; break;
		default: cout << "Hey!! You have to play by the rules!" << endl;
	}
	return 0;
}

bool getinputP2()
{
	cout << "Enter your move" << endl;
	cin >> b;
	switch(b)
	{
		case '1' : a[0][0] = 'O'; break;
		case '2' : a[0][1] = 'O'; break;
		case '3' : a[0][2] = 'O'; break;
		case '4' : a[1][0] = 'O'; break;
		case '5' : a[1][1] = 'O'; break;
		case '6' : a[1][2] = 'O'; break;
		case '7' : a[2][0] = 'O'; break;
		case '8' : a[2][1] = 'O'; break;
		case '9' : a[2][2] = 'O'; break;
		default: cout << "Hey!! You have to play by the rules!" << endl;
	}
	return 0;
}

	bool checkwinP1()
	{
		if(a[0][0] == 'X' && a[0][1] == 'X' && a[0][2] == 'X') return 1;
		if(a[1][0] == 'X' && a[1][1] == 'X' && a[1][2] == 'X') return 1;
		if(a[2][0] == 'X' && a[2][1] == 'X' && a[2][2] == 'X') return 1;
		if(a[0][0] == 'X' && a[1][0] == 'X' && a[2][0] == 'X') return 1;
		if(a[0][1] == 'X' && a[1][1] == 'X' && a[2][1] == 'X') return 1;
		if(a[0][2] == 'X' && a[1][2] == 'X' && a[2][2] == 'X') return 1;
		if(a[0][0] == 'X' && a[1][1] == 'X' && a[2][2] == 'X') return 1;
		if(a[2][0] == 'X' && a[1][1] == 'X' && a[0][2] == 'X') return 1;
	}

	bool checkwinP2()
	{
		if(a[0][0] == 'O' && a[0][1] == 'O' && a[0][2] == 'O') return 1;
		if(a[1][0] == 'O' && a[1][1] == 'O' && a[1][2] == 'O') return 1;
		if(a[2][0] == 'O' && a[2][1] == 'O' && a[2][2] == 'O') return 1;
		if(a[0][0] == 'O' && a[1][0] == 'O' && a[2][0] == 'O') return 1;
		if(a[0][1] == 'O' && a[1][1] == 'O' && a[2][1] == 'O') return 1;
		if(a[0][2] == 'O' && a[1][2] == 'O' && a[2][2] == 'O') return 1;
		if(a[0][0] == 'O' && a[1][1] == 'O' && a[2][2] == 'O') return 1;
		if(a[2][0] == 'O' && a[1][1] == 'O' && a[0][2] == 'O') return 1;
	}

When I don't have that else, the loop will continue going until there is a winner meaning that the players will overlap until one is a winner, but the problem is when I have that else, it ends the game early. Any suggestions?


Also, if a person makes an invalid move, it will skip that person and onto the next. I also need help in finding a way to make it so that it can only do one move at a time. If I press 123, the thing will output for me XOX. Any way so that I can make that an invalid move? an if with 1-9 maybe?

Need help creating a similar game of tic tac toe using 2 dimensional string array with 3 rows and 3 columns, there should be modules included as well As looping, telling the 2 players to select their locations on the board and should say when a player wins or show a tie. This has to be done in psepseudocode.

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.