使用java怎么編寫一個(gè)人機(jī)猜拳小游戲?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
完成人機(jī)猜拳互動(dòng)游戲的開發(fā),用戶通過控制臺(tái)輸入實(shí)現(xiàn)出拳,電腦通過程序中的隨機(jī)數(shù)實(shí)現(xiàn)出拳,每一局結(jié)束后都要輸出結(jié)果。當(dāng)用戶輸入n時(shí)停止游戲,并輸出總結(jié)果。效果如圖1-1所示。
圖1-1人機(jī)猜拳游戲
實(shí)現(xiàn)思路
圖1-2該項(xiàng)目的類圖
(1) 創(chuàng)建出用戶類。
定義用戶類Customer,定義類的屬性(name、score)和類的方法showFist()。
請(qǐng)思考getFist()中的switch語句該如何書寫。
(2) 創(chuàng)建計(jì)算機(jī)類Computer,實(shí)現(xiàn)計(jì)算機(jī)出拳。
計(jì)算機(jī)出拳是根據(jù)產(chǎn)生的隨機(jī)數(shù)來決定出拳的具體內(nèi)容。這點(diǎn)的實(shí)現(xiàn)和用戶類出拳類似,請(qǐng)自己完成。
(3) 創(chuàng)建游戲類,實(shí)現(xiàn)選擇對(duì)戰(zhàn)對(duì)手
Ø 創(chuàng)建游戲類Game,編寫其屬性。屬性有:甲方玩家、乙方玩家和對(duì)戰(zhàn)次數(shù)以及平手次數(shù)。
Ø 編寫游戲類的開始游戲方法startGame(),實(shí)現(xiàn)的效果如下圖1-3所示。
圖1.3對(duì)戰(zhàn)選手的選擇
(4) 分別調(diào)用用戶類和計(jì)算機(jī)類的出拳方法showFist(),接收返回值并比較,給出勝負(fù)結(jié)果,運(yùn)行結(jié)果如下圖1-4所示。
圖1-4用戶和電腦出拳對(duì)戰(zhàn)
(5) 實(shí)現(xiàn)循環(huán)對(duì)戰(zhàn),效果如下圖1-5所示。
圖1-5實(shí)現(xiàn)循環(huán)對(duì)戰(zhàn)
(6) 完善游戲類,顯示對(duì)戰(zhàn)結(jié)果,效果如下圖1-6所示。
圖1-6顯示對(duì)戰(zhàn)結(jié)果
(7) 建立測(cè)試類RunGame,調(diào)用Game類的startGame()方法。
代碼實(shí)現(xiàn)
Your 類代碼(用以表示你出的招式)
package chaiquan; public class your { public static int quan(int i) { switch(i) { case 1: System.out.println("你出拳:剪刀"); break; case 2: System.out.println("你出拳:石頭"); break; case 3: System.out.println("你出拳:布"); break; default: System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入"); break; } return i; } }
Customer 類(實(shí)現(xiàn)計(jì)算機(jī)隨機(jī)出拳以及對(duì)勝負(fù)結(jié)果的判斷)
package chaiquan; public class Customer { static int scor=0; static int scor1=0; static int scor2=0; static int chuzhao; public static int showFist() { chuzhao= (int) (Math.random() * 3) + 1; return chuzhao; } public static int getFist() { switch(chuzhao) { case 1: System.out.println("剪刀"); break; case 2: System.out.println("石頭"); break; case 3: System.out.println("布"); break; } return chuzhao; } public static int play(int i) { if(i==chuzhao) { System.out.println("此局:和局,嘿嘿,等著瞧吧!"); scor++; } else if(i==1&&chuzhao==2) { System.out.println("此局:很遺憾,你輸了"); scor1++; } else if(i==1&&chuzhao==3){ System.out.println("此局:恭喜,你贏了"); scor2++; } else if(i==2&&chuzhao==1) { System.out.println("此局:恭喜,你贏了"); scor2++; } else if(i==2&&chuzhao==3) { System.out.println("此局:很遺憾,你輸了"); scor1++; } else if(i==3&&chuzhao==1) { System.out.println("此局:很遺憾,你輸了"); scor1++; } else if(i==3&&chuzhao==2){ System.out.println("此局:恭喜,你贏了"); scor2++; } return i; } }
Game 類(開始游戲)
package chaiquan; public class Game { public static void main(String[] args) { GameGame.startGame(); } }
GameGame類(游戲真正的執(zhí)行進(jìn)程)
package chaiquan; import java.util.Scanner; public class GameGame { public static void startGame(){ int iii=1; System.out.println("---------------歡迎進(jìn)入游戲世界---------------\n\n\n\t\t********************\n\t\t****猜拳,開始****\n\t\t********************"); System.out.print("請(qǐng)選擇你想和誰對(duì)戰(zhàn)(1:劉備 2:孫權(quán) 3:曹操):"); Scanner sc=new Scanner(System.in); int a=sc.nextInt(); while(true){ System.out.print("要開始嗎?(y/n):"); String b=sc.next(); if(b.equals("n")){ System.out.println("結(jié)束"); return; } else if(b.equals("y")){ while(true) { System.out.print("請(qǐng)出拳:1.剪刀 2.石頭 3.布(輸入相應(yīng)數(shù)字):"); int c=sc.nextInt(); Computer.quan(c); System.out.print(Qwe.getName1(a)+"出拳:"); Customer.showFist(); Customer.getFist(); Customer.play(c); System.out.print("是否開始下一輪(y/n):"); String m=sc.next(); if(m.equals("y")) { iii++; } else if(m.equals("n")) { System.out.println("------------------------------------------"); System.out.println(Qwe.getName1(a)+"VS玩家"); System.out.println("對(duì)戰(zhàn)次數(shù):"+iii+"出拳一樣的情況有"+Customer.scor+"次"); if(Customer.scor1<Customer.scor2) { System.out.println("結(jié)果:恭喜恭喜,你贏了"); break; } else if(Customer.scor1==Customer.scor2){ System.out.println("平局!"); break; } else { System.out.println("很遺憾你輸了!"); break; } } else { System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入;"); break; } } } else{ System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入"); continue; } } }}
User 類(選擇游戲玩家)
package chaiquan; public class user { static String name; public static String getName1(int i) { switch(i) { case 1: name="劉備"; break; case 2: name="孫權(quán)"; break; case 3: name="曹操"; break; } return name; } }
看完上述內(nèi)容,你們掌握使用java怎么編寫一個(gè)人機(jī)猜拳小游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁標(biāo)題:使用java怎么編寫一個(gè)人機(jī)猜拳小游戲-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.rwnh.cn/article48/pcdhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、網(wǎng)站改版、外貿(mào)建站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容