View Single Post
  #1 (permalink)  
Old 06-04-2007, 09:11 AM
flupke1 flupke1 is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Rep Power: 0
flupke1 is on a distinguished road
Default need help with simple C++ TicTacToe game with AI

Hi there,

I'm a Dutch student (and a complete newbie to programming) working on an assignment for school. The assignment is to program a basic tic tac toe game (using visual C++ 6.0 or 2003 .NET) for one player against the computer. I have to use basic programming, only loops and basic functions etc. I don't need to have a great looking GUI. I only have to get the game working.

I'm stuck on two problems:

1: Got a bug in the main function when the program is running and it's a tie.

2: I can't seem to find the solution to build a AI computer player.

Got the game working for 2 players so far, but can anybody please help me with the bug (when it's a tie) or to get the computer player working???

I would be very gratefull!

tnx Flupke

p.s. here's my code for so far:


Code:
#include <iostream>
using namespace std;

char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
void drawBoard();

bool checkWinner();
void movePlayer(bool);
void moveComputer(bool);
bool possibleMove(int);

void main()
    {
    bool player = false;		/* true = X , false = O */
	int i= 0;

	drawBoard();

	while(!checkWinner())
		if ( i==0 || i==2  || i==4 || i==6 || i==8 )
		{
			i++;
			{
			if(player == true)
			    player = false;
			else
		        player = true;
		        movePlayer(player);
			}
		}
	    else if ( i==1 || i == 3  || i==5 || i==7 )
		{
			i++;
			{
			if(player == true)
			    player = false;
			else
		        player = true;
		        moveComputer(player);
			}
		}
		else
		cout << " It's a TIE !!! " << endl;
		cin.get();

    
    if(player == true)
		cout << "Player one WINS !!!" << endl;
	else
		cout << "The computer WINS !!!" << endl;
    }
    

void drawBoard()
    {
   		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 checkWinner()
    {
			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 movePlayer(bool whichPlayer)
	{
        int place;
        cout << "\n Player one, make your move: ";
        cin >> place;
        if(possibleMove(place))
            {
				if(whichPlayer == true)
				board[place-1] = 'X';
				else
				board[place-1] = 'O';
            }
        else
            movePlayer(whichPlayer);
            drawBoard();
    }
   
void moveComputer(bool whichPlayer)
	{
        int place;
        cout << "\n Computer, make your move: ";
        cin >> place;
        if(possibleMove(place))
            {
				if(whichPlayer == true)
				board[place-1] = 'X';
				else
				board[place-1] = 'O';
            }
        else
            moveComputer(whichPlayer);
            drawBoard();
    }

bool possibleMove(int place)
    {
		if(board[place-1] == 'X' || board[place-1] == 'O')
        return false;
        else
        return true;
    }

Last edited by v0id; 06-04-2007 at 09:17 AM. Reason: Added code-tags.
Reply With Quote

Sponsored Links