0

I'm pretty fresh in the computers world and started to work on c. now i'm building a little project about generating code (shown below):

My main:

#include <stdio.h>
#include "connect.h"
#define EASY 20
#define NORMAL 15
#define HARD 10
#define CRAZY 30
int main ()
{
    int choice = 1;
    char enter, returnV;
    system("cls");
    printf("Welcome to ""THE MAGSHIMIM CODE BREAKER""!!!\n");
    printf("\n");
    printf("A secret password was chosen to protect the credit card of Pancratius,\nthe descendant of Antiochus\n");
    printf("Your mission is to stop Pancratius by revealing his secret password.\n");
    printf("the rules are the follows:\n");
    printf("\n");
    printf("1. In each round you try to guess the secret password (4 distinct digits)\n");
    printf("2. After every guess you'll receive two hints about the password");
    printf("\nHITS    the number of digits in your guess witch were exactly right.\n");
    printf("MISSES  the number of digits in your guess witch belong to the password but weremiss-placed\n");
    printf("3 - if you'll fail to guess the password after a certain number of rounds \tPancratius will buy all the gift to Hanukkah!!!\n");
    printf("\n");
    printf("Press Enter to continue...\n");
    enter = getch();
    if (enter = '\n'){
        do{
             system("cls");
             printf("please choose level:\n");
             printf("1 - easy (20 rounds)\n");
             printf("2 - normal (15 rounds)\n");
             printf("3 - hard (10 rounds)\n");
             printf("4 - crazy (random number of rounds 5-25)\n");
             printf("Make a choice:");
             _flushall();
             scanf("%1d",&choice);         
             if (choice == 1 || choice == 2 || choice == 3 || choice == 4 ){
                 switch(choice){
                     case (1):
                         system("cls");
                         returnV = levels(EASY);
                         break;
                     case (2):
                         system("cls");
                         returnV = levels(NORMAL);
                         break;
                     case (3):
                         system("cls");
                         returnV = levels(HARD);
                         break;        
                     case (4):
                         system("cls");
                         returnV = levels(CRAZY);
                         break;        
                 switch(returnV){
                     case('y'):
                         break;
                     case('n'):
                         return 0;
                         break;                       
                 }    
                }
            }
             else if (choice != 1 || choice != 2 || choice != 3 || choice != 4 ){
                 system("cls");
                 printf("please choose level:\n");
                 printf("1 - easy (20 rounds)\n");
                 printf("2 - normal (15 rounds)\n");
                 printf("3 - hard (10 rounds)\n");
                 printf("4 - crazy (random number of rounds 5-25)\n");
                 printf("Make a choice:");
                 _flushall();
                 scanf("%1d",&choice);
             }
        } while (returnV != 'n');           
    }  
    system("PAUSE");           
}

header file to connect:

char levels (int level);

"levels" function:

#include <stdio.h>
#include "connect.h"
#include <time.h>
#include <stdlib.h>
char cases (int myCase, char crazyl);
char levels (int level) {       
    char crazyl = 'n',playerChoice;
    switch (level){
         case (20):
             playerChoice = cases (level,crazyl);
             printf ("\n%c\n",playerChoice);
             return playerChoice;
             break;
         case (15):
             playerChoice = cases (level,crazyl);
             return playerChoice;
             break;
         case (10):
             playerChoice = cases (level,crazyl);
             return playerChoice;
             break;
         case (30):
             crazyl = 'x';
             level = rand() % (25 - 5 + 1);
             playerChoice = cases (level,crazyl);
             return playerChoice;
     }           
}

int ranGen (int code1,int code2, int code3, int code4);
char cases (int level,char crazyl)
{
    unsigned int check;
    char choise = 't',code1,code2,code3,code4;
    if(level > 0){
       while (level > 0){
            if(crazyl == 'n'){
            printf("Write your guess (only 1-6, no ENTER is needed) [%d guess left]\n",level);
            }
            else if (crazyl == 'x'){
           printf("Write your guess (only 1-6, no ENTER is needed) [xxx guess left]\n");
             }

    code1 = getch();
    printf("%c",code1);
    code2 = getch();
    printf("\t%c",code2);
    code3 = getch();
    printf("\t%c\t",code3);
    code4 = getch();
    printf("%c\n",code4);      
    if(code1 > '0' && code1 < '7' && code2 > '0' && code2 < '7' && code3 > '0' && code3 < '7' && code4 > '0' && code4 < '7'){
         check = ranGen(code1,code2,code3,code4);
         level--;
    } else {
         printf("\nnot a good number only numbers between 1-6\n");
         level--;
    }
    if (level == 0) {
        printf("FOR GOD SAKE WE LOST!!!\n");
        printf("The secret password was:\n");
        while (choise != 'y' || choise != 'n') {
            if (choise != 'y' || choise != 'n') {
                 printf("keep playing? (y/n):");
                 _flushall();
                 scanf("%1c",&choise);
                   if (choise == 'y' || choise == 'n' ) {
                       return choise;
                   } else { 
                       continue;
                   }
            }
        }

    }      
        }
    }  
}

