1. 程式人生 > >猜拳遊戲程式碼(java編寫)

猜拳遊戲程式碼(java編寫)

猜拳遊戲

實現功能:
1.定義電腦類,玩家類
2.可以新增玩家,選擇玩家進行遊戲
3.遊戲分為人機遊戲和雙人遊戲兩種模式
4.玩家可以設定每輪遊戲局數,每局遊戲均顯示猜拳結果,系統根據結果為玩家積分,勝出玩家獲得一積分。
5.每輪遊戲結束,顯示玩家積分,系統根據玩家積分判斷玩家輸贏。
6.每輪遊戲結束,玩家可以繼續開始新一輪遊戲,也可以退出遊戲。


//電腦類
class ComputerPlayer {
    private String name="電腦";
    private int score=0;
    public int guessing
(){ return (int)(Math.random()*3)+1; } public String getName() { return name; } public int getScore() { return score; } public void setScore() { score=0; } public void addScore() { score++; } } //玩家類 class People { private
String name=""; private int score=0; private static int number=0; People(){ number++; name="玩家"+number; } People(String name){ number++; this.name=name; } public int guessing(){ Scanner sc=new Scanner(System.in); int result; System.out
.println("請輸入您的猜拳結果:(1.石頭 2.剪刀 3.布)"); result=sc.nextInt(); return result; } public String getName() { return name; } public int getScore() { return score; } public void setScore() { score=0; } public void addScore() { score++; } public static int getNumber() { return number; } } //遊戲類 class Game{ //人機遊戲 public void peopleAndComputerPK(People person,ComputerPlayer computer){ Scanner sc=new Scanner(System.in); boolean start=true; int startCount=0,count=0,startJudge=1; while(start){ startCount++; person.setScore(); computer.setScore();; System.out.println("請輸入第"+startCount+"輪遊戲局數:"); count=sc.nextInt(); int n=1; while(count-->0){ int pResult,cResult,win; System.out.println("第"+n+++"局:"); System.out.println("玩家出拳中......"); pResult=person.guessing(); System.out.println("電腦出拳中......"); cResult=computer.guessing(); win=judge(pResult,cResult); if(win==1){ person.addScore(); } if(win==2){ computer.addScore(); } result(person.getName(), pResult, computer.getName(), cResult, win); } int winAll=judgeAll(person.getScore(),computer.getScore()); reaultAll(n-1, person.getName(), person.getScore(), computer.getName(), computer.getScore(), winAll); System.out.println("本輪遊戲結束,是否進入下一輪遊戲?(1.是 2.否 )"); startJudge=sc.nextInt(); if(startJudge==2){ start=false; } } System.out.println("人機遊戲結束!"); } //雙人遊戲 public void peopleAndPeoplePK(People person1,People person2){ Scanner sc=new Scanner(System.in); boolean start=true; int startCount=0,count=0,startJudge=1; while(start){ startCount++; person1.setScore(); person2.setScore(); System.out.println("請輸入第"+startCount+"輪遊戲局數:"); count=sc.nextInt(); int n=1; while(count-->0){ int p1Result,p2Result,win; System.out.println("第"+n+++"局:"); System.out.println(person1.getName()+"出拳中......"); p1Result=person1.guessing(); System.out.println(person2.getName()+"出拳中......"); p2Result=person2.guessing(); win=judge(p1Result,p2Result); if(win==1){ person1.addScore(); } if(win==2){ person2.addScore(); } result(person1.getName(), p1Result, person2.getName(), p2Result, win); } int winAll=judgeAll(person1.getScore(),person2.getScore()); reaultAll(n-1, person1.getName(), person1.getScore(), person2.getName(), person2.getScore(), winAll); System.out.println("本輪遊戲結束,是否進入下一輪遊戲?(1.是 2.否 )"); startJudge=sc.nextInt(); if(startJudge==2){ start=false; } } System.out.println("雙人遊戲結束!"); } //判斷猜拳勝負 private static int judge(int first,int second){ if(first==second){ return 0; }else if(first==1&&second==2||first==2&&second==3||first==3&&second==1){ return 1; }else{ return 2; } } //判斷每輪遊戲勝負 private static int judgeAll(int first,int second){ if(first==second){ return 0; }else if(first>second){ return 1; }else{ return 2; } } //輸出出拳結果 private static String threeResults(int i){ String result=""; switch(i){ case 1: result="石頭"; break; case 2: result="剪刀"; break; case 3: result="布"; break; } return result; } //輸出每局結果 private static void result(String person1,int result1,String person2,int result2,int win){ System.out.println(person1+": "+threeResults(result1)+" PK "+person2+": "+threeResults(result2)); if(win==0){ System.out.println("平局!"); }else if(win==1){ System.out.println(person1+"勝,積分+1!"); }else{ System.out.println(person2+"勝,積分+1!"); } } //輸出每輪結果 private static void reaultAll(int count,String person1,int score1,String person2,int score2,int winAll){ System.out.print("總共"+count+"局:"); System.out.println("其中"+person1+" 積分 "+score1+"分,"+person2+" 積分 "+score2+"分."); if(winAll==0){ System.out.println("最終平局!"); }else if(winAll==1){ System.out.println("最終"+person1+"勝!"); }else{ System.out.println("最終"+person2+"勝!"); } } } public class TestDemo { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int meanSel,peopleNumber=0; boolean peopleJudge=false; People[] players=new People [10]; Game game=new Game(); System.out.println("歡迎進入猜拳遊戲!"); System.out.println("請輸入您的選擇:\n1.人機遊戲\n2.雙人遊戲\n3.新增玩家\n0.退出遊戲"); meanSel=sc.nextInt(); while(meanSel!=0){ switch(meanSel){ case 1://人機遊戲 ComputerPlayer computer=new ComputerPlayer(); if(peopleJudge){ int num=players.length; if(peopleNumber==1){ num=0; }else{ String name=""; boolean judge=false; while(!judge){ System.out.println("請輸入玩家姓名:"); name=sc.next(); for(int i=0;i<peopleNumber;i++){ if(players[i].getName().equals(name)){ num=i; judge=true; break; } } if(!judge){ System.out.println("玩家資訊錯誤,請重新輸入玩家姓名:"); } } } game.peopleAndComputerPK(players[num], computer); }else{ People person=new People(); game.peopleAndComputerPK(person, computer); } break; case 2://雙人遊戲 if(peopleJudge){ String name1=""; String name2=""; boolean judge1=false; boolean judge2=false; int num1=0; int num2=0; while(!judge1){ System.out.println("請輸入玩家a姓名:"); name1=sc.next(); for(int i=0;i<peopleNumber;i++){ if(players[i].getName().equals(name1)){ num1=i; judge1=true; break; } } if(!judge1){ System.out.println("玩家a資訊錯誤,請重新輸入玩家a姓名:"); } } if(peopleNumber==1){ num2=peopleNumber; players[num2]=new People(); }else{ while(!judge2){ System.out.println("請輸入玩家b姓名:"); name2=sc.next(); for(int i=0;i<peopleNumber;i++){ if(players[i].getName().equals(name2)){ num2=i; judge2=true; break; } } if(!judge2){ System.out.println("玩家b資訊錯誤,請重新輸入玩家b姓名:"); } } } game.peopleAndPeoplePK(players[num1], players[num2]); }else{ People person1=new People(); People person2=new People(); game.peopleAndPeoplePK(person1, person2); } break; case 3://新增玩家 peopleJudge=true; int num; String name=""; System.out.println("請輸入新增玩家人數:"); num=sc.nextInt(); System.out.println("請輸入"+num+"個玩家的姓名:"); while(num-->0){ name=sc.next(); players[peopleNumber++]=new People(name); } System.out.println("新增完成!"); break; default: System.out.println("您的選擇輸入錯誤!"); break; } System.out.println("歡迎進入猜拳遊戲!"); System.out.println("請輸入您的選擇:\n1.人機遊戲\n2.雙人遊戲\n3.新增玩家\n0.退出遊戲"); meanSel=sc.nextInt(); } System.out.println("遊戲結束,歡迎下次再玩!"); } }