猜拳遊戲
阿新 • • 發佈:2017-06-27
void end 當前 tin .get oid nbsp err sta
package ppt10lang包.猜拳遊戲; public abstract class Player { private String name;//用戶名 private int score;//積分 public Player() { } public Player(String name) { super(); this.name = name; } public String getName() { return name; }public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } /** * 出拳 */ public abstract int showFist(); }
package ppt10lang包.猜拳遊戲; import java.util.Scanner;public class Person extends Player{ public Person(String pname){ super(pname); } @Override public int showFist() { System.out.println("請用戶輸入1石頭 2剪刀 3布"); Scanner input = new Scanner(System.in); int fist = input.nextInt(); return fist; } }
package ppt10lang包.猜拳遊戲; import java.util.Random; /** * 電腦出拳 * @author Administrator * */ public class Computer extends Player{ public Computer(String cname){ super(cname); } @Override public int showFist() { //隨機數 1-3 Random random = new Random(); int fist = random.nextInt(3) +1; return fist; } }
package ppt10lang包.猜拳遊戲; public class Game { private Person p; private Computer c; // private int i = 1; public Game(Person p,Computer c){ this.p = p; this.c = c; } /** * pk * 判斷當前局的輸贏結果 */ public void pk(){ //提升部分:五局 for (int i = 1; i <= 5; i++) { System.out.println("第" + i +"局"); //獲取玩家出拳的數字 int pfist = p.showFist(); //獲取電腦出拳的數字 int cfist = c.showFist(); //獲取玩家出拳的具體 String pValue = getFistValue(pfist); //獲取電腦出拳的具體 String cValue = getFistValue(cfist); //比較規則 if ((pfist == 1 && cfist == 2) ||(pfist == 2 && cfist == 3) || (pfist == 3 && cfist == 1) ) { System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "電腦出拳:" + cValue); System.out.println("恭喜你:" + p.getName() ); //用戶積分 +1 p.setScore( p.getScore() + 1 ); }else if (pfist == cfist) { System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "電腦出拳:" + cValue); System.out.println("平局" ); }else{ System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "電腦出拳:" + cValue); System.out.println("你輸了" + ",電腦:" + c.getName() + "贏了"); c.setScore( c.getScore() + 1 ); } } // i++; // 提升完之後:調用顯示結果的方法 showResult(); } //獲取猜拳對應的中文 public String getFistValue(int fist){ String fistValue = ""; switch (fist) { case 1: fistValue = "石頭"; break; case 2: fistValue = "剪刀"; break; case 3: fistValue = "布"; break; default: break; } return fistValue; } //最終結果 public void showResult() { System.out.println("最終結果:"); System.out.println(p.getName() +"最終積分: " + p.getScore()); System.out.println(c.getName() +"最終積分: " + c.getScore()); } }
package ppt10lang包.猜拳遊戲; import java.util.Scanner; public class TestGame { public static void main(String[] args) { // 玩家對象 Person p = new Person("小白"); // 電腦對象 Computer c = new Computer("電腦CC"); Game game = new Game(p, c); // game.pk(); Scanner input = new Scanner(System.in); while (true) { game.pk(); System.out.println("是否繼續,繼續Y"); String str = input.next(); //忽略大小寫 if (!"Y".equalsIgnoreCase(str)) { break; } } //未提升之前:調用顯示結果 // game.showResult(); } }
猜拳遊戲