金庸群俠傳
遊戲執行介面:
遊戲實現原始碼:
1 package game.logic; 2 //人類 3 public class Person { 4 public String name; //名字 5 6 Person(){ 7 8 } 9 10 Person(String name){ 11 this.name = name; 12 } 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 }
1 package game.logic; 2 3 //會武功的人類 4 public class KongFuPerson extends Person { 5 6 protected int blood = 1000; // 預設都是1000點血 7 8 KongFuPerson() { 9 10 } 11 12 public int getBlood() { 13 return blood; 14 } 15 16 public void setBlood(int blood) { 17 this.blood = blood; 18 } 19 20 KongFuPerson(String name) { 21 this.name = name; 22 } 23 24 // 普通招數 25 public void commonSkill(KongFuPerson P) { 26 P.blood -= 50; //攻擊敵人50點血 27 } 28 29 //殺招式 30 public void bestSkill(KongFuPerson P) { 31 P.blood -= 100; //攻擊敵人100點血 32 } 33 }
1 package game.logic; 2 3 //武功絕學類(介面) 4 public interface Secrets { 5 public void secrets(KongFuPerson P); 6 }
1 package game.logic; 2 3 //武功高手類 4 public class KongFuMaster extends KongFuPerson implements Secrets{ 5 6 public void secrets(KongFuPerson P) { 7 P.blood -= 300;//攻擊敵人300點血 8 9 } 10 11 }
1 package game.roles; 2 3 import game.logic.KongFuMaster; 4 import game.logic.KongFuPerson; 5 6 //令狐沖類 7 public class Linghuchong extends KongFuMaster{ 8 9 public Linghuchong(){ 10 11 } 12 13 public Linghuchong(String name){ 14 this.name = name; 15 } 16 public void secrets(KongFuPerson P) { 17 // TODO Auto-generated method stub 18 super.secrets(P); 19 System.out.println(this.getName()+"使用武功絕學獨孤九劍攻擊"+P.getName()+"300點血"); 20 } 21 22 public void commonSkill(KongFuPerson P) { 23 // TODO Auto-generated method stub 24 super.commonSkill(P); 25 System.out.println(this.getName()+"使用越女劍法攻擊"+P.getName()+"50點血"); 26 } 27 28 public void bestSkill(KongFuPerson P) { 29 // TODO Auto-generated method stub 30 super.bestSkill(P); 31 System.out.println(this.getName()+"使用華山劍法攻擊"+P.getName()+"100點血"); 32 } 33 34 35 }
1 package game.roles; 2 3 import game.logic.KongFuMaster; 4 import game.logic.KongFuPerson; 5 6 //歐陽鋒類 7 public class Ouyangfeng extends KongFuMaster{ 8 public Ouyangfeng(){ 9 10 } 11 12 public Ouyangfeng(String name){ 13 this.name = name; 14 } 15 public void secrets(KongFuPerson P) { 16 // TODO Auto-generated method stub 17 super.secrets(P); 18 System.out.println(this.getName()+"使用武功絕學蛤蟆功攻擊"+P.getName()+"300點血"); 19 } 20 21 public void commonSkill(KongFuPerson P) { 22 // TODO Auto-generated method stub 23 super.commonSkill(P); 24 System.out.println(this.getName()+"使用毒蛇攻擊"+P.getName()+"50點血"); 25 } 26 27 public void bestSkill(KongFuPerson P) { 28 // TODO Auto-generated method stub 29 super.bestSkill(P); 30 System.out.println(this.getName()+"使用毒蛇柺杖攻擊"+P.getName()+"100點血"); 31 } 32 }
1 package game.app; 2 3 import game.logic.KongFuMaster; 4 import game.roles.Linghuchong; 5 import game.roles.Ouyangfeng; 6 7 import java.util.Scanner; 8 9 public class Game { 10 11 public static Scanner in = new Scanner(System.in); 12 public static int choice;//使用者的選擇 13 public static KongFuMaster player;//玩家 14 public static KongFuMaster computer;//電腦玩家 15 public static boolean isFirst = true;//預設玩家打電腦 16 17 //遊戲角色初始化方法 18 public static void init(){ 19 switch(choice){ 20 case 1: 21 player = new Linghuchong("令狐沖"); 22 computer = new Ouyangfeng("歐陽鋒"); 23 break; 24 case 2: 25 player = new Ouyangfeng("歐陽鋒"); 26 computer = new Linghuchong("令狐沖"); 27 break; 28 } 29 System.out.println("您選擇了:"+ player.getName()); 30 } 31 32 //顯示遊戲選單 33 public static void Menu(){ 34 System.out.println("********歡迎進入金庸群俠傳***********"); 35 System.out.println("1.令狐沖 2.歐陽鋒"); 36 System.out.println("請選擇:"); 37 choice = in.nextInt(); 38 } 39 40 //遊戲PK方法 41 public static void beginCombat(KongFuMaster player , KongFuMaster computer){ 42 //生成一個1-3之間的隨機數 43 int temp = (int)(Math.random()*10)%3; //0-2 44 temp++; 45 switch(temp){ 46 case 1: 47 if(Game.isFirst){ 48 player.commonSkill(computer); 49 }else{ 50 computer.commonSkill(player); 51 } 52 break; 53 case 2: 54 if(Game.isFirst){ 55 player.bestSkill(computer); 56 }else{ 57 computer.bestSkill(player); 58 } 59 break; 60 case 3: 61 if(Game.isFirst){ 62 player.secrets(computer); 63 }else{ 64 computer.secrets(player); 65 } 66 break; 67 } 68 Game.isFirst = !Game.isFirst; 69 } 70 71 //判斷遊戲是否結束 72 public static boolean isComplete(){ 73 if(Game.player.getBlood()<=0||Game.computer.getBlood()<=0){ 74 return true; 75 }else{ 76 return false; 77 } 78 } 79 80 //顯示遊戲結果 81 public static void showResult(){ 82 if(Game.player.getBlood()<=0){ 83 System.out.println("君子報仇十年不晚,我們們後會有期。。。。"); 84 }else{ 85 System.out.println("別羨慕哥,哥只是個傳說。。。。"); 86 } 87 } 88 89 public static void main(String[] args){ 90 Game.Menu();//顯示選單 91 Game.init();//初始化遊戲 92 93 do{ 94 //開始PK 95 Game.beginCombat(player, computer); 96 }while(!Game.isComplete());//判斷是否結束 97 98 Game.showResult();//顯示遊戲結果 99 } 100 }