Created
October 8, 2019 18:58
-
-
Save martincostello/49c1c7369747956ae49a67b58e3e04c2 to your computer and use it in GitHub Desktop.
University Year 2 C++ Project - A Noughts and Crosses (Tic Tac Toe) Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
using namespace std; | |
char matrix[3][3]; | |
int win(0); | |
int loss(0); | |
int draw(0); | |
char player; | |
char computer; | |
int grid() | |
{ | |
cout << "\n\t\t 1 2 3\n"; | |
cout << "\t\t 1 " << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << "\n"; | |
cout << "\t\t ---|---|---\n"; | |
cout << "\t\t 2 " << matrix[1][0] << " | " << matrix[1][1] << " | " << matrix[1][2] << "\n"; | |
cout << "\t\t ---|---|---\n"; | |
cout << "\t\t 3 " << matrix[2][0] << " | " << matrix[2][1] << " | " << matrix[2][2] << "\n\n\n"; | |
} | |
int table() | |
{ | |
cout << "n\t\t Player | Wins | Losses | Draws\n"; | |
cout << "\t\t--------|------|--------|-------\n"; | |
cout << "\t\t " << player << " | " << win << " | " << loss << " | " << draw << "\n"; | |
cout << "\t\t--------|------|--------|-------\n"; | |
cout << "\t\t " << computer << " | " << loss << " | " << win << " | " << draw << "\n\n\n\n"; | |
} | |
int main() | |
{ | |
cout << "\n\n\t\t***********************\n"; | |
cout << "\t\t* *\n"; | |
cout << "\t\t* Noughts & Crosses *\n"; | |
cout << "\t\t* *\n"; | |
cout << "\t\t* by Martin Costello. *\n"; | |
cout << "\t\t* *\n"; | |
cout << "\t\t***********************\n\n\n\n"; | |
cout << "Please choose your player (O or X) : "; | |
cin >> player; | |
while (player!='X' && | |
player!='O' && | |
player!='x' && | |
player!='o') | |
{ | |
cout << "\nThat is not a valid player choice.\n\n"; | |
cout << "Please choose your player (O or X) : "; | |
cin >> player; | |
} | |
if (player=='o') | |
{ | |
player='O'; | |
} | |
if (player=='x') | |
{ | |
player='X'; | |
} | |
if (player=='O') | |
{ | |
computer='X'; | |
} | |
if (player=='X') | |
{ | |
computer='O'; | |
} | |
int moves(0); | |
int m; | |
int n; | |
char cont('y'); | |
while (cont=='Y' || cont=='y') | |
{ | |
for (m = 0; m < 3; m++) | |
{ | |
for (n = 0; n < 3; n++) | |
{ | |
matrix[m][n] = '\0'; | |
} | |
} | |
int i; | |
int j; | |
while (moves < 10) | |
{ | |
grid(); | |
cout << "\nYou are " << player << ". Please choose the row and column for your move.\n"; | |
cout << "Column : "; | |
cin >> j; | |
cout << "\nRow : "; | |
cin >> i; | |
for (i>3 || i<1 || j>3 || j<1 || ('X' == matrix[i-1][j-1] || 'O' == matrix[i-1][j-1])) | |
{ | |
cout << "\nThis is not a valid move.\n"; | |
cout << "Row : "; | |
cin >> i; | |
cout << "\nColumn : "; | |
cin >> j; | |
} | |
matrix[i-1][j-1]=player; | |
moves++; | |
cout << "\n\n"; | |
grid(); | |
if (matrix[0][0] == player && | |
matrix[0][0] == matrix[1][0] && | |
matrix[1][0] == matrix[2][0]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[0][1] == player && | |
matrix[0][1] == matrix[1][1] && | |
matrix[1][1]==matrix[2][1]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[0][2] == player && | |
matrix[0][2] == matrix[1][2] && | |
matrix[1][2] == matrix[2][2]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[0][0] == player && | |
matrix[0][0] == matrix[0][1] && | |
matrix[0][1] == matrix[0][2]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[1][0] == player && | |
matrix[1][0] == matrix[1][1] && | |
matrix[1][1] == matrix[1][2]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[2][0] == player && | |
matrix[2][0] == matrix[2][1] && | |
matrix[2][1] == matrix[2][2]) | |
{ | |
cout << "You have won!"; | |
win++; | |
break; | |
} | |
if (matrix[0][0] == player && | |
matrix[0][0] == matrix[1][1] && | |
matrix[1][1] == matrix[2][2]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (matrix[0][2] == player && | |
matrix[0][2] == matrix[1][1] && | |
matrix[1][1] == matrix[2][0]) | |
{ | |
cout << "You have won!\n"; | |
win++; | |
break; | |
} | |
if (moves == 9) | |
{ | |
cout << "There are no more valid moves. Game over.\n"; | |
draw++; | |
break; | |
} | |
if ((matrix[0][1] == computer && matrix[0][2] == computer && matrix[0][0] == '\0') || | |
(matrix[1][0] == computer && matrix[2][0] == computer && matrix[0][0] == '\0') || | |
(matrix[1][1] == computer && matrix[2][2] == computer && matrix[0][0] == '\0')) | |
{ | |
i=0; | |
j=0; | |
} | |
else if ((matrix[0][0] == computer && matrix[0][2] == computer && matrix[0][1] == '\0') || | |
(matrix[1][1] == computer && matrix[2][1] == computer && matrix[0][1] == '\0')) | |
{ | |
i=0; | |
j=1; | |
} | |
else if ((matrix[0][0] == computer && matrix[0][1] == computer && matrix[0][2] == '\0') || | |
(matrix[1][1] == computer && matrix[2][0] == computer && matrix[0][2] == '\0') || | |
(matrix[1][2] == computer && matrix[2][2] == computer && matrix[0][2] == '\0')) | |
{ | |
i=0; | |
j=2; | |
} | |
else if ((matrix[0][0] == computer && matrix[2][0] == computer && matrix[1][0] == '\0') || | |
(matrix[1][1] == computer && matrix[1][2] == computer && matrix[1][0] == '\0')) | |
{ | |
i=1; | |
j=0; | |
} | |
else if ((matrix[0][0] == computer && matrix[2][2] == computer && matrix[1][1] == '\0') || | |
(matrix[0][1] == computer && matrix[2][1] == computer && matrix[1][1] == '\0') || | |
(matrix[0][2] == computer && matrix[2][0] == computer && matrix[1][1] == '\0') || | |
(matrix[1][0] == computer && matrix[1][2] == computer && matrix[1][1] == '\0')) | |
{ | |
i=1; | |
j=1; | |
} | |
else if ((matrix[0][2] == computer && matrix[2][2] == computer && matrix[1][2] == '\0') || | |
(matrix[1][0] == computer && matrix[1][1] == computer && matrix[1][2] == '\0')) | |
{ | |
i=1; | |
j=2; | |
} | |
else if ((matrix[0][0] == computer && matrix[1][0] == computer && matrix[2][0] == '\0') || | |
(matrix[0][2] == computer && matrix[1][1] == computer && matrix[2][0] == '\0') || | |
(matrix[2][1] == computer && matrix[2][2] == computer && matrix[2][0] == '\0')) | |
{ | |
i=2; | |
j=0; | |
} | |
else if ((matrix[0][1] == computer && matrix[1][1] == computer && matrix[2][1] == '\0') || | |
(matrix[2][0] == computer && matrix[2][2] == computer && matrix[2][1] == '\0')) | |
{ | |
i=2; | |
j=1; | |
} | |
else if ((matrix[0][0] == computer && matrix[1][1] == computer && matrix[2][2] == '\0') || | |
(matrix[0][2] == computer && matrix[1][2] == computer && matrix[2][2] == '\0') || | |
(matrix[2][0] == computer && matrix[2][1] == computer && matrix[2][2] == '\0')) | |
{ | |
i=2; | |
j=2; | |
} | |
else if ((matrix[0][1] == player && matrix[0][2] == player && matrix[0][0] == '\0') || | |
(matrix[1][0] == player && matrix[2][0] == player && matrix[0][0] == '\0') || | |
(matrix[1][1] == player && matrix[2][2] == player && matrix[0][0] == '\0')) | |
{ | |
i=0; | |
j=0; | |
} | |
else if ((matrix[0][0] == player && matrix[0][2] == player && matrix[0][1] == '\0') || | |
(matrix[1][1] == player && matrix[2][1] == player && matrix[0][1] == '\0')) | |
{ | |
i=0; | |
j=1; | |
} | |
else if ((matrix[0][0] == player && matrix[0][1] == player && matrix[0][2] == '\0') || | |
(matrix[1][1] == player && matrix[2][0] == player && matrix[0][2] == '\0') || | |
(matrix[1][2] == player && matrix[2][2] == player && matrix[0][2] == '\0')) | |
{ | |
i=0; | |
j=2; | |
} | |
else if ((matrix[0][0] == player && matrix[2][0] == player && matrix[1][0] == '\0') || | |
(matrix[1][1] == player && matrix[1][2] == player && matrix[1][0] == '\0')) | |
{ | |
i=1; | |
j=0; | |
} | |
else if ((matrix[0][0] == player && matrix[2][2] == player && matrix[1][1] == '\0') || | |
(matrix[0][1] == player && matrix[2][1] == player && matrix[1][1] == '\0') || | |
(matrix[0][2] == player && matrix[2][0] == player && matrix[1][1] == '\0') || | |
(matrix[1][0] == player && matrix[1][2] == player && matrix[1][1] == '\0')) | |
{ | |
i=1; | |
j=1; | |
} | |
else if ((matrix[0][2] == player && matrix[2][2] == player && matrix[1][2] == '\0') || | |
(matrix[1][0] == player && matrix[1][1] == player && matrix[1][2] == '\0')) | |
{ | |
i=1; | |
j=2; | |
} | |
else if ((matrix[0][0] == player && matrix[1][0] == player && matrix[2][0] == '\0') || | |
(matrix[0][2] == player && matrix[1][1] == player && matrix[2][0] == '\0') || | |
(matrix[2][1] == player && matrix[2][2] == player && matrix[2][0] == '\0')) | |
{ | |
i=2; | |
j=0; | |
} | |
else if ((matrix[0][1] == player && matrix[1][1] == player && matrix [2][1] =='\0') || | |
(matrix[2][0] == player && matrix[2][2] == player && matrix [2][1] =='\0')) | |
{ | |
i=2; | |
j=1; | |
} | |
else if ((matrix[0][0] == player && matrix[1][1] == player && matrix[2][2] == '\0') || | |
(matrix[0][2] == player && matrix[1][2] == player && matrix[2][2] == '\0') || | |
(matrix[2][0] == player && matrix[2][1] == player && matrix[2][2] == '\0')) | |
{ | |
i=2; | |
j=2; | |
} | |
else | |
{ | |
if (matrix[1][1] == '\0') | |
{ | |
i=1; | |
j=1; | |
} | |
else if (matrix[0][0] == '\0') | |
{ | |
i=0; | |
j=0; | |
} | |
else if (matrix[0][2] == '\0') | |
{ | |
i=0; | |
j=2; | |
} | |
else if (matrix[2][0] == '\0') | |
{ | |
i=2; | |
j=0; | |
} | |
else if (matrix[2][2] == '\0') | |
{ | |
i=2; | |
j=2; | |
} | |
else if (matrix[0][1] == '\0') | |
{ | |
i=0; | |
j=1; | |
} | |
else if (matrix[1][0] == '\0') | |
{ | |
i=1; | |
j=0; | |
} | |
else if (matrix[1][2] == '\0') | |
{ | |
i=1; | |
j=2; | |
} | |
else if (matrix[2][1] == '\0') | |
{ | |
i=2; | |
j=1; | |
} | |
} | |
matrix[i][j]=computer; | |
moves++; | |
cout << " " << endl; | |
cout << " " << endl; | |
cout << " " << endl; | |
cout << " " << endl; | |
cout << " " << endl; | |
cout << " " << endl; | |
cout << "Now it is the computer's turn." << endl; | |
cout << " " << endl; | |
grid(); | |
if (matrix[0][0] == computer && | |
matrix[0][0] == matrix[1][0] && | |
matrix[1][0] == matrix[2][0]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[0][1] == computer && | |
matrix[0][1] == matrix[1][1] && | |
matrix[1][1] == matrix[2][1]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[0][2] == computer && | |
matrix[0][2] == matrix[1][2] && | |
matrix[1][2] == matrix[2][2]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[0][0] == computer && | |
matrix[0][0] == matrix[0][1] && | |
matrix[0][1] == matrix[0][2]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[1][0] == computer && | |
matrix[1][0] == matrix[1][1] && | |
matrix[1][1] == matrix[1][2]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[2][0] == computer && | |
matrix[2][0] == matrix[2][1] && | |
matrix[2][1] == matrix[2][2]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[0][0] == computer && | |
matrix[0][0] == matrix[1][1] && | |
matrix[1][1] == matrix[2][2]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
if (matrix[0][2] == computer && | |
matrix[0][2] == matrix[1][1] && | |
matrix[1][1] == matrix[2][0]) | |
{ | |
cout << "The computer has won!\n"; | |
loss++; | |
break; | |
} | |
} | |
moves=0; | |
table(); | |
cout << "\n\n\nWould you like to play again? (Y or N) :"; | |
cin >> cont; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment