Tic Tac Toe board not updating and prints 2 boards

Im trying to make a Tic Tac Toe board for an assignment. Right now it compiles fine but when I enter an x or o it does not update the board it just prints out the '.'s that the board is initialized to. Also, when I get to player 2 the board will printout twice so its 6 rows or 3 columns. The project uses a main to ask who starts then goes to a play function that asks for inputs and calls the makeMove function to place the x's and o's and the print function to displaay the updated board. Can anyone help me to identify where I have gone wrong in my code?

1
2
3
4
5
6
7
8
9
10
11
12
bool Board::makeMove(int rowIn, int columnIn, char currentPlayer)

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 /**********************************************************************
* 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 = 3; row < 3; row++)
	{
	for(int column = 3; column < 3; column++)
	{
            cout << playBoard[row][column] << ' ';
        }
	    cout << endl;
	}
	
    
} 


Here is the main and play function where it is called.

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
#include "TicTacToe.hpp"
#include "Board.hpp"
#include <iostream>


	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;

			TicTacToe game(select);//object declared

			game.play();//play function call
			

		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;//sets player 1 to x or o based on main
}

/**********************************************************************
* 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()
{ 
	char rowIn, columnIn, player2, currentPlayer;	
	
	cout << player1 << " has chosen the begin the game. " << endl;
	do
	{
		newPlay.print();//prints initial board normally

	cout << "\nPlease enter the coordinates for your move: ";
		cin >> rowIn >> columnIn;//take in values for row and clolumn
		currentPlayer = player1;sets current player to player1
		
	while (!newPlay.makeMove(rowIn, columnIn, currentPlayer)//incoplete loop that checks for valid input
	
        {			 
	cout << "\nInvalid entry, please enter valid coordinates";
	cout << \nPlease enter valid coordinates: "
           cin >> rowIn, columnIn, currentPlayer;
        }									
	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;
        }
				
	    newPlay.print();//should print updated board, prints normal board
	}
	while (newPlay.gameState() == Continue); 
cout << \nPlease enter valid coordinates: "
You forgot the semicolon.

currentPlayer = player1;sets current player to player1
Shouldnt the last part be a comment?

1
2
3
4
5
6
7
8
9
10
11
12
 void Board::print()

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


This function is broken. You initialize int row and int column as 3. But what you tell the function is that you should run these 2 for loops as long as row is less than 3. But row is never less than 3 because its 3 to begin with.
Last edited on
Thank you for your reply,

I do see the mistake in the code I posted. I made a mistake in copying the code over from a putty server so those were typographical errors from me transcribing.

I did fix them though but the problem is still there, the board is initialized with and prints out as
...
...
...
for player 1 and after player1 enters their coordinates the board prints as
...
...
...
...
...
...
so there is an extra board and no update to the board for an x or o. Is I don't see a problem in my makeMove function so maybe Im not calling it right or the variables Im passing are wrong. Do you see anything Im missing there?
So, now I seem to have and issue with this statement

1
2
3
4
5
6
while (!newPlay.makeMove(rowIn, columnIn, currentPlayer)//incoplete loop that checks for valid input
	
        {			 
	cout << "\nInvalid entry, please enter valid coordinates";
	cout << \nPlease enter valid coordinates: "
           cin >> rowIn, columnIn, currentPlayer; 


Every input reads as invalid. Im not sure what changed with this statement, or function,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**********************************************************************
* 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;
}


Again, is there something with this makeMove function that is making it return a false value whenever called?
cin >> rowIn, columnIn, currentPlayer; This wont work, it has to be

cin >> rowIn >> columnIn >> currentPlayer;

Do you get any errors? If so, please post those errors here.
Topic archived. No new replies allowed.