Important alert: (current site time 4/20/2014 11:36:18 AM EDT)
 

VB icon

Tic Tac Toe

Email
Submitted on: 11/6/2012 12:19:58 PM
By: Abdullah Alghaznawy 
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general)
Views: 2609
 
     Two players Tic Tac Toe game, for education purpose. A good example in putting functions, arrays, and global variables all together to work. It was my first code I've ever wrote, so you won't find comments. If your PC generates question marks, (the Arabic letter 'غ' might do so) substitute it with the letter 219 (ASCII)

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Tic Tac Toe
// Description:Two players Tic Tac Toe game, for education purpose.
A good example in putting functions, arrays, and global variables all together to work.
It was my first code I've ever wrote, so you won't find comments.
If your PC generates question marks, (the Arabic letter 'غ' might do so) substitute it with the letter 219 (ASCII)
// By: Abdullah Alghaznawy
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13865&lngWId=3//for details.//**************************************

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int NumberOfTurns;
bool turn;
char a1 = ' ', a2 = ' ', a3 = ' ',
	 b1 = ' ', b2 = ' ', b3 = ' ',
	 c1 = ' ', c2 = ' ', c3 = ' ';
void TheBoard();
char XO_Indicator();
void InputSquare();
bool WinnerCheck();
void GameStructure();
void GetPlayerName();
void PlayerName();
const int MAX = 100;
char player_1 [ MAX ];
char player_2 [ MAX ];
char input_square [ MAX ];
int main()
{ 
	cout << "Two players Tic Tac Toe game" << endl
		 << "----------------------------" << endl;
	cout << "How to play:\n Input first player name, and second player name." << endl
		 << " Use corresponding numpad numbers to input your turn" << endl;
	cout << endl;
 GameStructure();
 system("pause");
}
void GameStructure()
{	
	GetPlayerName();
	for(NumberOfTurns = 1; NumberOfTurns <= 9; ++NumberOfTurns)
	{
		turn = !turn;
		system("cls");
		TheBoard();		
		InputSquare();
		if(!WinnerCheck() && NumberOfTurns == 9)
		{
			system("cls");
			TheBoard();		
			cout << endl;
			cout << "Drow." << endl;
			return;
		}
		if(WinnerCheck())
		{
			system("cls");
			TheBoard();	 cout << endl;
			PlayerName();cout << " wins." << endl;
			return;
		}		
	}
}
void TheBoard()
{
	cout << endl;
	cout << ' ' << "غغغغغغغغغغغغغغغغغغغغغغغ" 
		 << endl;
	//First row of 3 cells
	for(int c = 0 ; c++ < 3 ;)
		cout << ' '	 << "غغ" << " " << (c==2?a1:' ') << " "
					 << "غغ" << " " << (c==2?b1:' ') << " "
					 << "غغ" << " " << (c==2?c1:' ') << " "
					 << "غغ" << endl;
	cout << ' ' << "غغغغغغغغغغغغغغغغغغغغغغغ" << endl;
	//Second row of 3 cells
	for(int c = 0 ; c++ < 3 ;)
		cout << ' '	 << "غغ" << " " << (c==2?a2:' ') << " "
					 << "غغ" << " " << (c==2?b2:' ') << " "
					 << "غغ" << " " << (c==2?c2:' ') << " "
					 << "غغ" << endl;
	cout << ' ' << "غغغغغغغغغغغغغغغغغغغغغغغ" << endl;
	//Third row of 3 cells
	for(int c = 0 ; c++ < 3 ;)
		cout << ' '	 << "غغ" << " " << (c==2?a3:' ') << " "
					 << "غغ" << " " << (c==2?b3:' ') << " "
					 << "غغ" << " " << (c==2?c3:' ') << " "
					 << "غغ" << endl;
	cout << ' ' << "غغغغغغغغغغغغغغغغغغغغغغغ" << endl;
	return;
}
char XO_Indicator()
{
	if(turn == 1)return 'X';
	else return 'O';
}
void InputSquare()
{
	while(true)
		{
			cout << endl;	PlayerName();
			cout << " turn(" << XO_Indicator() << "): ";
			cin >> input_square;
			if(input_square[0] == '7' && a1 == ' ')	a1 = XO_Indicator();
	else	if(input_square[0] == '4' && a2 == ' ')	a2 = XO_Indicator();
	else	if(input_square[0] == '1' && a3 == ' ')	a3 = XO_Indicator();
	else	if(input_square[0] == '8' && b1 == ' ')	b1 = XO_Indicator();
	else	if(input_square[0] == '5' && b2 == ' ')	b2 = XO_Indicator();
	else	if(input_square[0] == '2' && b3 == ' ')	b3 = XO_Indicator();
	else	if(input_square[0] == '9' && c1 == ' ')	c1 = XO_Indicator();
	else	if(input_square[0] == '6' && c2 == ' ')	c2 = XO_Indicator();
	else	if(input_square[0] == '3' && c3 == ' ')	c3 = XO_Indicator();
	else	continue;
			break;
		}
}
bool WinnerCheck()
{
	if((a1==XO_Indicator()&&
	 a2==XO_Indicator()&&
	 a3==XO_Indicator() )||
	 (b1==XO_Indicator()&&
		b2==XO_Indicator()&&
		b3==XO_Indicator() )||
	 (c1==XO_Indicator()&&
	 c2==XO_Indicator()&&
		c3==XO_Indicator() )||
	 (a1==XO_Indicator()&&
	 b1==XO_Indicator()&&
		c1==XO_Indicator() )||
	 (a2==XO_Indicator()&&
	 b2==XO_Indicator()&&
		c2==XO_Indicator() )||
	 (a3==XO_Indicator()&&
	 b3==XO_Indicator()&&
		c3==XO_Indicator() )||
	 (a1==XO_Indicator()&&
	 b2==XO_Indicator()&&
		c3==XO_Indicator() )||
	 (a3==XO_Indicator()&&
	 b2==XO_Indicator()&&
		c1==XO_Indicator() ))
		return 1;
	else return 0;
}
void GetPlayerName()
{
	cout << "First player name: ";
	cin.getline(player_1, sizeof player_1);
	cout << "Second player name: ";
	cin.getline(player_2, sizeof player_2);
}
void PlayerName()
{
	switch(XO_Indicator())
			{
			case 'X':	cout << player_1;break;
			case 'O':	cout << player_2;
			}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.