java 集合小游戏 ->武将对战V1.0.0(待完善...)

1. 项目需求

  1. 建立一个武将对象(编号,姓名,所属地,性别出生年,去世年,武力值)
  2. 获取字符串类型的武将数据.
  3. 拆分武将数据并将数据封装到对象中
  4. 输出所有武将的信息
  5. 寿命最高的武将筛选
  6. 武力值最高的武将筛选
  7. 男性武力值最低的武将筛选
  8. 女性武力值最高的武将筛选
  9. 武力值最高的前十名
  10. 对战

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();
    }
}

  • 35
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 28
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aming_sth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值