Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package oving3;
- import java.util.ArrayList;
- import acm.graphics.GImage;
- import acm.program.GraphicsProgram;
- /*
- * @startuml
- * class Card {
- * String suit
- * int face
- * }
- * class CardDeck {
- * +Card getCard(int i)
- * }
- * CardDeck --> "*" Card : cards
- * @enduml
- */
- public class CardDeck extends GraphicsProgram {
- ArrayList<Card> cards;
- public void init(){
- final String suits = "SHDC";
- cards = new ArrayList<Card>();
- for (int i = 0; i <= suits.length(); i++) {
- for(int face = 1; face <= 13; face++){
- Card card = new Card();
- if (i == 0) card.suit = "S";
- else if (i == 1) card.suit = "H";
- else if (i == 2) card.suit = "D";
- else if (i == 3) card.suit = "C";
- card.face = face;
- cards.add(card);
- }
- }
- }
- public void run(){
- for (int i = 0; i <= 3; i++) {
- for (int j = 1; j <= 13; j++) {
- if(i==0){
- GImage image = createGImage(getCard(i).suit, getCard(j).face);
- image.setLocation(j*100,25);
- add(image);
- }
- if(i==1){
- GImage image = createGImage(getCard(i+12).suit, getCard(j).face);
- image.setLocation(j*100,250);
- add(image);
- }
- if(i==2){
- GImage image = createGImage(getCard(i+24).suit, getCard(j).face);
- image.setLocation(j*100,450);
- add(image);
- }
- if(i==3){
- GImage image = createGImage(getCard(i+36).suit, getCard(j).face);
- image.setLocation(j*100,650);
- add(image);
- }
- }
- }
- }
- GImage createGImage(String suit, int value) {
- String name="";
- switch(suit.charAt(0)){
- case 'H':
- name+="hearts";
- break;
- case 'D':
- name+="diamonds";
- break;
- case 'C':
- name+="clubs";
- break;
- case 'S':
- name+="spades";
- break;
- }
- name+="-";
- switch(value) {
- case 1:
- name+="a";
- break;
- case 11:
- name+="j";
- break;
- case 12:
- name+="q";
- break;
- case 13:
- name+="k";
- break;
- default:
- name+="" + value;
- }
- name+="-150.png";
- return new GImage("oving3/img/" + name);
- }
- public Card getCard(int i) {
- return cards.get(i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement