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
|
#include <iostream>
#include <string>
using namespace std;
bool verifyWin (char);
int showTable (int, int);
char display [5][5] = { { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' } };
int x, y;
int main (){
string game("Tic Tac Toe");
cout<<game<<endl;
char board [3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } },
player[2] = { ' ', ' ' };
int t = 'y';
bool gameWon = false;
while (t != 'n')
{
int turns = 0;
cout << "Player 1 enter the character you would like to play with: ";
cin >> player[0];
while (player[0] == ' ' || player[0] == ' ')
{
cout << "Do not enter a space, enter a character: ";
cin >> player[0];
}
cout << endl << "Player 2 enter the character you are going to use (no spaces): ";
cin >> player[1];
while (player[1] == ' ' || player[1] == ' ')
{
cout << "Do not enter a space, enter a character: ";
cin >> player[1];
}
while (turns != 9 && !gameWon)
{
cout << endl << endl;
bool first = true;
bool second = true;
while (first && turns != 9)
{
first = false;
showTable(x, y);
cout << endl << "Player 1: Enter 2 numbers (1-3) to place your symbol: ";
cin >> x >> y;
while (x > 3 || x < 1 || y > 3 || y < 1)//user can't enter anything less or more than number 1-3
cout << "You must enter numbers from 1 to 3, enter again: ";
cin >> x >> y;
}
if (board[x - 1][y - 1] != ' ') //user has to enter in an empty space
{
first = true;
cout << "That spot is already taken." << endl ;
}
else
{
board[x - 1][y - 1] = player[0];
x = (x * 2) - 1;
y = (y * 2) - 1;
display[x - 1][y - 1] = player[0];
if (verifyWin(player[0])) gameWon = true; //call and statement,
turns++;
//places board position by substracting from each variable
}
}
}
cout << endl;
showTable(x, y);
if (verifyWin(player[0]))
cout << "Player 1 wins" <<endl;
else
if (verifyWin(player[1]))
cout <<"Player 2 wins" << endl;
else
{
cout<<"The game is a draw." << endl;
cout << "Would you like to play again? (y)?(n)?: ";
cin >> t;
if (t != 'n')
for (int x = 1; x <= 3; x++)
for (int y = 1; y <= 3; y++)
board[x - 1][y - 1] = ' ';
display[(x * 2) - 2][(y * 2) - 2] = ' ';
}
cout<<endl;
return 0;
}
|