Important alert: (current site time 7/15/2013 6:16:48 PM EDT)
 

VB icon

Basic TicTacToe

Email
Submitted on: 12/7/2005 11:34:00 PM
By: Dan R 
Level: Beginner
User Rating: By 5 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 22253
(About the author)
 
     Just another simple TicTacToe Game
 
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: Basic TicTacToe
// Description:Just another simple TicTacToe Game
// By: Dan R
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=10094&lngWId=3//for details.//**************************************

//Program: Basic TicTacToe
//Author: Dan R (NoName)
//Date: 12/07/2005
#include <iostream>
using namespace::std;
#include <stdlib.h>
void displayBoard();
bool checkWin();
void move(bool);
bool isLegal(int);
char board[9] = {'1','2','3','4','5','6','7','8','9'};
void main()
{
	bool player = false; // true = X false = O
	displayBoard();
	while(!checkWin())
	{
		if(player == true)
			player = false;
		else
			player = true;
		move(player);
	}
	if(player == true)
		cout << "Player 1 WINS" << endl;
	else
		cout << "Player 2 WINS" << endl;
}
void displayBoard()
{
	system("cls");
	cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl
		 << " ---------" << endl
		 << " " << board[3] << " | " << board[4] << " | " << board[5] << endl
		 << " ---------" << endl
		 << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}
bool checkWin()
{
	if(board[0] == board[1] && board[2] == board[0] )
		return true;
	else if(board[3] == board[4] && board[5] == board[3])
		return true;
	else if(board[6] == board[7] && board[8] == board[6])
		return true;
	else if(board[0] == board[3] && board[6] == board[0])
		return true;
	else if(board[1] == board[4] && board[7] == board[1])
		return true;
	else if(board[2] == board[5] && board[8] == board[2])
		return true;
	else if(board[0] == board[4] && board[8] == board[0])
		return true;
	else if(board[2] == board[4] && board[6] == board[2])
		return true;
	else
		return false;
}
void move(bool who)
{
	int spot;
	if(who == true)
		cout << "\nEnter your move Player 1: ";
	else
		cout << "\nEnter your move Player 2: ";
	cin >> spot;
	if(isLegal(spot))
	{
		if(who == true)
			board[spot-1] = 'X';
		else
			board[spot-1] = 'O';
	}
	else
		move(who);
	displayBoard();
}
bool isLegal(int spot)
{
	if(board[spot-1] == 'X' || board[spot-1] == 'O')
		return false;
	else
		return true;
}


Other 3 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
12/8/2005 7:38:46 AMBrenton Andrew Saunders

Very nice! Might I suggest a computerized player 2? 5 out of 5 from me!
(If this comment was disrespectful, please report it.)

 
12/9/2005 1:05:23 AM

I liked the code and it ran nice, a computerized player 2 would be nice if there is no other person to play against, and i noticed one problem, when there is a cat(no winner and all the spaces are used up.) the program stills asks for a move from the player, when it should end and tell us that it was a cat. Just something to think about, other than that i liked the code.
(If this comment was disrespectful, please report it.)

 
4/21/2006 4:11:44 PMtim

the program crashes when a number < 1 is entered. it also takes numbers such as 40950 as a spot to place an X / O. It wouldn't be difficult to add a loop, checking to make sure that the number entered was > 0 || < 10 . it also crashes if the number entered is too large. i admit, that entering such numbers is completely illogical, but your program should have something to do in case such an even occurs. i also had a problem with the whole "it's a tie" thing. why was this case not included? its a nice little piece of code, but it needs work.
(If this comment was disrespectful, please report it.)

 
4/21/2006 7:33:41 PMtim

Correction: > 0 && < 10.
(If this comment was disrespectful, please report it.)

 
6/18/2006 12:56:31 PMBob Simmons

also if you enter a word or a character it crashes
(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.