Thread: Tic Tac Toe game

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    Tic Tac Toe game

    I need help in the player move function. How would I go about storing a 'x' or 'o' in an array and keep it there? As it is, I'm using the integer i as the user input and I don't know how I can take the input, assign it to a cell in the array, and make it stay there. Any help would be great. Thanks.
    Code:
    #include <stdio.h>
    
    /****** here is the info ******/
    
    void info()
    {
     printf("TIC-TAC-TOE\nWritten by Gino Tozzi\n");
     printf("Your Symbol 'x'");
     printf("Computer Symbol 'o'");
    }
    
    /****** initializes board ******/
    
    void init(char cell[])
    {
     int i;
     for (i=0; i>9; i++) cell[i] = ' ';
    }
    
    /****** this prints the gameboard ******/
    
    void gameBoard(char cell[])
    {
     printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
     printf("---|---|---\n");
     printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
     printf("---|---|---\n");
     printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
    }
    
    /****** this handles player moves ******/
    
    void userMove(char cell[])
    {
     int i;
     gameBoard();
     printf("Your Move (0-8): ");
     scanf("%d", &i);
     while ( i < 0 || i > 8 ) 
          { printf("Invalid Move"); userMove(); }
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    in your userMove() function, you're going to need to make 'i' as a static int, so it keeps it's value between calls. Also, your call to gameBoard() is going to need the char cell[] param passed to that function. You should keep the count of the number of moves, not the player. You are also going to need a function to determine if there is a winner, or if there is a tie game and then ask if the player wants to play again.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    What would be a good technique to store and keep information about all the cells? The plan is in the main function, I'm going to switch off between the player function and computer function. As it is, if I do that the value of i is going to keep changing. Am I going to need 8 different integer variables? If that's the case I don't know how I would implement that.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nafix View Post
    What would be a good technique to store and keep information about all the cells? The plan is in the main function, I'm going to switch off between the player function and computer function. As it is, if I do that the value of i is going to keep changing. Am I going to need 8 different integer variables? If that's the case I don't know how I would implement that.
    I don't know what info you think you need to save about the cells. The choice is either to make the board global, or pass the board around to nearly all your functions via it's base address. Once the value is assigned (x or o), then it's not going to have any problems "sticking".

    Where PLAYER, HUMAN and COMP are defined global int's.
    Code:
    if (PLAYER == HUMAN)
       PLAYER = COMP;
    else
       PLAYER = HUMAN;
    As mentioned, you'll probably want to count the moves, to easily tell when the game is a cat game. If the game always starts with the same player moving first, then you can use the move_number % == 0 type logic to figure out who has the next move.

    I suggest the first way however; it's way clearer and more robust.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (i=0; i>9; i++) cell[i] = ' ';
    You want < there, not >.

    Code:
    void userMove(char cell[])
    {
     int i;
     gameBoard();
     printf("Your Move (0-8): ");
     scanf("&#37;d", &i);
     while ( i < 0 || i > 8 ) 
          { printf("Invalid Move"); userMove(); }
    
    }
    There's no need for that function to be recursive. Never mind that it's an infinite loop. Something like this would work just fine. It has slightly better error checking, too.
    Code:
    void userMove(char cell[])
    {
     int i;
     gameBoard();
     printf("Your Move (0-8): ");
     while (scanf("%d", &i) != 1 || i < 0 || i > 8 ) {
      printf("Invalid Move");
     }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (scanf("&#37;d", &i) != 1
    Until something which isn't a number is typed in, then the loop just locks up forever trying to convert the unconvertable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oops . . .

    The trouble with the code is that it doesn't make allowances for invalid input. It assumes that you'll get a number eventually. If you wanted to implement the same thing with a loop, you could use something like this:
    Code:
    for(;;) {
        scanf("&#37;d", &i)
        if(i < 0 || i > 8) {
            printf("Invalid Move");
        }
        else break;
    }
    It would be better, of course, to check the return value of scanf().
    Last edited by dwks; 11-10-2007 at 01:48 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. tic tac toe AI roadblock >:-(|)
    By dark_rocket in forum Game Programming
    Replies: 5
    Last Post: 06-12-2006, 05:13 AM
  4. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  5. not 3x3 tic tac toe game AI
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 08-29-2001, 04:02 AM