Tic Tac Toe comp move

This is a discussion on Tic Tac Toe comp move within the C++ Programming forums, part of the General Programming Boards category; Hey guys I am making a tic tac toe game and it is 90% finished, but I am not allowed ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Tic Tac Toe comp move

    Hey guys I am making a tic tac toe game and it is 90% finished, but I am not
    allowed to use classes.

    The problem I have is on the computer move. If the computer hit's a sqaure the
    player has already chosen, would it be wise to use a recursive step to pick
    another square that is empty?

    This is the code for a the [0][0] sqaure on the computer move:

    Code:
    // function to simulate the computer's move
    void computerMove ( char brd[][ 3 ], const char *let, const char *cmp ) {
    	int choosePos = ( 1 + rand() % 9 );
    	
    	switch ( choosePos ) {
    		case 1: 
    			{
    				if ( brd[ 0 ][ 0 ] == *let ) {
    				}
    
    				brd[ 0 ][ 0 ] = *cmp;
    			}
    			break;
    The code to exexute this would be placed within the "if statement" that
    is checking if the player already exisits there. Or instead of recursion would
    a true false loop be advisable?

    When I compile and run the code as it is I win since it isnt finished .

    Any advise appricaited.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What you want is the minimax algorithm. Look it up

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    And you'll probably win every time, since there is NO LOGIC to the computer move. Shouldn't the move be based on whether there is a move to block the opponent, if necessary? And if a block is not in order, what square would be best for the computer turn? You need some logic to pick the BEST square for the computer move.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I would say that recursion is wrong here. You might get the same number a lot of times and have a stack overflow. It is a possibility, maybe rare, but it exists so its wrong.

    Use a while loop:
    Code:
    int choosePos;
    while(1)
    choosePos = ( 1 + rand() % 9);
    switch(choosePos)
    {
        case 1:
            if ( brd[ 0 ][ 0 ] != *let) 
                brd[ 0 ][ 0 ] = *cmp;
         ...
    }
    EDIT: Of course you can do this simpler without a switch statement. Like
    Code:
    while(1) {
        x = rand() % 3;
        y = rand() % 3;
        if (brd[x][y] != *let) {
            brd[x][y] = *cmp;
            break;
       }
    }
    and checking of course BEFORE if the game has finished in order not to get a deadlock...
    Last edited by C_ntua; 01-07-2009 at 08:14 AM.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    You should use a self organizing map to let teh computer learn to play the correct moves. Obviously it can only make moves that are legal, but since it will never try these if they arent presented as options, you dont have to worry about it chosing illegal moves.

    I am assuming this is a homework assignment, so check with the instructor if you can use structs, as a struct under c can also have member functions similar to a class, but its not a class.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Thank you guys for the tips and advice. All noted and very much appriciated
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  3. help in tic tac toe with looping only
    By amreena in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 06:42 AM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

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