TicTacToe board wont update, prints twice

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2642 Views - Last Post: 24 February 2015 - 06:07 PM Rate Topic: -----

#16 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: TicTacToe board wont update, prints twice

Posted 24 February 2015 - 04:29 PM

Hi, in the header file board.hpp, you don't need to include the TicTacToe or iostream header files. The std namespace is not used anywhere.


#ifndef BOARD_H
#define BOARD_H

//#include "TicTacToe.hpp"
//#include <iostream>

//using namespace std;

enum State {Win, Draw, Continue};

class Board
{
  private:
    //declare the array for our board with 3 rows and 3 columns
    char playBoard[3][3];

  public:
    Board();

    bool  makeMove(int, int, char);
    State gameState();
    void  print();

};

#endif




In the board.cpp file you don't need to include the TicTacToe header file.

In the gameState function, line can win if it is all the '.' character.

State Board::gameState()

{
        
  {
    if ((playBoard[0][0] == playBoard[0][1]) && 
        (playBoard[0][1] == playBoard[0][2])) 
      return Win;




In the TicTacToe header file, you don't need the iostream header or std namespace.
#ifndef TICTACTOE_H
#define TICTACTOE_H

#include "Board.hpp"

// #include <iostream>
// using namespace std;

class TicTacToe
{
  private:
    Board newPlay;
    char  player1;

  public:
    TicTacToe(char);
    void play();
};

#endif




In TicTacToe.cpp you don't need to include Board.hpp, as it is included in the TicTacToe header file.

In the play function the setting of player2 is required only once, so does not need to be in the loop.

  if (player1 == 'x')
  {
    player2 = 'o';
  }
  else //if (player1 == 'o')
  {
    player2 = 'x';
  }




After player1 makes a move you call gameState but do not check the returned value.

You print the board before player1 moves, and before player2 moves and after player2 moves.
Was This Post Helpful? 0
  • +
  • -

#17 sulliaa8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-February 15

Re: TicTacToe board wont update, prints twice

Posted 24 February 2015 - 05:53 PM

View Postjimblumberg, on 23 February 2015 - 09:07 PM, said:

Please post the latest code, along with the include files.


Jim


Sorry for the last response, my wife was going to choke me if I worked on this any longer yesterday, I did make progress though. Here is the code now, all the functions seem to work and it output everything correctly but I think there is still a logic issue with my win scenario's in gameState. Anyway here it is.

TicTacToe files
//Header
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include "Board.hpp"
#include <iostream>

	using namespace std;

class TicTacToe

{
  		private:
			Board newPlay;
			char player1;

		public:
			void play()	;
			TicTacToe(char);
};
#endif

//.cpp
#include "TicTacToe.hpp"
#include "Board.hpp"
#include <iostream>
#include <cstdlib>

	using namespace std;

		int main()


{
	char select;

	cout << "This game will generate a tic tac toe board and allow the user ";
	cout << "\nto enter coordinates for player1 and player2 . It will continue ";
    cout << "\nwith the game until someone wins or it is a draw.." << endl;

		cout << "\n\nPlease select who player1 will be, either 'x' or 'o': ";
			cin >> select; //sets player1 for play function

			TicTacToe game(select); //creates object and sends variable to constructor

			game.play();//starts the game
			
cin.get();
cin.get();
		return 0;
}

/**********************************************************************
* int TicTacToe::TicTacToe()
*
* This is the constructor which wil take in the user inut for player1 and
* pass that to the game object.
***********************************************************************/
TicTacToe::TicTacToe(char select)
{
	player1 = select;	
}

/**********************************************************************
* void TicTacToe::play()
*
* This function will take in who is player one then loop through each
* players turn until there is a winner or a draw.
***********************************************************************/
void TicTacToe::play()
{ 
	int rowIn, columnIn;
	char player2, currentPlayer;	
	
	cout << player1 << " has chosen the begin the game. " << endl;
	do
	{
		newPlay.print();

		cout << "\nPlayer 1 please enter the coordinates for your move: ";
			cin >> rowIn >> columnIn;
			currentPlayer = player1; 
		
			while (!newPlay.makeMove(rowIn, columnIn, currentPlayer))//while loop that checks for valid input sends inputs to makeMove
				{           
				 cout << "\nInvalid entry, please enter valid coordinates";
				 cout << "\nPlease enter valid coordinates: ";
				 cin >> rowIn >> columnIn;
				}

		if (newPlay.gameState() == Win) //checks logic in gamState
			{
				cout << "\n" << currentPlayer <<" has won the game!" << endl;
				return;
			}
			else if (newPlay.gameState() == Draw)
				{
					newPlay.print();
					cout << "\nThe game is a draw." << endl; 
					return;
				}		

			newPlay.print();//should print updated board, here it prints the double board
	
	     if (player1 == 'x')//if else to change players
			 {
				 player2 = 'o';
			 }
			else if (player1 == 'o')
				 {
					player2 = 'x';
				 }

  cout << "\nPlayer 2, please enter the coordinates for your move: ";
          cin >> rowIn >> columnIn;

      currentPlayer = player2;//sets current player to player2 for makeMove function

					 while (!newPlay.makeMove(rowIn, columnIn, currentPlayer))
						{
							cout << "\nInvalid entry, please enter valid coordinates";
							cout << "\nPlease enter valid coordinates: ";
							cin >> rowIn >> columnIn;
						}

		if (newPlay.gameState() == Win)
			{
				cout << "\n" << currentPlayer <<" has won the game!" << endl;
				return;
			}
		else if (newPlay.gameState() == Draw)
				{
					newPlay.print();
					cout << "\nThe game is a draw." << endl; 
					return;
				}
		
	}
    while (newPlay.gameState() == Continue);//while gameState returns Continue the program continues
	   
	
	
		
}



Here is the Board files
//Header
#ifndef BOARD_H
#define BOARD_H
#include "TicTacToe.hpp"
#include <iostream>

    using namespace std;

	enum State {Win, Draw, Continue};

	class Board

{
    private:
      char playBoard[3][3];//declare the array for our board with 3 rows and 3 columns

    public:
        bool makeMove(int, int, char);
        int gameState();
        void print();
        Board();


};

#endif // BOARD_H

//.cpp
#include "TicTacToe.hpp"
#include "Board.hpp"
#include <iostream>


/**********************************************************************
* Board::Board
*
* This default constructor initializes the array to empty.
***********************************************************************/

Board::Board()
{
for (int row = 0; row < 3; row++)
	
    for (int column = 0; column < 3; column++)

		{
         playBoard[row][column] = '.';
		}
	
}


/**********************************************************************
* bool Board::makeMove
*
* This function will take as parameters two integers for coordinates and
* a character of x or o for keeping track of who's turn it is.
***********************************************************************/
bool Board::makeMove(int rowIn, int columnIn, char currentPlayer)

{
         if (playBoard[rowIn][columnIn] == '.') 
           
		 {
			 playBoard[rowIn][columnIn] = currentPlayer;
            return true;
		 }

            else                
                   return false;
}
             


/**********************************************************************
* int Board::gameState()
*
* This function will keep track of the status of the game and return a
* integer of the state of the game. It determines win, draw, or continue.
***********************************************************************/
int Board::gameState()

{

	{
		if ((playBoard[0][0] == playBoard[0][1]) && (playBoard[0][1] == playBoard[0][2]) && (playBoard [0][0] != '.'))
			return Win;
		else if ((playBoard[1][0] == playBoard[1][1]) && (playBoard[1][1] == playBoard[1][2]) && (playBoard [1][0] != '.'))
			return Win;
		else if ((playBoard[2][0] == playBoard[2][1]) && (playBoard[2][1] == playBoard[2][2]) && (playBoard [2][0] != '.'))
			return Win;
		else if ((playBoard[0][0] == playBoard[1][0]) && (playBoard[1][0] == playBoard[2][0]) && (playBoard [0][0] != '.'))
			return Win;
		else if ((playBoard[0][1] == playBoard[1][1]) && (playBoard[1][1] == playBoard[2][1]) && (playBoard [0][1] != '.'))
			return Win;
		else if ((playBoard[0][2] == playBoard[1][2]) && (playBoard[1][2] == playBoard[2][2]) && (playBoard [0][2] != '.'))
			return Win;
		else if ((playBoard[0][0] == playBoard[1][1]) && (playBoard[1][1] == playBoard[2][2]) && (playBoard [0][0] != '.'))
			return Win;
		else if ((playBoard[0][2] == playBoard[1][1]) && (playBoard[1][1] == playBoard[2][0]) && (playBoard [2][0] != '.'))
			return Win;
	}
	
	for(int row = 0 ; row < 3 ; row++)
    {
        for(int column = 0 ; column < 3 ; column++)
		{
			if (playBoard[row][column] == '.') 
			return Continue;
		}

	}	

	
	return Draw;
}

/**********************************************************************
* int Board::print()
*
* This function will print a copy of the game board to the screen after
* every turn and reflect all inputs up to that point.
***********************************************************************/
void Board::print()

{
    for(int row = 0; row < 3; row++)
	{
		for(int column = 0; column < 3; column++)
		{
            cout << playBoard[row][column] << ' ';
        }
			cout << endl;
	}
	
    
}



I know its a bit clumsy but I am happy it works.
Was This Post Helpful? 0
  • +
  • -

#18 sulliaa8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-February 15

Re: TicTacToe board wont update, prints twice

Posted 24 February 2015 - 06:07 PM

View Post#define, on 24 February 2015 - 04:29 PM, said:

Hi, in the header file board.hpp, you don't need to include the TicTacToe or iostream header files. The std namespace is not used anywhere.


-Oh wow I didnt even pay attension to that thank you for pointing that out.


In the board.cpp file you don't need to include the TicTacToe header file.

In the gameState function, line can win if it is all the '.' character.

-I did figure out the part with the '.' character after testing and made correction but i think that my be the clumsy part of it.

In the TicTacToe header file, you don't need the iostream header or std namespace.



After player1 makes a move you call gameState but do not check the returned value.

-I caught that as well, the way I placed them works but it doesn't print the board after the last move for a win or draw


You print the board before player1 moves, and before player2 moves and after player2 moves.


Thanks for pointing these out, I was seeing double by the end of yesterday and had to stop. I'll make the rest of the corrections as well. This site has been a great help!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2