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
|
#include <iostream>
#include <cmath>
using namespace std;
const int row = 3;
const int col = 3;
class TicTacToe
{
public:
void printBoard();
void printBoardGame(int, int, char);
void playGame();
void endGame(char);
int inputR();
int inputC();
bool checkWin(char);
bool check(int, int);
char playerTurn(int, int);
private:
char board[3][3];
};
void TicTacToe::printBoard()
{
int i, j, k, l;
for (k = 0; k < row; k++)
{
for (l = 0; l < col; l++)
{
board[k][l] = '*';
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
cout << board[i][j];
}
cout << endl;
}
}
char TicTacToe::playerTurn(int i, int j)
{
char player;
if (((i+j) % 2) == 0)
player = 'X';
else
player = 'O';
cout << "Player "<<player<<"'s turn."<<endl;
return player;
}
int TicTacToe::inputR()
{
int x;
cout << "Input X coordinate(1-3): ";
cin >> x;
while ((x < 1)||(x>3))
{
cout << "Invalid number. Try again: ";
cin >> x;
}
return x-1;
}
int TicTacToe::inputC()
{
int y;
cout << "Input Y coordinate(1-3): ";
cin >> y;
while ((y < 1)||(y>3))
{
cout << "Invalid number. Try again: ";
cin >> y;
}
return y-1;
}
void TicTacToe::printBoardGame(int x, int y, char player)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board [x][y] = player;
cout << board[i][j];
}
cout << endl;
}
}
bool TicTacToe::check(int x, int y)
{
if (board[x][y] != '*')
return false;
else return true;
}
void TicTacToe::playGame()
{
int i, j, x, y;
char t;
bool z, q;
printBoard();
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
t = playerTurn(i, j);
x = inputR();
y = inputC();
q = check(x,y);
while(q == false)
{
cout << "Invalid input!"<<endl;
inputR();
inputC();
check(x,y);
}
printBoardGame(x, y, t);
z = checkWin(t);
if (z == true)
{
j = col+1;
i = row+1;
}
}
}
if (j == col)
cout << "Game over! It's a tie!";
}
bool TicTacToe::checkWin(char player)
{
if (((board[0][0]) == (board[0][1])) && ((board[0][1]) == (board[0][2])) &&((board[0][0]) != '*'))
{endGame(player); return true;}
else if (((board[1][0]) == (board[1][1])) && ((board[1][1]) == (board[1][2])) &&((board[1][1]) != '*'))
{endGame(player); return true;}
else if (((board[2][0]) == (board[2][1])) && ((board[2][1]) == (board[2][2])) &&((board[2][2]) != '*'))
{endGame(player); return true;}
else if (((board[0][0]) == (board[1][0])) && ((board[1][0]) == (board[2][0])) &&((board[0][0]) != '*'))
{endGame(player); return true;}
else if (((board[0][1]) == (board[1][1])) && ((board[1][1]) == (board[2][1])) &&((board[0][1]) != '*'))
{endGame(player); return true;}
else if (((board[0][2]) == (board[1][2])) && ((board[1][2]) == (board[2][2])) &&((board[0][2]) != '*'))
{endGame(player); return true;}
else if (((board[0][0]) == (board[1][1])) && ((board[1][1]) == (board[2][2])) &&((board[2][2]) != '*'))
{endGame(player); return true;}
else if (((board[0][2]) == (board[1][1])) && ((board[1][1]) == (board[2][0])) &&((board[0][2]) != '*'))
{endGame(player); return true;}
else return false;
}
void TicTacToe::endGame(char player)
{
cout << "Game Over! Player "<<player<<" wins!";
}
int main()
{
TicTacToe object;
object.playGame();
return 0;
}
|