2-APL UPC project.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

upping correct version of Env

fdesan 84604d97 3f62528d

+137 -950
+137 -950
code/2apl_platform/environments/cardtable/Env.java
··· 1 - package blockworld; 2 - 3 - /* 4 - 3APL Blockworld program by Jelle Herold, copyright 2003. 5 - Written for the Intelligent Systems group at Utrecht University. 6 - This is LGPL software. 7 - 8 - $Id: Env.java,v 1.7 2004/12/27 02:22:41 cvs-3apl Exp $ 9 - */ 1 + package cardtable; 10 2 11 3 // 2APL imports 12 4 import apapl.Environment; ··· 31 23 import java.io.ObjectOutputStream; 32 24 import java.io.OutputStream; 33 25 34 - import blockworld.lib.ObsVectListener; 35 - import blockworld.lib.Signal; 36 - import blockworld.lib.ObsVect; 26 + import java.util.Random; 37 27 38 - public class Env extends Environment implements ObsVectListener 28 + public class Env extends Environment 39 29 { 40 - // To hold our reference to the window 41 - final protected Window main_window; 42 - 43 - // max distance of cells visible to each agent 44 - protected int _senserange = 5; 45 - 46 - // size of environment 47 - protected Dimension m_size = new Dimension( 16, 16 ); 48 - 49 - private HashMap<String,Agent> agentmap = new HashMap<String,Agent>(); 50 - 51 - /* ---- ALL the stuff in the environment -----*/ 52 - 53 - // list of agents (Agent) 54 - protected ObsVect _agents = new ObsVect( this ); 55 - 56 - // id for identifiable objects 57 - protected String _objType = "default"; 58 - 59 - 60 - 61 - 62 - /* ------------------------------------------*/ 63 - 64 - 65 - /* ---- SIGNALS -------------------------------*/ 66 - // / emitted on collection of a bomb in the bomb trap 67 - public transient Signal signalBombTrapped = new Signal( "env bomb trapped" ); 68 - 69 - public transient Signal signalSenseRangeChanged = new Signal( 70 - "env sense range changed" ); 71 - 72 - // emitted if environment is resized 73 - public transient Signal signalSizeChanged = new Signal( "env size changed" ); 74 - 75 - // emitted if bomb traps location changed 76 - public transient Signal signalTrapChanged = new Signal( 77 - "env trap position changed" ); 78 - 79 - /* ------------------------------------------*/ 30 + private HashMap<String,Agent> agentmap = new HashMap<String,Agent>(); 80 31 32 + private int numberOfPlayers, numberOfNotaries, numberOfGatekeepers; 81 33 82 34 // The default constructor 83 35 public Env() 84 36 { 85 37 super(); 86 38 // Create the window 87 - 88 - main_window = new Window( this ); 89 - } 90 - 91 - /* Called from 2APL */ 92 - 93 - // Enter the agent into the world 94 - // Succesful returns true, else the ExternalActionFailedException exception is thrown 95 - public Term enter( String sAgent, APLNum xt, APLNum yt, APLIdent colort) throws ExternalActionFailedException 96 - { 97 - int x = xt.toInt(); 98 - int y = yt.toInt(); 99 - String color = colort.toString(); 100 - 101 - // Get the agent 102 - Agent agent = getAgent(sAgent); 103 - 104 - // Give a signal that we want to move 105 - agent.signalMove.emit(); 106 - 107 - writeToLog( "Agent entered: " +agent.getName()); 108 - 109 - Point position = new Point(x,y); 110 - String pos = "("+x+","+y+")"; 111 - 112 - // Agent already entered 113 - if( agent.isEntered() ) 114 - { 115 - writeToLog( "agent already entered" ); 116 - throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" has already entered."); 117 - } 118 - 119 - // Is this position within the world? 120 - if (isOutOfBounds(position)) 121 - { 122 - throw new ExternalActionFailedException("Position "+pos+" out of bounds."); 123 - } 124 - // Is this position free? 125 - if( !isFree( position ) ) 126 - { 127 - throw new ExternalActionFailedException("Position "+pos+" is not free."); 128 - } 129 - 130 - // Update the agent his position 131 - agent._position = position; 132 - 133 - // Which color does the agent want to be 134 - int nColorID = getColorID(color); 135 - agent._colorID = nColorID; 136 - 137 - // Redraw so we can see the agent 138 - validatewindow(); 139 - main_window.repaint(); 140 - 141 - // We came so far, this means success! 142 - agent.signalMoveSucces.emit(); 143 - 144 - return wrapBoolean(true); 145 - } 146 - 147 - // Move the agent north 148 - public Term north(String sAgent) throws ExternalActionFailedException 149 - { 150 - // Get the correct agent 151 - Agent agent = getAgent(sAgent); 152 - 153 - // Get the agent his position 154 - Point p = (Point) agent.getPosition().clone(); 155 - p.y = p.y - 1; 39 + //m_window = new Window( this ); 156 40 157 - // Set the position for the agent 158 - boolean r = setAgentPosition( agent, p); 159 - 160 - // can't move north 161 - if (!r) throw new ExternalActionFailedException("Moving north failed."); 162 - 163 - // Redraw the window 164 - validatewindow(); 165 - main_window.repaint(); 166 - return wrapBoolean(r); 41 + numberOfPlayers=0; 42 + numberOfNotaries=0; 43 + numberOfGatekeepers=0; 167 44 } 168 45 169 - // Move the agent east 170 - public Term east(String sAgent) throws ExternalActionFailedException 171 - { 172 - // Get the correct agent 173 - Agent agent = getAgent(sAgent); 174 - 175 - // Get the agent his position 176 - Point p = (Point) agent.getPosition().clone(); 177 - p.x = p.x + 1; 178 - 179 - // Set the position for the agent 180 - boolean r = setAgentPosition( agent, p); 181 - 182 - // can't move north 183 - if (!r) throw new ExternalActionFailedException("Moving north failed."); 184 - 185 - // Redraw the window 186 - validatewindow(); 187 - main_window.repaint(); 188 - return wrapBoolean(r); 46 + public Term enterAsNotary(String sAgent) throws ExternalActionFailedException { 47 + Agent agent = getAgent(sAgent); 48 + writeToLog("Notary sit: " +agent.getName()); 49 + 50 + if(numberOfNotaries>0) 51 + throw new ExternalActionFailedException("There is already a notary at the table."); 52 + else 53 + numberOfNotaries++; 54 + 55 + agent._type=2; 56 + 57 + return wrapBoolean(true); 189 58 } 190 59 191 - // Move the agent south 192 - public Term south(String sAgent) throws ExternalActionFailedException 193 - { 194 - // Get the correct agent 195 - Agent agent = getAgent(sAgent); 196 - 197 - // Get the agent his position 198 - Point p = (Point) agent.getPosition().clone(); 199 - p.y = p.y + 1; 200 - 201 - // Set the position for the agent 202 - boolean r = setAgentPosition( agent, p); 203 - 204 - // can't move north 205 - if (!r) throw new ExternalActionFailedException("Moving north failed."); 206 - 207 - // Redraw the window 208 - validatewindow(); 209 - main_window.repaint(); 210 - return wrapBoolean(r); 60 + public Term enterAsGatekeeper(String sAgent) throws ExternalActionFailedException { 61 + Agent agent = getAgent(sAgent); 62 + writeToLog("Gatekeeper in the system: " +agent.getName()); 63 + 64 + if(numberOfGatekeepers>0) 65 + throw new ExternalActionFailedException("There is already a gatekeeper in the system."); 66 + else 67 + numberOfGatekeepers++; 68 + 69 + agent._type=1; 70 + 71 + return wrapBoolean(true); 211 72 } 212 - 213 - // Move the agent west 214 - public Term west(String sAgent) throws ExternalActionFailedException 215 - { 216 - // Get the correct agent 217 - Agent agent = getAgent(sAgent); 218 - 219 - // Get the agent his position 220 - Point p = (Point) agent.getPosition().clone(); 221 - p.x = p.x - 1; 222 - 223 - // Set the position for the agent 224 - boolean r = setAgentPosition( agent, p); 225 - 226 - // can't move north 227 - if (!r) throw new ExternalActionFailedException("Moving north failed."); 228 73 229 - // Redraw the window 230 - validatewindow(); 231 - main_window.repaint(); 232 - return wrapBoolean(r); 233 - } 234 - 235 - // Pickup a bomb 236 - public Term pickup( String sAgent ) throws ExternalActionFailedException 74 + // Sit the player at the table 75 + public Term sit(String sAgent, APLIdent player) throws ExternalActionFailedException 237 76 { 238 - // Get the agent 239 - Agent agent = getAgent(sAgent); 240 - 241 - // Let everyone know we are going to pick up a bomb 242 - agent.signalPickupBomb.emit(); 243 - 244 - // see if we are not already carrying a bomb 245 - if( agent.atCapacity() ) 246 - { 247 - writeToLog( "Pickup bomb failed" ); 248 - throw new ExternalActionFailedException("Pickup bomb failed"); 249 - } 250 - 251 - // we are not already carying a bomb so get this one 252 - TypeObject bomb = removeBomb( agent.getPosition() ); 253 - if( bomb == null ) 254 - { 255 - writeToLog( "Pickup bomb failed" ); 256 - throw new ExternalActionFailedException("Pickup bomb failed"); 257 - } 258 - 259 - // Yes 260 - agent.signalPickupBombSucces.emit(); 261 - 262 - // there was a bomb at that position, so set token 263 - agent.pickBomb(bomb); 77 + String player_name = player.toString(); 264 78 265 - // show what happened 266 - validatewindow(); 267 - main_window.repaint(); 79 + Agent agent = getAgent(player_name); 268 80 269 - return wrapBoolean(true); 270 - } 271 - 272 - // Drop a bomb 273 - public Term drop( String sAgent ) throws ExternalActionFailedException 274 - { 275 - // Get the agent 276 - Agent agent = getAgent(sAgent); 277 - // we are going to drop a bomb 278 - agent.signalDropBomb.emit(); 279 - 280 - TypeObject bomb = agent.senseBomb(); 281 - // see if we are actually carrying a bomb 282 - if( bomb == null) 81 + // is already sit? 82 + if(agent.isSit()) 283 83 { 284 - writeToLog( "Drop bomb failed" ); 285 - throw new ExternalActionFailedException("Drop bomb failed"); 84 + writeToLog( "agent already sit" ); 85 + throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" is already sit."); 286 86 } 287 - 288 - Point pos = agent.getPosition(); 289 - // see if we can drop that bomb here 290 87 291 - if( !addBomb(pos) && (isTrap( pos ) == null 292 - || agent.senseBomb( isTrap( pos ).getType() ) == null) ) 88 + // are there already 5 players at the table? 89 + if (getNumberOfPlayers()==5) 293 90 { 294 - writeToLog( "Drop bomb failed" ); 295 - throw new ExternalActionFailedException("Drop bomb failed"); 91 + throw new ExternalActionFailedException("Already 5 players sit at the table."); 296 92 } 297 93 298 - if(isTrap( pos ) != null 299 - && agent.senseBomb( isTrap( pos ).getType() ) != null) 300 - { 301 - signalBombTrapped.emit(); 302 - } 303 - 304 - // unset token 305 - agent.dropBomb(); 306 - 307 - agent.signalDropBombSucces.emit(); 308 - 309 - // Show it 310 - validatewindow(); 311 - main_window.repaint(); 312 - 313 - // return success 314 - return wrapBoolean(true); 315 - } 316 - 317 - // What is the agent his Sense Range 318 - public Term getSenseRange(String agent) 319 - { 320 - // the below function is also used by EnvView 321 - int r = getSenseRange(); 322 - return new APLList(new APLNum(r)); 323 - } 324 - 325 - // Sense all agents. This does not include self. 326 - public synchronized Term senseAllAgent(String sAgent) throws ExternalActionFailedException 327 - { 328 - //Collection c = senseAllAgents(getAgent(agent).getPosition()); 329 - Point position = getAgent(sAgent).getPosition(); 330 - // iterate over all agents 331 - Vector all = new Vector(); 332 - 333 - Iterator j = _agents.iterator(); 334 - while( j.hasNext() ) { 335 - Agent agent = (Agent) j.next(); 336 - Point p = agent.getPosition(); 337 - 338 - // Changed SA: when there are no other agents, this return null, which 339 - // causes Java to throw an exception and never return the empty list. 340 - if (p == null) 341 - continue; 342 - 343 - // skip self 344 - if( p.equals( position ) ) 345 - continue; 94 + // Update the agent his position 95 + agent._position = getPosition(); 96 + agent._type=0; // is a player 346 97 347 - all.add( agent ); 348 - } 349 - 350 - LinkedList<Term> listpar = new LinkedList<Term>(); 351 - for(Object i : all) { 352 - final Agent a = (Agent) i; 353 - APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y)); 354 - listpar.add(tmp); 355 - } 356 - return new APLList(listpar); 357 - } 358 - 359 - // Sense the given agent his position 360 - public synchronized Term sensePosition(String sAgent) throws ExternalActionFailedException 361 - { 362 - Point p = getAgent(sAgent).getPosition(); 363 - return new APLList(new APLNum(p.x),new APLNum(p.y)); 364 - } 365 - 366 - // is there a trap in the senserange of the agent? 367 - public synchronized Term senseTraps(String agent) throws ExternalActionFailedException 368 - { 369 - // Get the agent his position 370 - Point position = getAgent(agent).getPosition(); 98 + // Increase number of players 99 + increaseNumberOfPlayers(); 371 100 372 - // iterate over all traps and decide according to distance if it is in 373 - // vision range 374 - Vector visible = new Vector(); 375 - 376 - Iterator i = _traps.iterator(); 377 - while( i.hasNext() ) { 378 - TypeObject t = (TypeObject) i.next(); 379 - if( position.distance( t.getPosition() ) <= _senserange ) 380 - visible.add( t ); 381 - } 101 + // Redraw so we can see the agent 102 + //validatewindow(); 103 + //m_window.repaint(); 382 104 383 - return convertCollectionToTerm(visible); 384 - } 385 - 386 - // Get all the traps in the env 387 - public synchronized Term senseAllTraps(String agent) 388 - { 389 - // iterate over all traps 390 - Vector all = new Vector(); 105 + // We came so far, this means success! 106 + //agent.signalMoveSucces.emit(); 391 107 392 - Iterator i = _traps.iterator(); 393 - while( i.hasNext() ) 394 - { 395 - all.add( (TypeObject) i.next() ); 396 - } 397 - 398 - return convertCollectionToTerm(all); 399 - } 400 - 401 - // Sends a bom in the senserange of the agent 402 - public synchronized Term senseBombs(String agent) throws ExternalActionFailedException 403 - { 404 - // Get the agent his position 405 - Point position = getAgent(agent).getPosition(); 406 - 407 - // iterate over all bombs and decide according to distance if it is in 408 - // vision range 409 - Vector visible = new Vector(); 108 + writeToLog("Player sit: " +agent.getName()); 410 109 411 - Iterator i = _bombs.iterator(); 412 - while( i.hasNext() ) 413 - { 414 - TypeObject b = (TypeObject) i.next(); 415 - if( position.distance( b.getPosition() ) <= _senserange ) 416 - visible.add( b ); 417 - } 418 - 419 - return convertCollectionToTerm(visible); 110 + return new APLNum(agent._position); 420 111 } 421 - 422 - // Find all the bombs in the environment 423 - public synchronized Term senseAllBombs (String agent) 424 - { 425 - // iterate over all bombs 426 - Vector all = new Vector(); 427 112 428 - Iterator i = _bombs.iterator(); 429 - while( i.hasNext() ) 430 - { 431 - all.add( (TypeObject) i.next() ); 432 - } 433 - 434 - return convertCollectionToTerm(all); 435 - } 436 - 437 - // Sense the stones in the agent senserange 438 - public synchronized Term senseStones(String agent) throws ExternalActionFailedException 113 + public Term chooseDealer(String sAgent) throws ExternalActionFailedException 439 114 { 440 - // Get the agent his position 441 - Point position = getAgent(agent).getPosition(); 442 - 443 - // iterate over all stones and decide according to distance if it is in 444 - // vision range 445 - Vector visible = new Vector(); 446 - 447 - Iterator i = _stones.iterator(); 448 - while( i.hasNext() ) 115 + if (getNumberOfPlayers()!=5) 449 116 { 450 - TypeObject t = (TypeObject) i.next(); 451 - if( position.distance( t.getPosition() ) <= _senserange ) 452 - visible.add( t ); 117 + throw new ExternalActionFailedException("There are not 5 players sit at the table."); 453 118 } 454 - 455 - return convertCollectionToTerm(visible); 456 - } 457 - 458 - // Sense all stones 459 - public synchronized Term senseAllStones (String agent) 460 - { 461 - // iterate over all traps 462 - Vector all = new Vector(); 463 119 464 - Iterator i = _stones.iterator(); 465 - while( i.hasNext() ) 466 - { 467 - all.add( (TypeObject) i.next() ); 468 - } 469 - 470 - return convertCollectionToTerm(all); 471 - } 472 - 473 - // Sense visible area for agents. This does not include self. 474 - public synchronized Term senseAgent(String sAgent) throws ExternalActionFailedException 475 - { 476 - //Collection c = senseAgents(getAgent(agent).getPosition()); 477 - Point position = getAgent(sAgent).getPosition(); 478 - 479 - // iterate over all agents and decide according to distance if it is in 480 - // vision range 481 - Vector visible = new Vector(); 482 - 483 - Iterator j = _agents.iterator(); 484 - 485 - while( j.hasNext() ) 486 - { 487 - Agent agent = (Agent) j.next(); 120 + Random rand = new Random(); 121 + int n = 5; 122 + int randnum = rand.nextInt(n+1); 488 123 489 - Point p = agent.getPosition(); 490 - 491 - // Changed SA: when there are no other agents, this return null, which 492 - // causes Java to throw an exception and never return the empty list. 493 - if (p == null) 494 - continue; 495 - 496 - // skip self 497 - if( p.equals( position ) ) 498 - continue; 124 + Agent a = getAgent(randnum); 499 125 500 - // agent within visible range 501 - if( position.distance( p ) <= _senserange ) 502 - visible.add( agent ); 503 - } 504 - 505 - LinkedList<Term> listpar = new LinkedList<Term>(); 506 - for(Object i : visible) { 507 - final Agent a = (Agent) i; 508 - APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y)); 509 - listpar.add(tmp); 510 - } 511 - return new APLList(listpar); 126 + return new APLIdent(a.getName()); 512 127 } 513 - 514 - 515 128 516 129 /* Standard functions --------------------------------------*/ 517 130 518 131 private void notifyAgents(APLFunction event, String... receivers) { 519 - throwEvent(event, receivers); 132 + // throwEvent(event, receivers); 520 133 } 521 134 522 135 private void notifyEvent(String parm1, Point ptPosition) 523 136 { 524 - APLNum nX = new APLNum((double)(ptPosition.getX())); 525 - APLNum nY = new APLNum((double)(ptPosition.getY())); 526 - 527 - // Send an external event to all agents within the senserange. 528 - ArrayList<String> targetAgents = new ArrayList<String>(); 529 - for (Agent a : agentmap.values()) 530 - { 531 - // Changed SA: I got no idea why there is always 1 agent which does not exists, 532 - // but this fixes the exceptions 533 - if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange())) 534 - targetAgents.add(a.getName()); 535 - } 536 - 537 - writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents); 538 - 539 - if (!targetAgents.isEmpty()) 540 - { 541 - notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0])); 542 - } 137 + // APLNum nX = new APLNum((double)(ptPosition.getX())); 138 + // APLNum nY = new APLNum((double)(ptPosition.getY())); 139 + // 140 + // // Send an external event to all agents within the senserange. 141 + // ArrayList<String> targetAgents = new ArrayList<String>(); 142 + // for (Agent a : agentmap.values()) 143 + // { 144 + // // Changed SA: I got no idea why there is always 1 agent which does not exists, 145 + // // but this fixes the exceptions 146 + // if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange())) 147 + // targetAgents.add(a.getName()); 148 + // } 149 + // 150 + // writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents); 151 + // 152 + // if (!targetAgents.isEmpty()) 153 + // { 154 + // notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0])); 155 + // } 543 156 } 544 157 545 158 // Add an agent to the environment ··· 551 164 writeToLog("linking " + sAgent + ""); 552 165 } else{ 553 166 final Agent agent = new Agent(sAgentMain); 554 - _agents.add(agent); 167 + //_agents.add(agent); 555 168 agentmap.put(sAgent, agent); 556 169 writeToLog("agent " + agent + " added"); 557 170 } ··· 569 182 570 183 // there can be several agent 571 184 if (!agentmap.containsValue(a)) { 572 - _agents.remove(a); 573 - a.reset(); 185 + //_agents.remove(a); 186 + //a.reset(); 574 187 } 575 188 576 189 writeToLog("Agent removed: " + sAgent); ··· 588 201 589 202 590 203 591 - /* Helper functions --------------------------------------*/ 592 - 593 - // Get the size of the blockworld 594 - public synchronized Term getWorldSize(String agent) 595 - { 596 - int w = getWidth(); 597 - int h = getHeight(); 598 - return new APLList(new APLNum(w),new APLNum(h)); 599 - } 600 - 204 + /* Helper functions --------------------------------------*/ 205 + 601 206 // Get the agent from its name 602 207 private synchronized Agent getAgent(String name) throws ExternalActionFailedException 603 208 { ··· 608 213 else return a; 609 214 610 215 } 216 + 217 + // Get the agent from its position 218 + private synchronized Agent getAgent(int position) throws ExternalActionFailedException 219 + { 220 + Agent a = null; 221 + Iterator it = agentmap.keySet().iterator(); 222 + while(it.hasNext()) { 223 + Object name = it.next(); 224 + a = agentmap.get(name); 225 + if(a!=null) { 226 + if(a.getPosition()!=null) { 227 + if(a.getPosition().intValue()==position) { 228 + return a; 229 + } 230 + } 231 + } 232 + } 233 + if (a==null) throw new ExternalActionFailedException("No such agent at position: "+position); 234 + 235 + return a; 236 + } 611 237 612 238 private static String getMainModule(String sAgent) 613 239 { ··· 618 244 return sAgent.substring(0, dotPos); 619 245 } 620 246 621 - // Get the environment width 622 - public synchronized int getWidth() { return m_size.width; } 623 - 624 - // Get the environment height 625 - public synchronized int getHeight() { return m_size.height; } 626 - 627 - // Return the agents 628 - public synchronized Collection getBlockWorldAgents() 629 - { 630 - return new Vector(_agents); 631 - } 632 - 633 - // convert a collection to a term 634 - private static Term convertCollectionToTerm(Collection c) 635 - { 636 - LinkedList<Term> listpar = new LinkedList<Term>(); 637 - for(Object i : c) { 638 - final TypeObject o = (TypeObject) i; 639 - APLListVar tmp = new APLList(new APLIdent(o.getType()),new APLNum(o.getPosition().x),new APLNum(o.getPosition().y)); 640 - listpar.add(tmp); 641 - } 642 - return new APLList(listpar); 643 - } 644 - 645 - // Get senserange 646 - public int getSenseRange() 647 - { 648 - return _senserange; 649 - } 650 247 651 248 // Redrawing the window is a nightmare, this does some redraw stuff 652 - private void validatewindow() 653 - { 654 - Runnable repaint = new Runnable() 655 - { 656 - public void run() 657 - { 658 - //try {Thread.sleep(500);} catch(Exception e) {} 659 - main_window.doLayout(); 660 - 661 - /*if (!m_window.isVisible()) 662 - { 663 - m_window.setVisible( true ); 664 - }*/ 665 - } 666 - }; 667 - SwingUtilities.invokeLater(repaint); 668 - } 669 - 670 - // Move the agent 671 - private synchronized boolean setAgentPosition( Agent agent, Point position) 672 - { 673 - agent.signalMove.emit(); 674 - 675 - if( isOutOfBounds( position ) ) 676 - return false; 677 - 678 - // suspend thread if some other agent is blocking our entrance 679 - 680 - // Is the position free? 681 - if( !isFree( position ) ) 682 - return false; 683 - 684 - agent.signalMoveSucces.emit(); 685 - 686 - // there may be other threads blocked because this agent was in the way, 687 - // notify 688 - // them of the changed state of environment 689 - synchronized( this ) 690 - { 691 - notifyAll(); 692 - } 249 + // private void validatewindow() 250 + // { 251 + // Runnable repaint = new Runnable() 252 + // { 253 + // public void run() 254 + // { 255 + // //try {Thread.sleep(500);} catch(Exception e) {} 256 + // m_window.doLayout(); 257 + // 258 + // /*if (!m_window.isVisible()) 259 + // { 260 + // m_window.setVisible( true ); 261 + // }*/ 262 + // } 263 + // }; 264 + // SwingUtilities.invokeLater(repaint); 265 + // } 693 266 694 - // set the agent position 695 - agent._position = position; 696 - return true; 697 - } 698 - 699 - // check if point is within environment boundaries 700 - // return false is p is within bounds 701 - protected boolean isOutOfBounds( Point p ) 702 - { 703 - if( (p.x >= m_size.getWidth()) || (p.x < 0) || (p.y >= m_size.getHeight()) || (p.y < 0) ) 704 - { 705 - return true; 706 - } 707 - 708 - return false; 709 - } 710 - 711 - // Is the position free? 712 - public synchronized boolean isFree( final Point position ) 713 - { 714 - return (isStone( position )) == null && (isAgent( position ) == null); 715 - } 716 - 717 - // Check for agent at coordinate. \return Null if there is no agent at the 718 - // specified coordinate. Otherwise return a reference to the agent there. 719 - public synchronized Agent isAgent( final Point p ) 720 - { 721 - synchronized (_agents) { 722 - Iterator i = _agents.iterator(); 723 - while( i.hasNext() ) { 724 - final Agent agent = (Agent) i.next(); 725 - if( p.equals( agent.getPosition() ) ) 726 - return agent; 727 - } 728 - return null; 729 - } 730 - } 731 - 732 - // Is there a stone at this point 733 - public synchronized TypeObject isStone( Point p ) 734 - { 735 - synchronized (_agents) { 736 - Iterator i = _stones.iterator(); 737 - while( i.hasNext() ) { 738 - TypeObject stones = (TypeObject) i.next(); 739 - if( p.equals( stones.getPosition() ) ) 740 - return stones; 741 - } 742 - return null; 743 - } 744 - } 745 - 746 - // see if there is a trap at the specified coordinate 747 - public synchronized TypeObject isTrap( Point p ) { 748 - synchronized (_traps) { 749 - Iterator i = _traps.iterator(); 750 - while( i.hasNext() ) { 751 - TypeObject trap = (TypeObject) i.next(); 752 - if( p.equals( trap.getPosition() ) ) 753 - return trap; 754 - } 755 - return null; 756 - } 757 - } 758 - 759 - public synchronized TypeObject isBomb( Point p ) 760 - { 761 - synchronized (_bombs) { 762 - Iterator i = _bombs.iterator(); 763 - while( i.hasNext() ) { 764 - TypeObject bomb = (TypeObject)i.next(); 765 - if(p.equals(bomb.getPosition())) 766 - return bomb; 767 - } 768 - return null; 769 - } 770 - } 771 - 772 - // Remove bomb at position TODO Jaap; why is this different then remove stone??? 773 - public synchronized TypeObject removeBomb( Point position ) 774 - { 775 - // find bomb in bombs list 776 - synchronized(this) { 777 - Iterator i = _bombs.iterator(); 778 - while (i.hasNext()) 779 - { 780 - TypeObject bomb = (TypeObject) i.next(); 781 - if (position.equals(bomb.getPosition())) 782 - { 783 - i.remove(); 784 - return bomb; 785 - } 786 - } 787 - } 788 - 789 - notifyEvent("bombRemovedAt", position); 790 - return null; 791 - 792 - } 793 - 794 - // remove stone at position 795 - public synchronized boolean removeStone( Point position ) 796 - { 797 - synchronized(_stones) { 798 - // find stone in stones list 799 - Iterator i = _stones.iterator(); 800 - while (i.hasNext()) 801 - //if( position.equals( i.next() ) ) { 802 - // Changed SA: 803 - if (position.equals(((TypeObject)i.next()).getPosition())) 804 - { 805 - i.remove(); 806 - 807 - // there may be other threads blocked because this agent was in 808 - // the way, notify 809 - // them of the changed state of environment 810 - synchronized( this ) 811 - { 812 - notifyAll(); 813 - } 814 - 815 - return true; 816 - } 817 - } 818 - notifyEvent("wallRemovedAt", position); 819 - return false; 820 - } 821 - 822 - // remove trap at position 823 - public synchronized boolean removeTrap( Point position ) 824 - { 825 - synchronized(_traps) { 826 - // find trap in traps list 827 - Iterator i = _traps.iterator(); 828 - while (i.hasNext()) { 829 - if (position.equals(((TypeObject)i.next()).getPosition())) 830 - { 831 - i.remove(); 832 - 833 - 834 - // Sohan: I believe this notification is unnecessary, commented it out: 835 - //synchronized( this ) { 836 - // notifyAll(); 837 - //} 838 - 839 - return true; 840 - } 841 - } 842 - } 843 - 844 - notifyEvent("trapRemovedAt", position); 845 - return false; 846 - } 847 - 848 - // Add a stone at the given position 849 - public synchronized boolean addStone( Point position ) throws IndexOutOfBoundsException 850 - { 851 - // valid coordinate 852 - if( isOutOfBounds( position ) ) 853 - throw new IndexOutOfBoundsException( "setStone out of range: " 854 - + position + ", " + m_size ); 855 - 856 - // is position clear of other stuff 857 - // Changed SA: 858 - if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 859 - return false; 860 - 861 - synchronized (_stones) { 862 - _stones.add( new TypeObject(_objType,position) ); 863 - } 864 - notifyEvent("wallAt", position); 865 - 866 - return true; 867 - } 868 - 869 - // Add a bomb to the environment 870 - public synchronized boolean addBomb( Point position ) throws IndexOutOfBoundsException 871 - { 872 - if( isOutOfBounds( position ) ) 873 - throw new IndexOutOfBoundsException( "addBomb outOfBounds: " 874 - + position + ", " + m_size ); 875 - 876 - // is position clear of other stuff 877 - if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 878 - return false; 879 - 880 - // all clear, accept bomb 881 - synchronized (_bombs) { 882 - _bombs.add( new TypeObject(_objType,position) ); 883 - } 884 - notifyEvent("bombAt", position); 885 - 886 - return true; 887 - } 888 - 889 - // Add a trap at the given position 890 - public synchronized boolean addTrap( Point position ) throws IndexOutOfBoundsException { 891 - // valid coordinate 892 - if( isOutOfBounds( position ) ) 893 - throw new IndexOutOfBoundsException( "setTrap out of range: " 894 - + position + ", " + m_size ); 895 - 896 - // is position clear of other stuff 897 - // Changed SA: 898 - if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 899 - return false; 900 - synchronized(_traps) { 901 - _traps.add( new TypeObject(_objType,position) ); 902 - } 903 - notifyEvent("trapAt", position); 904 - 905 - return true; 906 - } 907 267 908 268 // Print a message to the console 909 269 static public void writeToLog(String message) { 910 - //System.out.println("blockworld: " + message); 270 + System.out.println("cardtable: " + message); 911 271 } 912 272 913 273 // helper function to wrap a boolean value inside a ListPar. ··· 915 275 { 916 276 return new APLList(new APLIdent(b ? "true" : "false")); 917 277 } 918 - 919 - // Which color does the agent want to be! 920 - private int getColorID(String sColor) 921 - { 922 - if (sColor.equals("army") ) 923 - { 924 - return 0; 925 - } 926 - else if (sColor.equals("blue") ) 927 - { 928 - return 1; 929 - } 930 - else if (sColor.equals("gray") ) 931 - { 932 - return 2; 933 - } 934 - else if (sColor.equals("green") ) 935 - { 936 - return 3; 937 - } 938 - else if (sColor.equals("orange") ) 939 - { 940 - return 4; 941 - } 942 - else if (sColor.equals("pink") ) 943 - { 944 - return 5; 945 - } 946 - else if (sColor.equals("purple") ) 947 - { 948 - return 6; 949 - } 950 - else if (sColor.equals("red") ) 951 - { 952 - return 7; 953 - } 954 - else if (sColor.equals("teal") ) 955 - { 956 - return 8; 957 - } 958 - else if (sColor.equals("yellow") ) 959 - { 960 - return 9; 961 - } 962 - 963 - // Red is the default 964 - return 7; 965 - } 966 - 967 - // Set the senserange 968 - public void setSenseRange( int senserange ) 969 - { 970 - _senserange = senserange; 971 - signalSenseRangeChanged.emit(); 972 - } 278 + /* END Helper functions --------------------------------------*/ 973 279 974 - // helper function, calls setSize(Dimension) 975 - public void setSize( int width, int height ) 976 - { 977 - setSize( new Dimension( width, height ) ); 978 - } 979 - 980 - // resize world 981 - public void setSize( Dimension size ) 982 - { 983 - m_size = size; 984 - signalSizeChanged.emit(); 985 280 986 - Iterator i = _bombs.iterator(); 987 - while( i.hasNext() ) { 988 - if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) ) 989 - i.remove(); 990 - } 991 - i = _stones.iterator(); 992 - while( i.hasNext() ) { 993 - if( isOutOfBounds( (Point) i.next() ) ) 994 - i.remove(); 995 - } 996 - i = _traps.iterator(); 997 - while( i.hasNext() ) { 998 - if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) ) 999 - i.remove(); 1000 - } 281 + private int getNumberOfPlayers() { 282 + return numberOfPlayers; 1001 283 } 1002 284 1003 - // what kind of object is it, bomb, stone, wall ? 1004 - public String getObjType() 1005 - { 1006 - return _objType; 1007 - } 1008 - 1009 - // what kind of object is it, bomb, stone, wall ? 1010 - public void setObjType(String objType) 1011 - { 1012 - _objType = objType; 1013 - } 1014 - 1015 - // Remove everything 1016 - public void clear() 1017 - { 1018 - _stones.removeAllElements(); 1019 - _bombs.removeAllElements(); 1020 - _traps.removeAllElements(); 285 + private void increaseNumberOfPlayers() { 286 + numberOfPlayers++; 1021 287 } 1022 288 1023 - // Save the environment 1024 - public void save( OutputStream destination ) throws IOException 1025 - { 1026 - ObjectOutputStream stream = new ObjectOutputStream( destination ); 1027 - stream.writeObject( m_size ); 1028 - 1029 - stream.writeInt( _senserange ); 1030 - 1031 - stream.writeObject( (Vector) _stones ); 1032 - stream.writeObject( (Vector) _bombs ); 1033 - stream.writeObject( (Vector) _traps ); 1034 - stream.flush(); 1035 - } 1036 - 1037 - // Load the environment 1038 - public void load( InputStream source ) throws IOException, ClassNotFoundException 1039 - { 1040 - ObjectInputStream stream = new ObjectInputStream( source ); 1041 - Dimension size = (Dimension) stream.readObject(); 1042 - 1043 - int senserange = stream.readInt(); 1044 - 1045 - Vector stones = (Vector) stream.readObject(); 1046 - Vector bombs = (Vector) stream.readObject(); 1047 - Vector traps = (Vector) stream.readObject(); 1048 - 1049 - // delay assignments until complete load is succesfull 1050 - m_size = size; 1051 - _senserange = senserange; 1052 - 1053 - signalSizeChanged.emit(); 1054 - signalTrapChanged.emit(); 1055 - signalSenseRangeChanged.emit(); 1056 - 1057 - clear(); 1058 - 1059 - _stones.addAll( stones ); 1060 - _bombs.addAll( bombs ); 1061 - _traps.addAll( traps ); 289 + private void decreaseNumberOfPlayers() { 290 + numberOfPlayers--; 1062 291 } 1063 292 1064 - /* END Helper functions --------------------------------------*/ 1065 - 1066 - /* Listeners ------------------------------------------------*/ 1067 - 1068 - // / This listener is notified upon changes regarding the Agent list. 1069 - // / Please note that this only involves registering new agents or 1070 - // / removing existing agents. To track agent position changes, add 1071 - // / a listener to that specific agent. 1072 - // / \sa Agent 1073 - public void addAgentListener( ObsVectListener o ) 1074 - { 1075 - _agents.addListener( o ); 293 + private Integer getPosition() { 294 + return new Integer(numberOfPlayers); 1076 295 } 1077 - 1078 - // / This listener is notified upon changes regarding the Stones list. 1079 - public void addStonesListener( ObsVectListener o ) 1080 - { 1081 - _stones.addListener( o ); 1082 - } 1083 - 1084 - // / This listener is notified upon changes regarding the Bombs list. 1085 - public void addBombsListener( ObsVectListener o ) 1086 - { 1087 - _bombs.addListener( o ); 1088 - } 1089 - 1090 - // / This listener is notified upon changes regarding the Traps list. 1091 - public void addTrapsListener( ObsVectListener o ) 1092 - { 1093 - _traps.addListener( o ); 1094 - } 1095 - 1096 - /* END Liseteners ------------------------------------------*/ 1097 - 1098 - 1099 - /* Overrides for ObsVector ---------------------------------*/ 1100 - 1101 - public void onAdd( int index, Object o ) {} 1102 - 1103 - public void onRemove( int index, Object o ) 1104 - { 1105 - ((Agent) o).deleteObservers(); 1106 - } 1107 - 1108 - /* END Overrides for ObsVector ---------------------------------*/ 1109 296 }