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
|
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[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
while (1)
{
printGrid(grid);
goX(grid);
printGrid(grid);
if (check4Xwinner(grid) == 'Y')
{
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);
break;
case 2: // Dumb computer
printGrid(grid);
goO2(grid);
printGrid(grid);
break;
case 3: // Smart computer
printGrid(grid);
goO3(grid);
printGrid(grid);
break;
} // end switch
// Checks if O is winner
if (check4Owinner(grid) == 'Y')
{
cout << endl;
cout << "O's is the winner! \n";
cout << endl;
oWins++;
break;
} // end if
} // end while
cout << "Would you like to play the game again? [Y/N]: ";
cin >> playAgain;
if (playAgain == 'N' || playAgain == 'n')
{
cout << endl;
cout << "Results of games played \n";
cout << endl;
cout << "You won: " << xWins << " times! \n";
cout << "Your opponent won: " << oWins << " times... \n";
cout << "You tied: " << tie << " times. \n";
cout << endl;
ofstream fout;
fout.open("T3.ini", std::fstream::in |std::fstream::out|std::fstream::trunc);
fout << xWins << endl;
fout << oWins << endl;
fout << tie << endl;
}
} // end second while
fout.close();
return 0;
}
void printGrid(const char grid1[])
{
cout << " Players Table Reference Grid \n";
cout << endl;
cout << " " << grid1[0] << "|" << grid1[1] << "|" << grid1[2] << " Q|W|E" << endl;
cout << " --+-+-- --+-+--" << endl;
cout << " " << grid1[3] << "|" << grid1[4] << "|" << grid1[5] << " A|S|D" << endl;
cout << " --+-+-- --+-+--" << endl;
cout << " " << grid1[6] << "|" << grid1[7] << "|" << grid1[8] << " Z|X|C" << endl;
}
void goX(char user[])
{
char input;
while (1)
{
cout << "You are X's please select a square: ";
cin >> input;
while (1)
{
cout << "You are O's please chose a square: " << endl;
cin >> input2;
if (input2 == 'Q' || input2 == 'q')
{
if (user2[0] == ' ')
{
user2[0] = 'O';
|