集合文字小游戲 -> 武將對戰
- 1. 專案需求
- 2. 需求分析
- 2.1 建立一個武將物件(編號,姓名,所屬地,性別出生年,去世年,武力值)
- 2.2 獲取字串型別的武將資料
- 2.3 拆分武將資料
- 2.4 輸出所有武將的資訊
- 2.5 壽命最高的武將篩選
- 2.6 武力值最高的武將篩選
- 2.7 男性武力值最低的武將篩選
- 2.8 女性武力值最高的武將篩選
- 2.9 武力值最高的前十名
- 2.10 對戰
- 2.10.1 武將選擇
- 2.10.2 獲取武將資訊并計算數值
- 2.10.3 對戰回合資訊
- 2.10.4 勝負判斷
- 2.10.5 對戰模塊完整代碼
- 3 程式完整代碼(待改善)
1. 專案需求
- 建立一個武將物件(編號,姓名,所屬地,性別出生年,去世年,武力值)
- 獲取字串型別的武將資料.
- 拆分武將資料并將資料封裝到物件中
- 輸出所有武將的資訊
- 壽命最高的武將篩選
- 武力值最高的武將篩選
- 男性武力值最低的武將篩選
- 女性武力值最高的武將篩選
- 武力值最高的前十名
- 對戰
2. 需求分析
2.1 建立一個武將物件(編號,姓名,所屬地,性別出生年,去世年,武力值)
- 新建一個Person類
- 屬性有(編號,姓名,所屬地,性別出生年,去世年,武力值)
- toString方法
- get和set方法
- 構造方法

private String id;
private String name;
private String location;
private String sex;
private String birth;
private String death;
private String strength;
-
提供帶參構造和無參構造
-
重寫toString方法
這里型別都定義為String型別,方便資料的封裝
2.2 獲取字串型別的武將資料
- 以IO的形式獲取資料
- 使用方法:
- InputStreamReader
- FileInputStream
- String
- StringBuilder
- 檔案讀取
- 轉換字串

//資料
private static String data() throws IOException {
//創建輸入物件
InputStreamReader fis = new InputStreamReader(new FileInputStream("jingjie\\22.txt"), "UTF-8");
//創建String物件
String data = "";
//創建StringBuilder物件進行字串拼接
StringBuilder sb = new StringBuilder(data);
while (true) {
//獲取位元組
int read = fis.read();
//判斷是否讀取完畢
if (read != -1) {
//輸出
sb.append((char) read);
} else {
break;
}
}
fis.close();
return sb.toString();
}
這里采用檔案讀取的方式獲取
因為資料量比較大,所以這里定義一個方法 private static String data(),以獲取資料 .
InputStreamReader fis = new InputStreamReader(new FileInputStream("jingjie\\22.txt"), "UTF-8");
因為底層編碼方式不一樣,中文可能會出現亂碼,所以這里呼叫InputStreamReader物件方法,來創建FileInputStream物件,用來設定編碼,這里編碼采用UTF-8
用StringBuilder來進行資料的拼接
while (true) {
//獲取位元組
int read = fis.read();
//判斷是否讀取完畢
if (read != -1) {
//輸出
sb.append((char) read);
} else {
break;
}
}
fis.close();
這里回圈獲取每一個位元組,并呼叫append的方法來進行拼接,以得到一個完整的字串
2.3 拆分武將資料
- 我們得到的武將資料是一個字串,所以需要進行字串切割
- 使用方法:
- ArrayList集合
- split字串切割
- 構造方法

//獲取資料
String data = data();
//創建集合物件
ArrayList<Person> personList = new ArrayList<>();
//拆分單條資料
String[] dataString = data.split("\n");
//拆分屬性并存入集合
for (String s : dataString) {
String[] split = s.split("\t");
personList.add(new Person(split[0],split[1],split[2],split[3],split[4],split[5],split[6]));
}
String[] dataString = data.split("\n");
因為得到的是一個字串物件,所以我們首先對字串進行截取,呼叫split方法,以\n為間隔,獲取每一個單獨的物件,并存入字串陣列中
String[] split = s.split("\t");
再用增強for來遍歷陣列,再次呼叫split方法,以\t為間隔,獲取字串陣列中的每一個元素
personList.add(new Person(split[0],split[1],split[2],split[3],split[4],split[5],split[6]));
這里我們選用Person物件的帶參構造,將武將封裝進物件中
2.4 輸出所有武將的資訊
- 輸出所有武將庫中所有武將的資訊,沒什么好說的,很簡單的增強for
-
static void print(ArrayList<Person> personList) {
for (Person person : personList) {
System.out.println(person);
}
}
使用增強for遍歷列印所有武將的資訊
2.5 壽命最高的武將篩選
- 篩選獲取壽命最高的武將資料
- 使用方法:
- sort集合排序
- Comparator排序介面
- lambda運算式實作排序介面規則

private static void Maxlife(ArrayList<Person> personList) {
Collections.sort(personList, ((o1, o2) -> {
int i = Integer.parseInt(o1.getDeath()) - Integer.parseInt(o1.getBirth());
int j = Integer.parseInt(o2.getDeath()) - Integer.parseInt(o2.getBirth());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
}
lambda運算式重寫Comparator排序規則,以獲取壽命最大值
int i = Integer.parseInt(o1.getDeath()) - Integer.parseInt(o1.getBirth());
int j = Integer.parseInt(o2.getDeath()) - Integer.parseInt(o2.getBirth());
if (i == j) {
return 1;
} else {
return j - i;
}
2.6 武力值最高的武將篩選
類似于上一個排序,只是重寫的排序規則不同,so不多bb直接上代碼

static void maxStrength(ArrayList<Person> personList) {
Collections.sort(personList, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
2.7 男性武力值最低的武將篩選
- 獲取男性武力值最低的武將,注意這里只要男性,所以,我們要先進行篩選
- 使用方法:
- 創建集合
- Stream流編程,篩選
- filter()篩選方法
- forEach()終結方法
- sort陣列排序,重寫介面,類似前兩個功能

static void manMinStrength(ArrayList<Person> personList) {
//新建一個集合
ArrayList<Person> list = new ArrayList<>();
//Stream流編程,篩選出男性武將
personList.stream().filter(s -> s.getSex().equals("男")).forEach(list::add);
//對新陣列進行排序
Collections.sort(list, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return i - j;
}
}));
這里重點說一下Stream流編程
personList.stream().filter(s -> s.getSex().equals("男")).forEach(list::add);
將集合用stream方法轉換成流
filter()是一個篩選的方法,回傳值為true則留下,反之跳過
forEach()十個終結方法,用list::add添加入新的陣列中Ps:就是呼叫陣列的add方法,lamdba的簡化格式
陣列排序,和之前的方法類似,重寫規則實作介面
2.8 女性武力值最高的武將篩選
- 和上一個方法類似,直接上代碼

static void womenMaxStyength(ArrayList<Person> personList) {
//創建新集合
ArrayList<Person> list = new ArrayList<>();
//Stream流編程,篩選,存入新陣列
personList.stream().filter(s -> s.getSex().equals("女")).forEach(list::add);
//新陣列排序
Collections.sort(list, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
2.9 武力值最高的前十名
見2.6武力值最高的武將篩選,輸出陣列中前十的元素即可
2.10 對戰
讓用戶輸入編號,來選擇武將,系統隨機匹配對手,進行回合交戰,并輸出對戰結果
2.10.1 武將選擇

這個很簡單,就是輸入,然后根據輸入的編號,獲取武將物件
if (a > 473) {
System.out.println("您輸入的有誤,請重新輸入(1-474)輸入-1退出");
}
if (a == -1) {
System.exit(0);
}
這里要加一段健壯性判斷,以免用戶輸入錯誤
2.10.2 獲取武將資訊并計算數值

將資料型別轉換,并根據系數確定血量以及攻擊力
2.10.3 對戰回合資訊

每回合減血,直到有一方死亡
2.10.4 勝負判斷

判斷游戲的結果,根據血量進行判斷
2.10.5 對戰模塊完整代碼
static void pk(ArrayList<Person> personList) throws InterruptedException {
//pk
Scanner sc = new Scanner(System.in);
Random r = new Random();
//武將選擇
System.out.println("請輸入你要選擇的武將編號: (1-474)輸入-1退出");
int a = sc.nextInt() - 1;
while (a > 473){
if (a == -1) {
System.exit(0);
}else{
System.out.println("您輸入的有誤,請重新輸入(1-474)輸入-1退出");
a = new Scanner(System.in).nextInt() - 1;
}
}
//隨機一個武將對戰
int b = r.nextInt(474);
while (b == a) {
b = new Random().nextInt(474);
}
//輸出對戰資訊
System.out.println("您選擇的武將是: " + personList.get(a));
Thread.sleep(1000);
System.out.println("您對戰的武將是: " + personList.get(b));
Thread.sleep(1000);
//獲取兩個武將的資訊
Person p1 = personList.get(a);
Person p2 = personList.get(b);
//血量型別轉換
int d1 = Integer.parseInt(p1.getDeath());
int h1 = Integer.parseInt(p1.getBirth());
int d2 = Integer.parseInt(p2.getDeath());
int h2 = Integer.parseInt(p2.getBirth());
//計算血量
int health1 = (d1 - h1) * 100;
int health2 = (d2 - h2) * 100;
//武力值型別轉換
int s1 = Integer.parseInt(p1.getStrength());
int s2 = Integer.parseInt(p2.getStrength());
//輸出血量資訊
System.out.println("您的血量" + health1);
Thread.sleep(500);
System.out.println("對方的血量" + health2);
Thread.sleep(500);
//計算傷害值
int abs = Math.abs(s1 - s2);
//對戰進行
while (health1 >= 0 && health2 >= 0) {
//根據武力值判定傷害量
if (s1 > s2) {
//每回合剩余血量
health1 -= abs * 5;
health2 -= abs * 10;
//輸出對戰資訊
System.out.println("您對對方造成了" + (abs * 10) + "點傷害");
Thread.sleep(50);
System.out.println("對方對您造成了" + (abs * 5) + "點傷害");
Thread.sleep(50);
System.out.println("您的血量剩余" + health1);
Thread.sleep(50);
System.out.println("對方的血量剩余" + health2);
Thread.sleep(50);
System.out.println("-------------------------------------------");
} else {
//每回合剩余血量
health1 -= abs * 10;
health2 -= abs / 5;
//輸出對戰資訊
System.out.println("您對對方造成了" + abs * 10 + "點傷害");
Thread.sleep(50);
System.out.println("對方對您造成了" + (abs * 5) + "點傷害");
Thread.sleep(50);
System.out.println("您的血量剩余" + health1);
Thread.sleep(50);
System.out.println("對方的血量剩余" + health2);
Thread.sleep(50);
System.out.println("-------------------------------------------");
}
}
//勝負判斷
if (health1 > 0) {
System.out.println("您獲勝了!");
} else {
if (health2 > 0) {
System.out.println("您死亡了!");
} else {
System.out.println("同歸于盡!");
}
}
}
3 程式完整代碼(待改善)
package Exercise.Map.Demo01;
import java.io.*;
import java.util.*;
public class Demo01 {
public static void main(String[] args) throws InterruptedException, IOException {
//獲取資料
String data = data();
//創建集合物件
ArrayList<Person> personList = new ArrayList<>();
//拆分單條資料
String[] dataString = data.split("\n");
//拆分屬性并存入集合
for (String s : dataString) {
String[] split = s.split("\t");
personList.add(new Person(split[0], split[1], split[2], split[3], split[4], split[5], split[6]));
}
//界面
while (true) {
System.out.println("1.所有武將");
System.out.println("2.壽命最高的武將");
System.out.println("3.武力值最高的武將");
System.out.println("4.男性武力值最低的武將");
System.out.println("5.女性武力值最高的武將");
System.out.println("6.武力值最高的前十名");
System.out.println("7.小游戲:戰斗");
System.out.println("0.退出");
System.out.println();
int i = new Scanner(System.in).nextInt();
switch (i) {
case 1:
//所有武將
print(personList);
case 2:
//壽命最高的武將
Maxlife(personList);
System.out.println(personList.get(0));
break;
case 3:
//武力值最高的武將
maxStrength(personList);
System.out.println(personList.get(0));
break;
case 4:
//男性武力值最低的武將
manMinStrength(personList);
break;
case 5:
//女性武力值最高的武將
womenMaxStyength(personList);
break;
case 6:
//武力值最高的前十名
maxStrength(personList);
System.out.println(personList.get(0));
System.out.println(personList.get(1));
System.out.println(personList.get(2));
System.out.println(personList.get(3));
System.out.println(personList.get(4));
System.out.println(personList.get(5));
System.out.println(personList.get(6));
System.out.println(personList.get(7));
System.out.println(personList.get(8));
System.out.println(personList.get(9));
break;
case 7:
//pk
pk(personList);
break;
case 0:
//退出
System.exit(0);
default:
System.out.println("輸入有誤!");
break;
}
}
}
//pk
static void pk(ArrayList<Person> personList) throws InterruptedException {
//pk
Scanner sc = new Scanner(System.in);
Random r = new Random();
//武將選擇
System.out.println("請輸入你要選擇的武將編號: (1-474)輸入-1退出");
int a = sc.nextInt() - 1;
while (a > 473){
if (a == -1) {
System.exit(0);
}else{
System.out.println("您輸入的有誤,請重新輸入(1-474)輸入-1退出");
a = new Scanner(System.in).nextInt() - 1;
}
}
//隨機一個武將對戰
int b = r.nextInt(474);
while (b == a) {
b = new Random().nextInt(474);
}
//輸出對戰資訊
System.out.println("您選擇的武將是: " + personList.get(a));
Thread.sleep(1000);
System.out.println("您對戰的武將是: " + personList.get(b));
Thread.sleep(1000);
//獲取兩個武將的資訊
Person p1 = personList.get(a);
Person p2 = personList.get(b);
//血量型別轉換
int d1 = Integer.parseInt(p1.getDeath());
int h1 = Integer.parseInt(p1.getBirth());
int d2 = Integer.parseInt(p2.getDeath());
int h2 = Integer.parseInt(p2.getBirth());
//計算血量
int health1 = (d1 - h1) * 100;
int health2 = (d2 - h2) * 100;
//武力值型別轉換
int s1 = Integer.parseInt(p1.getStrength());
int s2 = Integer.parseInt(p2.getStrength());
//輸出血量資訊
System.out.println("您的血量" + health1);
Thread.sleep(500);
System.out.println("對方的血量" + health2);
Thread.sleep(500);
//計算傷害值
int abs = Math.abs(s1 - s2);
//對戰進行
while (health1 >= 0 && health2 >= 0) {
//根據武力值判定傷害量
if (s1 > s2) {
//每回合剩余血量
health1 -= abs * 5;
health2 -= abs * 10;
//輸出對戰資訊
System.out.println("您對對方造成了" + (abs * 10) + "點傷害");
Thread.sleep(50);
System.out.println("對方對您造成了" + (abs * 5) + "點傷害");
Thread.sleep(50);
System.out.println("您的血量剩余" + health1);
Thread.sleep(50);
System.out.println("對方的血量剩余" + health2);
Thread.sleep(50);
System.out.println("-------------------------------------------");
} else {
//每回合剩余血量
health1 -= abs * 10;
health2 -= abs / 5;
//輸出對戰資訊
System.out.println("您對對方造成了" + abs * 10 + "點傷害");
Thread.sleep(50);
System.out.println("對方對您造成了" + (abs * 5) + "點傷害");
Thread.sleep(50);
System.out.println("您的血量剩余" + health1);
Thread.sleep(50);
System.out.println("對方的血量剩余" + health2);
Thread.sleep(50);
System.out.println("-------------------------------------------");
}
}
//勝負判斷
if (health1 > 0) {
System.out.println("您獲勝了!");
} else {
if (health2 > 0) {
System.out.println("您死亡了!");
} else {
System.out.println("同歸于盡!");
}
}
}
//女性武力值最高的武將
static void womenMaxStyength(ArrayList<Person> personList) {
//創建新集合
ArrayList<Person> list = new ArrayList<>();
//Stream流編程,篩選,存入新陣列
personList.stream().filter(s -> s.getSex().equals("女")).forEach(list::add);
//新陣列排序
Collections.sort(list, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
System.out.println(list.get(0));
}
//男性武力值最低的武將
static void manMinStrength(ArrayList<Person> personList) {
//新建一個集合
ArrayList<Person> list = new ArrayList<>();
//Stream流編程,篩選出男性武將
personList.stream().filter(s -> s.getSex().equals("男")).forEach(list::add);
//對新陣列進行排序
Collections.sort(list, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return i - j;
}
}));
System.out.println(list.get(0));
}
//輸出
static void print(ArrayList<Person> personList) {
for (Person person : personList) {
System.out.println(person);
}
}
//最大武力值
static void maxStrength(ArrayList<Person> personList) {
Collections.sort(personList, ((o1, o2) -> {
int i = Integer.parseInt(o1.getStrength());
int j = Integer.parseInt(o2.getStrength());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
}
//壽命最高的武將
private static void Maxlife(ArrayList<Person> personList) {
Collections.sort(personList, ((o1, o2) -> {
int i = Integer.parseInt(o1.getDeath()) - Integer.parseInt(o1.getBirth());
int j = Integer.parseInt(o2.getDeath()) - Integer.parseInt(o2.getBirth());
if (i == j) {
return 1;
} else {
return j - i;
}
}));
}
//資料
private static String data() throws IOException {
//創建輸入物件
InputStreamReader fis = new InputStreamReader(new FileInputStream("jingjie\\22.txt"), "UTF-8");
//創建String物件
String data = "";
//創建StringBuilder物件進行字串拼接
StringBuilder sb = new StringBuilder(data);
while (true) {
//獲取位元組
int read = fis.read();
//判斷是否讀取完畢
if (read != -1) {
//輸出
sb.append((char) read);
} else {
break;
}
}
fis.close();
return sb.toString();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/271313.html
標籤:java
上一篇:Spring5總述(三)—— IOC操作Bean管理(Xml注入其他型別屬性)
下一篇:Java實作學生資訊管理系統
