Archived

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

Sir_Spritely

Thoughts on this code and improvement ideas

Recommended Posts

Sir_Spritely    122
I am currently getting stuck into C++, I have a programming background so doing the really basic simple stuff is straight forward. For a project I have written a simple Tic Tac Toe game just using functions and basic stuff. The next phase in my evil plan is to take this code and apply more advanced techniques such as pointers etc. Although I understand the more advanced techniques I have had problems implementing them and using them in a problem. I was hoping first off I could get some feedback on the current code, such as just how dreadful is it in the harsh reality of real life? Also some vague ideas to plant a seed so I can get stuck into revamping the code using more advanced techniques. Help appreciated.
//-----------------------------------------------------------------------------------------------

// Name: Standard.ccp

//

// Desc: This is simply the main .cpp file for whatever project happens to be in here today.

//

// Copyright (c) 2003, Paul Kirkbride.

//-----------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------

// Includes

//-----------------------------------------------------------------------------------------------

#include <stdafx.h>
#include <iostream.h>
#include <stdlib.h>									// This is used for the rand() function






//-----------------------------------------------------------------------------------------------

// Name: Function Prototypes

//-----------------------------------------------------------------------------------------------

void OnePlayer();
void TwoPlayer();
void GameRules();
int RandomNUM();
int RandomNUM1();




//-----------------------------------------------------------------------------------------------

// Name: main()

// Desc: This is the main entry function for the entire program.

//-----------------------------------------------------------------------------------------------

int main()
{
	int menuChoice = 0;

	while (menuChoice != 1 || 2 || 3)
	{
		cout << "\n\n-------------------------------------------------------------" <<endl;
		cout << "     T I C  T A C  T O E V0.1 (c) Paul Kirkbride 2003        " <<endl;
		cout << "-------------------------------------------------------------" <<endl;
		cout << "                       G A M E  M E N U                      " <<endl;
		cout << "-------------------------------------------------------------" <<endl;
		cout << "1. Play a one player game with computer as opponent          " <<endl;
		cout << "2. Play a two player game against another human player       " <<endl;
		cout << "3. Read the rules of the game                                " <<endl;
		cout << "4. Exit the game and return to windows                       " <<endl;
		cout << "Please enter your choice: ";
		cin >> menuChoice;

		switch ( menuChoice )
		{
			case 1: OnePlayer();
				break;
			case 2: TwoPlayer();
				break;
			case 3: GameRules();
				break;
			case 4: 
				return 0;
		}
	}
	
//	return 0;

}





//-----------------------------------------------------------------------------------------------

// Name: OnePlayer()

// Desc: This is the OnePlayer function which implements a one player version of Tic Tac Toe.

//-----------------------------------------------------------------------------------------------

