2-APL UPC project.
0
fork

Configure Feed

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

added card classes plus shuffle method

fdesan f52fd7bc 1839ca72

+382 -33
+55
code/2apl_platform/environments/cardtable/Card.java
··· 1 + package cardtable; 2 + 3 + import javax.swing.ImageIcon; 4 + 5 + public class Card { 6 + 7 + private Suit suitValue; 8 + private Rank rankValue; 9 + private ImageIcon cardImage; 10 + 11 + 12 + 13 + 14 + public Card( Suit suit, Rank rank, ImageIcon cardFace ) { 15 + cardImage = cardFace; 16 + suitValue = suit; 17 + rankValue = rank; 18 + } 19 + 20 + 21 + public static String getFilename( Suit suit, Rank rank ) { 22 + return rank.getSymbol() + suit.getSymbol() + ".gif"; 23 + } 24 + 25 + 26 + public Suit getSuit() { 27 + return suitValue; 28 + } 29 + 30 + public Rank getRank() { 31 + return rankValue; 32 + } 33 + 34 + 35 + public ImageIcon getCardImage() { 36 + return cardImage; 37 + } 38 + 39 + 40 + public int compareTo( Card AnotherCard ) { 41 + Card otherCard = AnotherCard; 42 + int suitDiff = suitValue.compareTo( otherCard.suitValue ); 43 + int rankDiff = rankValue.compareTo( otherCard.rankValue ); 44 + 45 + 46 + if ( rankDiff != 0 ) 47 + return rankDiff; 48 + else 49 + return suitDiff; 50 + 51 + } 52 + 53 + 54 + 55 + }
+55
code/2apl_platform/environments/cardtable/Deck.java
··· 1 + package cardtable; 2 + 3 + import java.util.*; 4 + import javax.swing.*; 5 + import javax.swing.ImageIcon; 6 + 7 + public class Deck { 8 + private List deck; 9 + private int index; 10 + 11 + public Deck() { 12 + deck = new ArrayList(); 13 + index = 0; 14 + Iterator suitIterator = Suit.VALUES.iterator(); 15 + while ( suitIterator.hasNext() ) { 16 + Suit suit = (Suit) suitIterator.next(); 17 + Iterator rankIterator = Rank.VALUES.iterator(); 18 + while ( rankIterator.hasNext() ) { 19 + Rank rank = (Rank) rankIterator.next(); 20 + ImageIcon cardImage = new ImageIcon( "cards/" + Card.getFilename( suit, rank ) ); 21 + Card card = new Card( suit, rank, cardImage ); 22 + addCard( card ); 23 + } 24 + } 25 + } 26 + 27 + public void addCard( Card card ) { 28 + deck.add( card ); 29 + } 30 + 31 + public Card dealCard() { 32 + if ( index >= deck.size() ) 33 + return null; 34 + else 35 + return (Card) deck.get( index++ ); 36 + } 37 + 38 + 39 + public void shuffle() { 40 + Collections.shuffle( deck ); 41 + index = 0; 42 + } 43 + 44 + public boolean isEmpty() { 45 + if ( index >= deck.size() ) 46 + return true; 47 + else 48 + return false; 49 + } 50 + 51 + public List getDeck() { 52 + return deck; 53 + } 54 + 55 + }
+64 -33
code/2apl_platform/environments/cardtable/Env.java
··· 7 7 8 8 // Standard java imports 9 9 import java.awt.Point; 10 - import java.util.Collection; 11 - import java.util.Iterator; 12 - import java.util.List; 13 - import java.util.Vector; 14 - import java.util.LinkedList; 10 + import java.util.*; 15 11 import javax.swing.SwingUtilities; 16 12 import java.awt.Dimension; 17 - import java.util.ArrayList; 18 - import java.util.HashMap; 19 13 20 14 import java.io.IOException; 21 15 import java.io.InputStream; ··· 23 17 import java.io.ObjectOutputStream; 24 18 import java.io.OutputStream; 25 19 26 - import java.util.Random; 27 - 28 20 public class Env extends Environment 29 21 { 30 22 private HashMap<String,Agent> agentmap = new HashMap<String,Agent>(); 31 23 32 24 private int numberOfPlayers, numberOfNotaries, numberOfGatekeepers, numberOfDealers; 25 + 26 + private Deck deck; 33 27 34 28 // The default constructor 35 29 public Env() ··· 41 35 numberOfPlayers=0; 42 36 numberOfNotaries=0; 43 37 numberOfGatekeepers=0; 38 + 39 + deck = new Deck(); 44 40 } 45 41 46 42 public Term enterAsNotary(String sAgent) throws ExternalActionFailedException { ··· 56 52 57 53 return wrapBoolean(true); 58 54 } 59 - 55 + 60 56 public Term enterAsDealer(String sAgent) throws ExternalActionFailedException { 61 57 Agent agent = getAgent(sAgent); 62 58 writeToLog("Dealer sit: " +agent.getName()); ··· 69 65 agent._type=3; 70 66 71 67 return wrapBoolean(true); 72 - } 68 + } 73 69 74 70 public Term enterAsGatekeeper(String sAgent) throws ExternalActionFailedException { 75 71 Agent agent = getAgent(sAgent); ··· 139 135 140 136 return new APLIdent(a.getName()); 141 137 } 142 - 138 + 139 + public Term shuffleDeck(String sAgent) throws ExternalActionFailedException { 140 + Agent agent = getAgent(sAgent); 141 + 142 + if (agent.getType()!=3) 143 + { 144 + throw new ExternalActionFailedException("Only the dealer can shuffle the deck."); 145 + } 146 + 147 + deck.shuffle(); 148 + 149 + notifyEvent("deckShuffled", deck); 150 + 151 + return wrapBoolean(true); 152 + } 153 + 143 154 /* Standard functions --------------------------------------*/ 144 155 145 156 private void notifyAgents(APLFunction event, String... receivers) { 146 - // throwEvent(event, receivers); 157 + throwEvent(event, receivers); 147 158 } 148 159 149 - private void notifyEvent(String parm1, Point ptPosition) 160 + private void notifyEvent(String parm1, Deck deck) throws ExternalActionFailedException 150 161 { 151 - // APLNum nX = new APLNum((double)(ptPosition.getX())); 152 - // APLNum nY = new APLNum((double)(ptPosition.getY())); 153 - // 154 - // // Send an external event to all agents within the senserange. 155 - // ArrayList<String> targetAgents = new ArrayList<String>(); 156 - // for (Agent a : agentmap.values()) 157 - // { 158 - // // Changed SA: I got no idea why there is always 1 agent which does not exists, 159 - // // but this fixes the exceptions 160 - // if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange())) 161 - // targetAgents.add(a.getName()); 162 - // } 163 - // 164 - // writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents); 165 - // 166 - // if (!targetAgents.isEmpty()) 167 - // { 168 - // notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0])); 169 - // } 162 + Agent d = getDealer(); 163 + 164 + 165 + LinkedList returnList = new LinkedList(); 166 + // Send an external event to all agents within the senserange. 167 + List deckList = deck.getDeck(); 168 + 169 + for (Iterator<Card> i = deckList.iterator( ); i.hasNext( ); ) { 170 + Card card = i.next(); 171 + Suit suit = card.getSuit(); 172 + APLIdent id = new APLIdent(suit.getName()); 173 + returnList.add(id); 174 + Rank rank = card.getRank(); 175 + id = new APLIdent(rank.getName()); 176 + returnList.add(id); 177 + } 178 + 179 + if (d!=null) 180 + { 181 + notifyAgents(new APLFunction(parm1,new APLList(returnList)), d.getName()); 182 + writeToLog("EVENT: "+parm1+"("+returnList.toString()+")"+" to "+d.getName()); 183 + } 170 184 } 171 185 172 186 // Add an agent to the environment ··· 245 259 } 246 260 } 247 261 if (a==null) throw new ExternalActionFailedException("No such agent at position: "+position); 262 + 263 + return a; 264 + } 265 + 266 + private synchronized Agent getDealer() throws ExternalActionFailedException { 267 + Agent a = null; 268 + Iterator it = agentmap.keySet().iterator(); 269 + while(it.hasNext()) { 270 + Object name = it.next(); 271 + a = agentmap.get(name); 272 + if(a!=null) { 273 + if(a.getType()==3) { 274 + return a; 275 + } 276 + } 277 + } 278 + if (a==null) throw new ExternalActionFailedException("There is no dealer at the moment."); 248 279 249 280 return a; 250 281 }
+51
code/2apl_platform/environments/cardtable/Hand.java
··· 1 + 2 + package cardtable; 3 + 4 + import java.util.*; 5 + 6 + 7 + public abstract class Hand implements Comparable { 8 + 9 + private java.util.List hand = new ArrayList(); 10 + 11 + 12 + public void addCard( Card card ) { 13 + hand.add( card ); 14 + } 15 + 16 + public Card getCard( int index ) { 17 + return (Card) hand.get( index ); 18 + } 19 + 20 + 21 + public Card removeCard( int index ) { 22 + return (Card) hand.remove( index ); 23 + } 24 + 25 + 26 + public void sort() { 27 + Collections.sort( hand ); 28 + } 29 + 30 + 31 + public boolean isEmpty() { 32 + return hand.isEmpty(); 33 + } 34 + 35 + 36 + public boolean containsCard( Card card ) { 37 + return false; 38 + } 39 + 40 + 41 + public int findCard( Card card ) { 42 + return hand.indexOf( card ); 43 + } 44 + 45 + 46 + 47 + public String toString() { 48 + return hand.toString(); 49 + } 50 + 51 + }
+99
code/2apl_platform/environments/cardtable/Rank.java
··· 1 + 2 + package cardtable; 3 + 4 + 5 + import java.util.*; 6 + 7 + public class Rank { 8 + private String name; 9 + private String symbol; 10 + private static boolean aceHigh = false; 11 + 12 + /** 13 + * The rank ace. 14 + */ 15 + public final static Rank ACE = new Rank( "Ace", "a" ); 16 + /** 17 + * The rank two. 18 + */ 19 + public final static Rank TWO = new Rank( "Two", "2" ); 20 + /** 21 + * The rank three. 22 + */ 23 + public final static Rank THREE = new Rank( "Three", "3" ); 24 + /** 25 + * The rank four. 26 + */ 27 + public final static Rank FOUR = new Rank( "Four", "4" ); 28 + /** 29 + * The rank five. 30 + */ 31 + public final static Rank FIVE = new Rank( "Five", "5" ); 32 + /** 33 + * The rank six. 34 + */ 35 + public final static Rank SIX = new Rank( "Six", "6" ); 36 + /** 37 + * The rank seven. 38 + */ 39 + public final static Rank SEVEN = new Rank( "Seven", "7" ); 40 + /** 41 + * The rank eight. 42 + */ 43 + public final static Rank EIGHT = new Rank( "Eight", "8" ); 44 + /** 45 + * The rank nine. 46 + */ 47 + public final static Rank NINE = new Rank( "Nine", "9" ); 48 + /** 49 + * The rank ten. 50 + */ 51 + public final static Rank TEN = new Rank( "Ten", "t" ); 52 + /** 53 + * The rank jack. 54 + */ 55 + public final static Rank JACK = new Rank( "Jack", "j" ); 56 + /** 57 + * The rank queen. 58 + */ 59 + public final static Rank QUEEN = new Rank( "Queen", "q" ); 60 + /** 61 + * The rank king. 62 + */ 63 + public final static Rank KING = new Rank( "King", "k" ); 64 + 65 + 66 + final static java.util.List VALUES = 67 + Collections.unmodifiableList( 68 + Arrays.asList( new Rank[] { TWO, THREE, FOUR, FIVE, SIX, SEVEN, 69 + EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } ) ); 70 + 71 + 72 + private Rank( String nameValue, String symbolValue ) { 73 + name = nameValue; 74 + symbol = symbolValue; 75 + } 76 + 77 + 78 + public String getName() { 79 + return name; 80 + } 81 + 82 + 83 + public String toString() { 84 + return name; 85 + } 86 + 87 + 88 + public String getSymbol() { 89 + return symbol; 90 + } 91 + 92 + 93 + public int compareTo( Object otherRankObject ) { 94 + Rank otherRank = (Rank) otherRankObject; 95 + return VALUES.indexOf( this ) - VALUES.indexOf( otherRank ); 96 + 97 + } 98 + 99 + }
+58
code/2apl_platform/environments/cardtable/Suit.java
··· 1 + package cardtable; 2 + 3 + import java.util.*; 4 + 5 + 6 + public final class Suit { 7 + private String name; 8 + private String symbol; 9 + 10 + /** 11 + * The suit clubs. 12 + */ 13 + public final static Suit CLUBS = new Suit( "Clubs", "c" ); 14 + /** 15 + * The suit diamonds. 16 + */ 17 + public final static Suit DIAMONDS = new Suit( "Diamonds", "d" ); 18 + /** 19 + * The suit hearts. 20 + */ 21 + public final static Suit HEARTS = new Suit( "Hearts", "h" ); 22 + /** 23 + * The suit spades. 24 + */ 25 + public final static Suit SPADES = new Suit( "Spades", "s" ); 26 + 27 + 28 + public final static java.util.List VALUES = 29 + Collections.unmodifiableList( 30 + Arrays.asList( new Suit[] { CLUBS, DIAMONDS, HEARTS, SPADES } ) ); 31 + 32 + 33 + private Suit( String nameValue, String symbolValue ) { 34 + name = nameValue; 35 + symbol = symbolValue; 36 + } 37 + 38 + public String getName() { 39 + return name; 40 + } 41 + 42 + public String getSymbol() { 43 + return symbol; 44 + } 45 + 46 + public String toString() { 47 + return name; 48 + } 49 + 50 + 51 + 52 + public int compareTo( Object otherSuitObject ) { 53 + Suit otherSuit = (Suit) otherSuitObject; 54 + return VALUES.indexOf( this ) - VALUES.indexOf( otherSuit ); 55 + } 56 + 57 + } 58 +