Help with C++ tic tac toe code

Page 1 of 1

0 Replies - 1606 Views - Last Post: 03 December 2007 - 02:43 PM Rate Topic: -----

#1 codekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 07

Help with C++ tic tac toe code

Posted 03 December 2007 - 02:43 PM

i have this tic tac toe program mostly complete but i think there are a few errors in my getusermove and getcomputermove functions..because my code will compile but it will not do anything after i enter my first move and if i enter another number it loops and says computer is moving to o.....any help getting this to run properly would be much appreciated!!!!! :D (i attached my code too)

#include <iostream>
#include <cstdlib>   // For rand function
#include <ctime>	 // For time function
using namespace std;

const int BOARD_SIZE = 9;


void DisplayBoard(const char board[]) 
{
	cout << endl;
	cout << board[0] << " | " << board[1] << " | " << board[2] << endl;
	cout << "-----------\n";
	cout << board[3] << " | " << board[4] << " | " << board[5] << endl;
	cout << "-----------\n";
	cout << board[6] << " | " << board[7] << " | " << board[8] << endl;
	cout << endl;
}

// Write other functions here!
int CheckForWinner (const char board [])
{
	int win=0;
	if (board[0] == 'X' && board[1] =='X' && board[2] =='X'||
		board[3] == 'X' && board[4] =='X' && board[5] =='X'||
		board[6] == 'X' && board[7] =='X' && board[8] =='X'||
		board[0] == 'X' && board[3] =='X' && board[6] =='X'||
		board[1] == 'X' && board[4] =='X' && board[7] =='X'||
		board[2] == 'X' && board[5] =='X' && board[8] =='X'||
		board[0] == 'X' && board[4] =='X' && board[8] =='X'||
		board[2] == 'X' && board[4] =='X' && board[6] =='X')
		return 2;
	else if(board[0] == 'O' && board[1] =='O' && board[2] =='O'||
		board[3] == 'O' && board[4] =='O' && board[5] =='O'||
		board[6] == 'O' && board[7] =='O' && board[8] =='O'||
		board[0] == 'O' && board[3] =='O' && board[6] =='O'||
		board[1] == 'O' && board[4] =='O' && board[7] =='O'||
		board[2] == 'O' && board[5] =='O' && board[8] =='O'||
		board[0] == 'O' && board[4] =='O' && board[8] =='O'||
		board[2] == 'O' && board[4] =='O' && board[6] =='O')
		return 3;
	else if(board[0] == 'X' && board[0] =='O' && board[1] =='X'||
		board[1] == 'O' && board[2] =='X' && board[2] =='O'||
		board[3] == 'X' && board[3] =='O' && board[4] =='X'||
		board[4] == 'O' && board[5] =='X' && board[5] =='O'||
		board[6] == 'X' && board[6] =='O' && board[7] =='X'||
		board[7] == 'O' && board[8] =='X' && board[8] =='O'||)
		return 1;
	else
		return 0;
}
}

void GetUserMove (char board[])
{
	int move=0;
	cout<< "Enter your move: ";
	cin>> move;
	if (move >= 1 && move <=9)
	{
		if (move==1)
			board[0] = 'X';
		else if (move == 2)
			board[1] = 'X';
		else if (move == 3)
			board[2] = 'X';
		else if (move == 4)
			board[3] = 'X';
		else if (move == 5)
			board[4] = 'X';
		else if (move == 6)
			board[5] = 'X';
		else if (move == 7)
			board[6] = 'X';
		else if (move == 8)
			board[7] = 'X';
		else if (move == 9)
			board[8] = 'X';
		else if (move != 9)
			cout<< "This spot is already taken. PLease choose an empty spot: ";
			cin>> move;
	}
	else
	{
		cout<< "Please enter a value 1-9: ";
		cin>> move;
	}
}

void GetComputerMove (char board[])
{ 
	int r= rand () % 9;
	while (board[r-1] != 'X' || board[r-1] != 'O' || board[r-1] > 0 && board[r-1]<9)
	{
		board[r-1]= 'O';
		cout<< "Computer is moving to "<< board[r-1]<<endl;
	}
	 r= rand() % 9;
}


void main()
{	
	char board[BOARD_SIZE] = {'1','2','3','4','5','6','7','8','9'};
	char turn = 'X';	// X always starts first
	int  win = 0;	   // Set to 1, 2, or 3 when game is over

	// Initialize random seed
	srand(unsigned int(time(NULL)));

	// Keep playing game until there's a tie or a winner
	while (win == 0)
	{	
		DisplayBoard(board);

		if (turn == 'X')
		{
			GetUserMove(board);
			turn = 'O';
		}
		else
		{
			GetComputerMove(board);
			turn = 'X';
		}	

		win = CheckForWinner(board);
	}

	DisplayBoard(board);

	cout << endl;
	if (win == 1)
		cout << "It's a tie." << endl;
	else if (win == 2)
		cout << "X wins!!" << endl;
	else if (win == 3)
		cout << "O wins!!" << endl;
	else
		cout << "There is a logic problem!" << endl;
}


Use :code: tags

This post has been edited by jayman9: 03 December 2007 - 05:45 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1