Now as you all are known with the rules about how to play the game, let's design code based on these rules:
Secret Strategies to win Tic-tac-toe:
1)Tic-tac-toe is a very mindful game. You have to be aware of every move of your opponent and even figure out which move you will make next.
2)To beat your opponent, be it a person or a computer even, you need to plan out a strategy.
3)You first need to figure out how you can make those three X’s in a row and stop your opponent from winning as well.
4)Watch your opponent’s every move. For example, if they put two O’s in a row, then you MUST put an X in the next square to those consecutive O’s or you’ll lose.
5)In short, you must be very vigilant while making your moves. This is the only way you’ll never lose a game of tic-tac-toe. If you’ll not win then at least you will have a tie
Here’s a beginner project: Tic Tac Toe. The code below contains the solution needed to create a tic tac toe game.
Code:
#include <iostream>
using namespace std;
char square[10] = {‘o’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′};
int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do
{
board();
player=(player%2)?1:2;
cout << “Player ” << player << “, enter a number: “;
cin >> choice;
mark=(player == 1) ? ‘X’ : ‘O’;
if (choice == 1 && square[1] == ‘1’)
square[1] = mark;
else if (choice == 2 && square[2] == ‘2’)
square[2] = mark;
else if (choice == 3 && square[3] == ‘3’)
square[3] = mark;
else if (choice == 4 && square[4] == ‘4’)
square[4] = mark;
else if (choice == 5 && square[5] == ‘5’)
square[5] = mark;
else if (choice == 6 && square[6] == ‘6’)
square[6] = mark;
else if (choice == 7 && square[7] == ‘7’)
square[7] = mark;
else if (choice == 8 && square[8] == ‘8’)
square[8] = mark;
else if (choice == 9 && square[9] == ‘9’)
square[9] = mark;
else
{
cout<<“Invalid move “;
player–;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<“==>aPlayer “<<–player<<” win “;
else
cout<<“==>aGame draw”;
cin.ignore();
cin.get();
return 0;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void board()
{
system(“cls”);
cout << “nntTic Tac Toenn”;
cout << “Player 1 (X) – Player 2 (O)” << endl << endl;
cout << endl;
cout << ” | | ” << endl;
cout << ” ” << square[1] << ” | ” << square[2] << ” | ” << square[3] << endl;
cout << “_____|_____|_____” << endl;
cout << ” | | ” << endl;
cout << ” ” << square[4] << ” | ” << square[5] << ” | ” << square[6] << endl;
cout << “_____|_____|_____” << endl;
cout << ” | | ” << endl;
cout << ” ” << square[7] << ” | ” << square[8] << ” | ” << square[9] << endl;
cout << ” | | ” << endl << endl;
}
let's look for different algorithms of implementation of this game in further blogs....
THANK YOU!!!
Secret Strategies to win Tic-tac-toe:
1)Tic-tac-toe is a very mindful game. You have to be aware of every move of your opponent and even figure out which move you will make next.
2)To beat your opponent, be it a person or a computer even, you need to plan out a strategy.
3)You first need to figure out how you can make those three X’s in a row and stop your opponent from winning as well.
4)Watch your opponent’s every move. For example, if they put two O’s in a row, then you MUST put an X in the next square to those consecutive O’s or you’ll lose.
5)In short, you must be very vigilant while making your moves. This is the only way you’ll never lose a game of tic-tac-toe. If you’ll not win then at least you will have a tie
Here’s a beginner project: Tic Tac Toe. The code below contains the solution needed to create a tic tac toe game.
Code:
#include <iostream>
using namespace std;
char square[10] = {‘o’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′};
int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do
{
board();
player=(player%2)?1:2;
cout << “Player ” << player << “, enter a number: “;
cin >> choice;
mark=(player == 1) ? ‘X’ : ‘O’;
if (choice == 1 && square[1] == ‘1’)
square[1] = mark;
else if (choice == 2 && square[2] == ‘2’)
square[2] = mark;
else if (choice == 3 && square[3] == ‘3’)
square[3] = mark;
else if (choice == 4 && square[4] == ‘4’)
square[4] = mark;
else if (choice == 5 && square[5] == ‘5’)
square[5] = mark;
else if (choice == 6 && square[6] == ‘6’)
square[6] = mark;
else if (choice == 7 && square[7] == ‘7’)
square[7] = mark;
else if (choice == 8 && square[8] == ‘8’)
square[8] = mark;
else if (choice == 9 && square[9] == ‘9’)
square[9] = mark;
else
{
cout<<“Invalid move “;
player–;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<“==>aPlayer “<<–player<<” win “;
else
cout<<“==>aGame draw”;
cin.ignore();
cin.get();
return 0;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void board()
{
system(“cls”);
cout << “nntTic Tac Toenn”;
cout << “Player 1 (X) – Player 2 (O)” << endl << endl;
cout << endl;
cout << ” | | ” << endl;
cout << ” ” << square[1] << ” | ” << square[2] << ” | ” << square[3] << endl;
cout << “_____|_____|_____” << endl;
cout << ” | | ” << endl;
cout << ” ” << square[4] << ” | ” << square[5] << ” | ” << square[6] << endl;
cout << “_____|_____|_____” << endl;
cout << ” | | ” << endl;
cout << ” ” << square[7] << ” | ” << square[8] << ” | ” << square[9] << endl;
cout << ” | | ” << endl << endl;
}
let's look for different algorithms of implementation of this game in further blogs....
THANK YOU!!!
Hey ! Can we get to know how to make tic -toe game with computer like .. I what will be the code for game computer vs human
ReplyDeleteyes friend! in further blogs we will sure put in some interesting stuff about tic-tac toe with computer ..
Deleteyes the beginners code helped😊
ReplyDeleteI appreciate your kind words..Thank you buddy!
DeleteExcellent... Helped a lot. 😇😇😇
ReplyDeleteExcellent
ReplyDeleteProject was helpful!
ReplyDelete🙏🙏
DeleteI tried the same code ... it worked...ty
ReplyDeleteAnytime 😊
DeleteExcellent!!! Worked very well..
ReplyDeleteThanks
DeleteNice post
ReplyDelete