Code:
#include<iostream>
using namespace std;
/* OK, the first problem I see with this is that your board is not properly initialized.
You should initialize the board in the construstor. your matrix would do better as a
class variable, instead of only visible in place. It might help to keep count of the
empty squares, too. Try to incluse the enum given in the problem. That's the type your
matrix should be. Use a loop to cycle through the columns and rows to set all the spaces
to "empty". You don't need column and for as class variables, be cause they are constantly
changing. Let's get the constructor correct, then we can worry about Place.
*/
class Board
{
public:
/* (Constructor) This initializes the board, which means it sets all the positions to empty
and does all else necessary to make the board ready for use
*/
Board();
//~Board();
//void Place(char, int, int);
/* Place: A procedure that take Player P and sets the column and Row of the board to be thr Player
P if that position is empty. If the position is ocupied, the player loses a turn.
*/
void Place(int, int); // right now testing to make sure things work in it
/* WIN: A function that returns true if Player P has won the game and false otherwise
*/
bool Win(char);
/* A function that returns true if the board represents a game in stalemate, and false otherwise */
bool Stalemate();
void DisplayBoard(char DisplayStatus[3][3]);
char ReturnBoard();
char TheCurrentBoard[3][3];
};
char Board::ReturnBoard()
{
return TheCurrentBoard[3][3];
}
void Board::DisplayBoard(char TheBoard[3][3])
{
cout << "The current board status is ";
cout<<"\n\t" << TheBoard[0][0] << " | " << TheBoard[0][1] << " | " << TheBoard[0][2];
cout<<"\n\t" << TheBoard[1][0] << " | " << TheBoard[1][1] << " | " << TheBoard[1][2];
cout<<"\n\t" << TheBoard[2][0] << " | " << TheBoard[2][1] << " | " << TheBoard[2][2];
cout <<"\n\n\n\n\n\n\n\n";
}
/*
bool Board::Win(char P)
{
cout << P << "Wins";
}
*/
Board::Board()
{
//displays the board and what spaces are what
int matrix[3][3]={1,2,3,4,5,6,7,8,9};
cout << " Here is the board layout";
cout << "\n";
cout << " Column Column Column " << endl;
cout << " Row__0,0__|__0,1__|___0,2__ " << endl;
cout << " Row__1,0__|__1,1__|___1,2__ " << endl;
cout << " Row__2,0__|__2,1__|___2,2__ " << endl;
}
void Board::Place(int Column,int Row)
{
int matrix[3][3]=
{
{1,2,3},
{4,5,6},
{7,8,9}
};
bool player=1;
char pieceX='X';
char pieceO='O';
char location[3][3]=
{
{'A','B','C'},
{'D','E','F'},
{'G','H','I'}
};
cout << " THe board currently";
cout<<"\n\t" << location[0][0] << " | " << location[0][1] << " | " << location[0][2];
cout<<"\n\t" << location[1][0] << " | " << location[1][1] << " | " << location[1][2];
cout<<"\n\t" << location[2][0] << " | " << location[2][1] << " | " << location[2][2];
cout <<"\n\n\n\n\n\n\n\n";
switch (Column)
{
case 0:
switch (Row)
{
case 0:
matrix[0][0];
// also add in if a certain player is valid, matrix [][] = X or 0
// like if (P)
if (player)
{
location[0][0] = 'X';
}
else
{
location[0][0] ='Y';
}
break;
case 1:
matrix[0][1];
//cout << matrix[0][1];
// also add in if a certain player is valid, matrix [][] = X or 0
// like if (P)
if (player)
{
location[0][1] = 'X';
}
else
{
location[0][1] ='Y';
}
break;
case 2:
matrix[0][2];
if (player)
{
location[0][2] = 'X';
}
else
{
location[0][2] ='Y';
}
break;
default:
cout<<"Opps!"<<endl;
break;
}
break;
case 1:
switch (Row)
{
case 0:
matrix[1][0];
if (player)
{
location[1][0] = 'X';
}
else
{
location[1][0] ='Y';
}
break;
case 1:
matrix[1][1];
if (player)
{
location[1][1] = 'X';
}
else
{
location[1][1] ='Y';
}
break;
case 2:
matrix[1][2];
if (player)
{
location[1][2] = 'X';
}
else
{
location[1][2] ='Y';
}
break;
default:
cout<<"Opps!"<<endl;
break;
}
break;
case 2:
switch (Row)
{
case 0:
matrix[2][0];
if (player)
{
location[2][0] = 'X';
}
else
{
location[2][0] ='Y';
}
break;
case 1:
matrix[2][1];
if (player)
{
location[2][1] = 'X';
}
else
{
location[2][1] ='Y';
}
break;
case 2:
matrix[2][2];
if (player)
{
location[2][2] = 'X';
}
else
{
location[2][2] ='Y';
}
break;
default:
cout<<"Opps!"<<endl;
break;
}
break;
default:
cout<<"Opps!"<<endl;
break;
} //end
cout << " THe board currently";
cout<<"\n\t" << location[0][0] << " | " << location[0][1] << " | " << location[0][2];
cout<<"\n\t" << location[1][0] << " | " << location[1][1] << " | " << location[1][2];
cout<<"\n\t" << location[2][0] << " | " << location[2][1] << " | " << location[2][2];
cout <<"\n\n";
//change player turn
//player=!player;
}//end while
/*
winning conditions are 1,2,3 matrix[0][0],matrix[0][1],matrix[0][2]
4,5,6 matrix[1][0],matrix[1][1],matrix[1][2]
7,8,9 matrix[2][0],matrix[2][1],matrix[2][2]
1,4,7 matrix[0][0],matrix[1][0],matrix[2][0]
2,5,8 matrix[0][1],matrix[1][1],matrix[2][1]
3,6,9 matrix[0][2],matrix[1][2],matrix[2][2]
1,5,9 matrix[0][0].matrix[1][1],matrix[2][2]
3,5,7 matrix[0][2],matrix[1][1],matrix[2][0]
*/
int main()
{
Board check;
char playerchoice= 'y';
int ThisColumn;
int ThisRow;
// should be while a player has not won the game or all the blocks are full repeat the game
while (playerchoice=='y') // only a repeat to test the board at the moment
{
cout << " Enter your selection as shown {Column, Row) ";
cout << "Please choose the column :" ;
cin >> ThisColumn;
cout << "Please choose the Row :";
cin >> ThisRow;
check.Place(ThisColumn, ThisRow);
check.DisplayBoard(check.ReturnBoard())
//check.Stalemate() check to see if there was a stalemate
//check.Win(P) check to see if a player won
cout << "Play again? y or n";
cin >> playerchoice;
}
system("pause");
return 0;
}