如何用Python编猜拳小游戏第一版

如何用Python编猜拳小游戏第一版,第1张

之前我一直在出Python教程,现在我要再次编一个Python 游戏 。告腊这次的 游戏 名叫猜拳小 游戏 ,用户输入出石头,剪刀,还是布。然后让电脑随机出石头,剪刀,布。最后判断是玩家赢了还是电脑赢了。

话不多说,给你们看一下源代码锋友宏。

这次的猜拳小 游戏 弄好了,就不出教程了,因为这里的代码银册上次写猜数字小 游戏 教程的时候好像都讲过。不过这个版本还是有些缺陷的,下次我会出第2版改进。

#include<iostream.h>#include <stdlib.h> #include <time.h> void show(int input) { switch(input) { case 0:cout<<("出的是石头!")breakcase 1:cout<<("出的是剪子!")breakcase 2:cout<<("出的是布!")breakdefault:} } void compare(int inputPlay,int inputComputer) { if(inputPlay==0&&inputComputer==1||inputPlay==1&&inputComputer==2||inputPlay==2&&inputComputer==1) { cout<<("\n玩家胜!\n")} else if(inputPlay==inputComputer) { cout<<("\n平局!\n")} else { cout<<("\n电脑胜!\n")} } void main() { char isGoOnint inputPlayint inputComputercout<<("猜拳游戏\n")cout<<举陆("游戏说明:\n\n")cout<<("石头--0 剪子--1 布--2\n")srand(time(NULL)) do { cout<<("\n请输入您要猜的拳:\n"橘答橡)cin>>inputPlayif(inputPlay>=0&&inputPlay<=2) { cout<<("您")show(inputPlay)inputComputer=rand()%3cout<<("电脑")show(inputComputer)compare(inputPlay,inputComputer) } else { cout<<("您输入有错误!\n")cout<<("\n说明:石头--0 剪子--1 布--2\n")} cout<<"\圆旁n您需要继续么? (Y/N)"cin>>isGoOn }while(isGoOn!='N'||isGoOn!='n')}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12232028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存