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
|
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <errno.h>
using namespace std;
using std::string;
enum SquareState { blank = ' ', X = 'X', O='O'};
class GameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* gameBoard;
public:
GameBoard() : WIDTH(3),HEIGHT(3)
{
gameBoard = new int[9];
for (int i = 0; i < 9; i++)
*(gameBoard + i) = blank;
}
~GameBoard() {delete[] gameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void GameBoard::setX(int h, int w)
{
*(gameBoard + h*HEIGHT + w) = X;
}
void GameBoard::set0(int h, int w)
{
*(gameBoard + h*HEIGHT + w) = O;
}
bool GameBoard::isTaken (int h, int w)
{
return *(gameBoard + h*HEIGHT + w) != ' ';
}
SquareState GameBoard::isLine()
{
if(*gameBoard==X && *(gameBoard +1)==X && *(gameBoard +2)==X)
return X;
if(*gameBoard==O && *(gameBoard +1)==O && *(gameBoard +2)==O)
return O;
if(*(gameBoard +3)==X && *(gameBoard +4)==X && *(gameBoard +5)==X)
return X;
if(*(gameBoard +3)==O && *(gameBoard +4)==O && *(gameBoard +5)==O)
return O;
if(*(gameBoard +6)==X && *(gameBoard +7)==X && *(gameBoard +8)==X)
return X;
if(*(gameBoard +6)==O && *(gameBoard +7)==O && *(gameBoard +8)==O)
return O;
if(*gameBoard==X && *(gameBoard +3)==X && *(gameBoard +6)==X)
return X;
if(*gameBoard==O && *(gameBoard +3)==O && *(gameBoard +6)==O)
return O;
if(*(gameBoard +1)==X && *(gameBoard +4)==X && *(gameBoard +7)==X)
return X;
if(*(gameBoard +1)==O && *(gameBoard +4)==O && *(gameBoard +7)==O)
return O;
if(*(gameBoard +2)==X && *(gameBoard +5)==X && *(gameBoard +8)==X)
return X;
if(*(gameBoard +2)==O && *(gameBoard +5)==O && *(gameBoard +8)==O)
return O;
if(*gameBoard==X && *(gameBoard +4)==X && *(gameBoard +8)==X)
return X;
if(*gameBoard==O && *(gameBoard +4)==O && *(gameBoard +8)==O)
return O;
if(*(gameBoard +2)==X && *(gameBoard +4)==X && *(gameBoard +6)==X)
return X;
if(*(gameBoard +2)==O && *(gameBoard +4)==O && *(gameBoard +6)==O)
return O;
return blank;
}
void GameBoard::draw()
{
using std::cout;
cout << "\n";
for (int i=0; i < HEIGHT; i++)
{
cout << (char)*(gameBoard + i*HEIGHT);
for(int c=1; c < WIDTH; c++)
cout << " | " << (char)*(gameBoard + i*WIDTH + c);
cout << "\n" << "-------" << "/n";
}
}
class Game
{
public:
GameBoard* doInput(string player, gameBoard* gb);
bool inRange(int test);
};
GameBoard* Game::doInput(string player, GameBoard* gb)
{
using std::cout;
using std::cin;
gb->draw();
string letter;
if (player.compare("one")==O)
letter = "X";
else if (player.compare("two")==O)
letter = "O";
else return gb;
int input1, input2;
do {
do {
cout << "\nPlayer " << player.c_str()
<< ", please enter a row number to put an "
<< letter.c_str() << ": ";
cin >> input1;
} while(!inRange(input1));
do{
cout << "\nPlease enter a column number to put an "
<< letter.c_str() << ": ";
cin >> input2;
} while(!inRange(input2));
} while (gb->isTaken(input1, input2));
if (player.compare("one") == O)
gb->setX(input1, input2);
else
gb->setO(input1, input2);
return gb;
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
}
int main( void )
{
using std::cout;
using std::cin;
GameBoard* gb = new GameBoard;
Game g;
string player1, player2;
cout << "Welcome to Tic Tac Toe!"
<< "\nPlayer one, please enter your name: ";
cin >> player1;
cout << "\nPlayer two, please enter your name: ";
cint >> player2;
while (gb->isLine()==' ' )
{
gb = g.doInput("one",gb);
gb = g.doInput("two",gb);
}
gb->draw();
if(gb->isLine() == X)
cout << "\nPlayer one, you win!\nGame Over.";
else
cout << "\nPlayer two, you win!\nGame Over.";
return 0;
}
|