Sign in to follow this  
Supaflyfrank

Finished Console Tic Tac Toe Clone what is next?

Recommended Posts

Hello. I finished my tic-tac-toe game for the console in C++ and would REALLY like some feed back on it. Ways to improve it,bugs etc... Anyway I finished my clone an d trying to decide what I should do next. I was thinking of just doing some direct x and make a pong clone. Is that a decent route to take. I know all about Opengl Allegro and all the others but for now I know I want to do these games in DirectX. But before I do so are their any other programs that most people do to test their skill in the console before they start doing 2D programming? Is pong really the simplist game to make. Any suggestions or help would be greatly apprecitated. Here's the code for the Tic Tac Toe Game
[source lang ="cpp"]

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class Board
{
	int board [3][3];
	int csymbol;
	int psymbol;
	int win;
public:
	bool quit;
	Board();
	int AnyoneWon();
	int CDecision(int fsymbol, int lsymbol); //fsymbol is the symbol of the player to see if they are about
	int CheckWin(int symbol); //to win lsymbol is the symbol to get in to the empty space that either
	void CMove(); //will block the player from winning or cause the computer to win.
	int CSymbol();
	char CurrentLetter(int first,int second);
	void DisplayBoard();
	int PSymbol();
	void Restart();
	bool SetSymbol(int x,int y,int symbol);
	void SetPiece(string input);
	bool SetPosition(string input);
	void WelcomeMessage();
	bool Tie();
	


};

int Board::PSymbol()
{
	return psymbol;
}

int Board::CSymbol()
{
	return csymbol;
}

bool Board::Tie()
{
	
	for(int x = 0;x<3;x++)
	{
		for(int y = 0;y<3;y++)
		{
			if(board[x][y]==0)
			{
			
				return 0;
			}
		}
	}

	win = 3;
	return 1;



}
void Board::WelcomeMessage()
{
	cout << "\t\tWelcome To Tic-Tac-Toe v1.0" << endl;
	cout << "To play this game just type in the letter and corresponding number to put your" << endl;
	cout << "piece in the proper square. Its just like BattleShip!" << endl;


}

bool Board::SetSymbol(int x,int y,int symbol)
{
	if(board[x][y]==0)
	{
		board[x][y] = symbol;
		return 1;
	}
	else
	{
		if(symbol!=csymbol)
		{
			cout << "There is a piece already here" << endl;
		}
		return 0;
	}

}
int Board::AnyoneWon()
{
	return win;
}
Board::Board()
{
	Restart();
}

void Board::Restart()
{
	for(int x = 0;x<3;x++)
	{
		for(int y = 0;y<3;y++)
			board[x][y] = 0;
	}
	win = 0;
}
void main()
{
	Board current;
	string input;
	srand(time(0));
	current.WelcomeMessage();
	cout << "Which piece would you like to be X's or O's" << endl;
	getline(cin,input);
	current.SetPiece(input);
	do
	{
		current.DisplayBoard();
		do
		{
			
			cout << "Your Move" << endl;
			getline(cin,input);
			if(input=="quit")
			{
				current.quit = 1;
				break;
			}
			if(current.SetPosition(input)==0)
			{
				continue;
			}
			if(current.Tie()==1)
			{
					cout << "There was a tie!" << endl;
			}
			else 
			{
				if(current.CheckWin(current.PSymbol())==current.PSymbol())
				{
					cout << endl << "You Win!" << endl;

				}	
				else if(current.CDecision(current.CSymbol(),current.CSymbol())==0)
				{
					if(current.CDecision(current.PSymbol(),current.CSymbol())==0)
					{
						current.CMove();
					}

				}
				else if(current.CheckWin(current.CSymbol())==current.CSymbol())
				{
					cout << endl << "Sorry You Lose!" << endl;

				}
		}
			current.DisplayBoard();

		}while(current.AnyoneWon()==0);

		cout << endl << "Would you like to play again? yes/no" << endl;
		int kcontinue = 0;
		do
		{

			getline(cin,input);
			if(input=="yes"||input=="YES")
			{
				current.Restart();
				kcontinue = 1;
			}
			else if(input=="no"||input=="No")
			{
				current.quit = 1;
				kcontinue = 1;
			}
			else
			{
				kcontinue = 0;
				cout << "Invalid Input Please Input Yes/No" << endl << endl;
			}
		}while(kcontinue!=1);
		

		

	}while(current.quit!=1);
	
	
	
	cout << "Thanks for playing Tic-Tac-Toe" << endl;
	system ("PAUSE");

}
void Board::SetPiece(string input)
{
	if(input[0]=='X'||input[0]=='x')
	{
		psymbol = 1;
		csymbol = 2;
	}
	if(input[0]=='O'||input[0]=='o')
	{
		psymbol = 2;
		csymbol = 1;
	}
	

}
void Board::DisplayBoard()
{
	cout << "     1     2     3     "<< endl;                 
	cout << "        |     |       " << endl;
	cout << "A    "<< CurrentLetter(0,0) <<"  |  "<< CurrentLetter(0,1) <<"  |  "<< CurrentLetter(0,2) <<"   " << endl;
	cout << "        |     |       " << endl;
	cout << "   -----------------" << endl;
	cout << "        |     |       " << endl;
	cout << "B    "<< CurrentLetter(1,0) <<"  |  "<< CurrentLetter(1,1) <<"  |  "<< CurrentLetter(1,2) <<"   " << endl;
	cout << "        |     |       " << endl;
	cout << "   -----------------" << endl;
	cout << "        |     |       " << endl;
	cout << "C    "<< CurrentLetter(2,0) <<"  |  "<< CurrentLetter(2,1) <<"  |  "<< CurrentLetter(2,2) <<"   " << endl;
	cout << "        |     |       " << endl;
	

}

