-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoardGame.java
162 lines (131 loc) · 3.5 KB
/
BoardGame.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.*;
public class BoardGame {
public static int maxPlayers;
public static int minPlayers;
public static int recommendedNumPlayers;
public GameBoard board;
public List<Player> players;
public List<Player.PlayerBoard> playerBoards;
public List<BoardGameListener> listeners;
public BoardGame(int numPlayers){
this.board = new GameBoard(numPlayers);
}
public void addListener(BoardGameListener bgl){
listeners.add(bgl);
}
public void removeListener(BoardGameListener bgl){
listeners.remove(bgl);
}
public int getNumPlayers(){
return playerBoards.size();
}
public static void setMaxPlayers(int num){
BoardGame.maxPlayers = num;
}
public static void setMinPlayers(int num){
BoardGame.minPlayers = num;
}
public static void setRecNumPlayers(int num){
BoardGame.recommendedNumPlayers = num;
}
public void addPlayer(String playerName, int position){
players.add(new Player(playerName, position));
firePlayerAddedEvent();
}
public void removePlayer(String playerName){
Iterator<Player> i = players.iterator();
while(i.hasNext()){
if (i.next().getName().equals(playerName)){
i.remove();
firePlayerRemovedEvent();
return;
}
}
}
private void firePlayerAddedEvent(){
BoardGameEvent bge = new BoardGameEvent(this);
Iterator<BoardGameListener> list = listeners.iterator();
while (list.hasNext()){
list.next().playerAdded(bge);
}
}
private void firePlayerRemovedEvent(){
BoardGameEvent bge = new BoardGameEvent(this);
Iterator<BoardGameListener> list = listeners.iterator();
while (list.hasNext()){
list.next().playerDropped(bge);
}
}
private void fireGameStartedEvent(){
BoardGameEvent bge = new BoardGameEvent(this);
Iterator<BoardGameListener> list = listeners.iterator();
while (list.hasNext()){
list.next().gameStarted(bge);
}
}
private void fireGamePausedEvent(){
BoardGameEvent bge = new BoardGameEvent(this);
Iterator<BoardGameListener> list = listeners.iterator();
while (list.hasNext()){
list.next().gamePaused(bge);
}
}
private void fireGameEndedEvent(){
BoardGameEvent bge = new BoardGameEvent(this);
Iterator<BoardGameListener> list = listeners.iterator();
while (list.hasNext()){
list.next().gameEnded(bge);
}
}
public static class GameBoard {
private int numPlayers;
public GameBoard(int num){
this.setNumPlayers(num);
}
/**
* @param numPlayers the numPlayers to set
*/
public void setNumPlayers(int numPlayers) {
this.numPlayers = numPlayers;
}
/**
* @return the numPlayers
*/
public int getNumPlayers() {
return numPlayers;
}
}
public static class Player {
private String name;
private int position;
public Player(String name, int position){
this.name = name;
this.position = position;
}
public String getName(){
return name;
}
public int getPosition(){
return position;
}
public static class PlayerBoard extends GameBoard {
public PlayerBoard(int num) {
super(num);
// TODO Auto-generated constructor stub
}
}
}
public static class BoardGameEvent extends EventObject {
private static final long serialVersionUID = 4543579682732986136L;
public BoardGameEvent(Object source){
super(source);
}
}
public static interface BoardGameListener {
public void playerAdded(BoardGameEvent e);
public void playerDropped(BoardGameEvent e);
public void gameStarted(BoardGameEvent e);
public void gamePaused(BoardGameEvent e);
public void gameEnded(BoardGameEvent e);
}
}