void OnePlayer()
{
	int userX, userY;
	bool EndGame = false;
	char userName[10];

	// Player one name entry

	cout << "\n\nPlease enter your first name: ";
	cin >> userName;
	cout << "\n\nWell, " << userName << " you will be playing the computer!";

	// Create and initiliase the tic tac toe game board

	int X, Y;
	char GameBoard [3] [3];

	for ( X = 0; X < 3; X++ )
	{
		for ( Y = 0; Y < 3; Y++ )
		{
			GameBoard[X] [Y] = ''*'';
		}
	}
	
	// Decide who starts the game with RandomNUM() which will generate a random number.  Set the

	// Counter variable to zero.

	int Starts = 0, Counter = 0;
	Starts = RandomNUM();
	
	if ( Starts == 0 )
	{
		cout << "\n\nThe computer will be the first to play!";
		cout << "\n\nThe computer chooses to be 0''s\n\n";

		// Set up a loop for optimisation reasons

		while ( ( Counter < 9 ) && ( EndGame != true ) )
		{
			// AI check to determine where the computer player will put his marker.  Note because

			// the function is only based on if the computer goes first the mark will be a zero

			// also as the computer is starting it will just find a random position on the board.


			// Start of computer AI to see if the computer has a chance of winning



			// Top or first row of game grid AI

			if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[0] [2] != ''X'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''0'' ) && ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[0] [0] != ''X'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[0] [2] == ''0'' ) && ( GameBoard[0] [1] != ''X'' ) ) )
			{
				GameBoard[0] [1] = ''0'';
				Counter++;
			}

			
			// Middle or second row of game grid AI

			else if ( ( ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[1] [2] != ''X'' ) ) )
			{
				GameBoard[1] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [2] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[1] [0] != ''X'' ) ) )
			{
				GameBoard[1] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[1] [2] == ''0'' ) && ( GameBoard[1] [1] != ''X'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}

			
			// Bottom or third row of game grid AI

			else if ( ( ( GameBoard[2] [0] == ''0'' ) && ( GameBoard[2] [1] == ''0'' ) && ( GameBoard[2] [2] != ''X'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[2] [1] == ''0'' ) && ( GameBoard[2] [0] != ''X'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[2] [1] != ''X'' ) ) )
			{
				GameBoard[2] [1] = ''0'';
				Counter++;
			}

			
			// First column of game grid AI

			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[2] [0] != ''X'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''0'' ) && ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[0] [0] != ''X'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[2] [0] == ''0'' ) && ( GameBoard[1] [0] != ''X'' ) ) )
			{
				GameBoard[1] [0] = ''0'';
				Counter++;
			}

			
			// Second or middle column of game grid AI

			else if ( ( ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[2] [1] != ''X'' ) ) )
			{
				GameBoard[2] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [1] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[0] [1] != ''X'' ) ) )
			{
				GameBoard[0] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[2] [1] == ''0'' ) && ( GameBoard[1] [1] != ''X'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}

			
			// Third column of the game grid AI

			else if ( ( ( GameBoard[0] [2] == ''0'' ) && ( GameBoard[1] [2] == ''0'' ) && ( GameBoard[2] [2] != ''X'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[1] [2] == ''0'' ) && ( GameBoard[0] [2] != ''X'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[1] [2] != ''X'' ) ) )
			{
				GameBoard[1] [2] = ''0'';
				Counter++;
			}

			
			// Diaganol top to bottom of the game grid AI

			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[2] [2] != ''X'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[0] [0] != ''X'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) && ( GameBoard[1] [1] != ''X'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}


			
			// Check to see if the computer need to stop the user from winning



			// Top or first row of game grid AI

			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[0] [1] == ''*'' ) ) )
			{
				GameBoard[0] [1] = ''0'';
				Counter++;
			}

			
			// Middle or second row of game grid AI

			else if ( ( ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[1] [2] == ''*'' ) ) )
			{
				GameBoard[1] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [2] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[1] [0] == ''*'' ) ) )
			{
				GameBoard[1] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[1] [2] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}

			
			// Bottom or third row of game grid AI

			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[2] [1] == ''X'' ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[2] [1] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[2] [1] == ''*'' ) ) )
			{
				GameBoard[2] [1] = ''0'';
				Counter++;
			}

			
			// First column of game grid AI

			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[1] [0] == ''*'' ) ) )
			{
				GameBoard[1] [0] = ''0'';
				Counter++;
			}

			
			// Second or middle column of game grid AI

			else if ( ( ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [1] == ''*'' ) ) )
			{
				GameBoard[2] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [1] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[0] [1] == ''*'' ) ) )
			{
				GameBoard[0] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[2] [1] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}

			
			// Third column of the game grid AI

			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[1] [2] == ''X'' ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[1] [2] == ''X'' ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[1] [2] == ''*'' ) ) )
			{
				GameBoard[1] [2] = ''0'';
				Counter++;
			}

			
			// Diaganol top to bottom of the game grid AI

			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == ''X'' ) && ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = ''0'';
				Counter++;
			}
			
			// If the computer does not need to place a marker to win the game or place a marker

			// to stop the computer from winning then choose a random location on the board.

			else
			{
				int randX = 0, randY = 0;
				do
				{
					randX = RandomNUM1();
					randY = RandomNUM1();
				}
				while ( GameBoard[randX] [randY] != ''*'' );

				GameBoard[randX] [randY] = ''0'';
				Counter++;
			}

			//Check to see if the computer has just secured victory for himself!

			
			if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[0] [2] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[1] [2] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == ''0'' ) && ( GameBoard[2] [1] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[1] [0] == ''0'' ) && ( GameBoard[2] [0] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[2] [1] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == ''0'' ) && ( GameBoard[1] [2] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == ''0'' ) && ( GameBoard[1] [1] == ''0'' ) && ( GameBoard[2] [2] == ''0'' ) ) )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [2] == ''0'' && GameBoard[1] [1] == ''0'' && GameBoard[2] [0] == ''0'' )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing

			}


			// As the game starts with the computer if it does go to a stalemate then it finishes 

			// with the computer which causes a problem here as the user attempts to write past 

			// the end of the array.  This check displays the computers final move on the  

			//  gameboard and then quits and returns back to the menu.

			if ( Counter == 9 )
			{
				// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}

				break;
			}
			else
			{
				// Do nothing

			}



			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}



			// As the computer has gone first if he has won in the above code then for the loop

			// to tell he has won it must go through the rest of the code.  I will do a check to

			// see if Endgame is true here so we can exit without going through the rest of the 

			// code if the computer has won and Endgame equals true.

			if ( EndGame == true )
			{
				break;
			}
			else
			{
				// Do nothing

			}



			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName << " please enter your choice for X value: ";
				cin >> userX;
				if ( userX < 0 || userX > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( userX < 0 || userX > 2 );
				
			
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> userY;
				if ( userY < 0 || userY > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( userY < 0 || userY > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[userX] [userY] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[userX] [userY] = ''X''; // X not being a variable but the users marker

				Counter++;
			}

			//Check to see if the user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[0] [2] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[1] [2] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == ''X'' ) && ( GameBoard[2] [1] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[1] [0] == ''X'' ) && ( GameBoard[2] [0] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [1] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[1] [2] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [2] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == ''X'' ) && ( GameBoard[1] [1] == ''X'' ) && ( GameBoard[2] [0] == ''X'' ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  

			}
						
		}
	}


	// If the random generator decides the user should start then...

	if ( Starts == 1 )
	{
		char userMark;
		char comMark;

		// User decides what marker to be

		cout << "\n\nYou, " << userName << " will be the first to go!\n\n";
		while ( userMark != ''0'' && userMark != ''X'' && userMark != ''x'' )
		{
			cout << "Please choose your preferred mark a 0 or an X:";
			cin >> userMark;
		}

		// If the user entered a lower case ''x'' then we will change it to upper case ''X''

		if ( userMark == ''x'' )
			userMark = ''X'';
		else
		{
			// Do nothing

		}
				
		// Set up a loop for optimisation reasons

		while ( ( Counter < 9 ) && ( EndGame != true ) )
		{
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName << " please enter your choice for X value: ";
				cin >> userX;
				if ( userX < 0 || userX > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( userX < 0 || userX > 2 );
				
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> userY;
				if ( userY < 0 || userY > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( userY < 0 || userY > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[userX] [userY] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[userX] [userY] = userMark;
				Counter++;
			}

			//Check to see if the user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[0] [1] == userMark ) && ( GameBoard[0] [2] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[1] [2] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[2] [1] == userMark ) && ( GameBoard[2] [2] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[1] [0] == userMark ) && ( GameBoard[2] [0] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [1] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[1] [2] == userMark ) && ( GameBoard[2] [2] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [2] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [0] == userMark ) ) )
			{
				cout << "\n\nCongratulations, " << userName << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  This is here to seperate the 

				// IF and ELSE If statements from this section with the next section

			}

			
			// Quick test to determine what marker the computers uses

			if ( userMark == ''X'' )
			{
				comMark = ''0'';
			}
			else
			{
				comMark = ''X'';
			}

			
			// As the game starts with te user if it does go to a stalemate then it finishes with the user

			// which causes a problem here as the computer AI attempts to write past the end of

			// the array.  This check displays the users final move on the gameboard and then quits

			// and returns back to the menu.

			if ( Counter == 9 )
			{
				// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}

				break;
			}
			else
			{
				// Do nothing

			}



			// AI check to determine where the computer player will put his marker.  


			// Start of computer AI to see if the computer has a chance of winning


			// Top or first row of game grid AI

			if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[0] [1] == comMark ) && ( GameBoard[0] [2] != userMark ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == comMark ) && ( GameBoard[0] [1] == comMark ) && ( GameBoard[0] [0] != userMark ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[0] [2] == comMark ) && ( GameBoard[0] [1] != userMark ) ) )
			{
				GameBoard[0] [1] = comMark;
				Counter++;
			}
			
			
			// Middle or second row of game grid AI

			else if ( ( ( GameBoard[1] [0] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[1] [2] != userMark ) ) )
			{
				GameBoard[1] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [2] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[1] [0] != userMark ) ) )
			{
				GameBoard[1] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [0] == comMark ) && ( GameBoard[1] [2] == comMark ) && ( GameBoard[1] [1] != userMark ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			
			
			// Bottom or third row of game grid AI

			else if ( ( ( GameBoard[2] [0] == comMark ) && ( GameBoard[2] [1] == comMark ) && ( GameBoard[2] [2] != userMark ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == comMark ) && ( GameBoard[2] [1] == comMark ) && ( GameBoard[2] [0] != userMark ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == comMark ) && ( GameBoard[2] [2] == comMark ) && ( GameBoard[2] [1] != userMark ) ) )
			{
				GameBoard[2] [1] = comMark;
				Counter++;
			}
			
			
			// First column of game grid AI

			else if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[1] [0] == comMark ) && ( GameBoard[2] [0] != userMark ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == comMark ) && ( GameBoard[1] [0] == comMark ) && ( GameBoard[0] [0] != userMark ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[2] [0] == comMark ) && ( GameBoard[1] [0] != userMark ) ) )
			{
				GameBoard[1] [0] = comMark;
				Counter++;
			}
			
			
			// Second or middle column of game grid AI

			else if ( ( ( GameBoard[0] [1] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[2] [1] != userMark ) ) )
			{
				GameBoard[2] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [1] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[0] [1] != userMark ) ) )
			{
				GameBoard[0] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [1] == comMark ) && ( GameBoard[2] [1] == comMark ) && ( GameBoard[1] [1] != userMark ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			
			
			// Third column of the game grid AI

			else if ( ( ( GameBoard[0] [2] == comMark ) && ( GameBoard[1] [2] == comMark ) && ( GameBoard[2] [2] != userMark ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == comMark ) && ( GameBoard[1] [2] == comMark ) && ( GameBoard[0] [2] != userMark ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == comMark ) && ( GameBoard[2] [2] == comMark ) && ( GameBoard[1] [2] != userMark ) ) )
			{
				GameBoard[1] [2] = comMark;
				Counter++;
			}
			
			
			// Diaganol top to bottom of the game grid AI

			else if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[2] [2] != userMark ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == comMark ) && ( GameBoard[1] [1] == comMark ) && ( GameBoard[0] [0] != userMark ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == comMark ) && ( GameBoard[2] [2] == comMark ) && ( GameBoard[1] [1] != userMark ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			
			
			
			// Check to see if the computer needs to stop the player from winning


			// Top or first row of game grid AI

		    else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[0] [1] == userMark ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[0] [1] == userMark ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[0] [2] == userMark ) && ( GameBoard[0] [1] == ''*'' ) ) )
			{
				GameBoard[0] [1] = comMark;
				Counter++;
			}
			
			
			// Middle or second row of game grid AI

			else if ( ( ( GameBoard[1] [0] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[1] [2] == ''*'' ) ) )
			{
				GameBoard[1] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [2] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[1] [0] == ''*'' ) ) )
			{
				GameBoard[1] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [0] == userMark ) && ( GameBoard[1] [2] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			
			
			// Bottom or third row of game grid AI

			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[2] [1] == userMark ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == userMark ) && ( GameBoard[2] [1] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[2] [2] == userMark ) && ( GameBoard[2] [1] == ''*'' ) ) )
			{
				GameBoard[2] [1] = comMark;
				Counter++;
			}
			
			
			// First column of game grid AI

			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[1] [0] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[1] [0] == userMark ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[2] [0] == userMark ) && ( GameBoard[1] [0] == ''*'' ) ) )
			{
				GameBoard[1] [0] = comMark;
				Counter++;
			}
			
			
			// Second or middle column of game grid AI

			else if ( ( ( GameBoard[0] [1] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [1] == ''*'' ) ) )
			{
				GameBoard[2] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [1] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [1] == ''*'' ) ) )
			{
				GameBoard[0] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [1] == userMark ) && ( GameBoard[2] [1] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			
			
			// Third column of the game grid AI

			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[1] [2] == userMark ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == userMark ) && ( GameBoard[1] [2] == userMark ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[2] [2] == userMark ) && ( GameBoard[1] [2] == ''*'' ) ) )
			{
				GameBoard[1] [2] = comMark;
				Counter++;
			}
			
			
			// Diaganol top to bottom of the game grid AI

			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [0] == userMark ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [0] == userMark ) && ( GameBoard[2] [2] == ''*'' ) ) )
			{
				GameBoard[2] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [2] == userMark ) && ( GameBoard[0] [0] == ''*'' ) ) )
			{
				GameBoard[0] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[2] [2] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [2] == userMark ) && ( GameBoard[0] [0] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [2] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[0] [2] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[2] [0] == userMark ) && ( GameBoard[1] [1] == ''*'' ) ) )
			{
				GameBoard[1] [1] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[1] [1] == userMark ) && ( GameBoard[2] [0] == userMark ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[2] [0] == userMark ) && ( GameBoard[1] [1] == userMark ) && ( GameBoard[0] [2] == ''*'' ) ) )
			{
				GameBoard[0] [2] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [0] == userMark ) && ( GameBoard[0] [2] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			else if ( ( ( GameBoard[0] [2] == userMark ) && ( GameBoard[0] [0] == userMark ) && ( GameBoard[2] [0] == ''*'' ) ) )
			{
				GameBoard[2] [0] = comMark;
				Counter++;
			}
			
			// If the computer does not need to place a marker to win the game or to stop the 

			// user from winning the game then choose a random location.

			else 
			{
				int randX = 0, randY = 0;
				do
				{
					randX = RandomNUM1();
					randY = RandomNUM1();
				}
				while ( GameBoard[randX] [randY] != ''*'' );

				GameBoard[randX] [randY] = comMark;
				Counter++;
			}

						
			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}


			//Check to see if the computer has just secured victory for himself!

			
			if ( GameBoard[0] [0] == comMark && GameBoard[0] [1] == comMark && GameBoard[0] [2] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[1] [0] == comMark && GameBoard[1] [1] == comMark && GameBoard[1] [2] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[2] [0] == comMark && GameBoard[2] [1] == comMark && GameBoard[2] [2] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [0] == comMark && GameBoard[1] [0] == comMark && GameBoard[2] [0] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [1] == comMark && GameBoard[1] [1] == comMark && GameBoard[2] [1] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [2] == comMark && GameBoard[1] [2] == comMark && GameBoard[2] [2] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [0] == comMark && GameBoard[1] [1] == comMark && GameBoard[2] [2] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
			else if ( GameBoard[0] [2] == comMark && GameBoard[1] [1] == comMark && GameBoard[2] [0] == comMark )
			{
				cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";
				EndGame = true;
			}
						

		} // Closing bracket while ( Counter < 9 || EndGame != true )

		
	}
}





