-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicGamesContainer.java
50 lines (40 loc) · 1.3 KB
/
BasicGamesContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package util;
import model.board.BasicBoard;
import model.board.BasicBoardMovement;
import model.exceptions.CorruptedFileException;
import model.game.AbstractGame;
import model.game.BasicGame;
import javax.inject.Singleton;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Singleton
public class BasicGamesContainer {
private Map<Integer, AbstractGame> games;
public BasicGamesContainer() {
this.games = new HashMap<>();
}
public int newGame(int limit, String name) throws Exception {
int gameId = games.size();
BasicBoard board = new BasicBoard();
try {
board.loadBoard(new File("basicBoard.txt"));
} catch (CorruptedFileException e) {
throw new Exception("Error when loading the board file");
}
BasicBoardMovement movement = new BasicBoardMovement(board);
BasicGame game = new BasicGame(movement, limit, name, gameId);
games.put(gameId, game);
return gameId;
}
public AbstractGame getGame(int id) {
return games.getOrDefault(id, null);
}
public List<AbstractGame> getGames() {
List<AbstractGame> games = new ArrayList<>();
games.addAll(this.games.values());
return games;
}
}