求大佬帮忙指导一下java入门基础题

weixin_50323868 2021-04-04 11:51:40
模拟一个猜拳游戏,有三个玩家,每个玩家,每一回合玩家可以出石 头、剪刀、布其中的一个,并且是随机的。回合进行,打印每位玩家 出的拳,若平局或只淘汰掉一位玩家,则开启下一轮,直到有人获胜 为止,并打印获胜者的信息。 请定义每个用户的行为和游戏的行为,要求体现面向对象的思想
...全文
223 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
KeepSayingNo 2021-04-06
  • 打赏
  • 举报
回复
先制定一个规则,什么能赢什么,然后进行采用生成随机数的方式生成出的什么东西,例如随机生成0到100的整数,对这些数模3,得到0,1,2;然后0:剪刀;1代表石头;2代表布
尼坤神 2021-04-05
  • 打赏
  • 举报
回复

package com.initialdt.test;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MoraGame {

    /**
     * 玩家
     */
    private static List<Person> persons = new ArrayList<>();

    public static void main(String[] args) {
        System.out.print("请输入玩家数:");
        Scanner scanner = new Scanner(System.in);
        int p = scanner.nextInt();
        if (p == 0) {
            System.out.println("无玩家参与,游戏结束。");
            return;
        }

        getPersons(p);
        setPersonMora();
        while (persons.size() > 1) {
            persons = calculation();
            setPersonMora();
        }
        persons.forEach(winner -> System.out.println("最终玩家:" + winner.getName() + "胜出!"));
    }

    private static void setPersonMora() {
        for (Person person : persons) {
            Mora mora = getMora(getRandom(3));
            if (mora == null) {
                continue;
            }
            System.out.println("玩家:" + person.getName() + ",出:" + mora.getName());
            person.setMora(mora);
        }
    }


    private static List<Person> calculation() {
        Person temp = null;
        List<Person> remaining = new ArrayList<>();
        for (Person person : persons) {
            if (temp == null) {
                temp = person;
            } else {
                if (temp.getMora().getKey() == 1 && person.getMora().getKey() == 3) {
                    // 1可以吃3 即剪刀吃布
                    System.out.println(person.getName() + "被淘汰!");
                    remaining.add(temp);
                } else if (temp.getMora().getKey() == person.getMora().getKey()) {
                    System.out.println(temp.getName() + "出" + temp.getMora().getName()
                            + person.getName() + "出" + person.getMora().getName()
                            + "两者持平!");
                    remaining.add(temp);
                    remaining.add(person);
                } else if (temp.getMora().getKey() < person.getMora().getKey()) {
                    System.out.println(temp.getName() + "被淘汰!");
                    remaining.add(person);
                    temp = person;
                } else {
                    System.out.println(person.getName() + "被淘汰!");
                    remaining.add(temp);
                }
            }
        }
        return repeat(remaining);
    }

    private static List<Person> repeat(List<Person> ps) {
        if (ps.isEmpty()) {
            return ps;
        }
        List<Person> repeats = new ArrayList<>();
        ps.forEach(p -> {
            long l = repeats.stream().filter(repeat -> repeat.getId() == p.getId()).count();
            if (l == 0) {
                repeats.add(p);
            }
        });
        return repeats;
    }

    private static void getPersons(int p) {
        for (int i = 1; i <= p; i++) {
            persons.add(new Person(i, "玩家" + i));
        }
    }

    private static int getRandom(int seed) {
        return (int) (Math.random() * seed) + 1;
    }

    private static Mora getMora(int key) {
        for (Mora mora : Mora.values()) {
            if (mora.getKey() == key) {
                return mora;
            }
        }
        return null;
    }

    private enum Mora {
        // 剪刀
        scissors(1, "剪刀"),
        // 石头
        stone(2, "石头"),
        // 布
        cloth(3, "布");

        private final int key;

        private final String name;

        Mora(int key, String name) {
            this.key = key;
            this.name = name;
        }

        public int getKey() {
            return key;
        }

        public String getName() {
            return name;
        }
    }

    private static class Person {
        private int id;
        private String name;

        private Mora mora;

        public Person(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Mora getMora() {
            return mora;
        }

        public void setMora(Mora mora) {
            this.mora = mora;
        }
    }
}

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