我正在为Mastermind游戏编码。规则不喜欢这些主题:
以下是规则:
随机获得4个数字(所有数字的范围在0到5之间);
您可以通过输入4个数字来猜测这些数字,用空格分隔,(条件与随机数相同);
输入后,您将被告知有多少完美和不完美的匹配 (完美匹配意味着玩家猜测4中任何一个在值和位置上都是正确的,不完美匹配意味着值正确但位置不正确)
你有10次机会,如果你在10次猜测后失败,程序会停止并打印出随机板。否则,如果您正确猜测4个数字,程序将停止并打印出游戏运行时和猜测正确后的尝试次数和时间。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
void MastemindRandom(int random[]);
int * getGuess();
int isValidNumbers(int a, int b, int c, int d);
void run(int * count);
int main(int argc, char** argv) {
time_t begin, end;
int count;
double timeTaken;
time(&begin); //count the time when the program start
run(&count);
time(&end);
if (count != 10) { //if player wins, print out the time taken
timeTaken = difftime(end, begin);
printf("time taken: %f", timeTaken);
}
return (EXIT_SUCCESS);
}
void run(int * count) {
int random[4], *guess = malloc(4);
int imperfect, perfect, flag[4] = { 0, 0, 0, 0 };
MastemindRandom(random); //doing the random
do {
imperfect = perfect = 0;
memset(flag, 0, sizeof(int) * 4);
printf("input your guess: ");
guess = getGuess();
for (int i = 0; i < 4; i++) {
if (random[i] == guess[i]) {
perfect++; //if player guess correctly any of 4 numbers, both in position and value, increase the perfect count and set flag of that position
flag[i] = 1;
continue;
}
for (int j = 0; j < 4; j++) {
if (flag[j] == 1) //if that position has been check before
continue;
if (random[i] == guess[j]) {
imperfect++; //if the player guess correct in value but not correct in position, increase the imperfect and flag it
flag[j] = 1;
break;
}
}
}
printf("you have %d perfect match and %d imperfect match\n", perfect, imperfect);
if (perfect == 4) {
(*count)++;
break;
} else {
printf("sorry you didn't win, better luck next time\n");
(*count)++;
}
} while (*count != 10);
if (*count == 10) {
printf("sorry you don't win the game, the real numbers are: ");
for (int i = 0; i < 4; i++) {
printf("%d ", random[i]);
}
} else {
printf("congratulation, you won after %d tries \n", *count);
}
}
void MastemindRandom(int random[]) {
srand(time(NULL));
for (int i = 0; i < 4; i++) {
random[i] = rand() % +6;
printf("%d ", random[i]);
}
printf("\n");
}
int * getGuess() {
int* numbers = malloc(4);
int a, b, c, d;
do {
scanf("%d %d %d %d", &a, &b, &c, &d);
while (getchar() != '\n'){}
} while (!isValidNumbers(a, b, c, d) && printf("Input from 0 to 5 only, input again: "));
numbers[0] = a;
numbers[1] = b;
numbers[2] = c;
numbers[3] = d;
return numbers;
}
int isValidNumbers(int a, int b, int c, int d) {
if (a < 0 || b < 0 || c < 0 || d < 0 ||
a > 5 || b > 5 || c > 5 || d > 5)
return 0;
else return 1;
}
我的代码运行没有错误,但在某些情况下,完美匹配和不完美匹配的计算是错误的。
这是输出(下面所有输出的前四个数字是我打印出来的随机数字):
当随机没有相等数字时的条件(仍然可以)
3 2 4 5
input your guess: 4 4 4 4
you have 1 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 2 3 5
you have 2 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 3 2 2
you have 0 perfect match and 3 imperfect match
sorry you didn't win, better luck next time
input your guess: 5 2 3 4
you have 1 perfect match and 3 imperfect match
sorry you didn't win, better luck next time
input your guess: 5 5 5 5
you have 1 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 2 3 4
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 3 4
you have 1 perfect match and 1 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 5 2 3
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 3 5 5
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 3 2 2
you have 0 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
sorry you don't win the game, the real numbers are: 3 2 4 5
RUN SUCCESSFUL (total time: 1m 50s)
当随机有两个相等的数字(有一些不正确)时的条件
3 2 3 5
input your guess: 3 3 2 5
you have 2 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 3 3 3
you have 2 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 3 5
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 4 3
you have 1 perfect match and 1 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 2 4 5
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time
input your guess: 5 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time
input your guess: 1 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time
条件当随机有3个相等的数字(有一些不正确)
3 3 3 4
input your guess: 3 4 3 4
you have 3 perfect match and 1 imperfect match //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 4 3 4 3
you have 1 perfect match and 3 imperfect match //wrong, should be 1 perfect and 2 imperfect
sorry you didn't win, better luck next time
input your guess: 4 4 4 3
you have 0 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 3 4
you have 3 perfect match and 1 imperfect match //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 5 3 3 4
you have 3 perfect match and 1 imperfect match //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 2 3 3 4
you have 3 perfect match and 1 imperfect match //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 3 3 3 2
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 2 3 4
you have 2 perfect match and 1 imperfect match //wrong, should be 2 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 3 3 4 4
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 3 2 4
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
sorry you don't win the game, the real numbers are: 3 3 3 4
RUN SUCCESSFUL (total time: 4m 53s)
我不想打印更多,因为基本上,我的逻辑是完全错误的。你能帮助我吗?我将非常感激