1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function Prototypes
void Display_Board(char[], string, string);
bool Rater(char[], int&, int&);
int main()
{
//Variables Definition
int score1, score2, count1, count2;
string Player1, Player2;
//Dynamic Memory Allocation
char *again = new char;
int *board = new int;
//Array for game board
char Board_array[10] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
cout << "\t\t\tWelcome to Tic Tac Toe" << endl << endl << endl;
//Prompts for user names
cout << "Player 1 enter your name" << endl;
getline(cin, Player1);
cout << endl;
cout << "Player 2 enter your name" << endl;
getline(cin, Player2);
//Resets the score
score1 = 0;
score2 = 0;
do
{
//Clears the screen
system("cls");
//Displays the game board
Display_Board(Board_array, Player1, Player2);
//Resets the number of board markers
count1 = 0;
count2 = 0;
//Loop for a game
while ((count1 + count2) != 9)
{
cout << endl << endl;
//Prompts the user for input
cout << Player1 << "'s turn: ";
cin >> *board;
//Input Validation
if (Board_array[(*board) - 1] == 'X' || Board_array[(*board) - 1] == '0')
*board = 20;
while (!(*board > 0 && *board < 10))
{
cout << "ERROR! Please enter a valid field from 1 - 9" << endl;
cin >> *board;
if (Board_array[(*board) - 1] == 'X' || Board_array[(*board) - 1] == '0')
*board = 20;
}
//Places 'X' on player's desired location
Board_array[(*board) - 1] = 'X';
//Clears the screen
system("cls");
//Displays the board for Player 2
Display_Board(Board_array, Player1, Player2);
count1++;
cout << endl << endl;
//Rates the input of user if required
if (Rater(Board_array, score1, score2))
{
cout << Player1 << " wins Congratulation!" << endl << endl;
break;
}
else if ((count1)+(count2) == 9)
break;
cout << endl << endl;
//Prompts the user for input
cout << Player2 << "'s turn: ";
cin >> *board;
//Input Validation
if (Board_array[(*board) - 1] == 'X' || Board_array[(*board) - 1] == '0')
*board = 20;
while (!(*board > 0 && *board < 10))
{
cout << "ERROR! Please enter a valid field from 1 - 9" << endl;
cin >> *board;
if (Board_array[(*board) - 1] == 'X' || Board_array[(*board) - 1] == '0')
*board = 20;
}
//Places '0' on player's desired location
Board_array[(*board) - 1] = '0';
count2++;
//Clears the screen
system("cls");
//Displays the game board for Player 2
Display_Board(Board_array, Player1, Player2);
cout << endl << endl;
if (Rater(Board_array, score1, score2))
{
cout << Player2 << " wins Congatulation!" << endl << endl;
break;
}
else if ((count1) + (count2) == 9)
break;
}
cout << "Do you want to play again Y/N : ";
cin >> *again;
while (!((*again == 'Y' || *again == 'y') || (*again == 'N' || *again == 'n')))
{
cout << "ERROR! Please enter a valid answer" << endl;
cin >> *again;
}
Board_array[0] = '1';
Board_array[1] = '2';
Board_array[2] = '3';
Board_array[3] = '4';
Board_array[4] = '5';
Board_array[5] = '6';
Board_array[6] = '7';
Board_array[7] = '8';
Board_array[8] = '9';
} while (*again == 'Y' || *again == 'y');
delete again, board;
cout << endl << endl;
if (score1 > score2)
cout << Player1 << " wins with " << score1 << " wins" << " against " << Player2 << " with " << score2 << " wins" << endl;
else if (score1 < score2)
cout << Player2 << " wins with " << score2 << " wins" << " against " << Player1 << " with " << score1 << " wins" << endl;
else
cout << "The match was a tie with both players scoring " << score1 << endl;
cin.get();
return 0;
}
void Display_Board(char array[], string name1, string name2)
{
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << setw(5) << array[0] << setw(5) << "|" << setw(5) << array[1] << setw(5) << "|" << setw(5) << array[2] << endl;
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << "------------------------------" << endl;
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << setw(5) << array[3] << setw(5) << "|" << setw(5) << array[4] << setw(5) << "|" << setw(5) << array[5] << endl;
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << "------------------------------" << endl;
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << setw(5) << array[6] << setw(5) << "|" << setw(5) << array[7] << setw(5) << "|" << setw(5) << array[8] << endl;
cout << setw(10) << "|" << setw(10) << "|" << endl;
cout << endl << endl << endl;
cout << name1 << ": X\n" << name2 << ": 0" << endl;
}
bool Rater(char array[], int &score1, int &score2)
{
//Horizontal test cases
if ((array[0] == 'X' && array[1] == 'X') && array[2] == 'X')
score1++;
else if ((array[0] == '0' && array[1] == '0') && array[2] == '0')
score2++;
else if ((array[3] == 'X' && array[4] == 'X') && array[5] == 'X')
score1++;
else if ((array[3] == '0' && array[4] == '0') && array[5] == '0')
score2++;
else if ((array[6] == 'X' && array[7] == 'X') && array[8] == 'X')
score1++;
else if ((array[6] == '0' && array[7] == '0') && array[8] == '0')
score2++;
//Vertical test cases
else if ((array[0] == 'X' && array[3] == 'X') && array[6] == 'X')
score1++;
else if ((array[0] == '0' && array[3] == '0') && array[6] == '0')
score2++;
else if ((array[1] == 'X' && array[4] == 'X') && array[7] == 'X')
score1++;
else if ((array[1] == '0' && array[4] == '0') && array[7] == '0')
score2++;
else if ((array[2] == 'X' && array[5] == 'X') && array[8] == 'X')
score1++;
else if ((array[2] == '0' && array[5] == '0') && array[8] == '0')
score2++;
//Diagonal Test Cases
else if ((array[0] == 'X' && array[4] == 'X') && array[8] == 'X')
score1++;
else if ((array[0] == '0' && array[4] == '0') && array[8] == '0')
score2++;
else if ((array[2] == 'X' && array[4] == 'X') && array[6] == 'X')
score1++;
else if ((array[2] == '0' && array[4] == '0') && array[6] == '0')
score2++;
else
return false;
return true;
}
|