//-----------------------------------------------------------------------------------------------

// Name: TwoPlayer()

// Desc: This is the TwoPlayer function which implements a two player version of Tic Tac Toe.

//-----------------------------------------------------------------------------------------------

void TwoPlayer()
{
	char userName1[10];
	char userName2[10];
	int user1X, user1Y, user2X, user2Y;
	char userMark1, userMark2;
	bool EndGame = false;


	// Player one name entry

	cout << "\n\nPlayer One, please enter your first name: ";
	cin >> userName1;

	// Player two name entry

	cout << "\n\nPlayer Two, please enter your first name: ";
	cin >> userName2;
	cout << "\n\nWell, " << userName1 << " you will be playing " << userName2;

	// Create and initiliase the tic tac toe game board

	int X, Y;
	char GameBoard [3] [3];

	for ( X = 0; X < 3; X++ )
	{
		for ( Y = 0; Y < 3; Y++ )
		{
			GameBoard[X] [Y] = ''*'';
		}
	}
	
	// Decide who starts the game with RandomNUM() which will generate a random number.  Set the

	// Counter variable to zero.

	int Starts = 0, Counter = 0;
	Starts = RandomNUM();
	
	if ( Starts == 0 )
	{
		// User1 decides what marker to be

		cout << "\n\nYou, " << userName1 << " will be the first to go!\n\n";
		while ( ( ( userMark1 != ''0'' ) && ( userMark1 != ''X'' ) && ( userMark1 != ''x'' ) ) )
		{
			cout << "Please choose your preferred mark a 0 or an X:";
			cin >> userMark1;
		}

		// If the user entered a lower case ''x'' then we will change it to upper case ''X''

		if ( userMark1 == ''x'' )
			userMark1 = ''X'';

		// Assign other user a marker based on the choosers decision

		if ( userMark1 == ''X'' )
		{
			userMark2 = ''0'';
		}
		else
		{
			userMark2 = ''X'';
		}



		// Set up a loop for optimisation reasons

		while ( ( Counter < 9 ) && ( EndGame != true ) )
		{
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName1 << " please enter your choice for X value: ";
				cin >> user1X;
				if ( user1X < 0 || user1X > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user1X < 0 || user1X > 2 );
				
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> user1Y;
				if ( user1Y < 0 || user1Y > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user1Y < 0 || user1Y > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[user1X] [user1Y] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[user1X] [user1Y] = userMark1;
				Counter++;
			}

			//Check to see if the starting user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[0] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  This is here to seperate the 

				// IF and ELSE If statements from this section with the next section

			}



			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}



			// Check to see if the last position has been filled by the starting user because

			// the game will also end with him and we need to skip this section.

			if ( Counter == 9 )
			{
				break;
			}
			else
			{
				// Do nothing

			}



			// Get the second users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName2 << " please enter your choice for X value: ";
				cin >> user2X;
				if ( user2X < 0 || user2X > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user2X < 0 || user2X > 2 );
				
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> user2Y;
				if ( user2Y < 0 || user2Y > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user2Y < 0 || user2Y > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[user2X] [user2Y] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[user2X] [user2Y] = userMark2;
				Counter++;
			}

			//Check to see if the starting user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[0] [1] == userMark2 ) && ( GameBoard[0] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[1] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == userMark2 ) && ( GameBoard[2] [1] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[1] [0] == userMark2 ) && ( GameBoard[2] [0] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [1] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark2 ) && ( GameBoard[1] [2] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [0] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  This is here to seperate the 

				// IF and ELSE If statements from this section with the next section

			}



			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}
		} // End While

	} // End if




	if ( Starts == 1 )
	{
		// User1 decides what marker to be

		cout << "\n\nYou, " << userName2 << " will be the first to go!\n\n";
		while ( ( ( userMark2 != ''0'' ) && ( userMark2 != ''X'' ) && ( userMark2 != ''x'' ) ) )
		{
			cout << "Please choose your preferred mark a 0 or an X:";
			cin >> userMark2;
		}

		// If the user entered a lower case ''x'' then we will change it to upper case ''X''

		if ( userMark2 == ''x'' )
			userMark2 = ''X'';

		// Assign other user a marker based on the choosers decision

		if ( userMark2 == ''X'' )
		{
			userMark1 = ''0'';
		}
		else
		{
			userMark1 = ''X'';
		}



		// Set up a loop for optimisation reasons

		while ( ( Counter < 9 ) && ( EndGame != true ) )
		{
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName2 << " please enter your choice for X value: ";
				cin >> user2X;
				if ( user2X < 0 || user2X > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user2X < 0 || user2X > 2 );
				
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> user2Y;
				if ( user2Y < 0 || user2Y > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user2Y < 0 || user2Y > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[user2X] [user2Y] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[user2X] [user2Y] = userMark2;
				Counter++;
			}

			//Check to see if the starting user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[0] [1] == userMark2 ) && ( GameBoard[0] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[1] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == userMark2 ) && ( GameBoard[2] [1] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[1] [0] == userMark2 ) && ( GameBoard[2] [0] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [1] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark2 ) && ( GameBoard[1] [2] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [2] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark2 ) && ( GameBoard[1] [1] == userMark2 ) && ( GameBoard[2] [0] == userMark2 ) ) )
			{
				cout << "\n\nCongratulations, " << userName2 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  This is here to seperate the 

				// IF and ELSE If statements from this section with the next section

			}



			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}



			// Check to see if the last position has been filled by the starting user because

			// the game will also end with him and we need to skip this section.

			if ( Counter == 9 )
			{
				break;
			}
			else
			{
				// Do nothing

			}



			// Get the second users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nOkay, " << userName1 << " please enter your choice for X value: ";
				cin >> user1X;
				if ( user1X < 0 || user1X > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user1X < 0 || user1X > 2 );
				
			// Get the users choice in the form of X and Y coordinates

			do
			{
				cout << "\n\nPlease enter your choice for Y: ";
				cin >> user1Y;
				if ( user1Y < 0 || user1Y > 2 )
				{
					cout << "\nYou must enter a coordinate between 0 and 2!\n\n";
				}
			}
			while ( user1Y < 0 || user1Y > 2 );

			// Check to see if location available, if not then error	

			if ( GameBoard[user1X] [user1Y] != ''*'' )
			{
				cout << "\n\nThat location has already been choosen, please select another!\n\n";
			}
			else
			{
				// Place the users position on the game board, in the array.

				GameBoard[user1X] [user1Y] = userMark1;
				Counter++;
			}

			//Check to see if the starting user has just secured victory for himself!

			if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[0] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[2] [0] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
			{
				cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
				EndGame = true;
			}
			else
			{
				// Do nothing just carry onto the next statement.  This is here to seperate the 

				// IF and ELSE If statements from this section with the next section

			}



			// Display the game board

			for ( X = 0; X < 3; X++ )
			{
				cout << endl;
				for ( Y = 0; Y < 3; Y++ )
				{
					cout << GameBoard[X] [Y];
				}
			}
		} // End While

	} // End if

}		
			
			





