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
|
class TicTacToe
{
private:
char board[3][3];
public:
TicTacToe();
void CreateBoard();
void PrintBoard();
void GetMove(int i , int j, int move);
void CheckPosition (int& i, int& j);
bool CheckBoard ();
int numberOfmoves();
};
int main(int argc, char *argv[]){
TicTacToe user;
int row, column, Xs,Os, turns=0;
user.CreateBoard();
user.PrintBoard();
int b= 9;
while(!user.CheckBoard()){
for(int index = 0; index <= 0; index++){
Xs = 1;
cout << "Player 1: Please enter a cell on the board.\n";
cin>>row>>column;
user.CheckPosition(row, column);
user.GetMove(row, column, 1);
}
for(int idx = 0; idx <= 0; idx++){
Os = 2;
cout << "Player 2: Please enter a cell on the board.\n";
cin>>row>>column;
user.CheckPosition(row, column);
user.GetMove(row, column, 2);
}
}
return 0;
}
void TicTacToe::CreateBoard(){
int size =3;
for (int row = 0; row < size; row++){
for (int column = 0; column < size; column++){
board[row][column] = '0';
}
}
cout<<"New board created!\n";
}
TicTacToe::TicTacToe(){
board[3][3]=0;
}
void TicTacToe::PrintBoard(){
int size =3;
for (int row = 0; row < size; row++){
for (int column = 0; column < size; column++){
cout <<'\t'<< board[row][column];
}
cout << endl;
}
}
//Gets the move from the user
void TicTacToe::GetMove(int i, int j, int move){
int size=3;
int row, column;
bool r=false, b;
if(move==1){
move='X';
}
if(move==2){
move='O';
}
while(r=false){ //if the player enters a cell that is taken he should be prompted to enter a new cell, but instead it just keeps over riding the previous move
for (int row = 0; row < size; row++){
for (int column = 0; column < size; column++){
if(row==i && column==j){
if(board[row][column]=0){ //this checks to see if the cell is taken
r=true;
break;
}
}
}
}
if(r=false){
cout<<"This cell is taken. Please enter a new cell.\n";
cin >>i>>j;
}
}
if(r=true){
board[i][j]=move;
}
PrintBoard();
}
void TicTacToe::CheckPosition(int& i, int& j){
while(i<0 || i>=3 && j<0 || j>=3){
cout<<"Please enter proper coordinates for a 3x3 grid.\n";
cin>>i>>j;
}
}
//checks to see if board is full and returns a bool
bool TicTacToe::CheckBoard(){//keeps giving me one extra move, so 10 instead of 9.
bool b;
int counter;
for (int row = 0; row < 3; row++){
for (int column = 0; column < 3; column++){
if(board[row][column]=='0'){
return false;
}
}
}
return true;
}
|