Something went wrong. Try again.
2-APL UPC project.
Something went wrong. Try again.
13 kB · 435 lines
Java
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436package cardtable;
// 2APL importsimport apapl.Environment;import apapl.ExternalActionFailedException;import apapl.data.*;
// Standard java importsimport java.awt.Point;import java.util.*;import javax.swing.SwingUtilities;import java.awt.Dimension;
import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;
public class Env extends Environment{ final protected Table table; private HashMap<String,Agent> agentmap = new HashMap<String,Agent>();
private int numberOfPlayers, numberOfNotaries, numberOfGatekeepers, numberOfDealers;
private Deck deck;
private Scoreboard sb; // The default constructor public Env() { super(); numberOfPlayers=0; numberOfNotaries=0; numberOfGatekeepers=0;
deck = new Deck(); sb = new Scoreboard();
//Create the window table = new Table(); } public Term enterAsNotary(String sAgent) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); writeToLog("Notary sit: " +agent.getName()); if(numberOfNotaries>0) throw new ExternalActionFailedException("There is already a notary at the table."); else numberOfNotaries++; agent._type=2; agent._position=-1; /* notary hasn't a real position, but it needs a value in order to be sit at the table */ return wrapBoolean(true); } public Term enterAsDealer(String sAgent) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); writeToLog("Dealer sit: " +agent.getName()); if(numberOfDealers>0) throw new ExternalActionFailedException("There is already a dealer at the table."); else numberOfDealers++; agent._type=3; agent._position=-1; /* dealer hasn't a real position, but it needs a value in order to be sit at the table */ return wrapBoolean(true); }
public Term enterAsGatekeeper(String sAgent) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); writeToLog("Gatekeeper in the system: " +agent.getName()); if(numberOfGatekeepers>0) throw new ExternalActionFailedException("There is already a gatekeeper in the system."); else numberOfGatekeepers++; agent._type=1; agent._position=-1; /* gk hasn't a real position, but it needs a value in order to be sit at the table */ return wrapBoolean(true); }
// Sit the player at the table public Term sit(String sAgent, APLIdent player) throws ExternalActionFailedException { String player_name = player.toString(); Agent agent = getAgent(player_name); // is already sit? if(agent.isSit()) { writeToLog( "agent already sit" ); throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" is already sit."); } // are there already 5 players at the table? if (getNumberOfPlayers()==5) { throw new ExternalActionFailedException("Already 5 players sit at the table."); } // Update the agent his position agent._position = getPosition(); agent._type=0; // is a player
// Increase number of players increaseNumberOfPlayers(); // Add it to the scoreboard sb.addPlayer(agent.getName());
// Add it to the gui table.addPlayer(player_name, agent._position, 0, 0); validatewindow(); table.repaint();
writeToLog("Player sit: " +agent.getName());
return new APLNum(agent._position); }
public Term chooseDealer(String sAgent) throws ExternalActionFailedException { if (getNumberOfPlayers()!=5) { throw new ExternalActionFailedException("There are not 5 players sit at the table."); }
Random rand = new Random(); int n = 5; int randnum = rand.nextInt(n+1);
Agent a = getAgent(randnum);
return new APLIdent(a.getName()); }
public Term shuffleDeck(String sAgent) throws ExternalActionFailedException { Agent agent = getAgent(sAgent);
if (agent.getType()!=3) { throw new ExternalActionFailedException("Only the dealer can shuffle the deck."); }
deck.shuffle();
Agent d = getDealer(); notifyEvent("deckShuffled", deck, d);
return wrapBoolean(true); }
public Term playCard(String sAgent, APLIdent suit, APLIdent rank) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); notifyEvent("cardPlayed", suit, rank, agent); Card played_card = deck.getCard(suit.toString(), rank.toString()); table.playedCard(agent.getName(), agent._position, played_card); return wrapBoolean(true); }
public Term updateScore(String sAgent, APLIdent player_name, APLNum score) throws ExternalActionFailedException { Agent agent = getAgent(player_name.toString()); sb.updateScore(player_name.toString(), score.toInt()); table.updateScore(agent.getName(), agent._position, score.toInt()); notifyEvent("scoreUpdated", player_name, score); return wrapBoolean(true); }
public Term updatePoints(String sAgent, APLIdent player_name, APLNum points) throws ExternalActionFailedException { Agent agent = getAgent(player_name.toString()); table.updatePoints(agent.getName(), agent._position, points.toInt()); return wrapBoolean(true); }
public Term updateBid(String sAgent, APLNum bid) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); table.updateBid(agent.getName(), agent._position, bid.toInt()); return wrapBoolean(true); } public Term briscolaDeclared(String sAgent, APLIdent suit, APLIdent rank) throws ExternalActionFailedException { Agent agent = getAgent(sAgent); Card played_card = deck.getCard(suit.toString(), rank.toString()); table.briscolaDeclared(agent.getName(), agent._position, played_card); return wrapBoolean(true); }
public Term handDealt(String sAgent, APLList cards, APLIdent player_name) throws ExternalActionFailedException { Agent agent = getAgent(sAgent);
if (agent.getType()!=3) { throw new ExternalActionFailedException("Only the dealer can deal cards."); }
LinkedList<Term> ll = cards.toLinkedList(); Hand hand = new Hand(); for(int i=0, j=0; i<8; i++, j+=2) { hand.addCard(deck.getCard(ll.get(j).toString(), ll.get(j+1).toString())); //System.out.println(hand[i].getSuit().getName()+" "+hand[i].getRank().getName()); }
Agent player_agent = getAgent(player_name.toString());
table.displayHand(player_name.toString(), player_agent._position, hand);
return wrapBoolean(true); }
/* Standard functions --------------------------------------*/ private void notifyAgents(APLFunction event, String... receivers) { throwEvent(event, receivers); } private void notifyEvent(String parm1, Deck deck, Agent d) { LinkedList returnList = new LinkedList(); List deckList = deck.getDeck();
String deckStr = new String("[ ");
for (Iterator<Card> i = deckList.iterator( ); i.hasNext( ); ) { Card card = i.next(); Suit suit = card.getSuit(); APLIdent id = new APLIdent(suit.getName()); deckStr += "<" + suit.getName() + ", "; returnList.add(id); Rank rank = card.getRank(); id = new APLIdent(rank.getName()); deckStr += rank.getName() + ">"; returnList.add(id); if(i.hasNext()) deckStr += ", "; } deckStr += " ]"; if (d!=null) { notifyAgents(new APLFunction(parm1,new APLList(returnList)), d.getName()); writeToLog("EVENT: "+parm1+" "+deckStr+" "+" to "+d.getName()); } }
private void notifyEvent(String parm1, APLIdent suit, APLIdent rank, Agent player) { ArrayList<String> targetAgents = new ArrayList<String>(); for (Agent a : agentmap.values()) { if ((a.isSit() && a.getType()==0) || (a.isSit() && a.getType()==2)) // player or notary targetAgents.add(a.getName()); } APLIdent player_name = new APLIdent(player.getName()); if (!targetAgents.isEmpty()) { notifyAgents(new APLFunction(parm1,suit,rank,player_name),targetAgents.toArray(new String[0])); writeToLog("EVENT: "+parm1+"("+suit.toString()+","+rank.toString()+","+player.getName()+")"+" to "+targetAgents); } }
private void notifyEvent(String parm1, APLIdent name, APLNum score) { ArrayList<String> targetAgents = new ArrayList<String>(); for (Agent a : agentmap.values()) { if ((a.isSit() && a.getType()==0)) // player targetAgents.add(a.getName()); }
if (!targetAgents.isEmpty()) { notifyAgents(new APLFunction(parm1,name,score),targetAgents.toArray(new String[0])); writeToLog("EVENT: "+parm1+"("+name.toString()+","+score.toString()+")"+" to "+targetAgents); } } // Add an agent to the environment public synchronized void addAgent(String sAgent) { String sAgentMain = getMainModule(sAgent); // Agent not yet in the environment if (agentmap.keySet().contains(sAgentMain)) { agentmap.put(sAgent,agentmap.get(sAgentMain)); writeToLog("linking " + sAgent + ""); } else{ final Agent agent = new Agent(sAgentMain); //_agents.add(agent); agentmap.put(sAgent, agent); writeToLog("agent " + agent + " added"); } } // Remove the agent from the environment public synchronized void removeAgent(String sAgent) { try { //String sAgentMain = getMainModule(sAgent); Agent a = getAgent(sAgent); agentmap.remove( sAgent ); // there can be several agent if (!agentmap.containsValue(a)) { //_agents.remove(a); //a.reset(); } writeToLog("Agent removed: " + sAgent); synchronized( this ) { notifyAll(); } } catch (ExternalActionFailedException e) {} } /* END Standard functions --------------------------------------*/ /* Helper functions --------------------------------------*/
// Get the agent from its name private synchronized Agent getAgent(String name) throws ExternalActionFailedException { Agent a = null; //a = agentmap.get(getMainModule(name)); a = agentmap.get(name); if (a==null) throw new ExternalActionFailedException("No such agent: "+name); else return a; }
// Get the agent from its position private synchronized Agent getAgent(int position) throws ExternalActionFailedException { Agent a = null; Iterator it = agentmap.keySet().iterator(); while(it.hasNext()) { Object name = it.next(); a = agentmap.get(name); if(a!=null) { if(a.getPosition()!=null) { if(a.getPosition().intValue()==position) { return a; } } } } if (a==null) throw new ExternalActionFailedException("No such agent at position: "+position);
return a; }
private synchronized Agent getDealer() throws ExternalActionFailedException { Agent a = null; Iterator it = agentmap.keySet().iterator(); while(it.hasNext()) { Object name = it.next(); a = agentmap.get(name); if(a!=null) { if(a.getType()==3) { return a; } } } if (a==null) throw new ExternalActionFailedException("There is no dealer at the moment.");
return a; } private static String getMainModule(String sAgent) { int dotPos; if ((dotPos = sAgent.indexOf('.')) == -1) return sAgent; else return sAgent.substring(0, dotPos); }
public void writeToLog(String message) { System.out.println("cardtable: " + message); table.writeLog(message); } // helper function to wrap a boolean value inside a ListPar. static public APLListVar wrapBoolean( boolean b ) { return new APLList(new APLIdent(b ? "true" : "false")); } /* END Helper functions --------------------------------------*/
private int getNumberOfPlayers() { return numberOfPlayers; } private void increaseNumberOfPlayers() { numberOfPlayers++; } private void decreaseNumberOfPlayers() { numberOfPlayers--; } private Integer getPosition() { return new Integer(numberOfPlayers); }
private void validatewindow() { Runnable repaint = new Runnable() { public void run() { //try {Thread.sleep(500);} catch(Exception e) {} table.doLayout(); /*if (!m_window.isVisible()) { m_window.setVisible( true ); }*/ } }; SwingUtilities.invokeLater(repaint); }}