Important alert: (current site time 10/2/2014 12:06:54 PM EDT)
 

VB icon

A simple Tic-Tac-Toe game.

Email
Submitted on: 7/31/2002 8:43:50 AM
By: Ethan Croteau 
Level: Beginner
User Rating: By 8 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 23373
author picture
(About the author)
 
     This is a simple Tic-Tac-Toe game that uses a 2D array. It illustrates how to constract a simple game. There are no bells and whistles, this is plain and simple C++.
 
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: A simple Tic-Tac-Toe game.
// Description:This is a simple Tic-Tac-Toe game that uses a 2D array. It illustrates how to constract a simple game. There are no bells and whistles, this is plain and simple C++.
// By: Ethan Croteau
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4485&lngWId=3//for details.//**************************************

/*****************************************************************************
 * Author: Ethan Croteau
 * Creation Date: 7/31/02
 * Description: This is a simple console app that allows you to play Tic-Tac-Toe.
 * Compile: This is ANSI C++, it was compiled with Borland CPP 5.5.
 *****************************************************************************/
#include <iostream.h>
using namespace std;
class TicTacToe
{
public:
TicTacToe();
int PlayerMove(int i);
void NextPlayer();
void WinCheck();
void DrawBoard();
private:
int board[3][3];
int turn; // player1 == 1, player2 == 2
int gameOver;
void PlayGame();
};
/*****************************************************************************
 * Clear the board, set turn for player1, set gameOver to false, draw the board,
 * then away we go!
 *****************************************************************************/
TicTacToe::TicTacToe()
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
 board[i][j] = 0;// 0 means empty
turn = 1; // player1
gameOver = 0;
DrawBoard();
PlayGame();
}
/*****************************************************************************
 * i is the board position that the player selects.
 * Calculate the x and y coordinates.
 * Ensure board position is valid. Check if game has been won. Switch Players. Update board.
 * Return: 0 if move was made, otherwise return the value stored in the board position.
 *****************************************************************************/
int TicTacToe::PlayerMove(int i)
{
int x = (i - 1)/3;
int y = ((i + 2) % 3);
int returnVal = board[x][y];
if (returnVal == 0)
{
board[x][y] = turn;
WinCheck();
if (!gameOver)
NextPlayer();
}
else
cout << "Invalid move, try again.\n";
DrawBoard();
return returnVal;
}
/*****************************************************************************
 * If turn equals 1, set it equal to 2. Otherwise set it equal to 1.
 * The switches the active player.
 *****************************************************************************/
void TicTacToe::NextPlayer()
{
if (turn == 1)
turn = 2;
else
turn = 1;
}
/*****************************************************************************
 * If the game has been won, set gameOver equal to turn.
 * Turn always contains a value that is boolean true: 1 or 2.
 *****************************************************************************/
void TicTacToe::WinCheck()
{
if ((board[0][0] == turn) && (board[1][0] == turn) && (board[2][0] == turn))
gameOver = turn;
else
if ((board[0][1] == turn) && (board[1][1] == turn) && (board[2][1] == turn))
gameOver = turn;
else
if ((board[0][2] == turn) && (board[1][2] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][0] == turn) && (board[0][1] == turn) && (board[0][2] == turn))
gameOver = turn;
else
if ((board[1][0] == turn) && (board[1][1] == turn) && (board[1][2] == turn))
gameOver = turn;
else
if ((board[2][0] == turn) && (board[2][1] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][0] == turn) && (board[1][1] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][2] == turn) && (board[1][1] == turn) && (board[2][0] == turn))
gameOver = turn;
}
/*****************************************************************************
 * If the game has been won, set gameOver equal to turn.
 * Turn always contains a value that is boolean true: 1 or 2.
 *****************************************************************************/
void TicTacToe::PlayGame()
{
int i;
while (gameOver!=turn)
{
//DrawBoard();
cout << "Player[" << turn << "] Please enter move: ";
cin >> i;
PlayerMove(i);
}
cout << "Player[" << turn << "] Wins!" << endl;
}
/*****************************************************************************
 * Display the game board using ASCII characters.
 *****************************************************************************/
