···11- package blockworld;
22-33-/*
44- 3APL Blockworld program by Jelle Herold, copyright 2003.
55- Written for the Intelligent Systems group at Utrecht University.
66- This is LGPL software.
77-88- $Id: Env.java,v 1.7 2004/12/27 02:22:41 cvs-3apl Exp $
99- */
11+package cardtable;
102113// 2APL imports
124import apapl.Environment;
···3123import java.io.ObjectOutputStream;
3224import java.io.OutputStream;
33253434-import blockworld.lib.ObsVectListener;
3535-import blockworld.lib.Signal;
3636-import blockworld.lib.ObsVect;
2626+import java.util.Random;
37273838-public class Env extends Environment implements ObsVectListener
2828+public class Env extends Environment
3929{
4040- // To hold our reference to the window
4141- final protected Window main_window;
4242-4343- // max distance of cells visible to each agent
4444- protected int _senserange = 5;
4545-4646- // size of environment
4747- protected Dimension m_size = new Dimension( 16, 16 );
4848-4949- private HashMap<String,Agent> agentmap = new HashMap<String,Agent>();
5050-5151- /* ---- ALL the stuff in the environment -----*/
5252-5353- // list of agents (Agent)
5454- protected ObsVect _agents = new ObsVect( this );
5555-5656- // id for identifiable objects
5757- protected String _objType = "default";
5858-5959-6060-6161-6262- /* ------------------------------------------*/
6363-6464-6565- /* ---- SIGNALS -------------------------------*/
6666- // / emitted on collection of a bomb in the bomb trap
6767- public transient Signal signalBombTrapped = new Signal( "env bomb trapped" );
6868-6969- public transient Signal signalSenseRangeChanged = new Signal(
7070- "env sense range changed" );
7171-7272- // emitted if environment is resized
7373- public transient Signal signalSizeChanged = new Signal( "env size changed" );
7474-7575- // emitted if bomb traps location changed
7676- public transient Signal signalTrapChanged = new Signal(
7777- "env trap position changed" );
7878-7979- /* ------------------------------------------*/
3030+ private HashMap<String,Agent> agentmap = new HashMap<String,Agent>();
80313232+ private int numberOfPlayers, numberOfNotaries, numberOfGatekeepers;
81338234 // The default constructor
8335 public Env()
8436 {
8537 super();
8638 // Create the window
8787-8888- main_window = new Window( this );
8989- }
9090-9191- /* Called from 2APL */
9292-9393- // Enter the agent into the world
9494- // Succesful returns true, else the ExternalActionFailedException exception is thrown
9595- public Term enter( String sAgent, APLNum xt, APLNum yt, APLIdent colort) throws ExternalActionFailedException
9696- {
9797- int x = xt.toInt();
9898- int y = yt.toInt();
9999- String color = colort.toString();
100100-101101- // Get the agent
102102- Agent agent = getAgent(sAgent);
103103-104104- // Give a signal that we want to move
105105- agent.signalMove.emit();
106106-107107- writeToLog( "Agent entered: " +agent.getName());
108108-109109- Point position = new Point(x,y);
110110- String pos = "("+x+","+y+")";
111111-112112- // Agent already entered
113113- if( agent.isEntered() )
114114- {
115115- writeToLog( "agent already entered" );
116116- throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" has already entered.");
117117- }
118118-119119- // Is this position within the world?
120120- if (isOutOfBounds(position))
121121- {
122122- throw new ExternalActionFailedException("Position "+pos+" out of bounds.");
123123- }
124124- // Is this position free?
125125- if( !isFree( position ) )
126126- {
127127- throw new ExternalActionFailedException("Position "+pos+" is not free.");
128128- }
129129-130130- // Update the agent his position
131131- agent._position = position;
132132-133133- // Which color does the agent want to be
134134- int nColorID = getColorID(color);
135135- agent._colorID = nColorID;
136136-137137- // Redraw so we can see the agent
138138- validatewindow();
139139- main_window.repaint();
140140-141141- // We came so far, this means success!
142142- agent.signalMoveSucces.emit();
143143-144144- return wrapBoolean(true);
145145- }
146146-147147- // Move the agent north
148148- public Term north(String sAgent) throws ExternalActionFailedException
149149- {
150150- // Get the correct agent
151151- Agent agent = getAgent(sAgent);
152152-153153- // Get the agent his position
154154- Point p = (Point) agent.getPosition().clone();
155155- p.y = p.y - 1;
3939+ //m_window = new Window( this );
15640157157- // Set the position for the agent
158158- boolean r = setAgentPosition( agent, p);
159159-160160- // can't move north
161161- if (!r) throw new ExternalActionFailedException("Moving north failed.");
162162-163163- // Redraw the window
164164- validatewindow();
165165- main_window.repaint();
166166- return wrapBoolean(r);
4141+ numberOfPlayers=0;
4242+ numberOfNotaries=0;
4343+ numberOfGatekeepers=0;
16744 }
16845169169- // Move the agent east
170170- public Term east(String sAgent) throws ExternalActionFailedException
171171- {
172172- // Get the correct agent
173173- Agent agent = getAgent(sAgent);
174174-175175- // Get the agent his position
176176- Point p = (Point) agent.getPosition().clone();
177177- p.x = p.x + 1;
178178-179179- // Set the position for the agent
180180- boolean r = setAgentPosition( agent, p);
181181-182182- // can't move north
183183- if (!r) throw new ExternalActionFailedException("Moving north failed.");
184184-185185- // Redraw the window
186186- validatewindow();
187187- main_window.repaint();
188188- return wrapBoolean(r);
4646+ public Term enterAsNotary(String sAgent) throws ExternalActionFailedException {
4747+ Agent agent = getAgent(sAgent);
4848+ writeToLog("Notary sit: " +agent.getName());
4949+5050+ if(numberOfNotaries>0)
5151+ throw new ExternalActionFailedException("There is already a notary at the table.");
5252+ else
5353+ numberOfNotaries++;
5454+5555+ agent._type=2;
5656+5757+ return wrapBoolean(true);
18958 }
19059191191- // Move the agent south
192192- public Term south(String sAgent) throws ExternalActionFailedException
193193- {
194194- // Get the correct agent
195195- Agent agent = getAgent(sAgent);
196196-197197- // Get the agent his position
198198- Point p = (Point) agent.getPosition().clone();
199199- p.y = p.y + 1;
200200-201201- // Set the position for the agent
202202- boolean r = setAgentPosition( agent, p);
203203-204204- // can't move north
205205- if (!r) throw new ExternalActionFailedException("Moving north failed.");
206206-207207- // Redraw the window
208208- validatewindow();
209209- main_window.repaint();
210210- return wrapBoolean(r);
6060+ public Term enterAsGatekeeper(String sAgent) throws ExternalActionFailedException {
6161+ Agent agent = getAgent(sAgent);
6262+ writeToLog("Gatekeeper in the system: " +agent.getName());
6363+6464+ if(numberOfGatekeepers>0)
6565+ throw new ExternalActionFailedException("There is already a gatekeeper in the system.");
6666+ else
6767+ numberOfGatekeepers++;
6868+6969+ agent._type=1;
7070+7171+ return wrapBoolean(true);
21172 }
212212-213213- // Move the agent west
214214- public Term west(String sAgent) throws ExternalActionFailedException
215215- {
216216- // Get the correct agent
217217- Agent agent = getAgent(sAgent);
218218-219219- // Get the agent his position
220220- Point p = (Point) agent.getPosition().clone();
221221- p.x = p.x - 1;
222222-223223- // Set the position for the agent
224224- boolean r = setAgentPosition( agent, p);
225225-226226- // can't move north
227227- if (!r) throw new ExternalActionFailedException("Moving north failed.");
22873229229- // Redraw the window
230230- validatewindow();
231231- main_window.repaint();
232232- return wrapBoolean(r);
233233- }
234234-235235- // Pickup a bomb
236236- public Term pickup( String sAgent ) throws ExternalActionFailedException
7474+ // Sit the player at the table
7575+ public Term sit(String sAgent, APLIdent player) throws ExternalActionFailedException
23776 {
238238- // Get the agent
239239- Agent agent = getAgent(sAgent);
240240-241241- // Let everyone know we are going to pick up a bomb
242242- agent.signalPickupBomb.emit();
243243-244244- // see if we are not already carrying a bomb
245245- if( agent.atCapacity() )
246246- {
247247- writeToLog( "Pickup bomb failed" );
248248- throw new ExternalActionFailedException("Pickup bomb failed");
249249- }
250250-251251- // we are not already carying a bomb so get this one
252252- TypeObject bomb = removeBomb( agent.getPosition() );
253253- if( bomb == null )
254254- {
255255- writeToLog( "Pickup bomb failed" );
256256- throw new ExternalActionFailedException("Pickup bomb failed");
257257- }
258258-259259- // Yes
260260- agent.signalPickupBombSucces.emit();
261261-262262- // there was a bomb at that position, so set token
263263- agent.pickBomb(bomb);
7777+ String player_name = player.toString();
26478265265- // show what happened
266266- validatewindow();
267267- main_window.repaint();
7979+ Agent agent = getAgent(player_name);
26880269269- return wrapBoolean(true);
270270- }
271271-272272- // Drop a bomb
273273- public Term drop( String sAgent ) throws ExternalActionFailedException
274274- {
275275- // Get the agent
276276- Agent agent = getAgent(sAgent);
277277- // we are going to drop a bomb
278278- agent.signalDropBomb.emit();
279279-280280- TypeObject bomb = agent.senseBomb();
281281- // see if we are actually carrying a bomb
282282- if( bomb == null)
8181+ // is already sit?
8282+ if(agent.isSit())
28383 {
284284- writeToLog( "Drop bomb failed" );
285285- throw new ExternalActionFailedException("Drop bomb failed");
8484+ writeToLog( "agent already sit" );
8585+ throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" is already sit.");
28686 }
287287-288288- Point pos = agent.getPosition();
289289- // see if we can drop that bomb here
29087291291- if( !addBomb(pos) && (isTrap( pos ) == null
292292- || agent.senseBomb( isTrap( pos ).getType() ) == null) )
8888+ // are there already 5 players at the table?
8989+ if (getNumberOfPlayers()==5)
29390 {
294294- writeToLog( "Drop bomb failed" );
295295- throw new ExternalActionFailedException("Drop bomb failed");
9191+ throw new ExternalActionFailedException("Already 5 players sit at the table.");
29692 }
29793298298- if(isTrap( pos ) != null
299299- && agent.senseBomb( isTrap( pos ).getType() ) != null)
300300- {
301301- signalBombTrapped.emit();
302302- }
303303-304304- // unset token
305305- agent.dropBomb();
306306-307307- agent.signalDropBombSucces.emit();
308308-309309- // Show it
310310- validatewindow();
311311- main_window.repaint();
312312-313313- // return success
314314- return wrapBoolean(true);
315315- }
316316-317317- // What is the agent his Sense Range
318318- public Term getSenseRange(String agent)
319319- {
320320- // the below function is also used by EnvView
321321- int r = getSenseRange();
322322- return new APLList(new APLNum(r));
323323- }
324324-325325- // Sense all agents. This does not include self.
326326- public synchronized Term senseAllAgent(String sAgent) throws ExternalActionFailedException
327327- {
328328- //Collection c = senseAllAgents(getAgent(agent).getPosition());
329329- Point position = getAgent(sAgent).getPosition();
330330- // iterate over all agents
331331- Vector all = new Vector();
332332-333333- Iterator j = _agents.iterator();
334334- while( j.hasNext() ) {
335335- Agent agent = (Agent) j.next();
336336- Point p = agent.getPosition();
337337-338338- // Changed SA: when there are no other agents, this return null, which
339339- // causes Java to throw an exception and never return the empty list.
340340- if (p == null)
341341- continue;
342342-343343- // skip self
344344- if( p.equals( position ) )
345345- continue;
9494+ // Update the agent his position
9595+ agent._position = getPosition();
9696+ agent._type=0; // is a player
34697347347- all.add( agent );
348348- }
349349-350350- LinkedList<Term> listpar = new LinkedList<Term>();
351351- for(Object i : all) {
352352- final Agent a = (Agent) i;
353353- APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y));
354354- listpar.add(tmp);
355355- }
356356- return new APLList(listpar);
357357- }
358358-359359- // Sense the given agent his position
360360- public synchronized Term sensePosition(String sAgent) throws ExternalActionFailedException
361361- {
362362- Point p = getAgent(sAgent).getPosition();
363363- return new APLList(new APLNum(p.x),new APLNum(p.y));
364364- }
365365-366366- // is there a trap in the senserange of the agent?
367367- public synchronized Term senseTraps(String agent) throws ExternalActionFailedException
368368- {
369369- // Get the agent his position
370370- Point position = getAgent(agent).getPosition();
9898+ // Increase number of players
9999+ increaseNumberOfPlayers();
371100372372- // iterate over all traps and decide according to distance if it is in
373373- // vision range
374374- Vector visible = new Vector();
375375-376376- Iterator i = _traps.iterator();
377377- while( i.hasNext() ) {
378378- TypeObject t = (TypeObject) i.next();
379379- if( position.distance( t.getPosition() ) <= _senserange )
380380- visible.add( t );
381381- }
101101+ // Redraw so we can see the agent
102102+ //validatewindow();
103103+ //m_window.repaint();
382104383383- return convertCollectionToTerm(visible);
384384- }
385385-386386- // Get all the traps in the env
387387- public synchronized Term senseAllTraps(String agent)
388388- {
389389- // iterate over all traps
390390- Vector all = new Vector();
105105+ // We came so far, this means success!
106106+ //agent.signalMoveSucces.emit();
391107392392- Iterator i = _traps.iterator();
393393- while( i.hasNext() )
394394- {
395395- all.add( (TypeObject) i.next() );
396396- }
397397-398398- return convertCollectionToTerm(all);
399399- }
400400-401401- // Sends a bom in the senserange of the agent
402402- public synchronized Term senseBombs(String agent) throws ExternalActionFailedException
403403- {
404404- // Get the agent his position
405405- Point position = getAgent(agent).getPosition();
406406-407407- // iterate over all bombs and decide according to distance if it is in
408408- // vision range
409409- Vector visible = new Vector();
108108+ writeToLog("Player sit: " +agent.getName());
410109411411- Iterator i = _bombs.iterator();
412412- while( i.hasNext() )
413413- {
414414- TypeObject b = (TypeObject) i.next();
415415- if( position.distance( b.getPosition() ) <= _senserange )
416416- visible.add( b );
417417- }
418418-419419- return convertCollectionToTerm(visible);
110110+ return new APLNum(agent._position);
420111 }
421421-422422- // Find all the bombs in the environment
423423- public synchronized Term senseAllBombs (String agent)
424424- {
425425- // iterate over all bombs
426426- Vector all = new Vector();
427112428428- Iterator i = _bombs.iterator();
429429- while( i.hasNext() )
430430- {
431431- all.add( (TypeObject) i.next() );
432432- }
433433-434434- return convertCollectionToTerm(all);
435435- }
436436-437437- // Sense the stones in the agent senserange
438438- public synchronized Term senseStones(String agent) throws ExternalActionFailedException
113113+ public Term chooseDealer(String sAgent) throws ExternalActionFailedException
439114 {
440440- // Get the agent his position
441441- Point position = getAgent(agent).getPosition();
442442-443443- // iterate over all stones and decide according to distance if it is in
444444- // vision range
445445- Vector visible = new Vector();
446446-447447- Iterator i = _stones.iterator();
448448- while( i.hasNext() )
115115+ if (getNumberOfPlayers()!=5)
449116 {
450450- TypeObject t = (TypeObject) i.next();
451451- if( position.distance( t.getPosition() ) <= _senserange )
452452- visible.add( t );
117117+ throw new ExternalActionFailedException("There are not 5 players sit at the table.");
453118 }
454454-455455- return convertCollectionToTerm(visible);
456456- }
457457-458458- // Sense all stones
459459- public synchronized Term senseAllStones (String agent)
460460- {
461461- // iterate over all traps
462462- Vector all = new Vector();
463119464464- Iterator i = _stones.iterator();
465465- while( i.hasNext() )
466466- {
467467- all.add( (TypeObject) i.next() );
468468- }
469469-470470- return convertCollectionToTerm(all);
471471- }
472472-473473- // Sense visible area for agents. This does not include self.
474474- public synchronized Term senseAgent(String sAgent) throws ExternalActionFailedException
475475- {
476476- //Collection c = senseAgents(getAgent(agent).getPosition());
477477- Point position = getAgent(sAgent).getPosition();
478478-479479- // iterate over all agents and decide according to distance if it is in
480480- // vision range
481481- Vector visible = new Vector();
482482-483483- Iterator j = _agents.iterator();
484484-485485- while( j.hasNext() )
486486- {
487487- Agent agent = (Agent) j.next();
120120+ Random rand = new Random();
121121+ int n = 5;
122122+ int randnum = rand.nextInt(n+1);
488123489489- Point p = agent.getPosition();
490490-491491- // Changed SA: when there are no other agents, this return null, which
492492- // causes Java to throw an exception and never return the empty list.
493493- if (p == null)
494494- continue;
495495-496496- // skip self
497497- if( p.equals( position ) )
498498- continue;
124124+ Agent a = getAgent(randnum);
499125500500- // agent within visible range
501501- if( position.distance( p ) <= _senserange )
502502- visible.add( agent );
503503- }
504504-505505- LinkedList<Term> listpar = new LinkedList<Term>();
506506- for(Object i : visible) {
507507- final Agent a = (Agent) i;
508508- APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y));
509509- listpar.add(tmp);
510510- }
511511- return new APLList(listpar);
126126+ return new APLIdent(a.getName());
512127 }
513513-514514-515128516129 /* Standard functions --------------------------------------*/
517130518131 private void notifyAgents(APLFunction event, String... receivers) {
519519- throwEvent(event, receivers);
132132+// throwEvent(event, receivers);
520133 }
521134522135 private void notifyEvent(String parm1, Point ptPosition)
523136 {
524524- APLNum nX = new APLNum((double)(ptPosition.getX()));
525525- APLNum nY = new APLNum((double)(ptPosition.getY()));
526526-527527- // Send an external event to all agents within the senserange.
528528- ArrayList<String> targetAgents = new ArrayList<String>();
529529- for (Agent a : agentmap.values())
530530- {
531531- // Changed SA: I got no idea why there is always 1 agent which does not exists,
532532- // but this fixes the exceptions
533533- if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange()))
534534- targetAgents.add(a.getName());
535535- }
536536-537537- writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents);
538538-539539- if (!targetAgents.isEmpty())
540540- {
541541- notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0]));
542542- }
137137+// APLNum nX = new APLNum((double)(ptPosition.getX()));
138138+// APLNum nY = new APLNum((double)(ptPosition.getY()));
139139+//
140140+// // Send an external event to all agents within the senserange.
141141+// ArrayList<String> targetAgents = new ArrayList<String>();
142142+// for (Agent a : agentmap.values())
143143+// {
144144+// // Changed SA: I got no idea why there is always 1 agent which does not exists,
145145+// // but this fixes the exceptions
146146+// if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange()))
147147+// targetAgents.add(a.getName());
148148+// }
149149+//
150150+// writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents);
151151+//
152152+// if (!targetAgents.isEmpty())
153153+// {
154154+// notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0]));
155155+// }
543156 }
544157545158 // Add an agent to the environment
···551164 writeToLog("linking " + sAgent + "");
552165 } else{
553166 final Agent agent = new Agent(sAgentMain);
554554- _agents.add(agent);
167167+ //_agents.add(agent);
555168 agentmap.put(sAgent, agent);
556169 writeToLog("agent " + agent + " added");
557170 }
···569182570183 // there can be several agent
571184 if (!agentmap.containsValue(a)) {
572572- _agents.remove(a);
573573- a.reset();
185185+ //_agents.remove(a);
186186+ //a.reset();
574187 }
575188576189 writeToLog("Agent removed: " + sAgent);
···588201589202590203591591- /* Helper functions --------------------------------------*/
592592-593593- // Get the size of the blockworld
594594- public synchronized Term getWorldSize(String agent)
595595- {
596596- int w = getWidth();
597597- int h = getHeight();
598598- return new APLList(new APLNum(w),new APLNum(h));
599599- }
600600-204204+ /* Helper functions --------------------------------------*/
205205+601206 // Get the agent from its name
602207 private synchronized Agent getAgent(String name) throws ExternalActionFailedException
603208 {
···608213 else return a;
609214610215 }
216216+217217+ // Get the agent from its position
218218+ private synchronized Agent getAgent(int position) throws ExternalActionFailedException
219219+ {
220220+ Agent a = null;
221221+ Iterator it = agentmap.keySet().iterator();
222222+ while(it.hasNext()) {
223223+ Object name = it.next();
224224+ a = agentmap.get(name);
225225+ if(a!=null) {
226226+ if(a.getPosition()!=null) {
227227+ if(a.getPosition().intValue()==position) {
228228+ return a;
229229+ }
230230+ }
231231+ }
232232+ }
233233+ if (a==null) throw new ExternalActionFailedException("No such agent at position: "+position);
234234+235235+ return a;
236236+ }
611237612238 private static String getMainModule(String sAgent)
613239 {
···618244 return sAgent.substring(0, dotPos);
619245 }
620246621621- // Get the environment width
622622- public synchronized int getWidth() { return m_size.width; }
623623-624624- // Get the environment height
625625- public synchronized int getHeight() { return m_size.height; }
626626-627627- // Return the agents
628628- public synchronized Collection getBlockWorldAgents()
629629- {
630630- return new Vector(_agents);
631631- }
632632-633633- // convert a collection to a term
634634- private static Term convertCollectionToTerm(Collection c)
635635- {
636636- LinkedList<Term> listpar = new LinkedList<Term>();
637637- for(Object i : c) {
638638- final TypeObject o = (TypeObject) i;
639639- APLListVar tmp = new APLList(new APLIdent(o.getType()),new APLNum(o.getPosition().x),new APLNum(o.getPosition().y));
640640- listpar.add(tmp);
641641- }
642642- return new APLList(listpar);
643643- }
644644-645645- // Get senserange
646646- public int getSenseRange()
647647- {
648648- return _senserange;
649649- }
650247651248 // Redrawing the window is a nightmare, this does some redraw stuff
652652- private void validatewindow()
653653- {
654654- Runnable repaint = new Runnable()
655655- {
656656- public void run()
657657- {
658658- //try {Thread.sleep(500);} catch(Exception e) {}
659659- main_window.doLayout();
660660-661661- /*if (!m_window.isVisible())
662662- {
663663- m_window.setVisible( true );
664664- }*/
665665- }
666666- };
667667- SwingUtilities.invokeLater(repaint);
668668- }
669669-670670- // Move the agent
671671- private synchronized boolean setAgentPosition( Agent agent, Point position)
672672- {
673673- agent.signalMove.emit();
674674-675675- if( isOutOfBounds( position ) )
676676- return false;
677677-678678- // suspend thread if some other agent is blocking our entrance
679679-680680- // Is the position free?
681681- if( !isFree( position ) )
682682- return false;
683683-684684- agent.signalMoveSucces.emit();
685685-686686- // there may be other threads blocked because this agent was in the way,
687687- // notify
688688- // them of the changed state of environment
689689- synchronized( this )
690690- {
691691- notifyAll();
692692- }
249249+// private void validatewindow()
250250+// {
251251+// Runnable repaint = new Runnable()
252252+// {
253253+// public void run()
254254+// {
255255+// //try {Thread.sleep(500);} catch(Exception e) {}
256256+// m_window.doLayout();
257257+//
258258+// /*if (!m_window.isVisible())
259259+// {
260260+// m_window.setVisible( true );
261261+// }*/
262262+// }
263263+// };
264264+// SwingUtilities.invokeLater(repaint);
265265+// }
693266694694- // set the agent position
695695- agent._position = position;
696696- return true;
697697- }
698698-699699- // check if point is within environment boundaries
700700- // return false is p is within bounds
701701- protected boolean isOutOfBounds( Point p )
702702- {
703703- if( (p.x >= m_size.getWidth()) || (p.x < 0) || (p.y >= m_size.getHeight()) || (p.y < 0) )
704704- {
705705- return true;
706706- }
707707-708708- return false;
709709- }
710710-711711- // Is the position free?
712712- public synchronized boolean isFree( final Point position )
713713- {
714714- return (isStone( position )) == null && (isAgent( position ) == null);
715715- }
716716-717717- // Check for agent at coordinate. \return Null if there is no agent at the
718718- // specified coordinate. Otherwise return a reference to the agent there.
719719- public synchronized Agent isAgent( final Point p )
720720- {
721721- synchronized (_agents) {
722722- Iterator i = _agents.iterator();
723723- while( i.hasNext() ) {
724724- final Agent agent = (Agent) i.next();
725725- if( p.equals( agent.getPosition() ) )
726726- return agent;
727727- }
728728- return null;
729729- }
730730- }
731731-732732- // Is there a stone at this point
733733- public synchronized TypeObject isStone( Point p )
734734- {
735735- synchronized (_agents) {
736736- Iterator i = _stones.iterator();
737737- while( i.hasNext() ) {
738738- TypeObject stones = (TypeObject) i.next();
739739- if( p.equals( stones.getPosition() ) )
740740- return stones;
741741- }
742742- return null;
743743- }
744744- }
745745-746746- // see if there is a trap at the specified coordinate
747747- public synchronized TypeObject isTrap( Point p ) {
748748- synchronized (_traps) {
749749- Iterator i = _traps.iterator();
750750- while( i.hasNext() ) {
751751- TypeObject trap = (TypeObject) i.next();
752752- if( p.equals( trap.getPosition() ) )
753753- return trap;
754754- }
755755- return null;
756756- }
757757- }
758758-759759- public synchronized TypeObject isBomb( Point p )
760760- {
761761- synchronized (_bombs) {
762762- Iterator i = _bombs.iterator();
763763- while( i.hasNext() ) {
764764- TypeObject bomb = (TypeObject)i.next();
765765- if(p.equals(bomb.getPosition()))
766766- return bomb;
767767- }
768768- return null;
769769- }
770770- }
771771-772772- // Remove bomb at position TODO Jaap; why is this different then remove stone???
773773- public synchronized TypeObject removeBomb( Point position )
774774- {
775775- // find bomb in bombs list
776776- synchronized(this) {
777777- Iterator i = _bombs.iterator();
778778- while (i.hasNext())
779779- {
780780- TypeObject bomb = (TypeObject) i.next();
781781- if (position.equals(bomb.getPosition()))
782782- {
783783- i.remove();
784784- return bomb;
785785- }
786786- }
787787- }
788788-789789- notifyEvent("bombRemovedAt", position);
790790- return null;
791791-792792- }
793793-794794- // remove stone at position
795795- public synchronized boolean removeStone( Point position )
796796- {
797797- synchronized(_stones) {
798798- // find stone in stones list
799799- Iterator i = _stones.iterator();
800800- while (i.hasNext())
801801- //if( position.equals( i.next() ) ) {
802802- // Changed SA:
803803- if (position.equals(((TypeObject)i.next()).getPosition()))
804804- {
805805- i.remove();
806806-807807- // there may be other threads blocked because this agent was in
808808- // the way, notify
809809- // them of the changed state of environment
810810- synchronized( this )
811811- {
812812- notifyAll();
813813- }
814814-815815- return true;
816816- }
817817- }
818818- notifyEvent("wallRemovedAt", position);
819819- return false;
820820- }
821821-822822- // remove trap at position
823823- public synchronized boolean removeTrap( Point position )
824824- {
825825- synchronized(_traps) {
826826- // find trap in traps list
827827- Iterator i = _traps.iterator();
828828- while (i.hasNext()) {
829829- if (position.equals(((TypeObject)i.next()).getPosition()))
830830- {
831831- i.remove();
832832-833833-834834- // Sohan: I believe this notification is unnecessary, commented it out:
835835- //synchronized( this ) {
836836- // notifyAll();
837837- //}
838838-839839- return true;
840840- }
841841- }
842842- }
843843-844844- notifyEvent("trapRemovedAt", position);
845845- return false;
846846- }
847847-848848- // Add a stone at the given position
849849- public synchronized boolean addStone( Point position ) throws IndexOutOfBoundsException
850850- {
851851- // valid coordinate
852852- if( isOutOfBounds( position ) )
853853- throw new IndexOutOfBoundsException( "setStone out of range: "
854854- + position + ", " + m_size );
855855-856856- // is position clear of other stuff
857857- // Changed SA:
858858- if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null )
859859- return false;
860860-861861- synchronized (_stones) {
862862- _stones.add( new TypeObject(_objType,position) );
863863- }
864864- notifyEvent("wallAt", position);
865865-866866- return true;
867867- }
868868-869869- // Add a bomb to the environment
870870- public synchronized boolean addBomb( Point position ) throws IndexOutOfBoundsException
871871- {
872872- if( isOutOfBounds( position ) )
873873- throw new IndexOutOfBoundsException( "addBomb outOfBounds: "
874874- + position + ", " + m_size );
875875-876876- // is position clear of other stuff
877877- if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null )
878878- return false;
879879-880880- // all clear, accept bomb
881881- synchronized (_bombs) {
882882- _bombs.add( new TypeObject(_objType,position) );
883883- }
884884- notifyEvent("bombAt", position);
885885-886886- return true;
887887- }
888888-889889- // Add a trap at the given position
890890- public synchronized boolean addTrap( Point position ) throws IndexOutOfBoundsException {
891891- // valid coordinate
892892- if( isOutOfBounds( position ) )
893893- throw new IndexOutOfBoundsException( "setTrap out of range: "
894894- + position + ", " + m_size );
895895-896896- // is position clear of other stuff
897897- // Changed SA:
898898- if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null )
899899- return false;
900900- synchronized(_traps) {
901901- _traps.add( new TypeObject(_objType,position) );
902902- }
903903- notifyEvent("trapAt", position);
904904-905905- return true;
906906- }
907267908268 // Print a message to the console
909269 static public void writeToLog(String message) {
910910- //System.out.println("blockworld: " + message);
270270+ System.out.println("cardtable: " + message);
911271 }
912272913273 // helper function to wrap a boolean value inside a ListPar.
···915275 {
916276 return new APLList(new APLIdent(b ? "true" : "false"));
917277 }
918918-919919- // Which color does the agent want to be!
920920- private int getColorID(String sColor)
921921- {
922922- if (sColor.equals("army") )
923923- {
924924- return 0;
925925- }
926926- else if (sColor.equals("blue") )
927927- {
928928- return 1;
929929- }
930930- else if (sColor.equals("gray") )
931931- {
932932- return 2;
933933- }
934934- else if (sColor.equals("green") )
935935- {
936936- return 3;
937937- }
938938- else if (sColor.equals("orange") )
939939- {
940940- return 4;
941941- }
942942- else if (sColor.equals("pink") )
943943- {
944944- return 5;
945945- }
946946- else if (sColor.equals("purple") )
947947- {
948948- return 6;
949949- }
950950- else if (sColor.equals("red") )
951951- {
952952- return 7;
953953- }
954954- else if (sColor.equals("teal") )
955955- {
956956- return 8;
957957- }
958958- else if (sColor.equals("yellow") )
959959- {
960960- return 9;
961961- }
962962-963963- // Red is the default
964964- return 7;
965965- }
966966-967967- // Set the senserange
968968- public void setSenseRange( int senserange )
969969- {
970970- _senserange = senserange;
971971- signalSenseRangeChanged.emit();
972972- }
278278+ /* END Helper functions --------------------------------------*/
973279974974- // helper function, calls setSize(Dimension)
975975- public void setSize( int width, int height )
976976- {
977977- setSize( new Dimension( width, height ) );
978978- }
979979-980980- // resize world
981981- public void setSize( Dimension size )
982982- {
983983- m_size = size;
984984- signalSizeChanged.emit();
985280986986- Iterator i = _bombs.iterator();
987987- while( i.hasNext() ) {
988988- if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) )
989989- i.remove();
990990- }
991991- i = _stones.iterator();
992992- while( i.hasNext() ) {
993993- if( isOutOfBounds( (Point) i.next() ) )
994994- i.remove();
995995- }
996996- i = _traps.iterator();
997997- while( i.hasNext() ) {
998998- if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) )
999999- i.remove();
10001000- }
281281+ private int getNumberOfPlayers() {
282282+ return numberOfPlayers;
1001283 }
100228410031003- // what kind of object is it, bomb, stone, wall ?
10041004- public String getObjType()
10051005- {
10061006- return _objType;
10071007- }
10081008-10091009- // what kind of object is it, bomb, stone, wall ?
10101010- public void setObjType(String objType)
10111011- {
10121012- _objType = objType;
10131013- }
10141014-10151015- // Remove everything
10161016- public void clear()
10171017- {
10181018- _stones.removeAllElements();
10191019- _bombs.removeAllElements();
10201020- _traps.removeAllElements();
285285+ private void increaseNumberOfPlayers() {
286286+ numberOfPlayers++;
1021287 }
102228810231023- // Save the environment
10241024- public void save( OutputStream destination ) throws IOException
10251025- {
10261026- ObjectOutputStream stream = new ObjectOutputStream( destination );
10271027- stream.writeObject( m_size );
10281028-10291029- stream.writeInt( _senserange );
10301030-10311031- stream.writeObject( (Vector) _stones );
10321032- stream.writeObject( (Vector) _bombs );
10331033- stream.writeObject( (Vector) _traps );
10341034- stream.flush();
10351035- }
10361036-10371037- // Load the environment
10381038- public void load( InputStream source ) throws IOException, ClassNotFoundException
10391039- {
10401040- ObjectInputStream stream = new ObjectInputStream( source );
10411041- Dimension size = (Dimension) stream.readObject();
10421042-10431043- int senserange = stream.readInt();
10441044-10451045- Vector stones = (Vector) stream.readObject();
10461046- Vector bombs = (Vector) stream.readObject();
10471047- Vector traps = (Vector) stream.readObject();
10481048-10491049- // delay assignments until complete load is succesfull
10501050- m_size = size;
10511051- _senserange = senserange;
10521052-10531053- signalSizeChanged.emit();
10541054- signalTrapChanged.emit();
10551055- signalSenseRangeChanged.emit();
10561056-10571057- clear();
10581058-10591059- _stones.addAll( stones );
10601060- _bombs.addAll( bombs );
10611061- _traps.addAll( traps );
289289+ private void decreaseNumberOfPlayers() {
290290+ numberOfPlayers--;
1062291 }
106329210641064- /* END Helper functions --------------------------------------*/
10651065-10661066- /* Listeners ------------------------------------------------*/
10671067-10681068- // / This listener is notified upon changes regarding the Agent list.
10691069- // / Please note that this only involves registering new agents or
10701070- // / removing existing agents. To track agent position changes, add
10711071- // / a listener to that specific agent.
10721072- // / \sa Agent
10731073- public void addAgentListener( ObsVectListener o )
10741074- {
10751075- _agents.addListener( o );
293293+ private Integer getPosition() {
294294+ return new Integer(numberOfPlayers);
1076295 }
10771077-10781078- // / This listener is notified upon changes regarding the Stones list.
10791079- public void addStonesListener( ObsVectListener o )
10801080- {
10811081- _stones.addListener( o );
10821082- }
10831083-10841084- // / This listener is notified upon changes regarding the Bombs list.
10851085- public void addBombsListener( ObsVectListener o )
10861086- {
10871087- _bombs.addListener( o );
10881088- }
10891089-10901090- // / This listener is notified upon changes regarding the Traps list.
10911091- public void addTrapsListener( ObsVectListener o )
10921092- {
10931093- _traps.addListener( o );
10941094- }
10951095-10961096- /* END Liseteners ------------------------------------------*/
10971097-10981098-10991099- /* Overrides for ObsVector ---------------------------------*/
11001100-11011101- public void onAdd( int index, Object o ) {}
11021102-11031103- public void onRemove( int index, Object o )
11041104- {
11051105- ((Agent) o).deleteObservers();
11061106- }
11071107-11081108- /* END Overrides for ObsVector ---------------------------------*/
1109296}