Third Game finished

Joined
Oct 22, 2005
Messages
168
Reaction score
1
I hope this is the right section...

well, anyways I have completed my third game, Tic-Tac-Toe. Ai is beatable but still tough. It took about a month to finish in C++. If you wish to download it.
http://www.madgamedesign.com/Tic-Tac-Toe.ace
http://www.madgamedesign.com/Tic-Tac-Toe.exe

:alienmental:

The source is a total of 831 lines.
I tried to make it easy to read.
Code:
#include <iostream>
#include <math.h>
#include <string>
using namespace std;


int usermove;
int game (1);
int move1;
int move2;
int move3;
int move4;
int move5;
int move6;
int move7;
int move8;
int move9;
string player;

class Box 
{
	public:
		char fill;
		int value;

};
Box box[9];
void lose()
{
	int quit;
	cout << box[6].fill << "|" << box[7].fill << "|" << box[8].fill << "\n";
	cout << "-----\n";	
	cout << box[3].fill << "|" << box[4].fill << "|" << box[5].fill << "\n";
	cout << "-----\n";
	cout << box[0].fill << "|" << box[1].fill << "|" << box[2].fill << "\n\n";
	cout << "You have lost Muhahahaha!!!!!!!!!\n";
	cout << "Press any key to quit.\n";
	cin >> quit;
	if(quit != '*')
	{
	game = 2;
	}
}
void win()
{
	int quit;
	cout << box[6].fill << "|" << box[7].fill << "|" << box[8].fill << "\n";
	cout << "-----\n";	
	cout << box[3].fill << "|" << box[4].fill << "|" << box[5].fill << "\n";
	cout << "-----\n";
	cout << box[0].fill << "|" << box[1].fill << "|" << box[2].fill << "\n\n";
	cout << "Congratulations, "; 
	cout << player; 
	cout << ", You have won! \n \n";
	cout << "Press any key to quit.\n";
	cin >> quit;
	if(quit != '*')
	{
	game = 2;
	}
}
void checkwin()
{
	//Checks to see if the player could win.
	if (box[0].fill == 'X' && box[1].fill == 'X' && box[2].fill == ' ') 
	{
		box[2].value += 100;
	}
	if (box[0].fill == 'X' && box[1].fill == ' ' && box[2].fill == 'X') 
	{
		box[1].value += 100;
	}
	if (box[0].fill == ' ' && box[1].fill == 'X' && box[2].fill == 'X') 
	{
		box[0].value += 100;
	}
	if (box[3].fill == ' ' && box[4].fill == 'X' && box[5].fill == 'X') 
	{
		box[3].value += 100;
	}
	if (box[3].fill == 'X' && box[4].fill == ' ' && box[5].fill == 'X') 
	{
		box[4].value += 100;
	}
	if (box[3].fill == 'X' && box[4].fill == 'X' && box[5].fill == ' ') 
	{
		box[5].value += 100;
	}
	if (box[6].fill == 'X' && box[7].fill == ' ' && box[8].fill == 'X') 
	{
		box[7].value += 100;
	}
	if (box[6].fill == ' ' && box[7].fill == 'X' && box[8].fill == 'X') 
	{
		box[6].value += 100;
	}
	if (box[6].fill == 'X' && box[7].fill == 'X' && box[8].fill == ' ') 
	{
		box[8].value += 100;
	}
	if (box[0].fill == 'X' && box[3].fill == 'X' && box[6].fill == ' ') 
	{
		box[6].value += 100;
	}
	if (box[0].fill == 'X' && box[3].fill == ' ' && box[6].fill == 'X') 
	{
		box[3].value += 100;
	}
	if (box[0].fill == ' ' && box[3].fill == 'X' && box[6].fill == 'X') 
	{
		box[0].value += 100;
	}
	if (box[1].fill == ' ' && box[4].fill == 'X' && box[7].fill == 'X') 
	{
		box[1].value += 100;
	}
	if (box[1].fill == 'X' && box[4].fill == ' ' && box[7].fill == 'X') 
	{
		box[4].value += 100;
	}
	if (box[1].fill == 'X' && box[4].fill == 'X' && box[7].fill == ' ') 
	{
		box[7].value += 100;
	}
	if (box[2].fill == 'X' && box[5].fill == ' ' && box[8].fill == 'X') 
	{
		box[5].value += 100;
	}
	if (box[2].fill == ' ' && box[5].fill == 'X' && box[8].fill == 'X') 
	{
		box[2].value += 100;
	}
	if (box[2].fill == 'X' && box[5].fill == 'X' && box[8].fill == ' ') 
	{
		box[8].value += 100;
	}
	if (box[0].fill == ' ' && box[4].fill == 'X' && box[8].fill == 'X') 
	{
		box[0].value += 100;
	}
	if (box[0].fill == 'X' && box[4].fill == ' ' && box[8].fill == 'X') 
	{
		box[4].value += 100;
	}
	if (box[0].fill == 'X' && box[4].fill == 'X' && box[8].fill == ' ') 
	{
		box[8].value += 100;
	}
	if (box[2].fill == ' ' && box[4].fill == 'X' && box[6].fill == 'X') 
	{
		box[2].value += 100;
	}
	if (box[2].fill == 'X' && box[4].fill == ' ' && box[6].fill == 'X') 
	{
		box[4].value += 100;
	}
	if (box[2].fill == 'X' && box[4].fill == 'X' && box[6].fill == ' ') 
	{
		box[6].value += 100;
	}
}

