VB icon

Basic TicTacToe

Email
Submitted on: 1/8/2015 12:14:00 AM
By: Dan R (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 2939
 
     Just another simple TicTacToe Game
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Basic TicTacToe
// Description:Just another simple TicTacToe Game
// By: Dan R (from psc cd)
//**************************************

//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;
}


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.