java簡單的人機猜拳小遊戲,java人機猜拳

來源:互聯網
上載者:User

java簡單的人機猜拳小遊戲,java人機猜拳
今天翻以前的代碼,偶然看見了自己剛學java的時候寫的一個簡單的猜拳小遊戲,非常基礎,不過挺有意思的,也包括了積分制,貼到這裡讓大家看看

package game;public class Computer {static int i;public static int shownum() {i = (int) (Math.random() * 3);return i;}public static String showFist() {String a[] = { "剪刀", "石頭", "布" };return a[i];}public static String choos(int a) {String b = null;switch (a) {case 1:b = "劉備";break;case 2:b = "孫權";break;case 3:b = "曹操";break;}return b;}// public static void main(String[] args) {// System.out.println(Computer.guess());// }}
package game;/* * 我們自己定義類的時候,要考慮的問題 *    1: 要職責明確  一個類,就幹自己專長的事情 *    2: IPO *     *//* *  customer 類 是做什麼用的,處理遊戲 */public class Customer {static String name = "玩家";static int score;public static String getFirst(int c) {String d = null;switch (c) {case 1:d = "剪刀";break;case 2:d = "石頭";break;case 3:d = "布";break;}return d;}}

package game;import java.util.Scanner;public class Game {public static void starGame() {System.out.println("---------歡迎進入遊戲世界---------");System.out.println("\t***************");System.out.println("\t****猜拳開始****");System.out.println("\t***************");System.out.println("請選擇你想和誰對戰(1:劉備2:孫權3:曹操)");int computerjifen=0;int gamerjifen=0;int equ=0;int count=0;Scanner sd = new Scanner(System.in);int a = sd.nextInt();Computer.choos(a);System.out.print("要開始嗎y/n:");String b=sd.next();do{Computer.shownum();int x=Computer.shownum()+1;Computer.showFist();if(b.equals("y")){//boolean f;System.out.print("\n請出拳:1.剪刀2.石頭3.布(請輸入相應數字)");int num = sd.nextInt();System.out.println("你出拳:"+Customer.getFirst(num));System.out.println(Computer.choos(a)+"出拳:"+Computer.showFist());if(x==num){System.out.println("此局:平局,嘿嘿,你等著瞧吧!");equ++;}else if(x==1&&num==2){System.out.println("結果,你贏了");gamerjifen++;}else if(x==1&&num==3){System.out.println("結果,你輸了");computerjifen++;}else if(x==2&&num==1){System.out.println("結果,你輸了");computerjifen++;}else if(x==2&&num==3){System.out.println("結果,你贏了");gamerjifen++;}else if(x==3&&num==1){System.out.println("結果,你贏了");gamerjifen++;}else if(x==3&&num==2){System.out.println("結果,你輸了");computerjifen++;}count++;System.out.println("-------------------");System.out.println("\n是否開始下一輪y/n");String r=sd.next();//if(r.equals("y")){//f=true;//}if(r.equals("n")){if(computerjifen>gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,你輸了");break;}else if(computerjifen<gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,你贏了");break;}else{System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,平局");break;}}}else if(b.equals("n")){if(computerjifen>gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,你輸了");break;}else if(computerjifen<gamerjifen){System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,你贏了");break;}else{System.out.println("-------------------");System.out.println(Computer.choos(a)+"vs玩家");System.out.println(Computer.choos(a)+"的積分為"+computerjifen+",玩家的積分為"+gamerjifen);System.out.println("對戰次數"+count+",出的一樣的情況有"+equ+"次");System.out.println("結果,平局");break;}}else{System.out.println("請重新開始!!!!");break;}}while(true);sd.close();}}

package game;//import java.util.Scanner;public class GameTest {public static void main(String[] args) {Game.starGame();}}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.