char Board::CurrentLetter(int first,int second)
{
	if(board[first][second]==0)
	{
		return ' ';
	}
	if(board[first][second]==1)
	{
		return 'X';
	}
	if(board[first][second]==2)
	{
		return 'O';
	}
	return 0;
}

bool Board::SetPosition(string input)
{
	char number[1]; 
	number[0] = input[1];
	int second = atoi(number);
	if(second>3||second<1)
	{
		cout << "This isnt a valid position" << endl;
		return 0;
	}
	if(input[0]=='A'||input[0]=='a')
	{
		if(SetSymbol(0,second-1,psymbol)==0)
		{
			return 0;
		}
		else
		return 1;
	}
	else if(input[0]=='B'||input[0]=='b')
	{

		if(SetSymbol(1,second-1,psymbol)==0)
		{
			return 0;
		}
		else
		return 1;
	}
	else if(input[0]=='C'||input[0]=='c')
	{

		if(SetSymbol(2,second-1,psymbol)==0)
		{
			return 0;
		}
		else
		return 1;
	}
	else
	{
		cout << "That isnt a vaild input" << endl;
		return 0;
	}
}


int Board::CheckWin(int symbol)
{
	
	int winner = 0;
	for(int x = 0;x<3;x++)
	{
		
		if(x==0)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[x+y][0]==symbol)
				{
					winner += 1;
				}
				else 
					break;
			}

			if(winner==3)
			{
				win = symbol;
				for(int z = 0;z<3;z++)
					for(int t = 0;t<3;t++)
				
				return symbol;
			}
			winner = 0;
			for(int y = 0;y<3;y++)
			{
				if(board[x][y]==symbol)
				{
					winner += 1;

				}
				else
					break;
			}

			if(winner==3)
			{
				win = symbol;
				return symbol;
			}
			winner = 0;
			for(int y = 0;y<3;y++)
			{
				if(board[x+y][y]==symbol)
				{
					winner += 1;
				}
				else
					break;
			}
			if(winner==3)
			{
				win = symbol;
				return symbol;
			}
			winner = 0;
		}
		if(x==1)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[x][y]==symbol)
				{
					winner += 1;
				}
				else
					break;
			}
			if(winner==3)
			{	
				win = symbol;
				return symbol;
			}
			winner = 0;
		}
		if(x==2)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[x][y]==symbol)
				{
					winner += 1;
				}
				else
					break;
			}
			if(winner==3)
			{		
				win = symbol;
				return symbol;
			}

			winner = 0;
			for(int y = 0;y<3;y++)
			{
				if(board[x-y][y]==symbol)
				{
					winner += 1;
				}
				else
					break;
			}
			if(winner==3)
			{	
				win = symbol;
				return symbol;
			}
		}

	}


	for(int x = 0,winner = 0;x<3;x++)
	{

		if(board[x][2]==symbol)
		{
			winner += 1;
		}
		if(winner==3)
		{
			win = symbol;
			return symbol;
		}
	}

	for(int x = 0,winner = 0;x<3;x++)
	{

		if(board[x][1]==symbol)
		{
			winner += 1;
		}
		else
		break;

		if(winner==3)
		{
			win = symbol;
			return symbol;
		}
	}

	return 0;
}




