limit the amount of players to 6

This commit is contained in:
Johannes Schmelz 2024-11-23 14:41:17 +01:00
parent 373b9e6d53
commit 7ada56e215
2 changed files with 52 additions and 1 deletions

View File

@ -7,11 +7,13 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
import pp.monopoly.model.LimitedLinkedList;
/** /**
* A class for helping with player actions and managing thier turns * A class for helping with player actions and managing thier turns
*/ */
public class PlayerHandler { public class PlayerHandler {
private List<Player> players = new LinkedList<>(); private List<Player> players = new LimitedLinkedList<>(6);
private Set<Player> readyPlayers = new HashSet<>(); private Set<Player> readyPlayers = new HashSet<>();
private ServerGameLogic logic; private ServerGameLogic logic;
private Player hostPlayer; private Player hostPlayer;

View File

@ -0,0 +1,49 @@
package pp.monopoly.model;
import java.util.LinkedList;
/**
* A LinkedList with a maximum size limit.
*
* @param <E> the type of elements held in this collection
*/
public class LimitedLinkedList<E> extends LinkedList<E> {
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;
}
}