C中的MasterMind游戏

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

题:

Mastermind是两个玩家的游戏。 在开始时,第一个玩家决定一个秘密密钥,这是一个序列(s1,s2,… sk),其中0 <si <= n,然后第二个玩家进行轮次猜测,其中每个猜测都是形式的(g1, g2,… gk),并且在每次猜测之后,第一个玩家计算猜测的分数。 猜测的分数等于我们有gi = si的i的数量。

例如,如果密钥是(4,2,5,3,1)并且猜测是(1,2,3,7,1),那么得分是2,因为g2 = s2并且g5 = s5。

给定一系列猜测和每个猜测的分数,您的程序必须确定是否存在至少一个生成这些精确分数的密钥。

输入:

第一行输入包含单个整数C(1 <= C <= 100)。 C测试案例如下。 每个测试用例的第一行包含三个整数n,k和q。 (1 <= n,k <= 11,1 <= q <= 8)。 接下来的q行包含猜测。

每个猜测由k个整数gi,1,gi,2,…. gi,k由单个空格分隔,然后是猜测bi的得分(1 <= gi,j <= n,对于所有1 <= i <= q,1 <= j <= k;且0 <= bi <= k)

输出:

对于每个测试用例,如果至少存在生成那些精确分数的密钥,则输出“是”(不带引号),否则输出“否”。

我写的代码是这样的:

#include  #include  #include  #include  #include  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 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 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:错误:’}’令牌之前的预期声明

除此之外,我的逻辑是否正确? 我犯过任何重大错误吗?

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

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

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

编辑:实际上,看起来源代码既不是C也不是C ++。 选一个并坚持下去。 它几乎是C,您需要将#include 更改为#include 并且您需要为bool类型添加#include 。 您还需要将YESNO更改为truefalse

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

在C ++中, this是一个关键字,用作指向类成员函数内当前对象的指针。

您必须使用该语言未保留的其他名称。

或者,如果这实际上是C代码,则不包括等C ++标头,并将.cc的文件重命名为.c

选择你的语言!

您不能在C ++中将其用作变量名。 这是一个语言关键字。 如果要编译C程序,请不要使用C ++编译器。