From 7ada56e21548ae34e799298d85561b0fa6c8753f Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Sat, 23 Nov 2024 14:41:17 +0100 Subject: [PATCH] limit the amount of players to 6 --- .../monopoly/game/server/PlayerHandler.java | 4 +- .../pp/monopoly/model/LimitedLinkedList.java | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java index 1bef778..c57089a 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java @@ -7,11 +7,13 @@ import java.util.HashSet; import java.util.List; import java.util.NoSuchElementException; import java.util.Set; + +import pp.monopoly.model.LimitedLinkedList; /** * A class for helping with player actions and managing thier turns */ public class PlayerHandler { - private List players = new LinkedList<>(); + private List players = new LimitedLinkedList<>(6); private Set readyPlayers = new HashSet<>(); private ServerGameLogic logic; private Player hostPlayer; diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java new file mode 100644 index 0000000..5a0285e --- /dev/null +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java @@ -0,0 +1,49 @@ +package pp.monopoly.model; + +import java.util.LinkedList; + +/** + * A LinkedList with a maximum size limit. + * + * @param the type of elements held in this collection + */ +public class LimitedLinkedList extends LinkedList { + + private final int maxSize; + + /** + * Constructs a LimitedLinkedList with the specified maximum size. + * + * @param maxSize the maximum number of elements this list can hold + */ + public LimitedLinkedList(int maxSize) { + if (maxSize <= 0) { + throw new IllegalArgumentException("Max size must be greater than 0"); + } + this.maxSize = maxSize; + } + + /** + * Adds an element to the list. If the list exceeds its maximum size, + * the oldest element (first) is removed. + * + * @param element the element to be added + * @return true if the element was added successfully + */ + @Override + public boolean add(E element) { + if (size() >= maxSize) { + return false; + } + return super.add(element); + } + + /** + * Gets the maximum size of this list. + * + * @return the maximum size + */ + public int getMaxSize() { + return maxSize; + } +}