The simplest C++ Tic Tac Toe game ever
This is the simplest C++ Tic Tac Toe game ever, it`s made using only the most basic statements: cout, cin, variables, if/else/else if, switch/case and loopsThe game uses 9 variables, one for each square of the Tic Tac Toe game board, each of them can have 3 different value:
0 - free square
1 - occupied by an x
2 - occupied by a 0
The board is redrawn after each move in a loop. The whole action of the game happens in this loop. First the board is checked for an eventual winner or for a tie. If the game is not over yet than it`s time to move. First the move is checked, if it`s not a valid move a message is displayed and the player needs to pick an another square, if the move is valid than the loop runs again, but this time is the other player`s turn.1 - occupied by an x
2 - occupied by a 0
#include <iostream>
using namespace std;
int main(){
//These variables are holding the value of each square (0 = free, 1 = x, 2 = o)
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
//This holds the number of the current player (1 = player1(x), 2 = player2(o)
int turn = 1;
//These variables are holding the row and column number inputted by a player
int row;
int column;
//These variables will be used later to check if a move is valid or not
bool validmove;
bool validrow;
bool validcolumn;
//The bellow variable holds the number of the winner, when the value is not 0 means the game is over
int winner = 0;
//The game loops again and again until somebody wins or it`s tie (winner = 3)
while(winner == 0){
//Draw the board from simple +,- and | characters
cout << "1 2 3" << endl << "+---+---+---+" << endl << "1 | ";
if(one == 0){
//For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)
cout << "|";
}
else if(one == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(two == 0){
cout << " |";
}
else if(two == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(three == 0){
cout << " |";
}
else if(three == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << "2 | ";
if(four == 0){
cout << "|";
}
else if(four == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(five == 0){
cout << " |";
}
else if(five == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(six == 0){
cout << " |";
}
else if(six == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << "3 | ";
if(seven == 0){
cout << "|";
}
else if(seven == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(eight == 0){
cout << " |";
}
else if(eight == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(nine == 0){
cout << " |";
}
else if(nine == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << endl;
//The following several lines will check if the game is over or not
//Check`s the first line of the board for a winner combination (xxx or ooo)
if(one == two && two == three && one != 0){
//If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares
winner = one;
}
//Checks the second line
if(four == five && five == six && four != 0){
winner = four;
}
//Checks the third line
if(seven == eight && eight == nine && seven != 0){
winner = seven;
}
//Checks the first column
if(one == four && four == seven && one != 0){
winner = one;
}
//Checks the second column
if(two == five && five == eight && two != 0){
winner = two;
}
//Checks the third column
if(three == six && six == nine && three != 0){
winner = three;
}
//Checks the diagonal from top left to bottom right
if(one == five && five == nine && one != 0){
winner = one;
}
//Checks the diagonal from bottom left to top right
if(three == five && five == seven && three != 0){
winner = three;
}
//If nobody wins this turn it checks to see if the board is full or not
if(one != 0 && two != 0 && three != 0 && four != 0 && five != 0 && six != 0 && seven != 0 && eight != 0 && nine != 0 && winner == 0){
//If the table is full, then it`s tie
winner = 3;
}
//If somebody won or it`s a tie shows a message and break out from the loop - the game is over
if(winner == 1 || winner == 2){
cout << "Congratulations! Player " << winner << " is the winner!";
break;
}
else if(winner == 3){
cout << "Tie!";
break;
}
If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)
cout << "Player " << turn << "'s turn:" << endl;
validmove = false;
//Loops until the player makes a valid move
while(!validmove){
validrow = false;
//Loops until the player chooses a valid row
while(!validrow){
cout << "Row: ";
cin >> row;
if(row == 1 || row == 2 || row == 3){
validrow = true;
}
else{
cout << endl << "Invalid row!" << endl;
}
}
validcolumn = false;
//Loops until the player chooses a valid column
while(!validcolumn){
cout << "Column: ";
cin >> column;
if(column == 1 || column == 2 || column == 3){
validcolumn = true;
}
else{
cout << endl << "Invalid column!" << endl;
}
}
//After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the validmove variable from false to true
switch(row){
case 1:
if(column == 1){
if(one == 0){
one = turn;
validmove = true;
}
}
else if(column == 2){
if(two == 0){
two = turn;
validmove = true;
}
}
else{
if(three == 0){
three = turn;
validmove = true;
}
}
break;
case 2:
if(column == 1){
if(four == 0){
four = turn;
validmove = true;
}
}
else if(column == 2){
if(five == 0){
five = turn;
validmove = true;
}
}
else{
if(six == 0){
six = turn;
validmove = true;
}
}
break;
case 3:
if(column == 1){
if(seven == 0){
seven = turn;
validmove = true;
}
}
else if(column == 2){
if(eight == 0){
eight = turn;
validmove = true;
}
}
else{
if(nine == 0){
nine = turn;
validmove = true;
}
}
}
//If validmove is still false, then the selected square is occupied and a message is displayed
if(!validmove){
cout << "The sellected square is occupied!" << endl << "Select again:" << endl;
}
else{
//Else if it was a valid move, then it`s the other player`s turn
if(turn == 1){
turn = 2;
}
else{
turn = 1;
}
}
cout << endl;
}
//The loop start`s over
}
system("pause");
}
The second version of this Tic Tac Toe game is ready with minor changes in the code, using a wee bit harder but more effective techniques: Tic Tac Toe v2using namespace std;
int main(){
//These variables are holding the value of each square (0 = free, 1 = x, 2 = o)
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
//This holds the number of the current player (1 = player1(x), 2 = player2(o)
int turn = 1;
//These variables are holding the row and column number inputted by a player
int row;
int column;
//These variables will be used later to check if a move is valid or not
bool validmove;
bool validrow;
bool validcolumn;
//The bellow variable holds the number of the winner, when the value is not 0 means the game is over
int winner = 0;
//The game loops again and again until somebody wins or it`s tie (winner = 3)
while(winner == 0){
//Draw the board from simple +,- and | characters
cout << "1 2 3" << endl << "+---+---+---+" << endl << "1 | ";
if(one == 0){
//For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)
cout << "|";
}
else if(one == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(two == 0){
cout << " |";
}
else if(two == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(three == 0){
cout << " |";
}
else if(three == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << "2 | ";
if(four == 0){
cout << "|";
}
else if(four == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(five == 0){
cout << " |";
}
else if(five == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(six == 0){
cout << " |";
}
else if(six == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << "3 | ";
if(seven == 0){
cout << "|";
}
else if(seven == 1){
cout << "x |";
}
else{
cout << "o |";
}
if(eight == 0){
cout << " |";
}
else if(eight == 1){
cout << " x |";
}
else{
cout << " o |";
}
if(nine == 0){
cout << " |";
}
else if(nine == 1){
cout << " x |";
}
else{
cout << " o |";
}
cout << endl << "+---+---+---+" << endl << endl;
//The following several lines will check if the game is over or not
//Check`s the first line of the board for a winner combination (xxx or ooo)
if(one == two && two == three && one != 0){
//If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares
winner = one;
}
//Checks the second line
if(four == five && five == six && four != 0){
winner = four;
}
//Checks the third line
if(seven == eight && eight == nine && seven != 0){
winner = seven;
}
//Checks the first column
if(one == four && four == seven && one != 0){
winner = one;
}
//Checks the second column
if(two == five && five == eight && two != 0){
winner = two;
}
//Checks the third column
if(three == six && six == nine && three != 0){
winner = three;
}
//Checks the diagonal from top left to bottom right
if(one == five && five == nine && one != 0){
winner = one;
}
//Checks the diagonal from bottom left to top right
if(three == five && five == seven && three != 0){
winner = three;
}
//If nobody wins this turn it checks to see if the board is full or not
if(one != 0 && two != 0 && three != 0 && four != 0 && five != 0 && six != 0 && seven != 0 && eight != 0 && nine != 0 && winner == 0){
//If the table is full, then it`s tie
winner = 3;
}
//If somebody won or it`s a tie shows a message and break out from the loop - the game is over
if(winner == 1 || winner == 2){
cout << "Congratulations! Player " << winner << " is the winner!";
break;
}
else if(winner == 3){
cout << "Tie!";
break;
}
If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)
cout << "Player " << turn << "'s turn:" << endl;
validmove = false;
//Loops until the player makes a valid move
while(!validmove){
validrow = false;
//Loops until the player chooses a valid row
while(!validrow){
cout << "Row: ";
cin >> row;
if(row == 1 || row == 2 || row == 3){
validrow = true;
}
else{
cout << endl << "Invalid row!" << endl;
}
}
validcolumn = false;
//Loops until the player chooses a valid column
while(!validcolumn){
cout << "Column: ";
cin >> column;
if(column == 1 || column == 2 || column == 3){
validcolumn = true;
}
else{
cout << endl << "Invalid column!" << endl;
}
}
//After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the validmove variable from false to true
switch(row){
case 1:
if(column == 1){
if(one == 0){
one = turn;
validmove = true;
}
}
else if(column == 2){
if(two == 0){
two = turn;
validmove = true;
}
}
else{
if(three == 0){
three = turn;
validmove = true;
}
}
break;
case 2:
if(column == 1){
if(four == 0){
four = turn;
validmove = true;
}
}
else if(column == 2){
if(five == 0){
five = turn;
validmove = true;
}
}
else{
if(six == 0){
six = turn;
validmove = true;
}
}
break;
case 3:
if(column == 1){
if(seven == 0){
seven = turn;
validmove = true;
}
}
else if(column == 2){
if(eight == 0){
eight = turn;
validmove = true;
}
}
else{
if(nine == 0){
nine = turn;
validmove = true;
}
}
}
//If validmove is still false, then the selected square is occupied and a message is displayed
if(!validmove){
cout << "The sellected square is occupied!" << endl << "Select again:" << endl;
}
else{
//Else if it was a valid move, then it`s the other player`s turn
if(turn == 1){
turn = 2;
}
else{
turn = 1;
}
}
cout << endl;
}
//The loop start`s over
}
system("pause");
}


5:36 AM
Csabi

3 comments:
The vertical borders and column numbers are not drawn correctly. You need to insert more spaces.
Some of the whitespace is removed by CSS...
Do you really not know what iomanip is? Also your function never has a return statement. It is really sad that almost all C++ tutorials online are written by people who don't know C++.
Post a Comment