C |两组数组之间的比较

时间:2016-01-09 18:56:03

标签: c arrays

我在计算机世界里很新鲜,并开始研究c。 现在我正在构建一个关于生成代码的小项目(如下所示):

我的主要人物:

#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");           
}

要连接的头文件:

char levels (int level);

“级别”功能:

#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]);
}

现在我的问题在于“水平”粘贴的ranGen功能。

我需要在1-6之间生成4个不同的随机数 (1234是好的,1878不行,2234也不行)并且用户将猜测数字,而如果他做了正确的数字,我可以说:

generated code = 2345
guess = 1364 

用户将获得:1 HIT和1 MISS,而如果用户输入2222,他将获得1 HIT和3 MISSES。

现在我很失落,任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我在你的ranGen()函数中看到了问题......

你有......

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

ranCode [3]为您提供该数组中的3个元素。数组元素从零开始计数,这样就可以得到ranCode [0],ranCode [1]和ranCode [2]。如果你想要ranCode [3]和代码[3],那么你需要将上面一行改为......

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

它为您提供4个元素,编号从0到3(0,1,2和3等于4个数字)。

我也不确定你要做什么......

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

...在您的程序中,它将首先评估括号内的内容,因此6 - 1 = 5,+ 1 = 6.所以从本质上讲,该行也将看起来像ranCode [0] = rand() %6;到编译器并将给出一个从0到5的数字。如果你想在结果中添加一个,你可以使用:ranCode [0] =(rand()%6)+ 1;,这将首先执行随机数,然后为它添加1到6(这是我认为你想要的骰子卷?)。

无论如何,你将code4传递给一个不存在的元素,超出范围并可能在某处破坏内存,这会导致未定义的行为,可能导致系统崩溃或影响另一个变量的内存等。