I have to make a Tic Tac Toe game for class and I have a problem with checking if the space is available when the player enters it. Everything else works fine.
Here is my main:
#include<iostream>
#include"tictactoe.h"
using namespace std;
int choice;
int main(){
TicTacToe myBoard = TicTacToe();
cout<<"Enter the number of the space you wish to choose."<<endl;
cout<<"The board spaces are as follows: "<<endl;
cout<<"1|2|3" <<endl;
cout<<"-|-|-" <<endl;
cout<<"4|5|6"<<endl;
cout<<"-|-|-"<<endl;
cout<<"7|8|9\n"<<endl;
while(!myBoard.getWin()){
myBoard.setPlayerCode(1);
cout<<"Player 1, enter the space you wish to choose: ";cin>>choice;
myBoard.setPlayerChoice(choice);
if(myBoard.getSpace()==0){
myBoard.setSpace(choice);
}while(myBoard.getSpace()!=0){
cout<<"That space is not available, please choose another one: ";cin>>choice;
myBoard.setPlayerChoice(choice);
myBoard.setSpace(choice);
}
myBoard.checkForWin();
myBoard.showBoard();
myBoard.setPlayerCode(2);
cout<<"Player 2, enter the space you wish to choose: ";cin>>choice;
myBoard.setSpace(choice);
myBoard.checkForWin();
myBoard.showBoard();
}
}
And my TicTacToe class:
using namespace std;
class TicTacToe{
private:
int space1;
int space2;
int space3;
int space4;
int space5;
int space6;
int space7;
int space8;
int space9;
int playerChoice;
int playerCode;
bool won;
bool valid;
public:
TicTacToe(){
space1=0; space2=0; space3=0; space4=0; space5=0; space6=0; space7=0; space8=0; space9=0;
won = false;
}
void setPlayerChoice(int playerChoice){
this->playerChoice=playerChoice;
}
void setSpace(int playerChoice){
//Sets the space to 1 or 2 depending on which player chose the space
if(playerChoice==1){
if(playerCode==1){
if(space1==0){
space1=1;
valid = true;
}else{
space1=space1;
valid = false;
}
}else if(playerCode==2){
if(space1==0){
space1=2;
valid = true;
}else{
space1=space1;
valid = false;
}
}
}else if(playerChoice==2){
if(playerCode==1){
space2=1;
}else if(playerCode==2){
space2=2;
}
}else if(playerChoice==3){
if(playerCode==1){
space3=1;
}else if(playerCode==2){
space3=2;
}
}else if(playerChoice==4){
if(playerCode==1){
space4=1;
}else if(playerCode==2){
space4=2;
}
}else if(playerChoice==5){
if(playerCode==1){
space5=1;
}else if(playerCode==2){
space5=2;
}
}else if(playerChoice==6){
if(playerCode==1){
space6=1;
}else if(playerCode==2){
space6=2;
}
}else if(playerChoice==7){
if(playerCode==1){
space7=1;
}else if(playerCode==2){
space7=2;
}
}else if(playerChoice==8){
if(playerCode==1){
space8=1;
}else if(playerCode==2){
space8=2;
}
}else if(playerChoice==9){
if(playerCode==1){
space9=1;
}else if(playerCode==2){
space9=2;
}
}else{
cout<<"Invalid entry" <<endl;
}
}
int getSpace(){
if(playerChoice==1){
return space1;
}
else if(playerChoice==2){
return space2;
}
else if(playerChoice==3){
return space3;
}
else if(playerChoice==4){
return space4;
}
else if(playerChoice==5){
return space5;
}
else if(playerChoice==6){
return space6;
}
else if(playerChoice==7){
return space7;
}
else if(playerChoice==8){
return space8;
}
else if(playerChoice==9){
return space9;
}else{
return 3;
}
}
void setPlayerCode(int playerCode){
//Sets whether player 1 or 2 is currently playing.
this->playerCode=playerCode;
}
void showBoard(){
cout<<space1 <<"|" <<space2 <<"|" <<space3 <<endl;
cout<<"-|-|-" <<endl;
cout<<space4 <<"|" <<space5 <<"|" <<space6 <<endl;
cout<<"-|-|-" <<endl;
cout<<space7 <<"|" <<space8 <<"|" <<space9 <<endl;
}
void checkForWin(){
if(space1==1&&space2==1&&space3==1){
cout<<"PLAYER 1 WINS!!!" <<endl;
won = true;
}
else{
won = false;
}
}
bool getWin(){
if(won){
return true;
}
if(!won){
return false;
}
}
bool isValid(){
if(valid){
return true;
}
if(!valid){
return false;
}
}
void setValid(bool valid){
this->valid = valid;
}
};
The assignment says to enter 1 and 2 instead of X and O's, so that is no mistake. I tried using a loop in my main class to see if the space is available, but regardless of whether it's available or not, it keeps saying not available.