Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

I need help with my tic tac toe game (c++)

Recommended Posts

I''ve been working on making Tic Tac Toe for the past two days now on and off a lot. Here''s what I''ve got so far:
#include <iostream>
#include <windows.h>

char Board[] = 
{ ''1'', ''|'', ''2'', ''|'', ''3'', ''\n'',
	''-'', ''-'', ''-'', ''-'', ''-'', ''\n'',
	''4'', ''|'', ''5'', ''|'', ''6'', ''\n'',
  ''-'', ''-'', ''-'', ''-'', ''-'', ''\n'',
  ''7'', ''|'', ''8'', ''|'', ''9'', ''\n'',
		                       ''\n'',
};

void DrawBoard();
void DrawBoard()
{
	for (int i = 0; i < 30; i++)
		{
		std::cout << Board[i];
		}
}

void Move();
void Move()
{
	int i;
	std::cin >> i;
	if (i = 1)
		{
		if (Board[0] == ''X'' || Board[0] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[1] == ''X'' || Board[1] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[2] == ''X'' || Board[2] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[3] == ''X'' || Board[3] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[4] == ''X'' || Board[4] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[5] == ''X'' || Board[5] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[6] == ''X'' || Board[6] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[7] == ''X'' || Board[7] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		if (Board[8] == ''X'' || Board[8] == ''O'')
			{
				std::cout << "Invalid move!" << std::endl;
				Move();
			}
		else
			{
			Board[0] = ''X'';
			std::cout << "\n" << std::endl;
			system("cls");
			DrawBoard();
			}
		}
}

int main()
{
	DrawBoard();
	Move();
	return 0;
}
Now, in the void Move() function, it checks to see if the space is already taken with about 9 if statements. I tried making a void Check() function with all those if statements in one single function so all I have to do is type one line to check if it''s a valid move or not. How can I make it so that I don''t have to copy those lines over and over again? BYW: I know I have some major errors in the source but this is my main concern now.
R.I.P. Mark Osback Solo Pa Mi Gente

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
// This code sets up your array
bool isValid[9];
int i;
for(i=0;i<9;++i){
isValid=true;
}
// This function is for checking
bool check(int h_index){
return (isValid[h_index]);
}
// If, let''s say, the fourth square is taken, you simply do:
isValid[3]=false;

I hope this helps.

Share this post


Link to post
Share on other sites
Well first and foremost, when you''re declaring a function prototype (a function with a semi-colon after the name and no function body), you can put the definition wherever you want. However, in your situation, you''re defining the function right afterwards, so theres really no need for the function prototype (ie, the duplicates you have: void Move(void); void Move(void) { }).

Second of all, inside your Move function, you have an if statement that looks like this:
if(i = 1)

The problem is, a single operator is for assignment, an two operators are for conditions.

So, if(i = 1) really turns out to be:

i = 1
if(1 = 1)

And thus it always runs, so change that to "==". (Once you implement the CheckMove function, this won''t matter, just for future reference).

Thirdly, I recommend you change the way your board is stored. Right now, you''re storing how the board should be formatted, where as you should really do that formatting when you''re drawing the board, but not for storage.

I would recommend something like:

char cBoard[]
{
'' '', '' '', '' '',
'' '', '' '', '' '',
'' '', '' '', '' ''
}


This would make checking your board significantly easier as you can just check the exact position the player is looking. Although, you could do it this way with your current set up. I just find it much cleaner my way.


bool CheckMove(int iSpot)
{
bool bValidMove = false;
if(cBoard[iSpot] == ''X'' || cBoard[iSpot] == ''O'')
bValidMove = false;
else
bValidMove = true;

return bValidMove;
}


And now your move function would look like:

void Move(void)
{
int i;
std :: cin >> i;
if(CheckMove(i) == true)
{
// Assign Spot On The Board To Them

}
else
{
// Don''t assign spot, take another move or skip their turn or whatever you want.

}

return;
}



That should get you going creatively again. Good luck =)

Share this post


Link to post
Share on other sites