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
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
string board[3][3] = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
bool is_win = false;
void print_board()
{
cout << "-----------------------\n";
cout << "| 1" << setw(7) << "| 2" << setw(8) << "| 3" << setw(6) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "|" << setw(3) << board [0][0] << setw(4) <<"|" << setw(4) << board [0][1] << setw(4) << "|" << setw(4)<< board [0][2] << setw(4) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "-----------------------\n";
cout << "| 4" << setw(7) << "| 5" << setw(8) << "| 6" << setw(6) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "|" << setw(3) << board [1][0] << setw(4) <<"|" << setw(4) << board [1][1] << setw(4) << "|" << setw(4)<< board [1][2] << setw(4) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "-----------------------\n";
cout << "| 7" << setw(7) << "| 8" << setw(8) << "| 9" << setw(6) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "|" << setw(3) << board [2][0] << setw(4) <<"|" << setw(4) << board [2][1] << setw(4) << "|" << setw(4)<< board [2][2] << setw(4) << "|" "\n";
cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
cout << "-----------------------\n";
}
void select_grid (int a, string b)
{
switch (a)
{
case 1:
board [0][0] = b;
break;
case 2:
board [0][1] = b;
break;
case 3:
board [0][2] = b;
break;
case 4:
board [1][0] = b;
break;
case 5:
board [1][1] = b;
break;
case 6:
board [1][2] = b;
break;
case 7:
board [2][0] = b;
break;
case 8:
board [2][1] = b;
break;
case 9:
board [2][2] = b;
break;
}
}
void win_condition ()
{
// player x wins across
if ( board[0][0] == "x" && board[0][1] == "x" && board[0][2] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
else if ( board[1][0] == "x" && board [1][1] == "x" && board [1][2] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
else if ( board[2][0] == "x" && board [2][1] == "x" && board [2][2] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
// payer O wins across
else if ( board[0][0] == "o" && board[0][1] == "o" && board[0][2] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
else if ( board[1][0] == "o" && board [1][1] == "o" && board [1][2] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
else if ( board[2][0] == "o" && board [2][1] == "o" && board [2][2] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
// player X wins down
else if ( board[0][0] == "x" && board[1][0] == "x" && board[2][0] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
else if ( board[0][1] == "x" && board [1][1] == "x" && board [2][1] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
else if ( board[0][2] == "x" && board [1][2] == "x" && board [2][2] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
// player O wins down
else if ( board[0][0] == "o" && board[1][0] == "o" && board[2][0] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
else if ( board[0][1] == "o" && board [1][1] == "o" && board [2][1] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
else if ( board[0][2] == "o" && board [1][2] == "o" && board [2][2] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
// player X wins diagonally
else if ( board[0][0] == "x" && board[1][1] == "x" && board[2][2] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
else if ( board[0][2] == "x" && board [1][1] == "x" && board [2][0] == "x")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player X wins the game!";
}
// player O wins diagonally
else if ( board[0][0] == "o" && board[1][1] == "o" && board[2][2] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
else if ( board[0][2] == "o" && board [1][1] == "o" && board [2][0] == "o")
{
is_win = true;
cout << "Tic Tac Toe, three in a row. Player O wins the game!";
}
}
int main()
{
int a;
string b;
cout << "\t\t***Lets play Tic Tac Toe***\n";
cout << "\n\n";
cout<< "Here is how we play:\n";
cout << "\n";
cout << "On your turn, enter the number of the box where you want to go\n";
cout << "and then enter whether you are placing an 'X' or an 'O'\n";
cout << "\n\n";
print_board();
cout << "\n\n";
for (int i = 0; i < 9; i++)
{
while (is_win != true)
{
cout << "Enter the number of the box you want to play: ";
cin >> a;
cout << "Enter if you are 'X' or 'O': ";
cin >> b;
select_grid(a, b);
cout << "\n\n";
print_board();
win_condition();
}
}
cout << "\n";
cout << "Game over!\n";
cout << "\n";
}
|