Hello guys, I made this simple C++ TicTacToe console game in about 30min and decided to share.
This is simple so the programs decides that,
Player 1 goes first and always has an X.
Player 2 goes second and always has an O.
You can take this code and edit it and release it yourself, just give me credits please
Ok, so I will make a tutorial on this later, when I feel like it, post it on YouTube so its a video, and commentate it. So look forward to that!
Here is the code, I even commented it for you
You are all welcome
This is simple so the programs decides that,
Player 1 goes first and always has an X.
Player 2 goes second and always has an O.
You can take this code and edit it and release it yourself, just give me credits please
Ok, so I will make a tutorial on this later, when I feel like it, post it on YouTube so its a video, and commentate it. So look forward to that!
Here is the code, I even commented it for you
Code:
#include <iostream>
using namespace std;
void main() {
// Initialize all the global variables
char Square1('1');
char Square2('2');
char Square3('3');
char Square4('4');
char Square5('5');
char Square6('6');
char Square7('7');
char Square8('8');
char Square9('9');
int PlayerTurn(1);
bool GameOver(true);
// Start the main loop
do {
// clear the screen every time the board is re drawn
system("CLS");
// Print board
cout << Square1 << "|" << Square2 << "|" << Square3 << endl;
cout << "-+-+-"<< endl;
cout << Square4 << "|" << Square5 << "|" << Square6 << endl;
cout << "-+-+-"<< endl;
cout << Square7 << "|" << Square8 << "|" << Square9 << endl;
// Set the player mark
char PlayerMark;
if (PlayerTurn == 1) {
PlayerMark = 'X';
} else {
PlayerMark = 'O';
}
// Ask the player for a move
cout << "It is Player " << PlayerTurn << "'s turn." << endl;
cout << "Option: ";
bool ValidMove;
// Loop to check if move is valid
do {
char NextMove;
cin >> NextMove;
ValidMove = true;
// if statements to check for valid move
if (NextMove == '1' && Square1 == '1') {
Square1 = PlayerMark;
} else if (NextMove == '2' && Square2 == '2') {
Square2 = PlayerMark;
} else if (NextMove == '3' && Square3 == '3') {
Square3 = PlayerMark;
} else if (NextMove == '4' && Square4 == '4') {
Square4 = PlayerMark;
} else if (NextMove == '5' && Square5 == '5') {
Square5 = PlayerMark;
} else if (NextMove == '6' && Square6 == '6') {
Square6 = PlayerMark;
} else if (NextMove == '7' && Square7 == '7') {
Square7 = PlayerMark;
} else if (NextMove == '8' && Square8 == '8') {
Square8 = PlayerMark;
} else if (NextMove == '9' && Square9 == '9') {
Square9 = PlayerMark;
} else {
cout << "Invalid Move. Try again." << endl;
cout << "Option: ";
ValidMove = false;
}
} while (!ValidMove);
GameOver = false;
bool WinGame = true;
// if statements to check if game is over
if (Square1 != '1') {
if (Square2 == Square1 && Square3 == Square1) {
GameOver = true;
}
if (Square4 == Square1 && Square7 == Square1) {
GameOver = true;
}
}
if (Square5 != '5') {
if (Square1 == Square5 && Square9 == Square5) {
GameOver = true;
}
if (Square2 == Square5 && Square8 == Square5) {
GameOver = true;
}
if (Square4 == Square5 && Square6 == Square5) {
GameOver = true;
}
if (Square3 == Square5 && Square7 == Square5) {
GameOver = true;
}
}
if (Square9 != '9') {
if (Square3 == Square9 && Square6 == Square9) {
GameOver = true;
}
if (Square7 == Square9 && Square8 == Square9) {
GameOver = true;
}
}
// if statement to see if it is a draw
if (Square1 != '1' && Square2 != '2' && Square3 != '3' &&
Square4 != '4' && Square5 != '5' && Square6 != '6' &&
Square7 != '7' && Square8 != '8' && Square9 != '9' && !GameOver)
{
GameOver = true;
WinGame = false;
}
if (GameOver) {
if (WinGame) {
system("CLS");
cout << "Player" << PlayerTurn << " wins!" << endl;
}
// Print ending board
cout << Square1 << "|" << Square2 << "|" << Square3 << endl;
cout << "-+-+-"<< endl;
cout << Square4 << "|" << Square5 << "|" << Square6 << endl;
cout << "-+-+-"<< endl;
cout << Square7 << "|" << Square8 << "|" << Square9 << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
cout << "Option: ";
char PlayAgain;
cin >> PlayAgain;
if (PlayAgain == 'y') {
GameOver = false;
// Clear the board
Square1 = '1';
Square2 = '2';
Square3 = '3';
Square4 = '4';
Square5 = '5';
Square6 = '6';
Square7 = '7';
Square8 = '8';
Square9 = '9';
}
PlayerTurn = 1;
} else {
// change the player turn to the opposite player
if (PlayerTurn == 1) {
PlayerTurn = 2;
} else {
PlayerTurn = 1;
}
}
} while (!GameOver);
}
You are all welcome