繼承、介面自我練習時所建立的小遊戲,一開始輸入名稱來建立對戰腳色,之後以輸入招式號碼的方式互相打鬥,最後沒血的一方就輸了。

人物種族

abstract public class Human {
int hp = 100;
int atk = 10;
int def = 4;
int 自我恢復 = 10;
int count = 0; public int 自我恢復() {
if (自我恢復 < 0) {
自我恢復 = 0;
System.err.println("過度使用,自我恢復已失效");
return 自我恢復;
} else {
自我恢復 -= count;
count++;
return 自我恢復;
}
}
}

職業

public interface 戰士 {
int ihp = 10;
int iatk = 4;
int idef = 3;
int 捨身攻擊damage = 20; public int 捨身攻擊(); public void 能力加成(); }

角色

public class 人類戰士 extends Human implements 戰士 {

    private String 玩家名稱;
private String 玩家職業 = "人類戰士";
private int 玩家hp = super.hp;
private int 玩家atk = super.atk;
private int 玩家def = super.def;
int 一般攻擊damage = 10;
String[] 技能 = { "一般攻擊", Integer.toString(一般攻擊damage), "捨身攻擊", Integer.toString(捨身攻擊damage), "自我恢復",
Integer.toString(自我恢復) }; 人類戰士(String name) {
玩家名稱 = name;
} public int 一般攻擊() {
System.out.println(玩家名稱 + "使用了一般攻擊");
return 一般攻擊damage;
} @Override
public int 捨身攻擊() {
System.out.println(玩家名稱 + "使用了捨身攻擊");
return 捨身攻擊damage; } @Override
public int 自我恢復() {
System.out.println(玩家名稱 + "使用了自我恢復,將可恢復" + super.自我恢復 + "血量");
super.自我恢復();
return super.自我恢復;
} @Override
public void 能力加成() { System.out.println();
System.out.println("玩家名稱\t" + this.玩家名稱);
System.out.println("玩家職業\t" + this.玩家職業);
this.玩家hp += ihp;
System.out.println("血量\t" + this.玩家hp);
this.玩家atk += iatk;
System.out.println("攻擊力\t" + this.玩家atk);
this.玩家def += idef;
System.out.println("防禦力\t" + this.玩家def);
} public String get玩家名稱() {
return 玩家名稱;
} public int get玩家hp() {
return 玩家hp;
} public void set玩家hp(int 玩家hp) {
this.玩家hp = 玩家hp;
} public int get玩家atk() {
return 玩家atk;
} public void set玩家atk(int 玩家atk) {
this.玩家atk = 玩家atk;
} public int get玩家def() {
return 玩家def;
} public void set玩家def(int 玩家def) {
this.玩家def = 玩家def;
}
}

