JAVA簡單猜拳遊戲(三)

來源:互聯網
上載者:User

接(二)

使用者類,電腦人類,我們已經編寫完了··剩下的就是遊戲類了,說白了,遊戲類的主要作用就是控制遊戲的流程,就相當於遊戲機的手柄。

不多說了,看代碼吧

Code:
  1. import java.util.Scanner;   
  2.   
  3. public class Game   
  4. {   
  5.              Person per;   
  6.              Computer com;   
  7.              int count;   
  8.              Scanner input = new Scanner(System.in);   
  9.   
  10.              //構造方法,初始化   
  11.              public Game()   
  12.              {   
  13.                      per = new Person();   
  14.                      com = new Computer();   
  15.                      count = 0;   
  16.              }   
  17.   
  18.              //開始遊戲   
  19.              public void  gameBegin()   
  20.             {   
  21.                             System.out.println("==========猜拳小遊戲==========");   
  22.                             System.out.println("==============================");   
  23.                             System.out.println("遊戲規則:1.拳頭 2.剪刀 3.包袱");   
  24.                             System.out.println("請選擇人物:1.小強  2.小毛  3.小明");   
  25.                             int number = input.nextInt();   
  26.   
  27.                             switch(number)   
  28.                            {   
  29.                                        case 1:   
  30.                                                    per.name = "小強";   
  31.                                                    System.out.println("您選擇的是小強");   
  32.                                                    break;   
  33.                                         case 2:   
  34.                                                    per.name = "小毛";   
  35.                                                    System.out.println("您選擇的是小毛");   
  36.                                                    break;   
  37.                                          case 3:   
  38.                                                     per.name = "小明";   
  39.                                                    System.out.println("您選擇的是小明");   
  40.                                                     break;   
  41.                            }   
  42.                               
  43.                            //記錄出的拳   
  44.            int perno;   
  45.                           int comno;   
  46.   
  47.                           System.out.println("要開始遊戲嗎?(y/n)");   
  48.                           String answer = input.next();   
  49.   
  50.                           while(answer.equalsIgnoreCase("y"))   
  51.                          {   
  52.                                    perno = per.showFist();   
  53.                                    comno = com.showFist();   
  54.   
  55.                                     //判斷結果   
  56.                if((perno==1&&comno==1)||(perno==2&&comno==2)||(perno==3&&comno==3))   
  57.                                   {   
  58.                                                System.out.println("平局");   
  59.                                                //記錄較量總次數   
  60.                     this.count ++;   
  61.                                   }   
  62.                                   else if((perno==1&&comno==2)||(perno==2&&comno==3)||(perno==3&&comno==1))   
  63.                                  {   
  64.                                                  System.out.println("玩家勝");   
  65.                                                  per.count ++;   
  66.                                                  this.count++;   
  67.                                  }   
  68.                                   else  
  69.                                 {   
  70.                                                     System.out.println("電腦勝");   
  71.                                                    com.count++;   
  72.                                                     this.count++;   
  73.                                 }   
  74.   
  75.                                System.out.println("是否要繼續?(y/n)");   
  76.                                 answer = input.next();   
  77.                          }     
  78.   
  79.                           //當結束時,顯示總結果   
  80.            showResult();                      
  81.             }   
  82.   
  83.             //顯示結果   
  84.      public void showResult()   
  85.           {   
  86.                     System.out.println("較量總次數:" + this.count);   
  87.                     System.out.println("玩家勝次數:" + per.count);   
  88.                     System.out.println("電腦勝次數:" + com.count);   
  89.           }   
  90. }  

接下來,編寫測試類別

Code:
  1. public class Test   
  2. {   
  3.              public static void main(String args [])   
  4.             {   
  5.                           Game game = new Game();   
  6.   
  7.                           game.gameBegin();   
  8.             }   
  9. }  

運行一下,看看吧··

到現在,這個簡單的JAVA猜拳小遊戲就完成了··

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.