當前位置:首頁 » 編程軟體 » 猜拳的編程

猜拳的編程

發布時間: 2023-07-13 15:19:07

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


}


}

Ⅱ 猜拳游戲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);
}

Ⅳ 用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;
}
}
}

熱點內容
帶鎖的鉛筆如何改密碼 發布:2025-02-07 06:18:05 瀏覽:163
ubuntu搭建samba伺服器 發布:2025-02-07 05:52:54 瀏覽:54
小型企業網如何配置可以互通 發布:2025-02-07 05:33:56 瀏覽:243
09年crv哪個配置好 發布:2025-02-07 05:17:31 瀏覽:555
nvm源碼編譯 發布:2025-02-07 05:13:19 瀏覽:126
防偽碼查詢源碼 發布:2025-02-07 05:09:39 瀏覽:769
安卓機的通知在哪裡 發布:2025-02-07 05:09:01 瀏覽:282
密碼74是什麼意思 發布:2025-02-07 05:02:10 瀏覽:47
蘋果es瀏覽器無法連接ftp 發布:2025-02-07 04:59:57 瀏覽:285
javaa和a 發布:2025-02-07 04:58:24 瀏覽:64