Thread: Problem with my game

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Problem with my game

    Hi im new in this forum and I got some problems with my tic-tca-toe game.

    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int i = 0;    
      int player = 0;          
      int go = 0;                 
      int row = 0;                    
      int column = 0;          
      int line = 0;                                
      int winner = 0;       
      char board[3][3] = {                
                           {'1','2','3'},       
                           {'4','5','6'},       
                           {'7','8','9'}        
                         };
    
    
       for( i = 0; i<9 && winner==0; i++)
       {
    
        printf("\n\n");
          printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
          printf("---+---+---\n");
          printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
          printf("---+---+---\n");
          printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
          
          player = i%2 + 1;     
     
       
          do
          {
             printf("\nPlayer %d, please enter the number of the square "
           "where you want to place your %c: ", player,(player==1)?'X':'O');
             scanf("%d", &go);
    
             row = --go/3;                               
             column = go%3;                        
          }while(go<0 || go>9 || board[row][column]>'9');
    
          board[row][column] = (player == 1) ? 'X' : 'O';   
    
          
          if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
             (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
            winner = player;
    
          else
            for(line = 0; line <= 2; line ++)
              if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
                 (board[0][line] == board[1][line] && board[0][line] == board[2][line]))
                winner = player;
          
    
       }
    
       printf("\n\n");
       printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
       printf("---+---+---\n");
       printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
       printf("---+---+---\n");
       printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
    
    
       if(winner == 0)
          printf("\nHow boring, it is a draw\n");
       else
          printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner);
    
    return 0;
    }

    The problem is that when any of the players wins the game dies. I can't find the problem and my friends couldnt find the problem either. I hope that someone here can find the solution to the problem.

    I appreciate all help I can get!

    ~regards

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What do you mean - the game dies? The game loop exits, the program prints its message and quits (perhaps closing the console window).

    The only thing wrong is:
    Code:
             row = --go/3;                               
             column = go&#37;3; 
        }while(go<0 || go>9 || board[row][column]>'9');
    Because of this, user input 10 passes the first two validations but causes the blue check to access out of bounds.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2
    Quote Originally Posted by anon View Post
    What do you mean - the game dies? The game loop exits, the program prints its message and quits (perhaps closing the console window).
    Just as you said, the program closes the console window. It prints the message and quits, the players wont be able to read the last message that the program prints.

    I dont want the program to close itself. It should print the last message telling who is the winner of the game and then ask them if they want to play again or quit the game.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After each scanf(), put a getchar() to pull off the remaining newline char.

    Then, just before the game should end, print "Program Finished, Press Enter When Ready", message, and add one more line of "getchar().

    If you want the game to loop back to a menu so it can be played again, then make it do that. That's the normal design for games.

    int main


    menu function
    initialization depending on menu choices made
    game play
    after game clean up
    back to top of menu function
    Last edited by Adak; 05-25-2008 at 09:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 10:49 AM
  2. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  3. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Problem with a game im making
    By TheGr8one in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 07:38 PM