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();}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。