void checklose()
{
		if (box[0].fill == '0' && box[1].fill == '0' && box[2].fill == '0') 
		{
			lose();
		}
		if (box[0].fill == '0' && box[4].fill == '0' && box[8].fill == '0') 
		{
			lose();
		}
		if (box[0].fill == '0' && box[3].fill == '0' && box[6].fill == '0') 
		{
			lose();
		}
		if (box[1].fill == '0' && box[4].fill == '0' && box[7].fill == '0') 
		{
			lose();
		}
		if (box[2].fill == '0' && box[5].fill == '0' && box[8].fill == '0') 
		{
			lose();
		}
		if (box[2].fill == '0' && box[4].fill == '0' && box[6].fill == '0') 
		{
			lose();
		}
		if (box[3].fill == '0' && box[4].fill == '0' && box[5].fill == '0') 
		{
			lose();
		}
		if (box[6].fill == '0' && box[7].fill == '0' && box[8].fill == '0') 
		{
			lose();
		}
}
void play ()
{
	if(game == 1)
	{
		cout << box[6].fill << "|" << box[7].fill << "|" << box[8].fill << "\n";
		cout << "-----\n";	
		cout << box[3].fill << "|" << box[4].fill << "|" << box[5].fill << "\n";
		cout << "-----\n";
		cout << box[0].fill << "|" << box[1].fill << "|" << box[2].fill << "\n\n";
		cout << player; 
		cout << " It is your turn where do you want to move?\n\n";
		cin >> usermove; 
		cout << "\n\n\n\n";
		switch(usermove)
		{
			case 1:
			if (box[0].fill == ' ') 
			{
					box[0].fill = 'X';
					box[0].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[0].fill == 'X' && box[1].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
					if (box[0].fill == 'X' && box[4].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
					if (box[0].fill == 'X' && box[3].fill == 'X' && box[6].fill == 'X') 
					{
						win();
					}
			}	
			else 
			{
				cout << player;
				cout << " that tile is already filled. \n\n";
				play();
			}
			break;
			case 2:
				if (box[1].fill == ' ') 
				{
					box[1].fill = 'X';
					box[1].value = 0;
					checkwin();
					if (box[0].fill == ' ' && box[1].fill == 'X' && box[2].fill == ' ')
					{
						box[0].value += 1;
						box[2].value += 1;
					}
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[0].fill == 'X' && box[1].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
					if (box[1].fill == 'X' && box[4].fill == 'X' && box[7].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 3:
				if (box[2].fill == ' ') 
				{
					box[2].fill = 'X';
					box[2].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[0].fill == 'X' && box[1].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
					if (box[2].fill == 'X' && box[4].fill == 'X' && box[6].fill == 'X') 
					{
						win();
					}
					if (box[2].fill == 'X' && box[5].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 4:
				if (box[3].fill == ' ') 
				{
					box[3].fill = 'X';
					box[3].value = 0;
					checkwin();
					if (box[0].fill == ' ' && box[3].fill == 'X' && box[6].fill == ' ')
					{
						box[0].value += 1;
						box[6].value += 1;
					}
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[3].fill == 'X' && box[4].fill == 'X' && box[5].fill == 'X') 
					{
						win();
					}
					if (box[0].fill == 'X' && box[3].fill == 'X' && box[6].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 5:
				if (box[4].fill == ' ') 
				{
					box[4].fill = 'X';
					box[4].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[0].fill == 'X' && box[4].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
					if (box[1].fill == 'X' && box[4].fill == 'X' && box[7].fill == 'X') 
					{
						win();
					}
					if (box[2].fill == 'X' && box[4].fill == 'X' && box[6].fill == 'X') 
					{
						win();
					}
					if (box[5].fill == 'X' && box[4].fill == 'X' && box[3].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 6:
				if (box[5].fill == ' ') 
				{
					box[5].fill = 'X';
					box[5].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == ' ' && box[5].fill == 'X' && box[8].fill == ' ')
					{
						box[2].value += 1;
						box[8].value += 1;
					}
					// Checks to see if the player has won.
					if (box[5].fill == 'X' && box[4].fill == 'X' && box[3].fill == 'X') 
					{
						win();
					}
					if (box[5].fill == 'X' && box[8].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 7:
				if (box[6].fill == ' ') 
				{
					box[6].fill = 'X';
					box[6].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == ' ' && box[1].fill == ' ' && box[2].fill == ' ' && box[3].fill == ' ' && box[4].fill == ' ' && box[5].fill == ' ' && box[6].fill == 'X' && box[7].fill == ' ' && box[8].fill == ' ')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[6].fill == 'X' && box[7].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
					if (box[6].fill == 'X' && box[3].fill == 'X' && box[0].fill == 'X') 
					{
						win();
					}
					if (box[6].fill == 'X' && box[4].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 8:
				if (box[7].fill == ' ') 
				{
					box[7].fill = 'X';
					box[7].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == ' ' && box[7].fill == 'X' && box[8].fill == ' ')
					{
						box[6].value += 1;
						box[8].value += 1;
					}
					// Checks to see if the player has won.
					if (box[6].fill == 'X' && box[7].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
					if (box[7].fill == 'X' && box[4].fill == 'X' && box[1].fill == 'X') 
					{
						win();
					}
						
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			case 9:
				if (box[8].fill == ' ') 
				{
					box[8].fill = 'X';
					box[8].value = 0;
					checkwin();
					if (box[8].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[6].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[2].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == 'X'&& box[4].fill == ' ')
					{
							box[4].value += 3;
					}
					if (box[0].fill == ' ' && box[1].fill == ' ' && box[2].fill == ' ' && box[3].fill == ' ' && box[4].fill == ' ' && box[5].fill == ' ' && box[6].fill == ' ' && box[7].fill == ' ' && box[8].fill == 'X')
					{
							box[4].value += 3;
					}
					// Checks to see if the player has won.
					if (box[6].fill == 'X' && box[7].fill == 'X' && box[8].fill == 'X') 
					{
						win();
					}
					if (box[8].fill == 'X' && box[4].fill == 'X' && box[0].fill == 'X') 
					{
						win();
					}
					if (box[8].fill == 'X' && box[5].fill == 'X' && box[2].fill == 'X') 
					{
						win();
					}
				}	
				else 
				{
					cout << player;
					cout << " that tile is already filled. \n\n";
					play();
				}
			break;
			default:
				cout << "Sorry, you must enter a number 1-9.\n\n\n\n";
				play();
		}
// Here the AI finds the highest value by process of emlination.
		if(box[0].value - box[1].value >= 0)
		{
			move1 = box[0].value;
		}
		else
		{
			move1 = box[1].value;
		}
//**************************************
		if(box[2].value - box[3].value >= 0)
		{
			move2 = box[2].value;
		}
		else
		{
			move2 = box[3].value;
		}
//**************************************
		if(box[4].value - box[5].value >= 0)
		{
			move3 = box[4].value;
		}
		else
		{
			move3 = box[5].value;
		}
//**************************************
		if(box[6].value - box[7].value >= 0)
		{
			move4 = box[6].value;
		}
		else
		{
			move4 = box[7].value;
		}
//**************************************
		if(move1 - move2 >= 0)
		{
			move6 = move1;
		}
		else
		{
			move6 = move2;
		}
//**************************************
		if(move3 - move4 >= 0)
		{
			move7 = move3;
		}
		else
		{
			move7 = move4;
		}
//**************************************
		if(move6 - move7 >= 0)
		{
			move8 = move6;
		}
		else
		{
			move8 = move7;
		}
//**************************************
		if(box[8].value - move8 >= 0)
		{
			move9 = box[8].value;
		}
		else
		{
			move9 = move8;
		}
//*****************
//Now the AI moves*
//*****************
		if(move9 == box[0].value && box[0].fill == ' ')
		{
			box[0].fill = '0';
			box[0].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[1].value && box[1].fill == ' ')
		{
			box[1].fill = '0';
			box[1].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[2].value && box[2].fill == ' ')
		{
			box[2].fill = '0';
			box[2].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[3].value && box[3].fill == ' ')
		{
			box[3].fill = '0';
			box[3].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[4].value && box[4].fill == ' ')
		{
			box[4].fill = '0';
			box[4].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[5].value && box[5].fill == ' ')
		{
			box[5].fill = '0';
			box[5].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[6].value && box[6].fill == ' ')
		{
			box[6].fill = '0';
			box[6].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[7].value && box[7].fill == ' ')
		{
			box[7].fill = '0';
			box[7].value = 0;
			checklose();
			play();
		}
		else if(move9 == box[8].value && box[8].fill == ' ')
		{
			box[8].fill = '0';
			box[8].value = 0;
			checklose();
			play();
		}
	}
//*****************************************
}		
void valuefill ()
{
	box[0].fill = ' ';
	box[1].fill = ' ';
	box[2].fill = ' ';
	box[3].fill = ' ';
	box[4].fill = ' ';
	box[5].fill = ' ';
	box[6].fill = ' ';
	box[7].fill = ' ';
	box[8].fill = ' ';
	box[0].value = 3;
	box[1].value = 2;
	box[2].value = 3;
	box[3].value = 2;
	box[4].value = 4;
	box[5].value = 2;
	box[6].value = 3;
	box[7].value = 2;
	box[8].value = 3;
	play ();
}
int main ()
{
	int start;
	cout << "********************************\n";
	cout << "********************************\n";
	cout << "** Welcome to Tic Tac Toe 1.0 **\n";
	cout << "********************************\n";
	cout << "*********** 1. Play ************\n";
	cout << "******** 2. How to Play ********\n";
	cout << "*********** 3. Exit ************\n";
	cout << "********************************\n";
	cout << "********************************\n";
	cin >> start;
	if(start == 1)
	{
		cout << "What is your player name? \n";
		cin >> player;
		cout << "Thanks " << player <<".\n";
		valuefill ();
	}
	else if(start == 2)
	{
		int play;
		cout << "Here is what the game board looks like. The numbers\nyou see in each box are the numbers you will type\nin to place your letter in that box.\n\n";
		cout << "7|8|9\n";
		cout << "-----\n";
		cout << "4|5|6\n";
		cout << "-----\n";
		cout << "1|2|3\n\n";
		cout << "Press 1 to continue. \n";
		cin >> play;
		if(play == 1)
		{
			cout << "What is your player name? \n";
			cin >> player;
			cout << "Thanks " << player <<".\n";
			valuefill ();
		}
	}
	else if(start == 3)
	{
		cout << "Bye Bye!\n";
	}
	else
	{
		cout << "You need to enter a number 1, 2, or 3.\n";
	}
}

Comments are welcomed! :) :D
 
Last edited by a moderator:
The AI is pretty tough.. I wont comment on your source because to me, it was an awsome effort, you got that AI down packed very well also. One suggestion, add something to add multiple lines to the command console to act as a clear screen function, be it your own method or imported.
 
The AI is pretty tough.. I wont comment on your source because to me, it was an awsome effort, you got that AI down packed very well also. One suggestion, add something to add multiple lines to the command console to act as a clear screen function, be it your own method or imported.

Yeah, that's what i'd also suggest. Very nice work mate. Looks good. :)
 
Not very efficient coding if you ask me, for instance comparison by elimination isn't required once you've already imported the math library (since it includes comparison functions). Don't try to re-invent the wheel ;)

Also variable initialisation can be done simpeler by using int move1, move2, move3, etc. Not to mention the fact that you use global variables for just about everything which is a TERRIBLE way to code - but you'll find that out soon enough once you make bigger projects.

Lastly, C++ is an object oriented language. Use that to your adventage!
 
Not very efficient coding if you ask me, for instance comparison by elimination isn't required once you've already imported the math library (since it includes comparison functions). Don't try to re-invent the wheel ;)

Also variable initialisation can be done simpeler by using int move1, move2, move3, etc. Not to mention the fact that you use global variables for just about everything which is a TERRIBLE way to code - but you'll find that out soon enough once you make bigger projects.

Lastly, C++ is an object oriented language. Use that to your adventage!


Fair quote. :P
 
That code could definitely use some optimisation, as the tic-tac-toe example is a standard algorithms design problem. For example,
Code:
cout << player;
					cout << " that tile is already filled. \n\n";
Is repeated quite a bit, it deserves its own function.

Also, the checking would be best done in a loop.

A lookup table would be the best implementation. There are only 19683 possible states of the board, including rotated and flipped states. Omitting the duplicates, it's only 765.

It reminded me of this interesting article: Great Debate V
...and this...
http://webster.cs.ucr.edu/AsmTools/MASM/TicTacToe/ttt_1.html

edit: I just saw your binary - 256Kb!?!? :eek:
 
Last edited:
For some reasons I get an error.

The application failed to initialize properly (0xc0000135). Click on OK to terminate the application.
 
For some reasons I get an error.

The application failed to initialize properly (0xc0000135). Click on OK to terminate the application.
You need to compile the source yourself...

I used MSCPP 12.00.8804 compiler with following options:
Code:
/Ox /GA /w /MD /link /filealign:512
Even with maximum opts there are still many repeated strings, that is why it compressed down to 4.3Kb.

Binary is attached (only 11.0Kb, don't know how OP got 256Kb :eek:)
 

Attachments

Last edited:
Back