[Java] 練習用對戰小遊戲
繼承、介面自我練習時所建立的小遊戲,一開始輸入名稱來建立對戰腳色,之後以輸入招式號碼的方式互相打鬥,最後沒血的一方就輸了。
人物種族
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);
}
}
[Java] 練習用對戰小遊戲的更多相关文章
- 中國區的代理協議的韓國遊戲廠商PatiGames
“與阿裏巴巴簽署旗下游戲「突突三國」在中國區的代理協議的韓國遊戲廠商PatiGames決定與阿裏巴巴終止合作.”相信這條前不久報導的新聞,很多人並不陌生,但這背後其實並不像表面那樣簡單.早在今年4月P ...
- Big Data應用:以"玩家意見"之數據分析來探討何謂"健康型線上遊戲"(上)
首先,所有資料都可以從網路上找到,只是我做了一些分析與整理而已.純粹分享心得~~ 最近再做研究的時候我跟我的同事K先生在某次偶然的討論中發現了一件有趣的事情. [疑~~~~~~~新楓之谷的玩家人氣指數 ...
- Java學習筆記(基本語法)
本文件是以學習筆記的概念為基礎,用於自我的複習紀錄,不過也開放各位的概念指證.畢竟學習過程中難免會出現觀念錯誤的問題.也感謝各位的觀念指證. 安裝JDK 在Oracle網站中找自己系統的JDK下載位置 ...
- [心得] 如何利用liquibase進行資料庫版本控制 - 實際練習
透過上一篇的基本觀念介紹,希望大家應該有一點點感覺了! 這篇我們就來做個簡單的版本演練,加深印象吧! 我使用的環境如下 System : Windows 7 Database : SQL Server ...
- [转]2010 Ruby on Rails 書單 與 練習作業
原帖:http://wp.xdite.net/?p=1754 ========= 學習 Ruby on Rails 最快的途徑無非是直接使用 Rails 撰寫產品.而這個過程中若有 mentor 指導 ...
- Java学习过程中的总结的小知识点(长期更新)
Java学习过程中的总结的小知识点 (主要是自己不会的知识和容易搞错的东西) 计算某个程序运行的时间 long stime=System.currentTimeMillis(); copy3(file ...
- Java内存管理的9个小技巧
Java内存管理的9个小技巧很多人都说“Java完了,只等着衰亡吧!”,为什么呢?最简单的的例子就是Java做的系统时非常占内存!一听到这样的话,一定会有不少人站出来为Java辩护,并举出一堆的性能测 ...
- Java 课程设计 "Give it up"小游戏(团队)
JAVA课程设计 "永不言弃"小游戏(From :Niverse) 通过Swing技术创建游戏的登陆注册界面,使用mySQL数据库技术完成用户的各项信息保存和游戏完成后的成绩保存. ...
- 《java入门第一季》之类小案例(模拟用户登录)
首先是做一个用户登录的小案例.在此基础上加入其它逻辑. import java.util.Scanner; /* * 模拟登录,给三次机会,并提示还有几次.如果登录成功,就可以玩猜数字小游戏了. * ...
随机推荐
- PAT A1103
PAT A1103 标签(空格分隔): PAT 解题思路: DFS #include <cstdio> #include <vector> using namespace st ...
- Python 函数的描述
函数 函数是带名字的代码块,用于完成具体的工作. 要执行函数定义的特定任务,可调用该函数.需要在程序中多次执行同一项任务时,你无需反复编写完成该任务的代码,而只需调用执行该任务的函数,让Python运 ...
- db2 常见错误以及解决方案[ErrorCode SQLState]
操作数据库流程中,遇到许多疑问,很多都与SQL CODE和SQL State有关,现在把一个完整的SQLCODE和SQLState不正确信息和有关解释作以下说明,一来可以自己参考,对DB2不正确自行找 ...
- Spark笔记
Spark基础 第一节:什么是Spark?Spark的特点和结构 1.什么是Spark? Spark是一个针对大规模数据处理的快速通用引擎. 类似MapReduce,都进行数据的处理 2.Spark的 ...
- n个骰子的点数之和
题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 解题思路:动态规划 第一步,确定问题解的表达式.可将f(n, s) 表示n个骰子点数的和为s的排列 ...
- 简单网络管理协议(SNMP)
SNMP是TCP/IP网络中应用最为广泛的网络管理协议,工作在TCP/IP参考模型的应用层,是一种面向无连接的协议 功能:SNMP的功能是使网络设备之间能方便的交换管理信息,从而使网络管理员了解网络运 ...
- cf A Simple Task---线段树
Examples Input 10 5abacdabcda7 10 05 8 11 4 03 6 07 10 1 Output cbcaaaabdd Input 10 1agjucbvdfk1 10 ...
- my first homepage
<!DOCTYPE html><html><head><style type="text/css">p{ text-indent:2 ...
- UML 类图口诀
UML类图口诀: (10G:实线继承)空三角实线指向被继承, (虚实:虚线实现)空三角虚线指向被实现, (恐惧:空棱形聚合)空棱形实线(棱形)指向聚合整体.部分作为成员变量. (十足:实棱形组合)实棱 ...
- border-radius,box-shadow兼容性解决办法
css3 border-radius不支持IE8/IE7的四种解决方法 标签: cssborder-radius兼容性 时间:2016-07-18 css3 border-radius用于设置HT ...