模拟球队比赛抽签规则,输出冠军之路

package com.youjiuye.map;

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

/**
 * 
 * @author 丢了风筝的线
 * @see 球队基类
 */
public class FootballTeam {
	private String name;

	// 比赛得分
	private double score;

	// 积分,每赢一场加一分
	private int integral;

	// 记录自己比赛队伍
	private List<FootballTeam> armsList = new ArrayList<FootballTeam>();

	// 记录初始的每个球队
	private static List<FootballTeam> list = new ArrayList<FootballTeam>();
	static {
		list.add(new FootballTeam("America"));
		list.add(new FootballTeam("Chan"));
		list.add(new FootballTeam("Japan"));
		list.add(new FootballTeam("Russia"));
		list.add(new FootballTeam("England"));
		list.add(new FootballTeam("Australian"));
		list.add(new FootballTeam("Malaysia"));
		list.add(new FootballTeam("India"));
		list.add(new FootballTeam("Philippines"));
		list.add(new FootballTeam("Afghan"));
		list.add(new FootballTeam("Pakistan"));
		list.add(new FootballTeam("Nz"));
		list.add(new FootballTeam("Brazil"));
		list.add(new FootballTeam("Beligan"));
		list.add(new FootballTeam("Korea"));
		list.add(new FootballTeam("Italy"));

	}

	public FootballTeam() {
	}

	public FootballTeam(String name) {
		super();

		this.name = name;
	}

	public List<FootballTeam> getArmsList() {
		return armsList;
	}

	public void setArmsList(List<FootballTeam> armsList) {
		this.armsList = armsList;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public String getName() {
		return name;
	}

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

	public static List<FootballTeam> getList() {
		return list;
	}

	public static void setList(List<FootballTeam> list) {
		FootballTeam.list = list;
	}

	@Override
	public boolean equals(Object obj) {
		FootballTeam ft = (FootballTeam) obj;

		return this.name == ft.name;
	}

	@Override
	public String toString() {
		return "FootballTeam [name=" + name + "]";
	}

}

package com.youjiuye.map;

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

/**
 * 
 * @author 丢了风筝的线
 *
 */
public class Work03 {
	public static void main(String[] args) {

		// 初始有的队伍
		List<FootballTeam> ft = FootballTeam.getList();

		// 存放第一次比赛的队伍
		List<FootballTeam> firstgamefts = new ArrayList<FootballTeam>();

		for (int i = 0; i < 4; i++) {
			int j = 4;
			groupGameCoil(ft, firstgamefts, i, j);
		}
		System.out.println("16进8");
		System.out.println(firstgamefts);

		// 存放第二次比赛的队伍
		List<FootballTeam> secondgamefts = new ArrayList<FootballTeam>();
		for (int i = 0; i < 2; i++) {
			int j = 4;
			groupGameCoil(firstgamefts, secondgamefts, i, j);
		}
		System.out.println("8进4");
		System.out.println(secondgamefts);

		// 存放第三次比赛的队伍
		List<FootballTeam> thirdgamefts = new ArrayList<FootballTeam>();
		List<FootballTeam> dft1 = grouping(secondgamefts, 2);
		List<FootballTeam> dft2 = grouping(secondgamefts, 2);
		compare(thirdgamefts, dft1);
		compare(thirdgamefts, dft2);

		System.out.println("4进2");
		System.out.println(thirdgamefts);

		List<FootballTeam> fourthgamefts = new ArrayList<FootballTeam>();
		compare(fourthgamefts, thirdgamefts);
		System.out.println("冠军");
		System.out.println(fourthgamefts);
		List<FootballTeam> list = fourthgamefts.get(0).getArmsList();
		System.out.println("冠军之路:");
		System.out.println(list);

	}

	public static void compare(List<FootballTeam> thirdgamefts, List<FootballTeam> dft) {
		for (FootballTeam footballTeam : dft) {
			footballTeam.setScore(Math.random() * 100);
		}
		if (dft.get(0).getScore() >= dft.get(1).getScore()) {
			thirdgamefts.add(dft.get(0));
			dft.get(0).getArmsList().add(dft.get(1));
		} else {
			thirdgamefts.add(dft.get(1));
			dft.get(0).getArmsList().add(dft.get(1));
		}

	}

	public static void groupGameCoil(List<FootballTeam> ft, List<FootballTeam> gamefts, int i, int j) {

		// 拿到分队
		List<FootballTeam> list = grouping(ft, j);
		// System.out.println("第 " + i + " 组:");
		// System.out.println(list);

		// 正在比赛的两个队伍
		List<FootballTeam> gameft1 = groupingGame(list);

		// 确认进级队伍
		ensureCoilin(gamefts, gameft1);

		ensureCoilin(gamefts, list);
	}

	public static void ensureCoilin(List<FootballTeam> gamefts, List<FootballTeam> gameft1) {

		// 确定那个对进级了
		if (gameft1.get(0).getScore() >= gameft1.get(1).getScore()) {
			gamefts.add(gameft1.get(0));
			gameft1.get(0).getArmsList().add(gameft1.get(1));
		} else {
			gamefts.add(gameft1.get(1));
			gameft1.get(1).getArmsList().add(gameft1.get(0));
		}

	}

	public static List<FootballTeam> groupingGame(List<FootballTeam> list1) {
		List<FootballTeam> gameft = new ArrayList<FootballTeam>();

		// 随机两两比赛对
		for (int i = 0; i < 2; i++) {

			// 随机一个球队
			int j = (int) (Math.random() + 2);

			// 设置这个球队的比赛分数
			list1.get(j).setScore(Math.random() * 100);
			gameft.add(list1.get(j));
			list1.remove(j);

		}
		return gameft;
	}

	public static List<FootballTeam> grouping(List<FootballTeam> ft, int i) {

		// 存放一组球队
		List<FootballTeam> groupList = new ArrayList<FootballTeam>();
		while (groupList.size() < i) {

			// 随机抽一个球队
			int j = (int) (Math.random() * ft.size());

			// 默认j,就认为是抽到的球队
			groupList.add(ft.get(j));

			// 移除已经抽到的球队,避免再次比配到该对
			ft.remove(ft.get(j));

		}
		return groupList;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值