Tic Tac Toe with screen updating

Published December 21, 2008
Advertisement
I haven't really had much to share so I had no real reason to make any prior entries. I've been programming for around 6+ years now, mainly in Visual Basic and a little in C#. I'm currently reviewing over C++ since I haven't used it enough to start working with an API like SDL, however I'm learning the general language as much as I can first. My friend had asked me to make a Tic Tac Toe game so I thought why not! It would be a good start, plus I had an idea to update the screen for every move.

Now my code might look "messy" since I did this half asleep and I don't believe I did any non-required checks for wins/draws.

// Tic Tac Toe// By: MrCpaw// [Notes: This is just a general tic tac toe, but with a game loop to redraw the screen.]#include #include using namespace std;// Public STARTchar PlayerSelect = ' ';// Setupbool GameActive = true;bool GameOver = false;bool Draw = false;char cOne = '1';char cTwo = '2';char cThree = '3';char cFour = '4';char cFive = '5';char cSix = '6';char cSeven = '7';char cEight = '8';char cNine = '9';bool PlayerXTurn = true;bool PlayAgain = true;// Public END// Win Checksint WinCheck();// Sqaure Check For Xint SquareCheckX(char cSquareCheckCharPX);// Sqaure Check For Oint SquareCheckO(char cSquareCheckCharPO);// Boardvoid GameBoard();int main(){	// Game Loop	while (GameActive == true)	{		// Draw Game Board		GameBoard();		// Player X Turn UPDATE		while (PlayerXTurn == true)		{			cout << "\nPlayer X, your turn!  Enter move:_\b";			cin >> PlayerSelect;			cin.get();			// Check For Sqaures			SquareCheckX(PlayerSelect);			// Call Win Check			WinCheck();			// Skip O Turn if Player X Won			if (GameOver == true || Draw == true)			{				PlayerXTurn = true;				break;			}		}		// Draw Game Board		GameBoard();		// Player O Turn UPDATE		while (PlayerXTurn == false)		{			cout << "\nPlayer O, your turn!  Enter move:_\b";			cin >> PlayerSelect;			cin.get();						// Call Win Check			WinCheck();			// Check For Sqaures			SquareCheckO(PlayerSelect);						// Call Win Check			WinCheck();			// Skip X Turn if Player O Won			if (GameOver == true || Draw == true)			{				PlayerXTurn = false;				break;			}		}		// Check to END		if (GameOver == true && Draw != true)		{			system("cls");			GameBoard();			cout << "\n\n\nYou Win!\n\n";			cout << "Type 'P' or 'p' to play again!  Type any key to quit!\n_\b";			cin >> PlayerSelect;			if (PlayerSelect == 'P' || PlayerSelect == 'p')			{				GameActive = true;				cOne = '1';				cTwo = '2';				cThree = '3';				cFour = '4';				cFive = '5';				cSix = '6';				cSeven = '7';				cEight = '8';				cNine = '9';				PlayerXTurn = true;				GameOver = false;			}			else			{				GameActive = false;			}		}		// Check to END DRAW		if (GameOver == true && Draw == true)		{			system("cls");			GameBoard();			cout << "\n\n\nDraw!\n\n";			cout << "Type 'P' or 'p' to play again!  Type any key to quit!\n_\b";			cin >> PlayerSelect;			if (PlayerSelect == 'P' || PlayerSelect == 'p')			{				GameActive = true;				cOne = '1';				cTwo = '2';				cThree = '3';				cFour = '4';				cFive = '5';				cSix = '6';				cSeven = '7';				cEight = '8';				cNine = '9';				PlayerXTurn = true;				GameOver = false;				Draw = false;			}			else			{				GameActive = false;			}		}	}	system("cls");	GameBoard();	cout << "\n\n\nYou Win!";	cin.get();	return 0;}void GameBoard(){		// Clear Then Draw		system("cls");		// Draw Board		cout << " | " << cOne << " | " << cTwo << " | " << cThree << " | " << endl;		cout << " | " << cFour << " | " << cFive << " | " << cSix << " | " << endl;		cout << " | " << cSeven << " | " << cEight << " | " << cNine << " | " << endl;}int WinCheck(){	// For X	// 1 - 2 - 3	if (cOne == 'X' && cTwo == 'X' && cThree == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 4 - 5 - 6	else if (cFour == 'X' && cFive == 'X' && cSix == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 7 - 8 - 9	else if (cSeven == 'X' && cEight == 'X' && cNine == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 1 - 4 - 7	else if (cOne == 'X' && cFour == 'X' && cSeven == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 2 - 5 - 8	else if (cTwo == 'X' && cFive == 'X' && cEight == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 3 - 6 - 9	else if (cThree == 'X' && cSix == 'X' && cNine == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 1 - 5 - 9	else if (cOne == 'X' && cFive == 'X' && cNine == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// 3 - 5 - 7	else if (cThree == 'X' && cFive == 'X' && cSeven == 'X')	{		PlayerXTurn = false;		GameOver = true;		return 0;	}	// For O	// 1 - 2 - 3	else if (cOne == 'O' && cTwo == 'O' && cThree == 'O')	{		GameOver = true;		return 0;	}	// 4 - 5 - 6	else if (cFour == 'O' && cFive == 'O' && cSix == 'O')	{		GameOver = true;		return 0;	}	// 7 - 8 - 9	else if (cSeven == 'O' && cEight == 'O' && cNine == 'O')	{		GameOver = true;		return 0;	}	// 1 - 4 - 7	else if (cOne == 'O' && cFour == 'O' && cSeven == 'O')	{		GameOver = true;		return 0;	}	// 2 - 5 - 8	else if (cTwo == 'O' && cFive == 'O' && cEight == 'O')	{		GameOver = true;		return 0;	}	// 3 - 6 - 9	else if (cThree == 'O' && cSix == 'O' && cNine == 'O')	{		GameOver = true;		return 0;	}	// 1 - 5 - 9	else if (cOne == 'O' && cFive == 'O' && cNine == 'O')	{		GameOver = true;		return 0;	}	// 3 - 5 - 7	else if (cThree == 'O' && cFive == 'O' && cSeven == 'O')	{		GameOver = true;		return 0;	}	// Check for Draw	else if (cOne != '1' && cTwo != '2' && cThree != '3' && cFour != '4' && cFive != '5' && cSix != '6' && cSeven != '7' && cEight != '8' && cNine != '9')	{		GameOver = true;		Draw = true;		return 0;	}	else	{		return 0;	}}int SquareCheckX(char cSquareCheckCharPX){	if (cSquareCheckCharPX == '1')	{		if (cOne == '1')		{			cOne = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '2')	{		if (cTwo == '2')		{			cTwo = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '3')	{		if (cThree == '3')		{			cThree = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '4')	{		if (cFour == '4')		{			cFour = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '5')	{		if (cFive == '5')		{			cFive = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '6')	{		if (cSix == '6')		{			cSix = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '7')	{		if (cSeven == '7')		{			cSeven = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '8')	{		if (cEight == '8')		{			cEight = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	if (cSquareCheckCharPX == '9')	{		if (cNine == '9')		{			cNine = 'X';			PlayerXTurn = false;		}		else		{			return 0;		}	}	// For Whatever reason if the checks go bad return	else	{		return 0;	}}int SquareCheckO(char cSquareCheckCharPO){	if (cSquareCheckCharPO == '1')	{		if (cOne == '1')		{			cOne = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '2')	{		if (cTwo == '2')		{			cTwo = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '3')	{		if (cThree == '3')		{			cThree = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '4')	{		if (cFour == '4')		{			cFour = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '5')	{		if (cFive == '5')		{			cFive = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '6')	{		if (cSix == '6')		{			cSix = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '7')	{		if (cSeven == '7')		{			cSeven = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '8')	{		if (cEight == '8')		{			cEight = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	if (cSquareCheckCharPO == '9')	{		if (cNine == '9')		{			cNine = 'O';			PlayerXTurn = true;		}		else		{			return 0;		}	}	// For Whatever reason if the checks go bad return	else	{		return 0;	}}


Well that's my Tic Tac Toe game for now, I'm off to bed then back to hitting the books.
0 likes 4 comments

Comments

Jason Z
Welcome to the C++ club!

I started out with VB as well - I think you'll enjoy the transition. C++ is much closer to the metal than VB or C#, so you get a better feel for how a computer works too.

Welcome to the journal club too!
December 21, 2008 10:02 AM
rip-off
You definitely need to look into using arrays in C++ =)
December 21, 2008 01:02 PM
LostSnow25
Welcome to the club!!!
December 21, 2008 04:57 PM
MrCpaw
Thanks. :)

I should of used a Multi-Dimensional Array for the game board but I didn't think of that at the time. I guess that must of been me being half asleep. :O

Right now I'm following these two books for my learning:

Accelerated C++ Practical Programming by Example

and

C++ Primer Plus (Reading this one first)

I must say I really do enjoy C++, not to mention I just love OOP languages!
December 22, 2008 01:03 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement