• Register
2 votes
269 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

Please log in or register to answer this question.

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,260 points

Related questions

1 vote
1 answer 432 views
432 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, 2020 Gavin 15.3k points
0 votes
1 answer 52 views
52 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10, 2020 sikandar 2.4k points
0 votes
1 answer 14 views
14 views
Problem: I just need someone to steer me in the right direction. Point out errors I have so I can try and fix them. I'm new to c++
asked Apr 6 neeraj 10.5k points
0 votes
1 answer 12 views
12 views
Problem: My code runs, but it doesn't print out anything on the board. I'm pretty sure I have done a couple of things wrong, and if someone could point me in the right direction, that would be great. Thanks for any help!
asked Apr 6 neeraj 10.5k points
0 votes
1 answer 7 views
7 views
Problem: how to Solve this: error: tic tac toe game in python HELP! I need help understanding this: tic tac toe game in python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
1 answer 33 views
33 views
Problem: Hello everyone, here I leave my code made in C ++ of the famous game Tic-Tac-Toe . The truth is that I am still learning this language on my own and I would like you to give me your opinions that I should edit or improve my code, also that I would like to achieve ... '7', '8', '9'}}; menu (numbers, Player, Name, Name_2); // Function where the menu and game options are located return 0; }
asked Dec 4, 2020 sasha 16.2k points
1 vote
1 answer 8 views
8 views
Problem: Need help with tic tac toe program optimization. Tic tac toe program c++.
asked Mar 25 PkGuy 23.5k points
0 votes
1 answer 19 views
19 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 21 shamimkhan 15.1k points
0 votes
0 answers 12 views
12 views
Problem: anyone who know this problem please help? tic tac toe minimax java source code
asked Mar 25 Rohit kr 17.5k points
1 vote
1 answer 14 views
14 views
Problem: How to make a java program to make a simple tic-tac-toe game ?
asked Mar 16 Raj Tyagi 14.9k points