c - C语言的MasterMind游戏

标签 c

<分区>

我把这个作为我的作业。编码有点麻烦。我使用 C 作为语言。

问题:

Mastermind is a game of two players. In the beginning, first player decides a secret key, which is a sequence (s1,s2,...sk) where 0 < si <= n, Then second player makes guesses in rounds, where each guess is of form (g1,g2, ...gk), and after each guess first player calculates the score for the guess. Score for a guess is equal to number of i's for which we have gi = si.

For example if the secret key is (4,2,5,3,1) and the guess is (1,2,3,7,1),then the score is 2, because g2 = s2 and g5 = s5.

Given a sequence of guesses, and scores for each guess, your program must decide if there exists at least one secret key that generates those exact scores.

输入:

First line of input contains a single integer C (1 <=C <= 100). C test-cases follow. First line of each test-case contains three integers n,k and q. (1 <=n,k <=11, 1<=q<=8). Next q lines contain the guesses.

Each guess consists of k integers gi,1, gi,2,....gi,k separated by a single space, followed by the score for the guess bi (1 <= gi,j <=n for all 1 <=i <=q, 1 <=j <=k; and 0 <= bi <=k )

输出:

For each test-case, output "Yes" (without quotes), if there exists at least a secret key which generates those exact scores, otherwise output "No".

我写的代码是这样的:

#include <stdio.h>
#include <string.h>
#include <stdarg.h>  
#include <stdlib.h>
#include <stdbool.h>

typedef struct set_t{    
    int count;     
    void **values; 
} *SetRef;

SetRef set_create(void *values, ...); 
int  set_count(SetRef this);
bool set_contains(SetRef this, void *value);  
int rscore(SetRef set1, void *value, int score);


int main(int argc,char **argv )
{
    int t = 0, n = 0 ,k = 0,q = 0, score = 0;
    char ch;
    int arr1[n];
    int arr2[n];
    printf("Please enter the number of test cases(between 1 to 100):");
    scanf("%d",&t);
    printf("\n");
    for ( int i = 1; i<=t;i++)
    {
        printf("Please enter values of n,k, q:");
        scanf("%i %i %i",&n, &k, &q);

        printf("\n");
        printf("Enter the values of secret key");
        score = 0;
        for ( int c = 0 ; c < n ; c++ )
        {
            scanf("%d",&arr1[c]);
         }
        printf("\n");
        printf("Enter the values of guess");
            for ( int c = 0 ; c < n ; c++ )
            {
                scanf("%d",&arr2[c]);
             }

        }
       SetRef set1 = set_create(&arr1); 
       SetRef set2 = set_create(&arr2); 
       for ( int i = 0; i < set2->count; i++){
            void *val = set2->values[i]; 
            score = rscore(set1, val,score);
        }        
        if ( score == set1->count)
            printf("Yes");
        else
            printf("No");
        printf("\n");
    }
}

SetRef set_create(void *values, ...) 
{   
    SetRef set = calloc(1, sizeof(struct set_t));    
    if (values)   
    {        
        int count = 1;     
         va_list args;       
         va_start(args, values);       
        while (va_arg(args, void *)) 
        {             
            count++;    
        }          
        va_end(args);       
        set->count = count;      
        set->values = calloc(count, sizeof(void *));      
        set->values[0] = values;    
        va_start(args, values);       
        int i = 1;         
        void *val;       
        while ((val = va_arg(args, void *)))
        {            
            set->values[i++] = val;      
        }       
        va_end(args);    
    }      
    return set; 
}  
int set_count(SetRef this) 
{    
    return this->count; 
} 
bool set_contains(SetRef this, void *value) 
{    
    for (int i = 0; i < this->count; i++)
    {      
        if (value == this->values[i])   
            return true;   
    }    
    return false;
}  

int rscore(SetRef set1, void *value, int score){
    void *val = value;
    if (set_contains(set1, val))
        score ++;
    return score;
}  

我有这些错误:

solution.cc:60:1: error: expected declaration before ‘}’ token

除此之外,我的逻辑是否正确?我是否犯了重大错误?

不知道如何解决这个问题。需要一些指导。

最佳答案

您正在将 C 源代码编译为 C++。 C 和 C++ 是不同的语言。

将文件重命名为 solution.c而不是 solution.cc , 并确保使用 C 编译器进行编译(例如 gcc 而不是 g++ )

编辑: 实际上,源代码看起来既不是 C 也不是 C++。选择一个并坚持下去。它几乎是 C,你需要更改 #include <cstdarg>#include <stdarg.h>你需要添加 #include <stdbool.h>对于 bool类型。您还需要更改 YESNOtruefalse .

bool如果您使用 Microsoft 的 MSC 编译器,则类型可能不可用,该编译器使用过时的 C 版本。

关于c - C语言的MasterMind游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9202302/

相关文章:

C - strtok(...) 上的意外段错误

c - 为什么我的新数组只复制并反转旧数组的前 10 个元素?

c - 如何防止 socket() 系统调用返回文件描述符 0,1 或 2?

c - 在c中获取变量动态

c - 更改矩阵大小后访问未分配的内存

c - 如何更改 GNU Enscript 中的语法高亮颜色?

c - 自由(): invalid next size(normal)

c - 如何在C中保存字符串的最后一个字符?

c - 如何将随机散列减少为 bool 值

C套接字编程: client send() but server select() doesn't see it