主程式

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner; public class TestGame { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
System.out.println("請輸入玩家A的名稱");
System.out.print("=>");
String 玩家A名稱 = sc.nextLine();
System.out.println("請輸入玩家B的名稱");
System.out.print("=>");
String 玩家B名稱 = sc.nextLine(); 人類戰士 玩家A = new 人類戰士(玩家A名稱); 玩家A.能力加成();
LinkedHashMap<String, String> 玩家A技能 = new LinkedHashMap<>();
for (int i = 0; i < 玩家A.技能.length; i += 2) {
玩家A技能.put(玩家A.技能[i], 玩家A.技能[i + 1]);
} 人類戰士 玩家B = new 人類戰士(玩家B名稱);
玩家B.能力加成();
LinkedHashMap<String, String> 玩家B技能 = new LinkedHashMap<>();
for (int i = 0; i < 玩家B.技能.length; i += 2) {
玩家B技能.put(玩家B.技能[i], 玩家B.技能[i + 1]);
} int 玩家A動作 = 0;
int 玩家B動作 = 0;
do {
do {
try {
System.out.println();
System.out.println(玩家A.get玩家名稱() + "請輸入使用招式的順序號碼 ");
int 招式號碼 = 0;
Iterator<Entry<String, String>> iiIterator = 玩家A技能.entrySet().iterator();
while (iiIterator.hasNext()) {
招式號碼++;
Map.Entry<String, String> entry = iiIterator.next();
System.out.println(招式號碼 + ")" + entry.getKey() + "\t預計產生效果(初步)\t" + entry.getValue());
} System.out.print("=>");
int 動作 = Integer.parseInt(sc.nextLine());
if (動作 > 玩家A技能.size()) {
throw new ArithmeticException();
} else {
玩家A動作 = 動作;
break;
}
} catch (Exception e) {
System.err.println("輸入錯誤");
}
} while (true); switch (玩家A動作) {
case 1:
玩家B.set玩家hp(玩家B.get玩家hp() - (玩家A.一般攻擊() - 玩家B.get玩家def()));
System.out.println(玩家B.get玩家名稱() + "受了" + 玩家A.一般攻擊damage + "傷害,血量還剩" + 玩家B.get玩家hp());
break;
case 2:
玩家B.set玩家hp(玩家B.get玩家hp() - (玩家A.捨身攻擊() - 玩家B.get玩家def()));
System.out.println(玩家B.get玩家名稱() + "受了" + 玩家A.捨身攻擊damage + "傷害,血量還剩" + 玩家B.get玩家hp());
玩家A.set玩家hp(玩家A.get玩家hp() - 玩家B.get玩家atk());
System.out.println(玩家A.get玩家名稱() + "受到反擊,損失" + 玩家B.get玩家atk() + "滴血,血量還剩" + 玩家A.get玩家hp());
break;
case 3:
玩家A.set玩家hp(玩家A.get玩家hp() + 玩家A.自我恢復());
System.out.println(玩家A.get玩家名稱() + "血量還剩" + 玩家A.get玩家hp());
break;
default:
break;
}
System.out.println();
if (玩家B.get玩家hp() <= 0 && 玩家A.get玩家hp() <= 0) {
System.out.println("無人生還");
break;
} else if (玩家B.get玩家hp() <= 0) {
System.out.println(玩家A.get玩家名稱() + "獲勝");
break;
} else if (玩家A.get玩家hp() <= 0) {
System.out.println(玩家B.get玩家名稱() + "獲勝");
break;
} do {
try {
System.out.println(玩家B.get玩家名稱() + "請輸入使用招式的順序號碼 ");
int 招式號碼 = 0;
Iterator<Entry<String, String>> iiIterator = 玩家A技能.entrySet().iterator();
while (iiIterator.hasNext()) {
招式號碼++;
Map.Entry<String, String> entry = iiIterator.next();
System.out.println(招式號碼 + ")" + entry.getKey() + "\t預計產生效果(初步)\t" + entry.getValue());
}
System.out.print("=>");
int 動作 = Integer.parseInt(sc.nextLine());
if (動作 > 玩家B技能.size()) {
throw new ArithmeticException();
} else {
玩家B動作 = 動作;
break;
}
} catch (Exception e) {
System.err.println("輸入錯誤");
}
} while (true); switch (玩家B動作) {
case 1:
玩家A.set玩家hp(玩家A.get玩家hp() - (玩家B.一般攻擊() - 玩家A.get玩家def()));
System.out.println(玩家A.get玩家名稱() + "受了" + 玩家B.一般攻擊damage + "傷害,血量還剩" + 玩家A.get玩家hp());
break;
case 2:
玩家A.set玩家hp(玩家A.get玩家hp() - (玩家B.捨身攻擊() - 玩家A.get玩家def()));
System.out.println(玩家A.get玩家名稱() + "受了" + 玩家B.捨身攻擊damage + "傷害,血量還剩" + 玩家A.get玩家hp());
玩家B.set玩家hp(玩家B.get玩家hp() - 玩家A.get玩家atk());
System.out.println(玩家B.get玩家名稱() + "受到反擊,損失" + 玩家A.get玩家atk() + "滴血,血量還剩" + 玩家B.get玩家hp());
break;
case 3:
玩家B.set玩家hp(玩家B.get玩家hp() + 玩家B.自我恢復());
System.out.println(玩家B.get玩家名稱() + "血量還剩" + 玩家B.get玩家hp());
break;
default:
break;
}
System.out.println();
if (玩家B.get玩家hp() <= 0 && 玩家A.get玩家hp() <= 0) {
System.out.println("無人生還");
break;
} else if (玩家B.get玩家hp() <= 0) {
System.out.println(玩家A.get玩家名稱() + "獲勝");
break;
} else if (玩家A.get玩家hp() <= 0) {
System.out.println(玩家B.get玩家名稱() + "獲勝");
break;
} } while (true);
}
}

最新文章

  1. C/C++二维数组分配内存
  2. STM32之PWM波形输出配置总结
  3. java 语言规范 java language specifications
  4. RelativeLayout实现左中右布局
  5. 直接运行可执行文件linux终端一闪而过
  6. OpenLDAP配置信息记录
  7. css3 一些属性
  8. easyui使用时出现这个Uncaught TypeError: Cannot read property &#39;combo&#39; of undefined
  9. 静态页面参数传递&amp;回调函数写法&amp;快速排序的实现方法
  10. 通过定时监听input框来实现onkeyup事件-
  11. 二分查找(非递归JAVA)
  12. Unity3D 热更新方案总结
  13. day6_自定义类型转换
  14. C++复习:类和对象
  15. linux 使用spinlock的配对关系问题
  16. hdu 4941 map的使用
  17. mysql--对库,表基本操作语句,增删改查
  18. Ubuntu之设置应用开机自启动
  19. ajax local.href不跳转的原因之一
  20. python学习,day3:函数式编程,带return

热门文章

  1. 总结Jquery中获取自定义属性使用.attr()和.data()以及.prop()的区别
  2. python 全栈开发笔记 2
  3. *args和**kwargs的区别
  4. ng-model-options 时延
  5. LeetCode 547 朋友圈
  6. SharePoint Framework 基于团队的开发(二)
  7. 说一下syslog日志吧~~~
  8. selenium中的alter弹框
  9. day14 内置函数二
  10. java+jenkins+testng+selenium+ant