mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-24 19:19:44 +01:00
limit the amount of players to 6
This commit is contained in:
parent
373b9e6d53
commit
7ada56e215
@ -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<Player> players = new LinkedList<>();
|
||||
private List<Player> players = new LimitedLinkedList<>(6);
|
||||
private Set<Player> readyPlayers = new HashSet<>();
|
||||
private ServerGameLogic logic;
|
||||
private Player hostPlayer;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user