//-----------------------------------------------------------------------------------------------

// Name: GameRules()

// Desc: This is the GameRules function which displays the rules of Tic Tac Toe.

//-----------------------------------------------------------------------------------------------

void GameRules()
{
	cout << "\n\nT I C  T A C  T O E V0.1  G A M E  R U L E S" <<endl;
	cout << "--------------------------------------------" <<endl;
	cout << "There are two sections of this game, giving the player the option of" <<endl;
	cout << "playing this game on their own against the computer or against" <<endl;
	cout << "another human opponent.  There is currently no scoring functionality" <<endl;
	cout << "but that might be implemented at a later date.  For those not familiar" <<endl;
	cout << "with Tic Tac Toe or Noughts and Crosses as it is known in various" <<endl;
	cout << "countries these simple rules will teach you this less than difficult game.\n\n" <<endl;
	cout << "The game will always decide who gets to put their marker on the" <<endl;
	cout << "gameboard first. This is done in a random fashion, the game grid" <<endl;
	cout << "is a simple grid 3 * 3 with the total number of nine slots.  Each" <<endl;
	cout << "play a marker is placed on the grid alternatately between players." <<endl;
	cout << "This is down until either the grid is full or a player has produced" <<endl;
	cout << "a winning line.  To win the game a player must have three of his" <<endl;
	cout << "markers in either a row, column or diaganol line.  Upon either the grid" <<endl;
	cout << "becoming full or a player winning the game will return to the menu screen.\n\n" <<endl;
	cout << "For further updates of this game including a DirectX version please contact me.\n\n";
}





