编程猜拳
界面很简单。
#include <time.h>
#include <stdlib.h>
#define stone 1
#define forfex 2
#define pack 3
/*输入1、2、3分别代表,石头,剪刀,布*/
int main()
{
int computer;
int hand;
for(;;)
{
printf("you hand:\n");
scanf("%d",&hand);
if(hand>3) {
printf("****Input wrong!*** \n ");
break;
}
srand((int)time(0));
computer=1+(int)(3.0*rand()/(RAND_MAX+1.0));
switch(hand)
{
case 1:
printf("You Stone \n");
printf("com=%d\n",computer);
if(computer==forfex) printf("☆★ You WIN ★☆\n");
if(computer==pack) printf(":( You LOSE \n");
if(hand==computer) printf("== equal == !\n");
printf("\n");
break;
case 2:
printf("You Forfex\n");
printf("com=%d\n",computer);
if(computer==stone) printf(":( You LOSE \n");
if(computer==pack) printf("☆★ You WIN ★☆\n");
if(hand==computer) printf("== equal == !\n");
printf("\n");
break;
case 3:
printf("You Pack\n");
printf("com=%d\n",computer);
if(computer==stone) printf("☆★ You WIN ★☆\n");
if(computer==forfex) printf(":( You LOSE \n");
if(hand==computer) printf("== equal == !\n");
printf("\n");
break;
}
}
getch();
return 0;
}
win xp + win tc 调试通过,但符号显示不出。
WIN XP + cygwin + gcc 调试通过,可正常显示符号
‘贰’ java编程 用循环方式实现和计算机玩猜拳的程序 (设定胜出条件--输3次或赢3次即退出)
大哥,刚刚看到你的就开始写了。各种功能都有,包括判断你输入的字符是否正确,假如不符合的字符就提示。不玩了就可以直接按0退出。
然后,只要是赢三次,输三次都自动退出,并输出你输赢,还可以自动共玩多少局,输赢局数统计!希望能帮到您。
/*
猜拳游戏思路
1、定义输入函数
2、提示用户输入猜拳数值
3、定义随机一个数作为电脑数值
4、判断[用户输入数值]与[电脑随机数值]
5、能够相等就是打平,不能相等就利用&&、||逻辑符判断输赢
6、设定数值1-石头2-剪刀3-布
*/
importjava.util.*;//嵌入Java.util包所有
publicclassCq{
publicstaticvoidmain(String[]args){
intwin=0;//赢的记录
intlose=0;//输的记录
intall=1;//计数总的局数
inta=1;//控制循环条件使用
System.out.println("--------------猜拳游戏---------------");
System.out.println("游戏规则:赢三次便赢,输三次便输。");
while(a>0){//假如a=0的话就不用继续玩
Scannerin=newScanner(System.in);//定义输入函数in,Scanner包功能,输入数值用的
System.out.println("请输入一个数值:1、石头2、剪刀3、布0、退出游戏");//提示输入数值
System.out.println("");//空行
intx=in.nextInt();//让用户输入X的数值
Randomon=newRandom();//定义电脑的随机数值的函数on
inty=on.nextInt(3)+1;//定义y随机函数数值范围(1--3)
if(x>=4){//判断用户是否输入非1--3范围
System.out.println("亲,请正确输入:1、石头2、剪刀3、布。你输入了:"+x);
}elseif(x==0){
a=0;
System.out.println("欢迎再次玩“猜拳游戏”!");
return;
}
else{
/*下面是判断用户输入x的数值嵌套if*/
if(x==y){
if(x==1){//判断打平的情况
System.out.println("你:石头------电脑:石头PK:平手"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}
elseif(x==2){
System.out.println("你:剪刀------电脑:剪刀PK:平手"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}else{
System.out.println("你:布------电脑:布PK:平手"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}
/*打平手的判断END*/
}elseif(x==1&&y==2||x==2&&y==3||x==3&&y==1){//开始判断赢的情况
if(x==1&&y==2){
win++;//win1
System.out.println("[你]:石头---VS---[电脑]:剪刀PK:赢了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}elseif(x==2&&y==3){
win++;//win2
System.out.println("[你]:剪刀---VS---[电脑]:布PK:赢了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}else{
win++;//win3
System.out.println("[你]:布---VS---[电脑]:石头PK:赢了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}
//判断赢的情况END
}else{//开始判断输的情况
if(x==1&&y==3){
lose++;
System.out.println("[你]:石头---VS---[电脑]:布PK:输了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}elseif(x==2&&y==1){
lose++;
System.out.println("[你]:剪刀---VS---[电脑]:石头PK:输了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}else{
lose++;
System.out.println("[你]:布---VS---[电脑]:剪刀PK:输了!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
all++;
}
}//判断输的情况END
if(win==3){
System.out.println("");
System.out.println("");
System.out.println("游戏结束:恭喜您!你已经赢了[电脑]三局!!!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
a=1;
return;
}elseif(lose==3){
a=1;
System.out.println("");
System.out.println("");
System.out.println("游戏结束:很遗憾,电脑赢了你三盘!继续加油!"+"共玩"+all+"局,"+"赢:"+win+",输:"+lose);
return;
}
else{continue;}
}//判断是否输入数值1-3范围,如果不是1-3会提醒重新输入END
}//while
}
}
‘叁’ 用1,2,3分别代表石头剪刀布,输入甲乙的猜 拳选择输出甲乙猜拳结果。(分支结构)。C语言程序设计
void func(int a,int b)
{
int num=0;
if((a==1)&&(b==1))
printf("打平\n");
else if((a==1)&&(b==2))
printf("甲获胜\n");
else if((a==1)&&(b==3))
printf("乙获胜\n");
else if((a==2)&&(b==1))
printf("乙获胜\n");
else if((a==2)&&(b==2))
printf("打平\n");
else if((a==2)&&(b==3))
printf("甲获胜\n");
else if((a==3)&&(b==1))
printf("甲获胜\n");
else if((a==3)&&(b==2))
printf("乙获胜\n");
else if((a==3)&&(b==3))
printf("打平\n");
}
‘肆’ 在学习java期间如何利用java制作一个简单的猜拳游戏编程
importjava.util.Scanner;
publicclassGame{
privateScannerscan=newScanner(System.in);
privateStringrule[]={"","剪刀","石头","布"};
privateStringrole[]={"","刘备","孙权","曹操"};
privateComputercomputer;
privateUseruser;
privateintroundCount;
privateGame(){
roundCount=0;
}
publicstaticvoidmain(String[]args){
Gamegame=newGame();
game.start();
}
publicvoidstart(){
computer=newComputer();
System.out.println("--欢迎进入游戏世界--");
System.out.println("********************");
System.out.println("**猜拳,开始**");
System.out.println("********************");
System.out.println();
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.print("请选择角色(1:刘备2.孙权3.曹操)");
intuserRole=scan.nextInt();
if(userRole>=1&&userRole<=3){
user=newUser(role[userRole]);
System.out.println();
System.out.print("要开始吗?(y/n)");
Stringstart=scan.next();
if(start.equals("y")){
round();
}
elseif(start.equals("n")){
}
}
}
privatevoidround(){
roundCount++;
System.out.println();
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
intuserRule=user.round();
if(userRule>=1&&userRule<=3){
intcomputerRule=computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否开始下一轮(y/n):");
Stringnext=scan.next();
if(next.equals("y")){
round();
}
elseif(next.equals("n")){
end();
}
}
}
privatevoidjudge(intur,intcr){
System.out.println("你出拳:"+rule[ur]);
System.out.println("电脑出拳:"+rule[cr]);
if(ur==cr){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
elseif((ur==1&&cr==3)||
(ur==2&&cr==1)||
(ur==3&&cr==2)){
System.out.println("结果:你赢了!");
user.win();
}
else{
System.out.println("结果:你输了!");
computer.win();
}
}
privatevoidend(){
System.out.println("--------------------------------------");
System.out.println(user.getName()+"VS"+computer.getName());
System.out.println("对战次数:"+roundCount);
if(computer.getWin()==user.getWin()){
System.out.println("结果:打成平手,下次再和你一分高下!");
}
elseif(computer.getWin()>user.getWin()){
System.out.println("结果:你输了!电脑赢了"+computer.getWin()+"次!");
}
else{
System.out.println("结果:你赢了!你赢了"+user.getWin()+"次!");
}
System.out.println("--------------------------------------");
}
classComputer{
privateintwin;
privateStringname;
publicComputer(){
win=0;
name="匿名";
}
publicintround(){
return(int)(System.currentTimeMillis()%3)+1;//随机返回1、2、3
}
publicStringgetName(){
returnname;
}
publicvoidwin(){
win++;
}
publicintgetWin(){
returnwin;
}
}
classUser{
privateintwin;
privateStringname="";
publicUser(Stringname){
this.name=name;
win=0;
}
publicintround(){
returnscan.nextInt();
}
publicStringgetName(){
returnname;
}
publicvoidwin(){
win++;
}
publicintgetWin(){
returnwin;
}
}
}
‘伍’ java猜拳游戏程序设计怎么做啊
importjava.util.Random;
classDianNao{
publicStringchuQuan(){
Randomrand=newRandom();
inti=rand.nextInt(3);
Stringstr="";
if(i==0){
str="石头";
}
if(i==1){
str="剪刀";
}
if(i==2){
str="布";
}
returnstr;
}
}
classCaiPan{
publicStringcaiJue(Stringstr1,Stringstr2){
Stringstr="输";
if(str1.equals("石头")&&str2.equals("剪刀")){
str="赢";
}
if(str1.equals("石头")&&str2.equals("布")){
str="输";
}
if(str1.equals("石头")&&str2.equals("石头")){
str="平";
}
if(str1.equals("剪刀")&&str2.equals("石头")){
str="输";
}
if(str1.equals("剪刀")&&str2.equals("布")){
str="赢";
}
if(str1.equals("剪刀")&&str2.equals("剪刀")){
str="平";
}
if(str1.equals("布")&&str2.equals("石头")){
str="赢";
}
if(str1.equals("布")&&str2.equals("剪刀")){
str="输";
}
if(str1.equals("布")&&str2.equals("布")){
str="平";
}
returnstr;
}
}
publicclassShiTouJianBu{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
CaiPancp=newCaiPan();
DianNaodn1=newDianNao();
DianNaodn2=newDianNao();
Stringstr1=dn1.chuQuan();
Stringstr2=dn2.chuQuan();
Stringresult=cp.caiJue(str1,str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(result);
}
}
就帮你到这吧
‘陆’ 猜拳游戏C语言编程
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define ORIGINGOLD 100 typedef struct { char name[100]; unsigned long score; } User; void Menu() { system("cls"); printf("========================made by 小恩 =====================\n"); printf("1:start game\n"); printf("2:show order\n"); printf("3:quit game\n"); printf("==========================================================\n"); } int choise() { int ch; printf("Input your choice(1~3): "); while(ch=getchar()) { if('\n'==ch) continue; while('\n'!=getchar()); if(ch>'3'||ch<'1') { printf("Error input,Try again(1~3): "); continue; } else break; } return ch; } int agree(const char *p) { char yes; printf("%s",p); while(scanf("%c",&yes),yes!='Y'&& yes!='N'&& yes!='y'&& yes!='n') { while('\n'!=getchar()); printf("Error input! Try again(Y/N): "); } while('\n'!=getchar()); if(yes=='Y'|| yes=='y') return 1; else return 0; } User getUser(const unsigned long score) { User gamer; int ch,i=0; memset(&gamer,0,sizeof(User)); printf("Please input the User's name:"); while(!strcmp(gamer.name,"")) { while((i<99) && ('\n'!=(ch=getchar()))) gamer.name[i++]=(char)ch; gamer.name[i]='\0'; } gamer.score=score; return gamer; } int getInt(int minR,int maxR) { int showhand; while(!scanf_s("%d",&showhand) || showhand<minR|| showhand>maxR) { while('\n'!=getchar()); printf("Invide input!Try again:(%d~%d) ",minR,maxR); } while('\n'!=getchar()); return showhand; } void save(const unsigned long score) { FILE *fp; int num=0; User gamer={"",0L},temp={"",0L},gm; if(fopen_s(&fp,"F:\\fist.gm","r+")) { if(fopen_s(&fp,"F:\\fist.gm","w")) { printf("Error,Can't open file F:\\fist.gm for save!\n"); exit(1);
希望采纳
‘柒’ C语言编写三局两胜的猜拳游戏编程
/*假设有A和B进行猜拳
'x'表示剪刀,'y'表示石头,'z'表示布
规则如下:
1、 石头 > 剪刀
2、 布 > 石头
3、 剪刀> 布
*/
#include <stdio.h>
#define WIN 1
#define LOSE -1
#define EQUAL 0
/*比赛规则函数*/
int game_rule(char a,char b)
{
switch(a)
case 'x':
{
switch(b)
case 'x':return EQUAL;
case 'y':return LOSE;
case 'z':return WIN;
}
case 'y':
{
switch(b)
case 'x':return WIN;
case 'y':return EQUAL;
case 'z':return LOSE;
}
case 'z':
{
switch(b)
case 'x':return LOSE;
case 'y':return WIN;
case 'z':return EQUAL;
}
}
/* main function*/
void main(void)
{
int ans;
int a = b = 0;
char A;
char B;
do
{
prinf("please input A:\n");
scanf("%c",&A);
getchar();
prinf("please input B:\n");
scanf("%c",&B);
getchar();
//有效性检查请自己加上
ans = game_rule(A,B);
if(ans==WIN)
{
a++;
prinf("A赢了%d局\n",a);
}
else if(ans==LOSE)
{
b++;
prinf("A赢了%d局\n",b);
}
if((a==2)||(b==2))
{
prinf("game over");
break;
}
}while(1);
}
‘捌’ C# 编写网络小游戏 猜拳
按照你的要求写了一个,比较的粗糙。没有实现你的10秒倒数,这个留给你自己去思考吧。我大致的说一说,写了一个服务器端 和 客户端。用到的技术有:多线程,c#网络编程。客户端连接到服务器端 通过服务器端处理和转发 猜拳的结果。在服务端中设定了连接的最大客户端数量为2 。也就是只能连2个客户端,可以视实际情况更改。写了一个下午,也没有好好的写注释。如果你是学习的话就加我讨论吧。以上是整体运行的效果,中间是服务器端,两边分别为客户端1 和客户端2////////////////////////////////////////////////////////////////////////////////服务器端界面:服务器端代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace Server
{
public partial class Form1 : Form
{
private TcpListener listener;
private List<TcpClient> clientList;
private Thread t;
private Thread t1;
private string outTos;
public Form1()
{
InitializeComponent();
setControls();
} private void setControls()
{
clientList = new List<TcpClient>(); button1.Text = "Start";
button1.Click += new EventHandler(button1_Click);
button2.Text = "Stop";
button2.Click += new EventHandler(button2_Click);
} private void StartServer()
{
IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
listener = new TcpListener(ip, 8500);
listener.Start();
richTextBox1.Text += string.Format("服务器:{0} 端口号:{1} 已经启动...\n",ip.ToString(),8500);
}
private void GetClient()
{
do
{
if (clientList.Count >= 2)
{
//clientList.Clear();
break;
}
else
{
clientList.Add(listener.AcceptTcpClient());
outTos += string.Format("Player:{0} is connecting\n", clientList[clientList.Count - 1].Client.LocalEndPoint);
if (clientList.Count == 2)
{
try
{
t1 = new Thread(GetMsg);
t1.Start();
}
catch
{
t1.Abort();
}
}
}
}
while (true);
}
private void GetMsg()
{
do
{
int byteRead = 0;
int bufferSize = 2000;
byte[] buffer = new byte[bufferSize]; NetworkStream stream1 = clientList[0].GetStream();
byteRead = stream1.Read(buffer, 0, bufferSize);
string msg = Encoding.Unicode.GetString(buffer, 0, byteRead); NetworkStream stream2 = clientList[1].GetStream();
byteRead = stream2.Read(buffer, 0, bufferSize);
string msg1 = Encoding.Unicode.GetString(buffer, 0, byteRead); if (msg != string.Empty && msg1 != string.Empty)
{
SendMsg(GetAnsower(msg, msg1));
}
}
while (true); } private void SendMsg(int win)
{
string p1 = string.Empty;
string p2 = string.Empty;
if (win == 0)
{
p1 = "平手";
p2 = "平手";
}
if (win == 1)
{
p1 = "赢了!";
p2 = "输了!";
}
if (win == 2)
{
p1 = "输了!";
p2 = "赢了!";
} NetworkStream stream1 = clientList[0].GetStream();
byte[] buffer1 = Encoding.Unicode.GetBytes(p1);
stream1.Write(buffer1, 0, buffer1.Length); NetworkStream stream2 = clientList[1].GetStream();
byte[] buffer2 = Encoding.Unicode.GetBytes(p2);
stream2.Write(buffer2, 0, buffer2.Length);
outTos += string.Format("消息已发送 玩家1:{0} 玩家2:{1}\n",p1,p2);
} private int GetAnsower(string m1,string m2)
{
int win = 0;
if (m1 == "剪刀")
{
if (m2 == "布") win = 1;
if (m2 == "石头") win = 2;
if (m2 == "剪刀") win = 0;
}
if (m1 == "石头")
{
if (m2 == "剪刀") win = 1;
if (m2 == "布") win = 2;
if (m2 == "石头") win = 0;
}
if (m1 == "布")
{
if (m2 == "石头") win = 1;
if (m2 == "剪刀") win = 2;
if (m2 == "布") win = 0;
}
return win;
} void button1_Click(object sender, EventArgs e)
{
try
{
StartServer();
t = new Thread(GetClient);
t.Start();
timer1.Start();
}
catch (Exception ex)
{
t.Abort();
t1.Abort();
timer1.Stop();
timer1.Stop();
}
}
void button2_Click(object sender, EventArgs e)
{
t.Abort();
t1.Abort();
listener.Stop();
timer1.Stop();
MessageBox.Show("服务器已关闭!");
} private void timer1_Tick(object sender, EventArgs e)
{
richTextBox1.Text += outTos;
outTos = string.Empty;
}
}
}
‘玖’ 设计c语言编程猜拳游戏并统计游戏结果次数
#include<stdio.h>
#include<stdlib.h>
int main()
{
char cq[][10]={"石头","剪刀","布"};
int guess=-1,r,youwin=0,mewin=0,daping=0,total=0;
srand(time(NULL));
while(1)
{
r=(int)((rand()/(RAND_MAX+1.0))*3);
printf("0、石头\n1、剪刀\n2、布\n3、退出\n我已出,请你出:");
scanf("%d",&guess);
if(3==guess)
{
break;
}
else
{
total++;
printf("这一次你出的是%s,我出的是%s,所以",cq[guess],cq[r]);
if(0==guess&&1==r || 1==guess&&2==r || 2==guess&&0==r)
{
youwin++;
printf("你赢了!\n");
}
else if(guess==r)
{
daping++;
printf("我们打平了!\n");
}
else
{
mewin++;
printf("我赢了!\n");
}
}
}
printf("总共玩了%d次,你赢了%d次,我赢了%d次,打平%d次!\n",total,youwin,mewin,daping);
system("PAUSE");
return EXIT_SUCCESS;
}
‘拾’ C语言 猜拳游戏编程
#include
using
namespace
std;
main
()
{
int
a,b,c,d;
char
e;
cout<<"1表示石头
2表示剪刀
3表示布."<
>a;
b=rand
()%3+1;
cout<<"电脑出的是"<
>e;
}
cout<
评论
0
0
0
加载更多
