From ee99d0496d1e7fb8450d0bcbd921ae3fd0a02c34 Mon Sep 17 00:00:00 2001 From: koz01 <koz01@pcfeib208-22-74.vsb.cz> Date: Wed, 22 Nov 2023 07:31:33 +0100 Subject: [PATCH] Generate tournaments --- src/main/java/cz/jezek/lab11/Tournament.java | 36 ++++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/cz/jezek/lab11/Tournament.java b/src/main/java/cz/jezek/lab11/Tournament.java index 8442be3..708a391 100644 --- a/src/main/java/cz/jezek/lab11/Tournament.java +++ b/src/main/java/cz/jezek/lab11/Tournament.java @@ -1,23 +1,41 @@ package cz.jezek.lab11; -import java.util.Collections; +import static cz.jezek.lab11.RandomGenerator.random; +import static cz.jezek.lab11.RandomGenerator.selectRandom; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toList; + +import java.io.Serializable; import java.util.List; +import java.util.stream.Stream; -public class Tournament { +public class Tournament implements Serializable{ + private static final long serialVersionUID = 1L; + private List<Player> players; private List<Match> matches; public static Tournament generate() { - // TODO 1.a Generate a list of 10 random players using the stream and then filter so that it does not contain players with the same name - List<Player> players = Collections.emptyList(); + // TO DO 1.a Generate a list of 10 random players using the stream and then filter so that it does not contain players with the same name + List<Player> players = Stream.generate(Player::generateRandom) + .distinct() + .limit(10) + .collect(toList()); // TODO 1.b Use the stream to generate a list of 50 matches between random players (from the list of players) with a random result. Make sure the player does not play with himself. - List<Match> matches = Collections.emptyList(); + List<Match> matches = Stream.generate(() -> generateRandomMatch(players)) + .filter(m -> m.getOponents().getPlayer1() != m.getOponents().getPlayer2()) + .limit(500) + .toList(); return new Tournament(players, matches); } + + private static Match generateRandomMatch(List<Player> players) { + return new Match(selectRandom(players), random.nextInt(11), selectRandom(players), random.nextInt(11)); + } public Tournament(List<Player> players, List<Match> matches) { this.players = players; @@ -45,11 +63,15 @@ public class Tournament { builder.append("\n\nPlayers:\n"); // TODO 2.a Use the stream to add a listing of all players to the builder. Individual players are separated by a "\ n" character - builder.append(/* list of all players */""); + builder.append(players.stream() + .map(Object::toString) + .collect(joining("\n"))); builder.append("\n\nMatches:\n"); // TODO 2.b Use the stream to add a listing of all entries to the builder. Individual matches are separated by a "\ n" character - builder.append(/* list of all matches */""); + builder.append(matches.stream() + .map(Object::toString) + .collect(joining("\n"))); return builder.toString(); } -- GitLab