Created
September 6, 2016 19:56
-
-
Save mlabisi/76662ff1839b9ad0779effc5983739ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// THIS CLASS CREATES A CARD OF A GIVEN RANK AND SUIT | |
package classes; | |
public class Card { | |
// class methods | |
private final int suit; | |
private final int rank; | |
// suit initializations | |
public final static int DIAMONDS = 1; | |
public final static int CLUBS = 2; | |
public final static int HEARTS = 3; | |
public final static int SPADES = 4; | |
// rank initializations | |
public final static int ACE = 1; | |
public final static int DEUCE = 2; | |
public final static int THREE = 3; | |
public final static int FOUR = 4; | |
public final static int FIVE = 5; | |
public final static int SIX = 6; | |
public final static int SEVEN = 7; | |
public final static int EIGHT = 8; | |
public final static int NINE = 9; | |
public final static int TEN = 10; | |
public final static int JACK = 11; | |
public final static int QUEEN = 12; | |
public final static int KING = 13; | |
// card constructor | |
public Card(int rank, int suit) { | |
assert isValidRank(rank); | |
assert isValidSuit(suit); | |
this.suit = suit; | |
this.rank = rank; | |
} | |
// class methods | |
public int getSuit(){ | |
return suit; | |
} | |
public int getRank(){ | |
return rank; | |
} | |
// assertions for rank and suit | |
public static boolean isValidRank(int rank) { | |
return ACE <= rank && KING >= rank; | |
} | |
public static boolean isValidSuit(int suit) { | |
return DIAMONDS <= suit && SPADES >= suit; | |
} | |
// convert the rank to a string | |
public static String rankToString(int rank) { | |
switch (rank) { | |
case ACE: | |
return "Ace"; | |
case DEUCE: | |
return "Deuce"; | |
case THREE: | |
return "Three"; | |
case FOUR: | |
return "Four"; | |
case FIVE: | |
return "Five"; | |
case SIX: | |
return "Six"; | |
case SEVEN: | |
return "Seven"; | |
case EIGHT: | |
return "Eight"; | |
case NINE: | |
return "Nine"; | |
case TEN: | |
return "Ten"; | |
case JACK: | |
return "Jack"; | |
case QUEEN: | |
return "Queen"; | |
case KING: | |
return "King"; | |
default: | |
return null; | |
} | |
} | |
// convert the suit to a string | |
public static String suitToString(int suit) { | |
switch (suit) { | |
case DIAMONDS: | |
return "Diamonds"; | |
case CLUBS: | |
return "Clubs"; | |
case HEARTS: | |
return "Hearts"; | |
case SPADES: | |
return "Spades"; | |
default: | |
return null; | |
} | |
} | |
// perform assertions for each condition | |
public static void main(String[] args) { | |
assert rankToString(ACE) == "Ace"; | |
assert rankToString(DEUCE) == "Deuce"; | |
assert rankToString(THREE) == "Three"; | |
assert rankToString(FOUR) == "Four"; | |
assert rankToString(FIVE) == "Five"; | |
assert rankToString(SIX) == "Six"; | |
assert rankToString(SEVEN) == "Seven"; | |
assert rankToString(EIGHT) == "Eight"; | |
assert rankToString(NINE) == "Nine"; | |
assert rankToString(TEN) == "Ten"; | |
assert rankToString(JACK) == "Jack"; | |
assert rankToString(QUEEN) == "Queen"; | |
assert rankToString(KING) == "King"; | |
assert suitToString(DIAMONDS) == "Diamonds"; | |
assert suitToString(CLUBS) == "Clubs"; | |
assert suitToString(HEARTS) == "Hearts"; | |
assert suitToString(SPADES) == "Spades"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.*; | |
package classes; | |
public class CardPicker { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
Deck deck = new Deck(); | |
int suit; | |
int rank; | |
System.out.print("Display all cards or Select Card? "); | |
String originalAnswer = input.nextLine(); | |
String answer = originalAnswer.toLowerCase(); | |
if (answer.equals("all") || answer.equals("display") || answer.equals("display all")) { | |
for (suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) { | |
for (rank = Card.ACE; rank <= Card.KING; rank++) { | |
Card card = deck.getCard(suit,rank); | |
System.out.format("%s of %s%n", card.rankToString(card.getRank()), | |
card.suitToString(card.getSuit())); | |
} | |
} | |
} else if (answer.equals("select") || answer.equals("select card")) { | |
System.out.println("Number of suit? \n1. Diamonds \n2. Clubs \n3. Hearts \n4.Spades"); | |
suit = Integer.parseInt(input.nextLine()); | |
System.out.println("Number of rank? \n1. Ace \n2. Deuce \n3 \n4 \n5 \n6 \n7 \n8 \n9 \n10 \n11. Jack \n12. Queen \n13. King"); | |
rank = Integer.parseInt(input.nextLine()); | |
Card card = deck.getCard(suit, rank); | |
System.out.print("Your card: "); | |
System.out.format("%s of %s%n ", card.rankToString(card.getRank()), | |
card.suitToString(card.getSuit())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import Arrays | |
import java.util.Arrays; | |
package classes; | |
// THIS CLASS CREATES A DECK MADE OF AN ARRAY OF CARDS | |
public class Deck { | |
// quantities to help with Array declaration | |
public static int numSuits = 4; | |
public static int numRanks = 13; | |
public static int numCards = numSuits * numRanks; | |
// declaration of a multidimensional array (called 'cards') of 'Card' objects | |
private Card[][] cards; | |
// constructor | |
public Deck() { | |
// creation/initialization of a MD array of cards | |
// first array is made of suits, second is made of ranks | |
cards = new Card[numSuits][numRanks]; | |
// for each suit, incrememnt by one until the maximum (spades) is reached | |
// start with suit = diamonds | |
for (int suit = Card.DIAMONDS; suit <= Card.SPADES ; suit++) { | |
// for each rank within each suit, increment by one til the max (king) is reached | |
// start with rank = ace | |
for (int rank = Card.ACE; rank <= Card.KING ; rank++) { | |
// at the index of the given suit - 1 and given rank - 1, create a new card object with | |
// the corresponding rank and suit | |
cards[suit - 1][rank - 1] = new Card(rank, suit); | |
} | |
} | |
} | |
// class method | |
public Card getCard(int suit, int rank) { | |
return cards[suit - 1][rank - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment