I need work

This is a discussion on I need work within the C Programming forums, part of the General Programming Boards category; I just finnshed my C book and I don't know what to do with everything I know! Can someone plz ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Post I need work

    I just finnshed my C book and I don't know what to do with everything I know! Can someone plz find something for me to do?

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Make a cool (not simple and lame) tic tac toe game.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    I can't I don't know how to use gfx....

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Then do it with text. Try to program in some AI.
    Away.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    :\ ... I don't know how I'm going to do that...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are some example of tic-tac-toe code on these forums (where others have posted their problems), try searching some out.

    As for the AI part, don't worry about that until you've got the basic game running. Then you can do some more research
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    The AI is stupid, the coordinate system is counter intuitive, and it's in C++, but it should give you a decent idea.

    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 */
    
    	system("cls");
      
      } while(done== ' ');
      
    
      system("cls");
    
      disp_matrix();
      
      if(done=='X') 
    	  cout << "You won!" << endl;
    
      else 
    	  cout << "I won!!!!" << endl;
    
      
    
      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) 
    		printf("\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 ' ';
    }

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by volk
    ... and it's in C++...
    This is an opinion:
    Is it C++? Just because you have a couple cout and cin statements does not make it a C++ program. It looks like standard C to me.

    Make a class, redesign your functions into methods of that class, and you'll be closer.

    Walt

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    This is an opinion:
    Is it C++? Just because you have a couple cout and cin statements does not make it a C++ program. It looks like standard C to me.

    Make a class, redesign your functions into methods of that class, and you'll be closer.

    Walt
    cout and cin are classes. That makes it C++. There you have your answer. Furthermore, "<<" and ">>" are over ridden operators. Again, C++ code.

    Just because someone who knows C could understand the code, doesn't mean it "isn't C++".

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by quzah
    cout and cin are classes. That makes it C++. There you have your answer. Furthermore, "<<" and ">>" are over ridden operators. Again, C++ code.
    I can accept that definition if you want to be a purist about it. I just question the 'intent' that simply changing a printf into cout converts a program into C++ code. It only requires a C++ compiler but the code written is still C.

    My definition of C++ is a little different. If only I/O is C++ and there are no other classes/methods used (either user-defined or other language-defined) I wouldn't count it as C++, as the above code is written.

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)

    Just an opinion, take it or leave it...
    Walt

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Talking

    YAY! The code works! No errors(on my compiler it usally gets errors on code I get off the internet), now it's time to see HOW it works...

    ...could someone make a C version?

  12. #12
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    Re: I need work

    Originally posted by Budgiekarl
    I just finnshed my C book and I don't know what to do with everything I know! Can someone plz find something for me to do?
    Originally posted by Budgiekarl ...could someone make a C version?
    Now you make your own C version!
    The keyboard is the standard device used to cause computer errors!

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Actually the check() function has a logical error in it that means it doesn't work like it is meant to. Maybe Budgiekarl could correct this function whilst taking on the task of converting the code to C
    DavT
    -----------------------------------------------

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    41
    What worng with check()? I don't see anything... and yes, I will try to convert it(C++ is really close to C!)

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Try the moves:

    HUMAN: 3 1
    COMPUTER: 1 1
    HUMAN: 3 2
    COMPUTER: 1 2
    HUMAN: 3 3 (Human should win at this point)
    COMPUTER: 1 3

    Computer claims victory (cheeky bugger!) I'll leave it to you to work out why....
    DavT
    -----------------------------------------------

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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