List3—1
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int human;
int comp;
int retry;
int judge;
srand(time(NULL));
printf("猜拳游戏开始!!!\n");
do {
comp = rand() % 3; //生成0~2之间的随机数,其中0对应石头,1对应剪刀,2对应布
do {
printf("\n\a石头剪刀布...(0)石头(1)剪刀(2)布:");
scanf("%d", &human); //读取玩家手势
} while (human < 0 || human>2);
printf("我出"); //显示计算机手势
switch (comp) {
case 0:printf("石头"); break;
case 1:printf("剪刀"); break;
case 2:printf("布"); break;
}
printf("\n");
judge = (human - comp + 3) % 3;
switch (judge) {
case 0:printf("平局。"); break;
case 1:printf("你输了。"); break;
case 2:printf("你赢了。"); break;
}
do {
printf("再来一次吗...(0)否(1)是");
scanf("%d", &retry);
} while (retry < 0 || retry>1);
} while (retry == 1);
return 0;
}
List3—2
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int human;
int comp;
int retry;
int judge;
srand(time(NULL));
printf("猜拳游戏开始!!!\n");
do {
comp = rand() % 3; //生成0~2之间的随机数,其中0对应石头,1对应剪刀,2对应布
do {
printf("\n\a石头剪刀布...(0)石头(1)剪刀(2)布:");
scanf("%d", &human); //读取玩家手势
} while (human < 0 || human>2);
printf("我出"); //显示计算机手势
switch (comp) {
case 0:printf("石头,"); break;
case 1:printf("剪刀,"); break;
case 2:printf("布,"); break;
}
printf("你出"); //显示计算机手势
switch (human) {
case 0:printf("石头。"); break;
case 1:printf("剪刀。"); break;
case 2:printf("布。"); break;
}
printf("\n");
judge = (human - comp + 3) % 3;
switch (judge) {
case 0:printf("平局。"); break;
case 1:printf("你输了。"); break;
case 2:printf("你赢了。"); break;
}
do {
printf("再来一次吗...(0)否(1)是");
scanf("%d", &retry);
} while (retry < 0 || retry>1);
} while (retry == 1);
return 0;
}
List3-7
在List3-2的程序中,表示玩家和计算机的手势的部分是分别用switch语句实现的,程序将近有10行。而改良版的程序只有1行,非常简洁。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
int i;
int human;
int comp;
int judge;
int retry;
char *hd[] = { "石头","剪刀","布" }; //手势
srand(time(NULL)); //设定随机数种子
printf("猜拳游戏开始!!!\n");
do {
comp = rand() % 3; //用随机数生成(0~2)之间的随机数
do {
printf("\n\a石头剪刀布...");
for (i = 0; i < 3; i++)
printf(" (%d)%s", i, hd[i]);
printf(":");
scanf("%d", &human); //读取玩家手势
} while (human < 0 || human>2);
printf("我出%s,你出%s。\n", hd[comp], hd[human]);
judge = (human - comp + 3) % 3;
switch (judge) {
case 0:printf("平局。"); break;
case 1:printf("你输了。"); break;
case 2:printf("你赢了。"); break;
}
printf("再来一次吗...(0)否(1)是:");
scanf("%d", &retry);
} while (retry == 1);
return 0;
}
List3-8
#include <stdio.h>
int main(void)
{
int i;
int human; //玩家的手势
int comp; //计算机的手势
int judge; //胜负
int retry; //再来一次?
char *hd[] = { "石头","剪刀","布" };
printf("猜拳游戏开始!!!");
do {
do {
printf("\n\a石头剪刀布...");
for (i = 0; i < 3; i++)
printf("(%d)%s", i, hd[i]);
putchar(':');
scanf("%d", &human);
} while (human < 0 || human>2);
comp = (human + 2) % 3; //计算机后出
printf("你出%s,我出%s。\n", hd[human], hd[comp]);
judge = (human - comp + 3) % 3;
switch (judge) {
case 0:printf("平局。"); break;
case 1:printf("你输了。"); break;
case 2:printf("你赢了。"); break;
}
printf("再来一次吗...(0否)(1)是:");
scanf("%d", &retry);
} while (retry == 1);
return 0;
}
List3-9
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int human; //玩家的手势
int comp; //计算机的手势
int win_no; //玩家获胜次数
int lose_no; //玩家失败次数
int draw_no; //平局次数
char *hd[] = { "石头","剪刀","布" }; //手势
/*---初始处理---*/
void initialize(void)
{
win_no = 0; //玩家获胜次数
lose_no = 0; //玩家失败次数
draw_no = 0; //平局次数
srand(time(NULL)); //设定随机数钟子
printf("猜拳游戏开始!!!\n");
}
/*---运行猜拳游戏(读取、生成手势)---*/
void jyanken(void)
{
int i;
comp = rand() % 3; //用随机数生成计算机的手势0~2
do {
printf("\n\a石头剪刀布...");
for (i = 0; i < 3; i++)
printf("(%d)%s ", i, hd[i]);
printf(":");
scanf("%d", &human); //读取玩家手势
} while (human < 0 || human>2);
}
/*---更新胜利、失败、平局次数---*/
void count_no(int result)
{
switch (result) {
case 0: draw_no++; break;
case 1: lose_no++; break;
case 2: win_no++; break;
}
}
/*---显示结果---*/
void disp_result(int result)
{
switch (result) {
case 0: printf("平局。"); break;
case 1: printf("你输了。"); break;
case 2: printf("你赢了。"); break;
}
}
/*---确认是否再次挑战---*/
int confirm_retry(void)
{
int x;
printf("再来一次吗...(0)否(1)是");
scanf("%d", &x);
return x;
}
int main(void)
{
int judge; //胜负
int retry; //再来一次?
initialize(); //初始化
do {
jyanken(); //运行猜拳游戏
/*显示计算机和玩家的手势*/
printf("你出%s,我出%s。\n", hd[human], hd[comp]);
judge = (human - comp + 3) % 3; //判断胜负
count_no(judge);
disp_result(judge);
retry = confirm_retry();
} while (retry == 1);
printf("%d胜%d负%d平。\n", win_no, lose_no, draw_no);
return 0;
}