void Board::CMove()
{
	int x;
	int y;
	do
	{
		x = (rand()%3);
		y = (rand()%3);
	}while(SetSymbol(x,y,csymbol)!=1);
}

int Board::CDecision(int fsymbol, int lsymbol)
{
	int winner = 0;

	for(int x = 0;x<3;x++)
	{
		if(x==0)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[0][y]==fsymbol)
				{
					winner += 1;
				}
			}
			if(winner == 2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[0][y]==0)
					{
						SetSymbol(0,y,lsymbol);
						return 1;
					}
				}	
			}
			winner = 0;
			for(int y = 0;y<3;y++)
			{
				
				if(board[x+y][0]==fsymbol)
				{				
					winner += 1;
				}
			}
			if(winner == 2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[x+y][0]==0)
					{
						SetSymbol(x+y,0,lsymbol);
						return 1;
					}
				}
			}
			winner = 0;
			for(int y = 0;y<3;y++)
			{

				if(board[x+y][y]==fsymbol)
				{
					winner += 1;		
				}
			}
			if(winner==2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[x+y][y]==0)
					{
						SetSymbol(x+y,y,lsymbol);
						return 1;
					}
				}
			}
			winner = 0;
		}
		if(x==1)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[x][y]==fsymbol)
				{
					winner += 1;
				}
			}
			if(winner==2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[x][y]==0)
					{
						SetSymbol(x,y,lsymbol);
						return 1;
					}
				}
			}
			winner = 0;

		}



		if(x==2)
		{
			for(int y = 0;y<3;y++)
			{
				if(board[x][y]==fsymbol)
				{
					winner += 1;
				}
			}
			if(winner==2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[x][y]==0)
					{
						SetSymbol(x,y,lsymbol);
						return 1;
					}
				}
			}
			winner = 0;

			for(int y = 0;y<3;y++)
			{
				if(board[x-y][y]==fsymbol)
				{
					winner += 1;
				}
			}
			if(winner==2)
			{
				for(int y = 0;y<3;y++)
				{
					if(board[x-y][y]==0)
					{
						SetSymbol(x-y,y,lsymbol);
						return 1;	
					}
				}
			}
			winner = 0;
		}
	}

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

			if(board[x][1]==fsymbol)
			{
				winner += 1;
			}
	}
	if(winner==2)
	{
		for(int x = 0;x<3;x++)
		{
			if(board[x][1]==0)
			{
				SetSymbol(x,1,lsymbol);
				return 1;
			}
		}
	}
	winner = 0;
	for(int x = 0;x<3;x++)
	{

			if(board[x][2]==fsymbol)
			{
				winner += 1;
			}
	}
	if(winner==2)
	{
		for(int x = 0;x<3;x++)
		{
			if(board[x][2]==0)
			{
				SetSymbol(x,2,lsymbol);
				return 1;
			}
		}
	}
	winner = 0;

	return 0;



}

Share this post


Link to post
Share on other sites
If you are going to start working with DirectX, then knowledge of C/C++ alone is not sufficent. First, you need to learn the fundamental basics of the Win32 API, either that or move on to C#, and use managed DirectX.

DirectX is not an easy beast to dance with, but once you learn the steps, it won't take long to master.

Share this post


Link to post
Share on other sites
I made a couple of text-based RPGs, and their pretty fun to make. I suggested it in another thread, but if you want to learn some of the Win32 API, you might want to try "Beginning Game Programming" by Michael Morrison. To make a simple game like Pong you probably don't need DirectX, because I made a Pong game with the Win32 API when I was only about half way through the book.

Share this post


Link to post
Share on other sites
SDL is a simple graphics API which has a direct link to OpenGL. You can play around with SDL and then eventually transform your project to use OpenGL. I think it's a great bridge to use. I suggest making a pong game using either SDL or Allegro as pong has all the basics of a simple game inside (input, graphics, real-time, winning states, etc).

Share this post


Link to post
Share on other sites
What you could do is make a choice as to if you will use managed code or unmanaged.
Managed code being .NET languages such as VB.NET and C#.
These are nice languages to start out with and a series of new tutorials are springing up that makes it a little easier.

DirectX would be a natural choice if you are going for managed languages.
It's all for your to decide. Start out small and play around... no need to rush things :)
btw, your stuff looks good.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this