Kontakt

Fakturační údaje

IČ: 87189224, BÚ: 1958653063/0800

Contact info in QR code

Siege (Obkličovačka)

Informace

Stáhni a hraj!

  • Spustitelný soubor hry si stáhnou oba hráči
  • Zakladatel hry
    • V nabídce "Game > Create" vytvoří novou hru
    • Pošle protihráči svou IP adresu (zjistí ji v levém dolním rohu okna)
  • Protihráč
    • Jakmile obdrží IP adresu zakladatele hry, v nabídce "Game > Join" se připojí ke hře
  • Cílem je zabrat co nejvíce soupeřových polí.
  • Pozn.: Síťové spojení funguje na LAN (lokální) síťi a na počítačích s veřejnou IP adresou.

Zdrojové kódy (zip)

Zdrojové kódy (prohlížení)

  1: package siege.core;
  2: 
  3: import java.util.Collection;
  4: import java.util.HashSet;
  5: import java.util.Iterator;
  6: import java.util.LinkedList;
  7: import java.util.Observable;
  8: import java.util.Observer;
  9: import siege.util.Log;
 10: 
 11: /**
 12:  * Region surrounded by points owned by certain single user
 13:  *
 14:  * Observe:
 15:  * - PlayboardPoint (borderline)
 16:  *
 17:  * Observers:
 18:  * - report when validity changes
 19:  * - Playboard
 20:  *
 21:  * @author Ondrej Macoszek, ondra@macoszek.cz
 22:  */
 23: public class Territory extends Observable implements Ownable, Observer
 24: {
 25:     /**
 26:      * Ownable properties
 27:      */
 28:     private Player owner;
 29: 
 30:     /**
 31:      * List of point that are part of border line
 32:      */
 33:     private LinkedList<PlayboardPoint> borderLine;
 34: 
 35:     /**
 36:      * All inner points inside the territory, excluding borderLine
 37:      */
 38:     private HashSet<PlayboardPoint> innerPoints;
 39: 
 40:     /**
 41:      * All points in territory including borderline
 42:      */
 43:     private HashSet<PlayboardPoint> territoryPoints;
 44: 
 45:     /**
 46:      * Captured enemy points
 47:      */
 48:     private HashSet<PlayboardPoint> opponentPoints;
 49: 
 50:     /**
 51:      * Determine if territory captured within terriory of other player.
 52:      */
 53:     private boolean isCaptured = false;
 54: 
 55:     private Playboard playboard;
 56: 
 57:     /**
 58:      * Constructor
 59:      * @param line borderline as linked list
 60:      */
 61:     public Territory(Collection<PlayboardPoint> line)
 62:     {
 63:         super();
 64:         setBorderLine(line);
 65:     }
 66: 
 67:     public void setPlayboard(Playboard playboard)
 68:     {
 69:         this.playboard = playboard;
 70:     }
 71: 
 72:     public Playboard getPlayboard()
 73:     {
 74:         if(playboard == null) {
 75:             playboard = Game.getInstance().getPlayboard();
 76:         }
 77:         return playboard;
 78:     }
 79:     
 80:     /**
 81:      * Set territory border line. Register territory as observer to borderline points
 82:      */
 83:     public void setBorderLine(Collection<PlayboardPoint> line)
 84:     {
 85:         Log.dump(line);
 86:         if(isValidBorderLine(line) == false) {
 87:             throw new IllegalArgumentException("Given borderline is invalid");
 88:         }
 89: 
 90:         for (PlayboardPoint point : line) {
 91:             point.setParentTerritory(this,PointStatus.BORDER);
 92:             point.addObserver(this);
 93:         }
 94:         borderLine = new LinkedList<PlayboardPoint>(line);
 95:         setOwner(borderLine.getFirst().getOwner());
 96:     }
 97: 
 98:     /**
 99:      * Get border line
100:      */
101:     public LinkedList<PlayboardPoint> getBorderLine()
102:     {
103:         return borderLine;
104:     }
105: 
106: 
107:     public static boolean isValidBorderLine(Collection<PlayboardPoint> givenLine)
108:     {
109:         boolean result = false;
110: 
111:         LinkedList<PlayboardPoint> line = new LinkedList<PlayboardPoint>(givenLine);
112:         if(line.size()>2) {
113:             Iterator<PlayboardPoint> it = line.iterator();
114:             PlayboardPoint prev = null;
115:             Player owner = line.getFirst().getOwner();
116:             while(it.hasNext()) {
117:                 PlayboardPoint point = it.next();
118:                 if(point.hasOwner(owner)==false) {
119:                     throw new IllegalArgumentException("Points of border line have different owners");
120:                 }
121:                 if(prev == null) {
122:                     prev = point;
123:                     continue;
124:                 }
125:                 PlayboardPoint current = point;
126:                 if(prev.isNearbyOf(current)==false) {
127:                     result = false;
128:                     break;
129:                 } else {
130:                     result = true;
131:                 }
132:                 prev = current;
133:             }
134:             if(result == true && prev.isNearbyOf(line.getFirst())) {
135:                 result = true;
136:             }
137:         }
138: 
139:         return result;
140:     }
141: 
142:     /**
143:      * Area is number of all points that belong to this territory
144:      */
145:     public int getArea()
146:     {
147:         if(territoryPoints == null) {
148:             territoryPoints = getTerritoryPoints();
149:         }
150:         return territoryPoints.size();
151:     }
152: 
153: 
154:     /**
155:      * Get points lying inside territory
156:      * @return inner points
157:      */
158:     public HashSet<PlayboardPoint> getInnerPoints()
159:     {
160:         if(innerPoints == null) {
161:             innerPoints = new HashSet<PlayboardPoint>(getTerritoryPoints());
162:             innerPoints.removeAll(borderLine);
163:             for (PlayboardPoint point : innerPoints) {
164:                 if(point.hasParentTerritory(this, PointStatus.INNER) == false) {
165:                     point.setParentTerritory(this, PointStatus.INNER);
166:                 }
167:             }
168:         }
169:         return innerPoints;
170:     }
171: 
172:     /**
173:      * Get all points that belong under this territory
174:      * @return territory points
175:      */
176:     public HashSet<PlayboardPoint> getTerritoryPoints()
177:     {
178:         TerritoryMap borderMap = new TerritoryMap(this);
179:         borderMap.removeOuterPoints();
180:         territoryPoints = new HashSet<PlayboardPoint>(borderMap.getTerritoryPoints());
181:         return territoryPoints;
182:     }
183: 
184:     /**
185:      * Get all points captured inside territory that belong to opponent
186:      * @return opponent points
187:      */
188:     public HashSet<PlayboardPoint> getOpponentPoints()
189:     {
190:         if(opponentPoints == null) {
191:             innerPoints = getInnerPoints();
192:             opponentPoints = new HashSet<PlayboardPoint>(innerPoints.size()/2);
193:             for (PlayboardPoint point : innerPoints) {
194:                 if(point.hasOwner() && point.hasOwner(owner)==false) {
195:                     opponentPoints.add(point);
196:                 }
197:             }
198:         }
199:         return opponentPoints;
200:     }
201: 
202:     /**
203:      * Test if territory is captured within opponents territory
204:      */
205:     public boolean isCaptured()
206:     {
207:         return isCaptured;
208:     }
209: 
210:     /**
211:      * Set captured status of territory
212:      */
213:     public void setCaptured(boolean status)
214:     {
215:         isCaptured = status;
216:         setChanged();
217:         notifyObservers();
218:     }
219: 
220: 
221:     /**
222:      * Ownable implementation
223:      * @see Ownable
224:      */
225: 
226:     public void setOwner(Player owner)
227:     {
228:         if(borderLine.getFirst().getOwner().equals(owner) == false) {
229:             throw new IllegalArgumentException("Owner of territory must be same as owner of borderline points");
230:         }
231:         this.owner = owner;
232:     }
233: 
234:     public Player getOwner()
235:     {
236:         return owner;
237:     }
238: 
239:     public boolean hasOwner()
240:     {
241:         boolean result = false;
242:         if(owner != null) {
243:             result = true;
244:         }
245:         return result;
246:     }
247: 
248:     public boolean hasOwner(Player owner)
249:     {
250:         boolean result = false;
251:         if(this.owner != null && this.owner.equals(owner)) {
252:             result = true;
253:         }
254:         return result;
255:     }
256:     
257:     
258:     /**
259:      * Observer implementation
260:      * @see Observer
261:      */
262:     public void update(Observable o, Object arg) {
263:         if(o instanceof PlayboardPoint) {
264:             // border line point report changes
265:             PlayboardPoint point = (PlayboardPoint)o;
266:             if(point.hasParentTerritory(PointStatus.INNER)) {
267:                 for(PlayboardPoint p : borderLine) {
268:                     p.deleteObserver(this);
269:                 }
270:                 setCaptured(true);
271:             }
272:         }
273:     }
274: 
275:     public String toString() {
276:         TerritoryMap tm = new TerritoryMap(this);
277:         tm.dump();
278:         return "";
279:     }
280: 
281: }
282: 
283: