Skip to content

Commit

Permalink
Adapt TileStack to avoid java.util.Stack in its implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaglam committed Oct 3, 2024
1 parent 053218b commit d831992
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/carcassonne/model/tile/TileStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;

/**
* The stack of tiles for a game.
* @author Timur Saglam
*/
public class TileStack {
private final Stack<Tile> tiles;
private final List<Tile> tiles;
private final Queue<Tile> returnedTiles;
private final Set<Tile> returnHistory;
private final int multiplier;
Expand All @@ -36,7 +36,7 @@ public TileStack(TileDistribution distribution, int multiplicator) {
*/
public TileStack(TileDistribution distribution, int multiplier, Long sortingSeed) {
this.multiplier = multiplier;
tiles = new Stack<>();
tiles = new LinkedList<>(); // LinkedList is better for a stack here
returnedTiles = new LinkedList<>();
returnHistory = new HashSet<>();
fillStack(distribution);
Expand All @@ -50,7 +50,7 @@ public TileStack(TileDistribution distribution, int multiplier, Long sortingSeed
}

/**
* Draws random tile from the stack and returns it
* Draws random tile from the stack and returns it.
* @return the tile or null if the stack is empty.
*/
public Tile drawTile() {
Expand All @@ -60,7 +60,8 @@ public Tile drawTile() {
if (tiles.isEmpty()) {
return returnedTiles.poll();
}
return tiles.pop();

return tiles.removeFirst();
}

/**
Expand All @@ -73,7 +74,7 @@ public int getInitialSize() {

/**
* Getter for the size of the stack.
* @return the amount of tiled on the stack.
* @return the amount of tiles on the stack.
*/
public int getSize() {
return tiles.size() + returnedTiles.size();
Expand Down

0 comments on commit d831992

Please sign in to comment.