//-----------------------------------------------------------------------------------------------

// Name: RandomNUM()

// Desc: This is the function which generates a random number to decide who plays off first.

//-----------------------------------------------------------------------------------------------

int RandomNUM()
{
	return rand() % 2;
}





//-----------------------------------------------------------------------------------------------

// Name: RandomNUM1()

// Desc: This is the function which generates a random number to decide where the computer

//       puts his marker if he cannot win or he has no need to stop the user from winning.

//-----------------------------------------------------------------------------------------------

int RandomNUM1()
{
	return rand() % 3;
}

Share this post


Link to post
Share on other sites
Bagpuss    122
OK, first of all, I don''t mean to be harsh, but I''ll be surprised if you get many takers, for several reasons.

Coding styles are nearly unique, what one person likes doing, another doesn''t. (Such as use streams, or old C IO routines, using std::string or char[] etc.)

You just dumped your entire code up there, a lot of people in here are busy, and will either get bored, or not even bother to read it. It''s one thing to look at 20 lines of code with a problem, another one entirely to look at masses of code, and have someone say "What can I do to imrove this ?" (The only reason I am replying is that I have asked questions before, that when I look back now were not ''the best questions'' I could hav asked, and so have been ignored or recieved little help. But at the same time, I understand how frustrating it is to get no feedback atall when you ask a question)

