繁体   English   中英

C | 两组数组之间的比较

[英]C | Comparison between two groups of 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。

现在我很迷失在这里,任何帮助将是巨大的。

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

你有...

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

ranCode [3]为您提供该数组中的3个元素。 数组元素从零开始计数,因此可以得到ranCode [0],ranCode [1]和ranCode [2]。 如果您想要ranCode [3]和code [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传递到不存在的元素,超出范围并可能破坏某处的内存,这将导致未定义的行为,可能使系统崩溃或影响另一个变量的内存等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM