Thread: tic tac toe anyone?

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    tic tac toe anyone?

    How do I get the matrix to remain stationary? Oh, and excuse that printf statement on the bottom...it seems easier than using cout.
    Code:
    #include <iostream>
    using namespace std;
    
    char matrix[3][3];  /* the tic tac toe matrix */
    
    char check(void);
    void init_matrix(void);
    void get_player_move(void);
    void get_computer_move(void);
    void disp_matrix(void);
    
    int main()
    {
      char done;
    
      cout << "This is the game of Tic Tac Toe" << endl;
      cout << "You will be playing against the computer" << endl;
    
      done =  ' ';
      init_matrix();
      
      do{
        
    	disp_matrix();
        
    	get_player_move();
        
    	done = check(); /* see if winner */
        
    	if(done!= ' ') 
    		break; /* winner!*/
        
    	get_computer_move();
        
    	done = check(); /* see if winner */
      
      } while(done== ' ');
      
      if(done=='X') 
    	  cout << "You won!" << endl;
    
      else 
    	  cout << "I won!!!!" << endl;
      
      disp_matrix(); /* show final positions */
    
      return 0;
    }
    
    /* Initialize the matrix. */
    void init_matrix(void)
    {
      int i, j;
    
      for(i=0; i<3; i++)
        for(j=0; j<3; j++) matrix[i][j] =  ' ';
    }
    
    /* Get a player's move. */
    void get_player_move(void)
    {
      int x, y;
    
      cout << "Enter X,Y coordinates for your move: ";
      cin  >> x >> y;
    
      x--; y--;
    
      if(matrix[x][y]!= ' '){
        cout << "Invalid move, try again." << endl;
    
        get_player_move();
      }
      else matrix[x][y] = 'X';
    }
    
    /* Get a move from the computer. */
    void get_computer_move(void)
    {
      int i, j;
      for(i=0; i<3; i++){
        for(j=0; j<3; j++)
          if(matrix[i][j]==' ') break;
        if(matrix[i][j]==' ') break;
      }
    
      if(i*j==9)  
      {
        cout << "draw" << endl;
        exit(EXIT_SUCCESS);
      }
      else
        matrix[i][j] = 'O';
    }
    
    /* Display the matrix on the screen. */
    void disp_matrix(void)
    {
      int t;
    
      for(t=0; t<3; t++) {
        printf(" %c | %c | %c ",matrix[t][0],
                matrix[t][1], matrix [t][2]);
        
    	if(t!=2) 
    		cout << "\n---|---|---\n";
      }
      cout << endl;
    }
    
    /* See if there is a winner. */
    char check(void)
    {
      int i;
    
      for(i=0; i<3; i++)  /* check rows */
        if(matrix[i][0]==matrix[i][1] &&
           matrix[i][0]==matrix[i][2]) 
    	   
    	   return matrix[i][0];
    
      for(i=0; i<3; i++)  /* check columns */
        if(matrix[0][i]==matrix[1][i] &&
           matrix[0][i]==matrix[2][i]) 
    	   
    	   return matrix[0][i];
    
      /* test diagonals */
      if(matrix[0][0]==matrix[1][1] &&
         matrix[1][1]==matrix[2][2])
           
    	   return matrix[0][0];
    
      if(matrix[0][2]==matrix[1][1] &&
         matrix[1][1]==matrix[2][0])
           
    	   return matrix[0][2];
    
      return ' ';
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    What do you mean by remain stationary? I'm confused by your question.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by PJYelton
    What do you mean by remain stationary? I'm confused by your question.
    If you look at the output of the code, you'll see that it keeps making a new matrix with the x and o positions. I want it to stop doing that.

    Let me give a brief example using something completely different:

    Instead of remaining stationary to do this:

    ___
    ***
    ***
    ***

    It does this:

    ___
    *
    ___
    **
    ___
    ***
    ___
    ***
    *
    ___
    ***
    **
    ___
    ***
    ***
    ___
    ***
    ***
    *
    ___
    ***
    ***
    **
    ___
    ***
    ***
    ***

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do you mean you want the board to stay in one place on the screen, and get updated with new moves?

    If so, checkout gotoxy or screen clearing
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, I understand. Yeah, probably the best way is to simply clear the screen after every move. There are a number of ways to do this, check out Hammers link above.
    Last edited by PJYelton; 03-24-2003 at 09:09 PM.

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
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM