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
|
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
class TicTacToe{
private:
char theBoard[3][3]; //variable
public:
TicTacToe(void);//constructor
void playOneGame(void);//member function
void switchPlayer(char &);//member function
void showBoard(void);// member function
void postMove(int, int, char);// member function
char determineWinner(void);// member function
};
int main(void){
//test the class by playing one game
TicTacToe Game1;
Game1.playOneGame();
}//end main
void TicTacToe::playOneGame(void){
//start a game and play until someone wins or a draw occurs
const int MaxMoves = 9;
char currentPlayer = 'O';
int row = 0;
int clmn = 0;
char theWinner = ' ';
int nmbrOfMoves = 0; //kep track of the number
do{
switchPlayer(currentPlayer);//change player from x to o or vice versa
showBoard();
cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
cout << "\nEnter your row (01, 02, 03): ";
cin >> row;
cout << "\nEnter your column (01, 02, 03): ";
cin >> clmn;
postMove(row, clmn, currentPlayer); //post the move to the board
theWinner = determineWinner(); //see if anyone won the game
nmbrOfMoves++;//keeptrack of the number of moves
}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));
showBoard();//show the ending board
if(theWinner != 'D') //declare a winner
cout << "\n\nTheWinner is player " << theWinner;
else
cout << "\n\nThe Game was a Draw" << endl;
}
TicTacToe::TicTacToe(void){
//intialize the array contents
for(int row= 0; row< 3; row ++){
for (int clmn=0; clmn<3; clmn++){
theBoard[row][clmn]=' ';
}
}
}
void TicTacToe::switchPlayer(char ¤tPlayer){
//switchs the current player
currentPlayer = ' ';
if (currentPlayer ='X')
{
(currentPlayer = 'O');
cout << "\nIt's player 1 turn" << endl;
}
else
(currentPlayer = 'X');
}
void TicTacToe::showBoard(){
//displays the board
cout << " Tic Tac Toe!" << endl << endl;
cout << " 1 2 3" << endl << endl;
cout << "1 " << theBoard[0][0] << " | " << theBoard[0][1] << " | " << theBoard[0][2] << endl;
cout << " -----------------" << endl;
cout << "2 " << theBoard[1][0] << " | " << theBoard[1][1] << " | " << theBoard[1][2] << endl;
cout << " -----------------" << endl;
cout << "3 " << theBoard[2][0] << " | " << theBoard[2][1] << " | " << theBoard[2][2] << endl << endl;
}
void TicTacToe::postMove(int row, int col, char value){
//gets the users move and post it to the board//showXO
char currentPlayer = ' ';
theBoard[row-1][col-1]= value;
if (row!=0 && row != 1 && row != 2)
{
cout << "\nInvalid move, try Again" << endl;
}
else if (col != 0 && col != 1 && col != 2)
{
cout << "\nInvalid move, try Again" << endl;;
}
else if(theBoard[row][col]== currentPlayer)
{
cout << "\nAlready Taken, Try Again" << endl;;
}
}
char TicTacToe::determineWinner(void){
//analyzes the board to see if there is a winner
//returns a X, O indicating the winner
//if the game is a draw then D is returned
//check the rows
for (int i = 0; i < 3; i++){
if (theBoard[i][0] == theBoard[i][1]
&& theBoard[i][1] == theBoard[i][2]
&& theBoard[i][0] != ' '){
return theBoard[i][0];
}
}
//check the clmns
for (int i = 0; i < 3; i++){
if (theBoard[0][i] == theBoard[1][i]
&& theBoard[1][i] == theBoard[2][i]
&& theBoard[0][i] != ' '){
return theBoard[0][i];
}
}
//check the diagnals
if (theBoard[0][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[2][2]
&& theBoard[0][0] != ' ') {
return theBoard[0][0];
}
if (theBoard[2][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[0][2]
&& theBoard[2][0] != ' ') {
return theBoard[2][0];
}
return 'D';
}
|