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 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;
// needed for random number generator
#include <ctime>
#include <cstdlib>
// for results to be copied to file
#include <fstream>
using std::ofstream;
//prototype
void printGrid(const char[]); // prints the blank grid and selection options
void goX(char[]); // determine if player can chose that spot
void goO1(char[]); // for second player
void goO2(char[]); // dumb computer function
void goO3(char[]); // Smart computer function
char check4Xwinner(char[]); // check if 'X' is the winner
char check4Owinner(char[]); // check if 'O' is the winner
char check4Tie(char[]); // check for a tie
int main ()
{
// identifying information
cout << endl;
cout << "Midterm: Tic Tac Toe \n";
cout << "Programmer: Joshua Henry \n";
// set blank tic tac toe grid
char grid[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
// random number genrator for computer player
srand(time(0));
int opponent; // for user to select who they will face
char playAgain = 'Y'; // for users input if they want to play again
int xWins = 0;
int oWins = 0;
int tie = 0;
ofstream fout;
cout << endl;
cout << " Welcome to the Game of Tic Tac Toe! \n";
cout << " These are the Rules: \n";
cout << endl;
cout << " 1: You are Player 1 and will be X's and must get 3 X's in a row to win \n";
cout << " 2: Your reference board is shown on the right \n";
cout << " use (Q,W,E,A,S,D,Z,X,C) to chose your square. \n";
cout << " 3: You have 3 choices for your opponent who will be O's! \n";
cout << " 4: Your choices are another person, a dumb computer, or a smart computer \n";
cout << " which will analyze your move and try to either block or win! \n";
cout << " 5. Finally have fun and try not to lose! \n";
cout << endl;
while ((playAgain == 'Y') || (playAgain == 'y'))
{
cout << endl;
cout << " Choose your opponent! \n";
cout << " You have 3 options, please select # (1-3): \n";
cout << endl;
cout << " 1: Human Opponent \n";
cout << " 2: Dumb Computer Player \n";
cout << " 3: Smart Computer Player \n";
cout << endl;
cout << "Please enter your selection: ";
cin >> opponent;
cout << endl;
char grid[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
printGrid(grid);
goX(grid);
printGrid(grid);
while (1)
{
if (check4Xwinner(grid) == 'Y')
{
printGrid(grid);
cout << endl;
cout << "X's is the winner! \n";
cout << endl;
xWins++;
break;
}
// Checks if tie
if (check4Tie(grid) == 'Y')
{
printGrid(grid);
cout << endl;
cout << "It's a tie.... \n";
cout << endl;
tie++;
break;
} // end if
// O moves. Specific player that moves is selected by user
switch(opponent)
{
case 1: // Human
printGrid(grid);
goO1(grid);
printGrid(grid);
goX(grid);
printGrid(grid);
break;
case 2: // Dumb computer
printGrid(grid);
goO2(grid);
printGrid(grid);
goX(grid);
printGrid(grid);
break;
case 3: // Smart computer
printGrid(grid);
goO3(grid);
printGrid(grid);
goX(grid);
printGrid(grid);
break;
} // end switch
// Checks if O is winner
if (check4Owinner(grid) == 'Y')
{
printGrid(grid);
cout << endl;
cout << "O's is the winner! \n";
cout << endl;
oWins++;
break;
} // end if
} // end while
char check4Xwinner(char xwins[])
{
if ((xwins[0] == 'X') && (xwins[3] == 'X') && (xwins[6] == 'X'))
return 'Y'; // X wins! 3 in a row down the left column
else if ((xwins[0] == 'X') && (xwins[4] == 'X') && (xwins[8] == 'X'))
return 'Y'; // X wins! 3 in a row diagonally across
else if ((xwins[0] == 'X') && (xwins[1] == 'X') && (xwins[2] == 'X'))
return 'Y'; // X wins! 3 in a row across the top row
else if ((xwins[1] == 'X') && (xwins[4] == 'X') && (xwins[7] == 'X'))
return 'Y'; // X wins! 3 in a row up/down the middle column
else if ((xwins[2] == 'X') && (xwins[4] == 'X') && (xwins[6] == 'X'))
return 'Y'; // X wins! 3 in a row diagonally across
else if ((xwins[3] == 'X') && (xwins[4] == 'X') && (xwins[5] == 'X'))
return 'Y'; // X wins! 3 in a row across the middle row
else if ((xwins[2] == 'X') && (xwins[5] == 'X') && (xwins[8] == 'X'))
return 'Y'; // X wins! 3 in a row down the right column
else if ((xwins[6] == 'X') && (xwins[7] == 'X') && (xwins[8] == 'X'))
return 'Y'; // X wins! 3 in a row across the bottom row
else
return 'N'; // No winner yet
} // end check4Xwinner
char check4Owinner(char oWins[])
{
if ((oWins[0] == 'O') && (oWins[3] == 'O') && (oWins[6] == 'O'))
return 'Y'; // O wins! 3 in a row down the left column
else if ((oWins[0] == 'O') && (oWins[4] == 'O') && (oWins[8] == 'O'))
return 'Y'; // O wins! 3 in a row diagonally across
else if ((oWins[0] == 'O') && (oWins[1] == 'O') && (oWins[2] == 'O'))
return 'Y'; // O wins! 3 in a row across the top row
else if ((oWins[1] == 'O') && (oWins[4] == 'O') && (oWins[7] == 'O'))
return 'Y'; // O wins! 3 in a row up/down the middle column
else if ((oWins[2] == 'O') && (oWins[4] == 'O') && (oWins[6] == 'O'))
return 'Y'; // O wins! 3 in a row diagonally across
else if ((oWins[3] == 'O') && (oWins[4] == 'O') && (oWins[5] == 'O'))
return 'Y'; // O wins! 3 in a row across the middle row
else if ((oWins[2] == 'O') && (oWins[5] == 'O') && (oWins[8] == 'O'))
return 'Y'; // O wins! 3 in a row down the right column
else if ((oWins[6] == 'O') && (oWins[7] == 'O') && (oWins[8] == 'O'))
return 'Y'; // O wins! 3 in a row across the bottom row
else
return 'N'; // No winner yet
} // end check4Owinner
|