50,527
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}