int ranGen(int code1,int code2, int code3, int code4) {
    unsigned int ranCode[3],check,code[3];
    code[0] = (int) code1 - 48;
    code[1] = (int) code2 - 48;
    code[2] = (int) code3 - 48;
    code[3] = (int) code4 - 48;       
    do {       
        ranCode[0] = rand() % (6 - 1 + 1);
        ranCode[1] = rand() % (6 - 1 + 1);
        ranCode[2] = rand() % (6 - 1 + 1);
        ranCode[3] = rand() % (6 - 1 + 1);
    } while (code[0] != ranCode[0] || code[1] != ranCode[1] || code[2] != ranCode[2] || code[3] != ranCode[3]);
}

Now my problem is in ranGen function at "levels" paste.

I need to Generate 4 random numbers between 1-6 that different from each other (1234 is ok, 1878 not ok, 2234 not ok either) and that the user will guess the digits while if he made a correct digits lets say:

generated code = 2345
guess = 1364 

The user will get: 1 HIT and 1 MISS while if user will input 2222 he will get 1 HIT and 3 MISSES.

Now im pretty lost here and any help will be great.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
The baron
  • 35
  • 6
  • You must post the code directly here. – Iharob Al Asimi Jan 09 '16 at 18:58
  • 1
    Better yet, post _only_ what is necessary to illustrate your specific problem. A _[***Short***, Self Contained, Correct Compilable, Example](http://sscce.org/)_ does not mean you have to post everything. – ryyker Jan 09 '16 at 19:03
  • 1
    Also, your HIT/MISS criteria is not clear. Please restate it in an unambiguous way. – ryyker Jan 09 '16 at 19:05
  • `if (choice == 1 || choice == 2 || choice == 3 || choice == 4)` is not needed, read about `default` in `switch` statement. – Iharob Al Asimi Jan 09 '16 at 19:07
  • 1
    Generate 4 different values from 6? See [recently answered SO question](http://stackoverflow.com/questions/34569182/randomize-4-different-numbers/34569282) by @iharob and yours truly. – Weather Vane Jan 09 '16 at 19:07
  • 1
    So your question is really how to pick four unique digits at random and the rest is just scaffolding that hasn't got anything to do with your question? You might want to delete the code and put the real question at the top. – M Oehm Jan 09 '16 at 19:08
  • Not directly related to your question, but `"Welcome to ""THE MAGSHIMIM CODE BREAKER""!!!\n"` is actually a concatenation of three strings; the doubled-up double quotes will vanish. If you need a double-quote inside double-qouted strings, try `\"`. – M Oehm Jan 09 '16 at 19:12
  • I believe you are confusing the terms *digits* and *numbers* in your last paragraph. Other than that, I do not fully understand the rules that define how HITS and MISSES are assigned. Can you elaborate more on this? – morido Jan 09 '16 at 19:20
  • `case` is a label statement. Do not add parenthesis around the labels. – too honest for this site Jan 09 '16 at 19:35

1 Answers1

0

I see the problems in your ranGen() function...

you have...

unsigned int ranCode[3],check,code[3];

ranCode[3] gives you 3 elements in that array. Array elements start counting at zero, so that would give you ranCode[0], ranCode[1] and ranCode[2]. If you want ranCode[3] and code[3], than you need to change the above line to...

unsigned int ranCode[4],check,code[4];

That gives you 4 elements, numbered from 0 to 3 (0, 1, 2 & 3 equals 4 numbers).

I'm also not certain what you are trying to do with...

ranCode[0] = rand() % (6 - 1 + 1);

...in your program, it will evaluate what is inside the brackets first, so 6 - 1 = 5, + 1 = 6. So in essence, that line will also look like ranCode[0] = rand() % 6; to the compiler and will give a number from 0 to 5. If you wish to add one to the result, you can use: ranCode[0] = (rand() % 6) + 1;, that would do the random number first, then add one to it for 1 to 6 (which is what I assume you wanted for a dice roll?).

Anyhow, you're passing code4 to a nonexistent element, out of range and probably corrupting memory somewhere, which would have undefined behaviour, possibly crash the system, or effect another variable's memory etc.

Neil Roy
  • 603
  • 5
  • 13