void TicTacToe::DrawBoard()
{
int temp[9];
int k = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if (board[i][j] == 0)
temp[k] = k+49;
else
 {
if (board[i][j] == 1)
 temp[k] = 88;
else
 temp[k] = 79;
 }
k++;
}
cout << "+---+---+---+\n";
cout <<"| " << (char)temp[0] << " | " << (char)temp[1] << " | " << (char)temp[2] << " | \n";
cout << "+---+---+---+\n";
cout <<"| " << (char)temp[3] << " | " << (char)temp[4] << " | " << (char)temp[5] << " | \n";
cout << "+---+---+---+\n";
cout <<"| " << (char)temp[6] << " | " << (char)temp[7] << " | " << (char)temp[8] << " | \n";
cout << "+---+---+---+\n";
}
/*****************************************************************************
 * Instantiate a TicTacToe object, which effectively starts your game play.
 *****************************************************************************/
int main()
{
TicTacToe Game;
return 0;
}


Other 1 submission(s) by this author

 


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

9/6/2002 8:42:55 PM

i have Microsoft Visual C++ and when i compiled this i got an error:
Compiling...
tic-tac-toe.cpp
c:\...tic-tac-toe.cpp(26) : error C2871: 'std' : does not exist or is not a namespace
Error executing cl.exe.
(If this comment was disrespectful, please report it.)

 
9/6/2002 8:44:20 PM

when i try to compile the program, i get this error:
Compiling...
Text1.cpp
C:\...\Text1.cpp(26) : error C2871: 'std' : does not exist or is not a namespace
Error executing cl.exe.
(If this comment was disrespectful, please report it.)

 
9/19/2002 10:27:55 AM

//i have Microsoft Visual C++ and when
//i
//compiled this i got an
//error:
//Compiling...
//tic-tac-toe.cpp
//c
//:\...tic-tac-toe.cpp(26) : error
//C2871:
//'std' : does not exist or is not a
//namespace
//Error executing cl.exe.

Try substituting 'using std::cout;' for 'using namespace std;'
(If this comment was disrespectful, please report it.)

 
9/20/2002 12:17:22 AMJason Woods

remove the ".h" from #include
(If this comment was disrespectful, please report it.)

 
9/20/2002 12:18:18 AMJason Woods

iostream.h
(If this comment was disrespectful, please report it.)

 
1/10/2003 10:19:03 PM

I have Visual C++ and I got the same error, comment out 'using namespace std;' and it runs fine.
(If this comment was disrespectful, please report it.)

 
2/1/2003 6:03:48 AMAaron Mueller

cool!
I have also coded a TicTacToe. Can you code a KI for the Player2 as a Computer Player) ?? I don't have an Idea ;(
(If this comment was disrespectful, please report it.)

 
3/2/2003 7:19:21 PM

take out the .h in #include then it works fine
(posted this as a disrespectful comment by mistake, hope you just found that funny :$)
(If this comment was disrespectful, please report it.)

 
4/27/2003 3:00:03 AM

I compiled this program, but when there is no winner, it keeps asking for input
(If this comment was disrespectful, please report it.)

 
4/27/2003 3:03:22 AM

I have complied this program, but it does not check for when there is no winners, and the tablet has been filled
(If this comment was disrespectful, please report it.)

 
9/20/2003 8:09:22 PM

I tried to run this program.. It worked well but when I entered a character instead of a number 0-9, infinte loop happened. It kept saying invalid move over and over again.
(If this comment was disrespectful, please report it.)

 
11/18/2003 8:24:03 PM

this program runs fine...just wanted to know how to add a loop command that would appear if the game came to a tie match...
(If this comment was disrespectful, please report it.)

 
7/13/2004 11:08:51 AM

Not sure why, but in iostream.h take out the .h (its line 1)
(If this comment was disrespectful, please report it.)

 
10/26/2004 12:59:29 PM

The program doesn't check if the board is full and there's no winner. Can somebody give me the last function "Game Over"?

(If this comment was disrespectful, please report it.)

 
11/24/2004 9:23:24 AM

the .h must deleted because the usingnamespace std; is allready included in .h so you could also delete usingnamespace std;
(If this comment was disrespectful, please report it.)

 
8/2/2006 4:21:13 AMChoy Renyu

Hi, i am currently doin a project for my schoolwork. But i am using very simple C programming to make it out. And i dont really understand yours. Do you think u can help to check mine out and add some comments?
(If this comment was disrespectful, please report it.)

 

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.