Thread: Code Breaker

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    Question Code Breaker

    Hi! I have this Code Breaker thing to do and I don't know what's wrong. I'm just a newbie, please help me out.
    Here's the instruction:
    The program would initially generate 4 distinct random digits number (no 2 digits are the same / unique). Then the user need to guess the random number. The program would give clues by giving 'black' and 'white' markers.
    (black marker - exact number in correct digit position
    white marker - exact number in wrong position)
    The user would also choose the level of difficulty he/she prefers (easy - 10 chances; average - 7 chances; difficult - 5 chances)

    So far, here's what I've done (no code regarding difficulty yet 'coz the basic still doesn't work )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main(void)  {
      int i, stime;
      long ltime;
      int x, j;
      int black = 0;
      int white = 0;
      int code[4];
      int easy[4];
      ltime = time(NULL);
      stime = (unsigned) ltime/2;
      srand(stime);
      code[i] = rand() % (9999-1000+1) + 1000;
      printf("%d", code[i]);
      printf("\n**CODE BREAKER**\n");
      printf("CHANCE #1\n");
      printf("Enter Guess: ");
      scanf("%d", &easy[4]);
      if(code[0] == easy[0]) {
        black = black + 1;
      }
      for(i = 1; i < 4; i++) {
        if(code[0] == easy[i]) {
          white = white + 1;
        }
      }
      if(code[1] == easy[1]) {
        black = black + 1;
      }
      if(code[1] == easy[0]) {
        white = white + 1;
      }
      for(i = 2; i < 4; i++) {
        if(code[1] == easy[i]) {
          white = white + 1;
        }
      }
      if(code[2] == easy[2]) {
        black = black + 1;
      }
      if(code[2] == easy[3]) {
        white = white +1;
      }
      for(i = 0; i < 2; i++) {
        if(code[2] == easy[i]) {
          white = white + 1;
        }
      }
      if(code[3] == easy[3]) {
        black = black + 1;
      }
      for(i = 0; i < 3; i++) {
        if(code[3] == easy[i]) {
          white = white + 1;
        }
      }
      if(black == 4) {
        printf("Congratulations! You guessed the number right!\n");
      } else {
        printf("%d Black Marks\n%d White Marks", black, white);
      }
    }
    Last edited by Salem; 01-25-2014 at 05:32 AM. Reason: Added code tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    First problem I see is you say you want 4 random numbers but you are only calling rand() once. Second problem is you are assigning to code[i] but you never gave i a value. This is equivalent to saying code[<insert any old integer here>]=...; This makes everything that happens involving code[0], code[1], code[2] etc... undefined.

    Third problem: no indentation or code tags so your code is hard to study. Fourth problem: you are not compiling with warnings turned on (if you were you probably would have received a warning about not initializing i).

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I got to say it and it's hard for me
    You got me crying like I thought I would never be
    Love is believing, but you let me down
    How can I love you when you ain't around

    And I get to the morning and you never call
    Love should be everything or not at all
    And it don't matter whatever you do
    I made a life out of loving you

    Only to find any dream that I follow is dying
    I'm crying in the rain
    I could be searching my world for a love everlasting
    Feeling no pain
    When will we meet again

    Why do you have to be a codebreaker
    Is it a lesson that I never knew
    Got to get out of the spell that I'm under
    My love for you

    Why do you have to be a codebreaker
    When I was being what you want me to be
    Suddenly, everything I ever wanted
    Has passed me by
    This world may end
    Not you and I

    My love is stronger than the universe
    My soul is crying for you and that cannot be reversed
    You made the rules and you could not see
    You made a life out of hurting me

    Out of my mind, I am held by the power of you, love
    Tell me when do we try
    Or should we say goodbye

    Why do you have to be a codebreaker
    When I was being what you want me to be
    Suddenly, everything I ever wanted
    Has passed me by

    Oh, why do you have to be a codebreaker
    Is it a lesson that I never knew
    Suddenly, everything I ever wanted
    My love for you

    Oh, why do you have to be a codebreaker
    When I was being what you want me to be
    Suddenly everything I ever wanted
    Has passed me by

    Tell me, why do you have to be a codebreaker

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Code:
    code[i] = rand() % (9999-1000+1) + 1000;
    how exactly does this generate four random numbers?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    One of the stranger things going on is dividing the return value of time by 2 before using it as a seed to srand. I can't imagine the rationale for that. And then casting it as unsigned but assigning it to a signed int. Curiouser and curiouser. The standard idiom is:
    Code:
        srand((unsigned)time(NULL));
    The line with rand is generating a random number between 1000 and 9999, in an attempt to generate all four digits at once. Not only does this procedure allow duplicate digits (contradicating the instructions) but it does not allow the initial digit to be 0. Then, as C99 indicated, it's assigned to lala land. You need to generate and assign the digits separately, testing for duplicates. (Or perhaps a shuffle of the 10 digits and take the first 4.)

    However, the first thing to do is to come up with an overall structure for your program. You will need a loop to allow for multiple guesses. And some functions would be nice. Your main might look something like this:
    Code:
    int main(void) {
        int code[NUM_DIGITS];
        int guess[NUM_DIGITS];
        int num_guesses = 0;
        int max_guesses;
        int correct;
    
        srand((unsigned)time(NULL));
        max_guesses = chooseLevel();
        generateCode(code);
    
        do {
            num_guesses++;
            getGuess(guess, num_guesses);
            correct = testGuess(code, guess);
        } while (correct < NUM_DIGITS && num_guesses < max_guesses);
    
        return 0;
    }
    The above is not perfect, but it's a start.

    And now, some music:

    Work so hard I couldn't unwind, get some money saved;
    Abuse my love a thousand times, however hard I tried.
    Codebreaker, your time has come, can't take your evil way;
    Go away, Codebreaker.
    Codebreaker!
    Codebreaker!
    Code!

    LED ZEPPELIN - HEARTBREAKER - YouTube
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. Tie-breaker!
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 08-12-2002, 11:20 PM
  4. array breaker
    By alokin in forum C Programming
    Replies: 6
    Last Post: 05-02-2002, 07:34 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread