用c语言制作一个简易的猜拳小游戏 ,如果跳出错误在getch()上可以改成_getch()试试即可,新版本vs可能认为getch()是不安全的所以不让用。
如果需要有图像的小游戏的话导入easyx,把输出语句修改成画图即可编程画图,记得修改图片位置哦
以下是完整代码,直接CV即可
main.c
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
void FinalResult(int i,int j)
{
if (i > j)
{
system("cls");
printf("You are the ultimate winner");
}
else if (i < j)
{
system("cls");
printf("The computer is the ultimate winner");
}
else if (i == j)
{
system("cls");
printf("The final result of this game is the same");
}
}
//出拳展示
void show(int i)
{
switch (i)
{
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
}
}
//当前回合胜负情况
int VS(int a, int b)
{
if (a == b)
{
printf("This round is the same!!!\n");
return 0;
}
switch (a)
{
case 1:
if (b == 2)
{
printf("石头VS剪刀\n");
printf("You are winner\n");
return 1;
}
else if (b == 3)
{
printf("石头VS布\n");
printf("Computer is winner!\n");
return 2;
}
break;
case 2:
if (b == 1)
{
printf("剪刀VS石头\n");
printf("Computer is winner!\n");
return 2;
}
else if (b == 3)
{
printf("剪刀VS布\n");
printf("You are winner\n");
return 1;
}
break;
case 3:
if (b == 1)
{
printf("布VS石头\n");
printf("You are winner\n");
return 1;
}
else if (b == 2)
{
printf("布VS剪刀\n");
printf("Computer is winner!\n");
return 2;
}
break;
}
}
//电脑出拳
int Computer()
{
int computer = 0;
srand((unsigned)time(NULL));
computer = rand() % 3+1;
return computer;
}
//用户出拳
int User()
{
int user = 0;
printf("输入1为石头——输入2为剪刀——输入3为布\n");
printf("请出拳\n");
user = getch();
user -= 48;
if (user < 1 || user > 3)
{
system("cls");
printf("输入非法!!!\n请重新输入\n");
printf("输入1为石头——输入2为剪刀——输入3为布\n");
user = getch();
user -= 48;
}
return user;
}
int main()
{
int user_number = 0;
int computer_number = 0;
int user_sum = 0;
int computer_sum = 0;
printf("本游戏进行五局\n");
for (int i = 0; i < 5 ; i++)
{
printf("--------------------------------------------------------------------------------------------------------\n");
printf("第%d回合,", i+1);
printf("目前比分%d : %d\n\n", user_sum, computer_sum);
user_number = User();
computer_number = Computer();
printf("\n用户:");
show(user_number);
printf("电脑:");
show(computer_number);
printf("\n");
switch(VS(user_number, computer_number))
{
case 1:
user_sum++;
break;
case 2:
computer_sum++;
break;
}
}
FinalResult(user_sum, computer_sum);
return 0;
}
运行错误,也可评论,有空会看尽力解答
标签:computer,猜拳,int,winner,C语言,小游戏,user,printf,return From: https://blog.csdn.net/2301_80657297/article/details/141091964