package com.accp; import java.awt.Image; import java.util.HashMap; import java.util.Map; /** * Game context (keeps the images to be used by the game) * @author SC * */ public class GameContext { private static Map<String, Image> map = new HashMap<String, Image>(); private GameContext() { } /** * add a picture * @param name name * @param image image object */ public static void addImage(String name, Image image){ map.put(name, image); } /** * retrieve a picture * @param name name * @return image object */ public static Image getImage(String name) { return map.get(name); } }
package com.accp; /** * Poker * @author SC * */ public class Poker { private Card[] cards = new Card[52]; public Poker() { String[] suites = {"Spades", "Hearts", "Flowers", "Diamonds"}; int[] faces = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; for(int i = 0; i < cards.length; i++) { cards[i] = new Card(suites[i / 13], faces[i % 13]); } } /** * Shuffle (random shuffle) */ public void shuffle() { for(int i = 0; i < cards.length; i++) { int pos = (int) (Math.random() * cards.length); Card temp = cards[i]; cards[i] = cards[pos]; cards[pos] = temp; } } /** * Licensing * @param index the position index of the first card * @return the card at the specified position */ public Card deal(int index) { return cards[index]; } }
package com.accp; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; /** * Card (one poker) * @author SC * */ public class Card { private String suite; // suit private int face; // 点数 public Card(String suite, int face) { this.suite = suite; this.face = face; } public String getSuite() { return suite; } public int getFace() { return face; } /** * draw cards * @param g brush * @param x abscissa * @param y ordinate */ public void draw(Graphics g, int x, int y) { // Take the border image from the context for drawing g.drawImage(GameContext.getImage("边框"), x, y, 100, 150, null); // Take the picture corresponding to the color from the context to draw g.drawImage(GameContext.getImage(suite), x + 10, y + 10, null); String str = ""; switch(face) { case 1: str += "A"; break; case 11: str += "J"; break; case 12: str += "Q"; break; case 13: str += "K"; break; default: str + = face; } // set the brush color if(suite.equals("黑桃") || suite.equals("草花")) { g.setColor(Color.BLACK); } else { g.setColor(Color.RED); } // set the brush font g.setFont(new Font("Times New Roman", Font.BOLD, 14)); // draw points g.drawString(str, x + 30, y + 30); } }
package com.accp; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; @SuppressWarnings("serial") public class GameFrame extends JFrame { private boolean initialized = false; // whether to finish preloading the image private String[] names = {"Border", "Flowers", "Diamonds", "Hearts", "Spades"}; private String[] filenames = {"border.jpg", "club.jpg", "diamond.jpg", "heart.jpg", "spade.jpg"}; private Poker p = new Poker(); // create a deck of poker private int index = 0; // where the cards are dealt private Card currentCard = null; // the currently dealt card private List<Card> list = new ArrayList<Card>(); // container for holding three cards private JButton nextButton, resetButton, nextThreeButton; public GameFrame() { for(int i = 0; i < filenames.length; i++) { GameContext.addImage(names[i], getToolkit().getImage(filenames[i])); } p.shuffle(); this.setTitle("Poker Game"); this.setSize(800, 600); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Set up a fluid layout manager for the window this.setLayout(new FlowLayout()); nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(index < 52) { currentCard = p.deal(index++); if(index == 52) { nextButton.setEnabled(false); nextThreeButton.setEnabled(false); resetButton.setEnabled(true); } repaint(); // repaint the window } } }); this.add(nextButton); resetButton = new JButton("reset"); resetButton.setEnabled(false); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { list.clear(); p.shuffle(); index = 0; currentCard = null; resetButton.setEnabled(false); nextButton.setEnabled(true); nextThreeButton.setEnabled(true); repaint(); } }); this.add(resetButton); nextThreeButton = new JButton("Send three"); nextThreeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentCard = null; list.clear(); for(int i = 1; i <= 3 && index < 52; i++) { list.add(p.deal(index++)); } if(index >= 52) { nextThreeButton.setEnabled(false); nextButton.setEnabled(false); resetButton.setEnabled(true); } repaint(); } }); this.add(nextThreeButton); } public void paint(Graphics g) { super.paint(g); if(!initialized) { // First preload the image in the invisible area of the window for(int i = 0; i < names.length; i++) { g.drawImage(GameContext.getImage(names[i]), -1000, -1000, null); } initialized = true; // preload is executed only once } if(currentCard != null) { // draw a card currentCard.draw(g, 300, 200); } else { // draw three card poker for(int i = 0; i < list.size(); i++) { list.get(i).draw(g, 150 + i * 125, 200); } } } public static void main(String[] args) { new GameFrame().setVisible(true); } }
package com.accp; public class Play { public static void main(String[] args) { Poker p = new Poker(); p.shuffle(); for(int i = 0; i < 52; i++) { System.out.println(p.deal(i)); } } }