What I will say from scanning your code is that you can reduce the code base by looking at it, and moving any duplicated code into a function, or calling it once when the program starts.

2 quick example here is the Player 1 Name input routine, ok, it''s only a few lines, but I assume that you will always have at least 1 player. Maybe you could make a function that takes an integer that indicates the number of players and 2 char arrays (or strings) and call that before you call the function to play one or 2 player.

Also, you have 2 identical pieces of code that initialise the Game board, maybe that could go into the same function (or a seperate one if you want).

I am not saying whether these are necessarily good or bad changes, but redundant code, and duplicated code can in the long term be a real pain in the ass, to test, maintain, and even remember why it''s still there, bloating your code.

Bp

Share this post


Link to post
Share on other sites
rypyr    252
Here''s some style thingies:

1)
int X, Y;
for ( X = 0; X < 3; X++ ) {
for ( Y = 0; ...)

a) Get into the habit of declaring variables one per line (just a style thing)

b) You are not initializing X or Y to anything, which is bad practice. Furthermore, since you''re not using X or Y outside the for loop, make it so:

for ( int x = 0; x < 3; ++x )

c) I have got into the habit of using the pre-increment operator as my preference (since they are more efficient for things like iterators). Using post-increment operator on an int is equally as optimal, but if you use pre-increment for ints you will get used to it for iterators.

