[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; /* * 模拟登录,给三次机会,并提示还有几次.如果登录成功,就可以玩猜数字小游戏了. * ...
随机推荐
- X-template
<body> <div id="app"> <hello-world></hello-world> </div> < ...
- 用 LSTM 做时间序列预测的一个小例子(转自简书)
问题:航班乘客预测 数据:1949 到 1960 一共 12 年,每年 12 个月的数据,一共 144 个数据,单位是 1000 下载地址 目标:预测国际航班未来 1 个月的乘客数 import nu ...
- Docker搭建LNMP网站平台
1.自定义网络 [root@linux-docker01 ~]# docker network create lnmp 67e7e0736b2c58f8f81eed50130803b34be0583f ...
- MVP 实践
今天有时间看了看google的官方文档,下载todo源码,仔细研读了一下,觉得其思想对开发还是有很大帮助的.个人认为,MVP通过Activity与业务逻辑的解耦,其作为Controller的职责更加单 ...
- hdu1011(树形背包)(提供一个特殊样例)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...
- RN android真机调试找不到设备
待完成…… 1.adb驱动安装 2.手机设置 3.添加adb_usb.ini文件
- 使用bitsadmin.exe 下载文件,配合bcn.bat玩出更多的花样~~
bitsadmin的简单介绍与基本用法: bitsadmin.exe 可以用来在windows 命令行下下载文件.bitsadmin是windows 后台智能传输服务的一个工具,windows 的自动 ...
- Shell 使用 expr 进行数学运算
1.语法格式: 第一种:expr $num1 operator $num2 第二种:$(($num1 operator $num2)) 2.expr 操作符: 注意:这里比较为true,返回 1.只支 ...
- HAProxy原理和配置
HAProxy原理和配置 目录 1.HAProxy简介 2.haproxy安装和配置说明 proxies配置参数 bind配置 Balance配置 基于cookie的会话绑定 统计接口启用相关的参数 ...
- 模块四-shutil模块
shutil模块 高级的文件处理模块 主要是文件的处理,移动,压缩和解压缩 shutil模块的使用方法: shutil.copyfile()#拷贝文件 shutil.copy()#拷贝文件和权限 sh ...