c++ tictactoe help with validating and ending

May 21, 2017 at 8:59am
I'm struggling with validating the move so you can't put in the same move that's already put in. Also struggling with ending the loop when the game is over. I got everything else working please help me!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// main.cpp : Defines the entry point for the console application.
//


#include "TicTacToe.h"
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	TicTacToe TicTacToe;
	while (1)
	{
		TicTacToe.main();
	}
	system("PAUSE");
    return 0;
}

Last edited on May 22, 2017 at 8:07am
May 21, 2017 at 2:41pm
Hello yoyotheyogurt,

To start with I added a get function to the class. Then in main I changed the condition of the while loop to use this get function to check if the game is over. With a condition of (1) this is an endless loop with no way out.

In the class I initialized the last three private variables.

In the class .cpp file I commented out lines 8 and 9 along with changing line 236 to while (!gameOver);. I also commented out lines 151 - 154 of the "win" function because this code does nothing.

The program runs better except the last choice entered does not display before it tells you that someone wins. Also the line 231 in function "TicTacToe::main()" could be better written to show which player is making the choice. And in "TicTacToe::main()" move function "win" down to be the last function called and your board will display properly.

In the function "TicTacToe::view()" you might want to put a space between each variable to make the display easier to read.

Hope that helps,

Andy
May 21, 2017 at 3:27pm
Hello yoyotheyogurt,

Upon further investigation I realized this is redundant:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void TicTacToe::view()
{
	system("cls");
	char board[3][3] = { square1, square2, square3, square4, square5, square6, square7, square8, square9 };
	board[0][0] = square1;
	board[0][1] = square2;
	board[0][2] = square3;
	board[1][0] = square4;
	board[1][1] = square5;
	board[1][2] = square6;
	board[2][0] = square7;
	board[2][1] = square8;
	board[2][2] = square9;

	cout << square1 << " " << square2 << " " << square3 << endl
                <<  square4 << " " << square5 << " " << square6 << endl
		<< square7 << " " << square8 << " " << square9 << endl;
}


In line 4 you define and initialize the array which makes lines 5 - 13 redundant.

Notice how I added the spaces to the cout at the end.

Hope that helps,

Andy
May 22, 2017 at 1:29am
sorry i'm rather new to using classes I don't fully know how to go about using the getfunction in a while loop any more tip is very appreciated here's what i have so far.
it says i cant' do !tictactoe.gameOver for obvious reason, i'm still learning about classes.

1
2
3
4
5
6
7
8
9
10

	char TicTacToe::getGameOver()
	{
		char choice = ' ';
		gameOver = false;
		return gameOver;
		end = false;
		return end;
		return 0;
	}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "TicTacToe.h"
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	TicTacToe TicTacToe;
	do
	{
		TicTacToe.main();
	} while (!(TicTacToe.getGameOver));

	system("PAUSE");
    return 0;
}
Last edited on May 22, 2017 at 8:41am
May 22, 2017 at 2:07am
ok so i updated the getgameover function to bool but I still dont know how to initilize it
Last edited on May 22, 2017 at 8:20am
May 22, 2017 at 3:35am
Hello yoyotheyogurt,

"gameOver" is private member variable of the class, so only a public member function of the class can access the private member variables and functions of the class.

What is generally refereed to as a getter function would be:

1
2
3
4
bool TicTacToe::GetGameOver()
{
    return gameOver;
}


And in the public section of the class put bool GetGameOver();.

In main the while loop condition would be while (!ticTacToe.GetGameOver())

And just to mention the opposite of get is set, along the lines of:

1
2
3
4
void TicTacToe::SetGameOver()
{
    gameOver = false;  // or true
}


Hope that helps,

Andy
Topic archived. No new replies allowed.