C 语言程序 Power of Logic

Power of Logic 是一个推理游戏,玩家的任务是用尽可能少的时间和步骤解开密码序列。当你输入不同的数字序列时,系统会给出提示,指示你输入的数字序列中,有多少个数字正确但位置错误,又有多少个数字和位置都正确。可惜这一游戏已经不再维护和更新了,最新的 iOS 设备上甚至无法下载。不过考虑到它的核心逻辑并不复杂,笔者用 C 语言写了一个低配版。由于能力有限,只借鉴了核心逻辑,没有做图形界面。

运行效果如图:

Power of Logic

下面附上最终版本的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void print() {
printf("Power of Logic for Macintosh,Powered by Mimi 1984,Copyright(C)Mimi 1984\n");
printf("Version 0.9.0Beta2 CHEAT 0000 DEBUG 1111 INFO 2222\n");
printf("Thanks for using the Beta version!\n\n");
}
int game() {
int i, j, n, ref[8], a[4], b[4], times = 0;
srand((unsigned int) time(0));
for (i = 0; i < 8; i++) ref[i] = i;
i = 0;
while (1) {
n = rand() % 8;
if (ref[n] != -1) {
a[i] = n;
ref[n] = -1;
if (++i == 4) break;
}
} //随机数生成,存入a[4]作为答案
input: printf("Input your answer\n"); //返回点input
int x = 0, y = 0, error = 0;
rewind(stdin); //清除键盘缓冲区
for (i = 0; i < 4; i++) {
int check = scanf("%d", &b[i]); //输入数据b[4]
if (check != 1) error += 1;
}
if (error != 0) {
printf("ERROR:Invalid input.\n");
goto input;
} //判断b[4]是否合法(整数)
if (b[0] == 0 && b[1] == 0 && b[2] == 0 && b[3] == 0) {
printf("The correct answer is:");
for (i = 0; i < 4; i++) {
printf(" %d", a[i]);
if (i == 3) printf("\n");
}
goto output;
} //输入作弊码0000,输出答案a[4],结束程序
if (b[0] == 1 && b[1] == 1 && b[2] == 1 && b[3] == 1) {
printf("DEBUG MODE.\nThe correct answer is:");
for (i = 0; i < 4; i++) {
printf(" %d", a[i]);
if (i == 3) printf("\n");
}
goto input;
} //输入调试码1111,输出答案a[4],进入调试模式
if (b[0] == 2 && b[1] == 2 && b[2] == 2 && b[3] == 2) {
printf("Sorry.No English Version Provided.\n");
goto input;
} //输入帮助码2222,查看帮助
for (i = 0; i < 3; i++) {
for (j = i + 1; j < 4; j++) {
if (b[i] == b[j]) {
printf("ERROR:No duplicate numbers allowed.\n");
goto input;
}
}
} //判断b[4]是否合法(重复数)
for (i = 0; i < 3; i++) {
if (b[i] <= -1 || b[i] >= 8) {
printf("ERROR:Only number 0~7 are allowed.\n");
goto input;
}
} //判断b[4]是否合法(范围0~7)
for (i = 0; i < 4; i++) {
if (a[i] == b[i]) x++;
}
printf("%d ", x); //数字正确,位置正确的个数x
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (a[i] == b[j]) y++;
}
}
y -= x;
printf("%d\n", y); //数字正确,位置不正确的个数y
if (times == 10) printf("Come On!You have guessed for too many times!\n");
if (times == 15) {
printf("Oh Fuck!You have guessed for TOO many times!\nThe correct answer is:");
for (i = 0; i < 4; i++) {
printf(" %d", a[i]);
if (i == 3) printf("\n");
}
goto output;
} //猜测次数检查
if (x == 4) {
printf("Bingo!\n");
times += 1;
}
else {
times += 1;
goto input;
} //全部猜对,结束程序,否则重猜
output: printf("Have Fun!\n"); //返回点output
return times; //猜测次数作为返回值
}
int main(void) {
time_t start, end;
int cost, times, score; //花费时间,猜测次数,得分
print();
while (1) {
printf("Press Enter To Start The Game.\n");
getchar();
time(&start); //计时开始
times = game();
time(&end); //计时结束
cost = difftime(end, start);
score = 100 - cost / 10 - times * 2;
if (score <= 0) score = 0; //负分归零系统
printf("Run Time:%d Seconds\n", cost);
printf("Times You Guessed:%d\n", times);
printf("Your Score:%d\n\n", score);
}
return 0;
}

本文更新于 2017 年 10 月 21 日:
得益于 C 与 JavaScript 的某些相似性,笔者成功将 Power of Logic 移植到 Web 端,制作了 JavaScript 版本。目前该游戏已经可以通过 ZSQ.IM > 应用 > 原创游戏 > Logic 访问。移植过程全面深化了笔者对于 JavaScript 与 HTML5 某些机制的理解,包括从网页实时读取内容与输出内容、通过按钮触发函数实现响应以及 setInterval()clearInterval() 的用法等。

JavaScript版本Power of Logic