Thread: Tic-tac-toe implementation problems

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't want to sound mean, but you still have such a crazy amount of redundacy in your code that it kind of leaps out at you. I think you are going to have trouble getting very far with this until you can spot that.

    When tabstop writes:
    Quote Originally Posted by tabstop View Post
    Obviously you got a little carried away with the cut and paste there, as they should check the different spots. (That should have been your hint that all you needed to do was
    Code:
    if (board[choice-1]=='X')
    I don't think s/he just meant that you needed to renumber the case statements because seven of them were identical. S/he meant that you might as well COMPLETELY ERASE THE ENTIRE SWITCH CASE STATEMENT and do this:

    Code:
    if (board[choice-1]=='X') printf("That space is taken,choose anohter");
    But there's the O's too! So maybe:

    Code:
    if (board[choice-1]=='X' || board[choice-1]=='O') printf("That space is taken,choose anohter");
    And to make use of the drawBoard function, you do this:
    Code:
    void drawBoard(char *board){
        printf("\n");
        printf("\t %c | %c | %c \n",board[0],board[1],board[2]);
        printf("\t---|---|--- \n");
        printf("\t %c | %c | %c \n",board[3],board[4],board[5]);
        printf("\t---|---|--- \n");
        printf("\t %c | %c | %c \n\n",board[6],board[7],board[8]);
        
        return;
    }
    // of course invoke it with:
    drawBoard(board);
    
    Another example of redundancy would be in the getXorO function, you ask for a choice, include some error checking, and then include a while loop to catch bad choices that does exactly the same thing. If you initialize intialChoice to zero, all you need is the while loop, because 0 !=1 and 0 !=2. While we are there, maybe it's unnecessary to cancel my game because I hit the wrong key, so -- your choice -- you could change the if a bit:

    Code:
    int getXorO(void){//player decides to be X or O here and returns the initialChoice to main
       
        int initialChoice=0;
           
        printf("Let's play TIC-TAC-TOE!\n\n");
        
        while(initialChoice!=1 && initialChoice!=2){
            
             printf("Do you want to be X or O (X moves first)? \n");
             printf("Enter 1 for X and 2 for O:");
             if(scanf("%i",&initialChoice)==1) break;
        }      
        return initialChoice;
        
    }//getMove returns initialChoice to be X or O
    Anyway, while I don't mean to just "tell you what to do", consider who you want repairing your car:

    1) an abstract expressionist
    2) a mechanic

    Try and rework what you've got down to about 50 lines and you will be in a much better position. It may seem like a tedious difference now, but it won't be if you multiply the size of your program by five or ten (or twenty, or as much as you like). You are also unlikely to find any performance advantages going the long route.
    Last edited by MK27; 10-26-2008 at 02:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Thanks for all the help and advice. I am a very novice programmer so a lot of things you guys are pointing out,on my own, I would not see them. I really really appreciate it. However, I am enjoying my self and learning a lot -even when I find myself stuck and frustrated.

    Question: Is java much harder to learn than C?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you are struggling with is probably only partly the language. The other half of the battle is to learn how to program. This is much harder, than to learn a language.

    C is not the easiest language to learn, mostly because it is not very "automatic" - you have to give it instructions on how to do everything, and it doesn't have much of a safetynet, so when you do something wrong, you won't be nicely told "you tried to access out of bounds in array A", but rather one of two things happen: 1. your program just shows some really weird behaviour (such as giving strange results), or 2. your program crashes. Many other languages have a nicer way to tell you that you've done things wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  3. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  4. Tic tac toe game
    By Seth in forum C Programming
    Replies: 9
    Last Post: 05-04-2003, 09:21 AM
  5. problems with my tic tac toe game
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2002, 08:59 PM