// CardDeck.java // // all card-handling functions, pictures of cards, etc. // ---------------------------------------------------------------------- // part of ArgumentGame (simulation of a rational argument) // game written for/designed by Prof. Ron Loui, loui@ai.cs.wustl.edu // based on the paper "An Argument Game" by Ronald Loui and Wiliam Chen // based on the game "LMNOP" // (see Loui-Norman-Stiefvater-Merrill-Olson-Costello [92] // code by Nina Kang, nkang@husc.harvard.edu, 8/96 import java.awt.*; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; // ArgumentGame section 2.1 // "Rules defining components: // "there are two players, designated in advance to be RED and BLACK... // * in our game, A and B // "cards are distinguished only by *color* and *value*, not by suit; // a card's opposite is a card of same value and opposing color." public class CardDeck { static ArgumentGame game; static boolean[] avoidances; // cards to avoid when drawing a new card // image library Image[] pictures; // faces of the cards Image[] corners; // thin strip of right side of card (white) Image[] deadCorners; // use when a corner's support is not live (gray) Image[] blankPicture; // back of the card Image[] blankCorner; // a thin strip of the right side of back (same GIF) static final int HEIGHT = 60; // height of card static final int PWIDTH = 40; // width of card face static final int CWIDTH = 10; // width of card corner static final int BLACK = 0; static final int RED = 1; CardDeck (ArgumentGame g) { game = g; avoidances = new boolean[13]; reset_avoidances(); pictures = new Image[52]; load_pictures (pictures, "./Cardset.GIF", PWIDTH, HEIGHT); corners = new Image[52]; load_pictures (corners, "./Sideset.GIF", CWIDTH, HEIGHT); deadCorners = new Image[52]; load_pictures (deadCorners, "./Deadset.GIF", CWIDTH, HEIGHT); blankPicture = new Image[1]; load_pictures (blankPicture, "./Back.GIF", PWIDTH, HEIGHT); Image[] tmp = new Image[4]; lp2 (tmp, "Back.GIF", CWIDTH, HEIGHT); blankCorner = new Image[1]; blankCorner[0] = tmp[3]; } // load array of images from big gif private void load_pictures (Image[] array, String gifName, int w, int h) { CropImageFilter cropFilter; Image GIF = game.getImage(game.getCodeBase(), gifName); for (int i = 0; i < array.length; i++) { cropFilter = new CropImageFilter ((w+1) * (i%13) + 1, (h+1) * (i/13) + 1, w, h); array[i] = game.createImage(new FilteredImageSource(GIF.getSource(), cropFilter)); } } // hack to obtain BlankCorner from the same GIF as BlankPicture private void lp2 (Image[] array, String gifName, int w, int h) { CropImageFilter cropFilter; Image GIF = game.getImage(game.getCodeBase(), gifName); for (int i = 0; i < array.length; i++) { cropFilter = new CropImageFilter ((w) * (i%13) + 1, (h) * (i/13) + 1, w, h); array[i] = game.createImage(new FilteredImageSource(GIF.getSource(), cropFilter)); } } // --------------------- CARD-DEALING FUNCTIONS ----------------------- public static void reset_avoidances () { for (int i = 0; i < avoidances.length; i++) avoidances[i] = false; } public static void set_avoidances (int i, boolean b) { avoidances[i % avoidances.length] = b; } public static void set_avoidances (int[] arr, boolean b) { for (int i = 0; i < arr.length; i++) avoidances[arr[i] % avoidances.length] = b; } public static int total_avoidances() { int counter = 0; for (int i = 0; i < avoidances.length; i++) { if (avoidances[i]) counter++; } return (counter); } public static int deal() { if (total_avoidances() == avoidances.length) return (-1); int i, card, suit; do { card = (int) (Math.random()*13); } while (avoidances[card]); suit = (int) (Math.random()*4); card += 13 * suit; return (card); } // --------------------- IMAGE-HANDLING FUNCTIONS ----------------------- public Image getPicture (int card) { if ((card < 0) || (card >= pictures.length)) { return (blankPicture[0]); } else { return (pictures[card]); } } public Image getCorner (int card) { if ((card < 0) || (card >= corners.length)) { return (blankCorner[0]); } else { return (corners[card]); } } public Image getDeadCorner (int card) { if ((card<0) || (card >= deadCorners.length)) { return (blankCorner[0]); } else { return (deadCorners[card]); } } // ---------------------- CARD-INFORMATION-HANDLING FUNCTIONS ----------- static public int getColor (int card) { if (card >= 26) { return (RED); } else { return (BLACK); } } static public java.awt.Color color (int card) { if (card >= 26) { return (Color.red); } else { return (Color.black); } } static public String toString (int card) { int val = card % 13 + 1; if (val == 1) return "A"; else if (val == 11) return "J"; else if (val == 12) return "Q"; else if (val == 13) return "K"; else return String.valueOf (val); } static public int getNumber (int card) { return (card % 13); } static public String textCard (int card) { String color; if (getColor (card) == RED) color = "Red "; else color = "Black "; return (color + toString (card)); } static boolean equivalent (int x, int y) { return ((x%13 == y%13) && ((int)(x/26) == (int)(y/26))); } static boolean opposite (int x, int y) { return ((x%13 == y%13) && ((int)(x/26) != (int)(y/26))); } static int getOpposite (int card) { return ((card + 26) % 52); } }