Please help!

This is a discussion on Please help! within the C++ Programming forums, part of the General Programming Boards category; This is my code: Code: #include <iostream.h> #include <stdio.h> #include <time.h> #include <stdlib.h> char playagain='y'; char number; char array[10]; char ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Please help!

    This is my code:
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    char playagain='y';
    char number;
    char array[10];
    char i='1';
    char compnumber;
    int main(void)
    {
    	array[1]='1';
    	array[2]='2';
    	array[3]='3';
    	array[4]='4';
    	array[5]='5';
    	array[6]='6';
    	array[7]='7';
    	array[8]='8';
    	array[9]='9';
    	srand(time(NULL));
    	compnumber=rand()%10;
    	while(playagain=='y')
    	{
    	i=1;
    	cout << array[1];
    	cout << "|";
    	cout << array[2];
    	cout << "|";
    	cout << array[3];
    	cout << "\n-----\n";
    	cout << array[4];
    	cout << "|";
    	cout << array[5];
    	cout << "|";
    	cout << array[6];
    	cout << "\n-----\n";
    	cout << array[7];
    	cout << "|";
    	cout << array[8];
    	cout << "|";
    	cout << array[9];
    	cout << "\n";
    	cout << "Please enter the number of the space you would like to place an X in: ";
    	cin >> number;
    	for(i=i-'0';i<10;(int)i++) // Player number calculation
    	{
    		if(number==i+'0' && array[i]!=i+'0') //Space already taken
    		{
    			cout << "That space is taken!\n";
    		}
    		if(number==i+'0' && array[i]==i+'0') //Assigns X
    		{
    			array[i]='X';
    		}
    	}
    	if((array[1]=='X' && array[2]=='X') || (array[1]=='O' && array[2]=='O')) //Computer AI. Stops player win or finishes computer win.
    	{
    		compnumber='3';
    	}
    	if((array[1]=='X' && array[5]=='X') || (array[1]=='O' && array[5]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[1]=='X' && array[4]=='X') || (array[1]=='O' && array[4]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[2]=='X' && array[5]=='X') || (array[2]=='O' && array[5]=='O'))
    	{
    		compnumber='8';
    	}
    	if((array[3]=='X' && array[2]=='X') || (array[3]=='O' && array[2]=='O'))
    	{
    		compnumber='1';
    	}
    	if((array[3]=='X' && array[5]=='X') || (array[3]=='O' && array[5]=='O'))
    	{
    		compnumber='7';
    	}
    	if((array[3]=='X' && array[6]=='X') || (array[3]=='O' && array[6]=='O'))
    	{
    		compnumber='9';
    	}
    	if((array[4]=='X' && array[5]=='X') || (array[4]=='O' && array[5]=='O'))
    	{
    		compnumber='3';
    	}
    	else;
    	{
    	for(i=i-'0';i<10;(int)i++) //Computer random number
    	{
    		while(compnumber==i+'0' && array[i]!=i+'0') //Space taken
    		{
    				compnumber=rand()%10;
    
    		}
    		if(compnumber==i+'0' && array[i]==i+'0') //Assigns O
    		{
    			array[i]='O';
    		}
    	}
    	}
    }
    return 0;
    }
    It's supposed to be a tic-tac-toe game, but the computer won't take a turn unless it fulfills one of the if statements to stop the player from winning, or to make the computer win. The if statements aren't all in there, but I like to make sure everything's working before I do the stuff I know will work. Please help me
    Last edited by Kespoosh; 03-26-2003 at 05:07 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Oh yeah....sorry!

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Are you saying you don't know what to do if none of the if statements are true? Just make the computer play a random move.

    You'll need to change all of your if statements to "else if" though, otherwise the comp can see a win in the first if statement, see a way to stop a human win in the last if statement, and it will stop the player instead of winning. Also, for the comp to play its best, you'll need to break up the checking to see if it wins and checking to see if it can stop a win. First check to see if comp has a winning move ANYWHERE, then check to see if can stop a player win.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Re: Please help!

    Originally posted by Kespoosh
    Code:
    	else;
    	{
    	for(i=i-'0';i<10;(int)i++) //Computer random number
    	{
    		while(compnumber==i+'0' && array[i]!=i+'0') //Space taken
    		{
    				compnumber=rand()%10;
    
    		}
    		if(compnumber==i+'0' && array[i]==i+'0') //Assigns O
    		{
    			array[i]='O';
    		}
    	}
    	}
    }
    return 0;
    }
    I'm trying! It won't play unless an if statement is fulfilled.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    In the code you have written, you have a semicolon after the else which shouldn't be there. Here is a small example from your code:
    Code:
    if((array[1]=='X' && array[2]=='X') || (array[1]=='O' && array[2]=='O'))
    {
       compnumber='3';
    }
    else if((array[1]=='X' && array[5]=='X') || (array[1]=='O' && array[5]=='O'))
    {
       compnumber='9';
    }
    else
    {
       // make a random move
    }

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Will this fix the problem? Or is it just another one of the things I screwed up on? :P

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21