Advertisement
6_2008-2009 Games #209408

Basic TicTacToe

Just another simple TicTacToe Game

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
//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;
}
Original Comments (3)
Recovered from Wayback Machine