2)
cout << "\n\nThe computer has just won the game, you pathetic loser!\n\n";

Typically you use endl to indicate the end of a line:

cout << endl << endl << "The computer ..." << endl << endl;

3)
int RandomNUM()
int RandomNUM1()

Why make this two functions? Why not make it one function and pass in a parameter that determines the random number range?

4)
char GameBoard [3] [3];

Someone else commented that you declare this twice and that you should initialize this in a common place. I agree with this an I would further suggest that you make this a class:

class GameBoard {
private:
char board[3][3];
public:
GameBoard() {...}
~GameBoard() {...}

char getSquare(int row, int col) {...}
bool isWon() {
// loop through and determine if any row, column
// or diagonal is set with all one symbol
}
};

5) PlayerOne() and PlayerTwo() are basically the entire game, I''m sure you''ve duplicated a lot of code between the two. Conceptually you have a Player, which can be a Human or a Computer Player. They will each have methods they implement differently:

class IPlayer {
public:
virtual getMove(GameBoard& board) = 0;
};

class HumanPlayer : public IPlayer {
// implement getMove() here...
};
class ComputerPlayer : public IPlayer {
// implement getMove() here...
};

Just some thoughts...

Regards,
Jeff

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You have lots of code that looks like this:

//Check to see if the starting user has just secured victory for himself!

