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.

Recommended Answers

All 7 Replies

After looking over your code the problem seems to be in main().

You have it so when you input your selection it sets the choice and the space THEN checks to see if the space is 0 or not. This will never be 0 because you are setting it before you check it. The easy fix to this is to set your choice check to see if that space is open with the while loop you have. Then set the space on the board.

Not sure if you are allowed to use arrays or anything but that since you are using a class I'll assume you can because it would condense your code by a ton.

This is a condenser/fix to your main() you can use it or you can just read the above and work it out yourself.

#include <iostream>
#include "tictactoe.h"

using namespace std;

int choice; //not gonna change this but I would either make myBoard global with choice or
			//put choice in with myBoard

int main()
{
	TicTacToe myBoard = TicTacToe();
	int playerTurn = 0; //keeps track of the players turn 0 = P1, 1 = P2

	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(playerTurn+1);
		cout<<"Player " << playerTurn+1 << ", enter the space you wish to choose: ";cin>>choice;
		myBoard.setPlayerChoice(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();
		playerTurn = (playerTurn+1)%2; //changes between 0 and 1 (P1 and P2)
	}
}

I tried that code above and it keeps saying "Player 1 enter the space you wish to choose: " and it takes an invalid number such as 0 to be entered before it will say "that space is not available, please choose another one." And it doesn't ever go to Player 2.

Did you copy paste this and over write your whole main.cpp?

Did you copy paste this and over write your whole main.cpp?

yes

Post exactly what you have in main.cpp because I can run it perfectly fine.

#include<iostream>
#include"tictactoe.h"

using namespace std;

int choice;



int main(){
	TicTacToe myBoard = TicTacToe();
	int playerTurn = 0;

	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(playerTurn+1);
		cout<<"Player " << playerTurn+1 << ", enter the space you wish to choose: ";cin>>choice;
		myBoard.setPlayerChoice(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();
			playerTurn = (playerTurn+1)%2; //changes between 0 and 1 (P1 and P2)

			}
	}
}

I didn't toggle plain text when copying the code so I had to remove all the # symbols, which appeared to have cause something to be erased, now I did that and it works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.