• Register
2 votes
26 views

Problem:

Hello, Kodlogs

I am looking for a code that makes a tic-tac-toe game board of two-player. I am searching for the codes for a long time over the internet but didn’t get codes that I understand easily.
Thanks for your effort guys. I must recognize your help.
Thanks again

12 7 7
15,250 points

1 Answer

2 votes

Solution:

Making a game like tic-tac-toe is a very basic level program but not that easy to understand. So, you can’t expect aa easy set of code from anyone. It has to contain a set of methods and steps at least to fulfill the requirements of a Tic-Tac-Toe game.

However, I am tried to keep it simpler for your better understanding. Please have a look.

#include <iostream>
#include <string>
using namespace std;
 
class TicTacToe{
private: 
	char board[3][3];
public:
	TicTacToe() { }
	void setTable(){
		int n = 1;
		for(int x = 0; x < 3; x++){
			for(int y = 0; y < 3; y++){
				board[x][y] = '0' + n;
				n++;
			}
		}
	}
	void printTable(){
		for(int x = 0; x < 3; x++){
			for(int y = 0; y < 3; y++)
				if(y < 2) { cout << board[x][y] << "|"; }
				else { cout << board[x][y] << endl; }
			if(x < 2) { cout << "-+-+-\n"; }
		}
	}
	void playerMove(char num, char player){
		bool wrongMove = true;//wrong move
		for(int x = 0; x < 3; x++){
			for(int y = 0; y < 3; y++)
			{
				if(board[x][y] == num){
                board[x][y] = player; wrongMove = false;
                }
			}
		}
		if(wrongMove == true) { cout << "Wrong move!\n"; }
	}
	bool checkWinner(char player, bool gameOver)
	{
		for(int x = 0; x < 3; x++)
			if(board[x][0] == board[x][1] && board[x][1] == board[x][2]) gameOver = true;
		for(int x = 0; x < 3; x++)
			if(board[0][x] == board[1][x] && board[1][x] == board[2][x]) gameOver = true;
		if(board[0][0] == board[1][1] && board[1][1] == board[2][2]) gameOver = true;
		if(board[0][2] == board[1][1] && board[1][1] == board[2][0]) gameOver = true;
		if(gameOver == true) 
		{
			cout << "Player " << player << " wins!\n\n";
		}
		return gameOver;
	}
	bool checkDraw(bool gameOver)
	{
		int n = 1, count = 0;
		for(int x = 0; x < 3; x++)
		{
			for(int y = 0; y < 3; y++)
			{
				if(board[x][y] == '0'+n) { count++; }
				n++;
			}
		}
		if(count < 1) { cout << "It's a draw!\n\n"; gameOver = true; }
		return gameOver;
	}
};
 
int main()
{
	bool done = false, gameOver = false;
	char player = 'O', num;
	TicTacToe myGame;
	myGame.setTable();
	do
	{
		if(player == 'X') { player = 'O'; }
		else { player = 'X'; } 
		myGame.printTable();
		cout << "Player \"" << player << "\" turn or (q) to quit> ";
		cin >> num; 
		cout << "\n";
		if(num == 'q') { cout << "Goodbye!...\n"; break; }
		myGame.playerMove(num, player);
		gameOver = myGame.checkWinner(player, gameOver);
		gameOver = myGame.checkDraw(gameOver);
		if(gameOver == true) 
		{	myGame.setTable(); 
			gameOver = false; 
		}
	}while(!done);
 
	system("pause");
	return 0;
}

Thank you too.

13 9 6
94,240 points

Related questions

1 vote
1 answer 21 views
21 views
Problem: I&rsquo;ve completed my basic java course yesterday. My instructor provided me a problem to solve as an assignment for the final test. The problem is to make a tic tac toe java code using a 2d array. I know how to use a 2d array and handle it. But I ... not looking for the whole solution, but yeah if you can provide it might help me a lot to understand the process better. Thanks, fellow!
asked Mar 31 Gavin 15.3k points
0 votes
1 answer 8 views
8 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10 sikandar 2.4k points
0 votes
1 answer 9 views
9 views
I have a function that I want to take, as a parameter, a variable size 2D array. So far I have this: void myFunction (double ** myArray) { myArray [x] [y] = 5; etc... } And I have showed an array in my code: double anArray [10] [10]; However, ... changes made to my Function should alter the state of an Array. If I understand correctly, I just want to pass as argument a pointer to a 2D array.
asked Aug 8 game 4.3k points
0 votes
1 answer 3 views
3 views
How to dynamically allocate a 2d array in c? What are the different ways to achieve allocation?
asked Oct 9 sikandar 2.4k points
1 vote
1 answer 33 views
33 views
Problem: Hi all, I am a beginner in C++ programming. I am reading and trying to learn the C++ functions. Let say, I have a two-dimensional array and I want the values to be printed via a function call. How to do that in C++? I&rsquo;ve given a ... format to do that? If not, please share your suggestions and ideas with me. I am looking forward to reading your comments eagerly. Thanks, guys.
asked May 10 adamSw 11.3k points