if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[0] [2] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[2] [0] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else if ( ( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}
else
{
// Do nothing just carry onto the next statement. This is here to seperate the

// IF and ELSE If statements from this section with the next section

}


It could be written a lot more compactly like this:

//Check to see if the starting user has just secured victory for himself!

if ( ( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[0] [2] == userMark1 ) ) ||
( ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) ) ||
( ( GameBoard[2] [0] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) ||
( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [0] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) ||
( ( GameBoard[0] [1] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [1] == userMark1 ) ) ||
( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [2] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) ||
( ( GameBoard[0] [0] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [2] == userMark1 ) ) ||
( ( GameBoard[0] [2] == userMark1 ) && ( GameBoard[1] [1] == userMark1 ) && ( GameBoard[2] [0] == userMark1 ) ) )
{
cout << "\n\nCongratulations, " << userName1 << " you have beaten the computer!\n\n";
EndGame = true;
}

Try to avoid using copy & paste feature of your editor. If you have to use it a lot, then you''re probably doing something very wrong.

Share this post


Link to post
Share on other sites
Sir_Spritely    122
Sorry for not the best question I have ever asked and thanks to everyone who took their time to give me feedback. It is really appreciated.

You have given me some useful ideas, thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I wrote a tic-tac-toe with computer opponent in Python, and tried to keep it small and tidy. Here:

import random

EMPTY = ''.''

class Board:
def __init__(self, width, height, winrow):
self._board = [[EMPTY] * height for x in range(width)]
self.width = width
self.height = height
self.winrow = winrow
self.moves = 0

def set(self, x, y, value):
self._board[x][y] = value
self.moves += 1

def get(self, x, y):
return self._board[x][y]

def show(self):
print ''+'' + ''-'' * self.width + ''+''
for y in range(self.height):
row = ''''.join([self.get(x, y) for x in range(self.width)])
print ''|'' + row + ''|''
print ''+'' + ''-'' * self.width + ''+''

def isTie(self):
return self.moves == self.width * self.height


dirs = [(1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1)]

def doesWin(board, newx, newy, p=None):
if not p:
p = board.get(newx, newy)

result = []
for dir in dirs:
x = newx + dir[0]
y = newy + dir[1]
length = 1
while 0 <= x < board.width and \
0 <= y < board.height and \
board.get(x, y) == p:
length += 1
x += dir[0]
y += dir[1]
result.append(length)

for i in range(4):
if result[i] + result[i + 4] > board.winrow:
return True
return False


def computerPlay(board, mark, markplr):
def canItWin(mark):
for x in range(board.width):
for y in range(board.height):
if board.get(x, y) == EMPTY and doesWin(board, x, y, mark):
return x, y
return None

for m in (mark, markplr):
pos = canItWin(m)
if pos:
return pos

while True:
x = random.randrange(board.width)
y = random.randrange(board.height)
if board.get(x, y) == EMPTY:
return x, y


def getInput(board):
while True:
try:
play = raw_input(''Position? '')
vals = play.split('','')
x = int(vals[0])
y = int(vals[1])
assert 0 <= x < board.width
assert 0 <= y < board.height
assert board.get(x, y) == EMPTY
return x, y
except (IndexError, ValueError, AssertionError):
print ''Example input: 0,1''


def tictactoe():
board = Board(3, 3, 3)
def play():
while True:
board.show()
def moveIter():
yield getInput(board), ''x'', ''Player''
yield computerPlay(board, ''o'', ''x''), ''o'', ''Computer''

for pos, mark, who in moveIter():
board.set(pos[0], pos[1], mark)
if doesWin(board, pos[0], pos[1]):
print who + '' won!''
return
if board.isTie():
print ''Tie!''
return
play()
board.show()


if __name__ == ''__main__'':
tictactoe()
It allows arbitrarily large boards but the computer doesn''t have much intelligence so it''ll play very badly with boards larger than 3x3

Share this post


Link to post
Share on other sites
patindahat    118
I didn''t look at any of the specifics, but I must say you have good commenting habits, and the big thing that I like that many people are bad for (expecially microsoft, and even myself but I''m working on finding what I think works best) is..... Consistency with style and commenting. You keep the same style on whitespace, commenting all those little things that make no difference to the compiler but everything to a programmer reading the code..... Keep that up cause its a must to keep any co-works that read your code happy.

Pat - Ex nihilo nihilus

Share this post


Link to post
Share on other sites
DrPizza    160
quote:
I didn''t look at any of the specifics, but I must say you have good commenting habits,

The only good commenting habit is "write code whose purpose and mechanism is so clear that it needs no comments".

Source code doesn''t lie. Comments do. Source code is always superior.

Share this post


Link to post
Share on other sites