Advertisement

TicTacToe Help...

Started by August 29, 2002 04:42 AM
19 comments, last by FiXXoR 22 years, 3 months ago
Ok, well this is my first time post. And im getting to understand C++ a little better after some practice and i decided to try a simple TicTacToe game, but now i have some problems. I''ll ask for player1''s move, then ill draw the board, but it will assign that input to my Move[8] char variable for the board output along with assigning X to the number i selected. Ill show you the code, i''m a newb so and suggestions to what i should do to make the code more efficent would help too, thanks.
  

#include <iostream>

using namespace std;

class TicTacToe
{
public:
	TicTacToe();
	void SetVariables();
	void DrawBoard();
	void SetMove1();
	void SetMove2();
	void Player1();
	void Player2();
	void SetTurn1();
	void SetTurn2();

private:
	char Move[8];
	char Player1Move;
	char Player2Move;
	bool Player1turn;
	bool Player2turn;
};

void TicTacToe::SetVariables()
{
	Move[0] = ''1'';
	Move[1] = ''2'';
	Move[2] = ''3'';
	Move[3] = ''4'';
	Move[4] = ''5'';
	Move[5] = ''6'';
	Move[6] = ''7'';
	Move[7] = ''8'';
	Move[8] = ''9'';
	Player1turn = true;

} //End SetVariables


TicTacToe::TicTacToe()
{
} //End Constructor



void TicTacToe::DrawBoard()
{
	cout << " " << Move[0] << " | " << Move[1] << " | " << Move[2] << endl;
	cout << "---+---+---" << endl;
	cout << " " << Move[3] << " | " << Move[4] << " | " << Move[5] << endl;
	cout << "---+---+---" << endl;
	cout << " " << Move[6] << " | " << Move[7] << " | " << Move[8] << endl;

} //End DrawBoard


void TicTacToe::Player1()
{
	cout << "Player1 make a move: ";
	cin >> Player1Move;

} //End Player1


void TicTacToe::Player2()
{
	cout << "Player2 make a move: ";
	cin >> Player2Move;

} //End Player2


void TicTacToe::SetMove1()
{ 
	if (Player1turn == true)
	{

	switch (Player1Move)
	{

	case ''1'':
		Move[0] = ''X'';
		break;
	case ''2'':
		Move[1] = ''X'';
		break;
	case ''3'':
		Move[2] = ''X'';
		break;
	case ''4'':
		Move[3] = ''X'';
		break;
	case ''5'':
		Move[4] = ''X'';
		break;
	case ''6'':
		Move[5] = ''X'';
		break;
	case ''7'':
		Move[6] = ''X'';
		break;
	case ''8'':
		Move[7] = ''X'';
		break;
	case ''9'':
		Move[8] = ''X'';
		break;

	default:
		cout << "\nNot a valid move!" << endl;

	} //End Switch

	} //End if statement


} //End SetMove1


void TicTacToe::SetMove2()
{
	if (Player2turn == true)
	{

	switch (Player2Move)
	{

	case ''1'':
		Move[0] = ''O'';
		break;
	case ''2'':
		Move[1] = ''O'';
		break;
	case ''3'':
		Move[2] = ''O'';
		break;
	case ''4'':
		Move[3] = ''O'';
		break;
	case ''5'':
		Move[4] = ''O'';
		break;
	case ''6'':
		Move[5] = ''O'';
		break;
	case ''7'':
		Move[6] = ''O'';
		break;
	case ''8'':
		Move[7] = ''O'';
		break;
	case ''9'':
		Move[8] = ''O'';
		break;

	default:
		cout << "\nNot a valid move!" << endl;

	} //End Switch

	} //End if statement

}

void TicTacToe::SetTurn1()
{
	Player1turn = true;
	Player2turn = false;
} //End Setturn1


void TicTacToe::SetTurn2()
{
	Player2turn = true;
	Player1turn = false;
} //End Setturn2


int main()
{
	TicTacToe Game;

	Game.SetVariables();
	Game.DrawBoard();
	Game.Player1();
	Game.SetMove1();
	Game.SetTurn2();
	Game.DrawBoard();
	Game.Player2();
	Game.SetMove2();
	Game.SetTurn1();
	Game.DrawBoard();
	Game.Player1();
	Game.SetMove1();
	Game.SetTurn2();
	Game.DrawBoard();

	return 0;

}  //End Main


  
If you run the code and look you''ll understand better what im trying to say, heh. Thanks in advance.
That should be Move[9], as the board has nine squares, numbered from 0 to 8.

You might want to loop over the game steps like while (game not finished) { ... } instead of having a huge list of all the steps.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
I''m just a newbie myself too, but I think you don''t need to have seperate functions for the two players, you can combine them somehow so it will look a little smarter. Another thing, I think you could use the constructor for the game loop, I''m not sure about that, I haven''t studied program structure much yet. So, somebody please confirm this?
Well siaspete, numbered 0 - 8 would make 9 arrays. Isnt this is how it works? Thanks for the suggestions. Still have to find the problem though.
In your class your define Move[8]. This is an 8-element array numbered from 0 to 7, which is not what you want. Overwriting the array memory like you''re doing could cause a crash.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Looks like your not doing any error checking to see if the spot was previously taken. Though I guess that will come later, along with checking for wins. I had to write a Tic-Tac-Toe program in May for a Computer Science course, so if you need any help, just ask and I can post some code.

|.dev-c++.|.the gimp.|.seti@home.|.try2hack.|.torn.|
Advertisement
Please... at least start with replacing your Player1/2() and SetMove1/2() with a function like this:

    void TicTacToe::Play(int nPlayer){	int nMove;		// Get move	cout << "Player" << nPlayer << " make a move: ";	cin >> nMove;  	// Make it zero-based	nMove -= 1; 	// Discard invalid move (also added collision checking)	if(nMove < 0 || nMove > 8 || this.Move[nMove] != (nMove + 0x30 + 1)) {		cout << "\nNot a valid move!" << endl;		return;	} 	// Otherwise set move	this.Move[nMove] = (nPlayer == 1) ? 'X' : 'O';}    

Not tested though, but should clean up some.

EDIT: Ooopss, some errors there...

[edited by - CWizard on August 30, 2002 7:27:13 PM]
Ohh, now i see siaspete, yeah now it works now, thanks alot. Yes its very basic right now and later on ill make more enhancements to it, i just wanna get it to be a working game right now. CWizard ill try your code out, i kinda see what your doing ill work on it. Thanks to everyone who replied.
hey, you should eventually get your game to play against a user.
Ok CWizard i got your code in and working, i understand how it works and all except this part..

Move[nMove] != (nMove + 0x30 + 1

Could you take the time to explain what thats doing? I would really appreciate it. And now im starting on the check win function its all a bunch of if statements now :^\.

Anyways thanks!

This topic is closed to new replies.

Advertisement