Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayStack<E> implements Stack<E> {
- private E[] data;
- private int size;
- public ArrayStack() {
- data = (E[])(new Object[1]);
- size = 0;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public E pop() {
- size--;
- return data[size-1];
- }
- protected boolean isFull() {
- return size == data.length;
- }
- public void push (E target) {
- if (isFull()) {
- stretch();
- }
- data[size] = target;
- size++;
- }
- public void stretch() {
- E[] newData = (E[])(new Object[data.length * 2]);
- for (int i = 0; i < data.length; i++) {
- newData[i] = data[i];
- }
- data = newData;
- }
- }
- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- public class Card {
- private int rank;
- private int suit;
- public static final int SPADES = 0;
- public static final int HEARTS = 1;
- public static final int DIAMONDS = 2;
- public static final int CLUBS = 3;
- public static final int ACE = 1;
- public static final int JACK = 11;
- public static final int QUEEN = 12;
- public static final int KING = 13;
- public Card(int rank, int suit) {
- this.rank = rank;
- this.suit = suit;
- }
- public int getRank() {
- return rank;
- }
- public int getSuit() {
- return suit;
- }
- public boolean equals(Object that) {
- if (this == that) {
- return true;
- }
- if (that == null) {
- return false;
- }
- if (getClass() != that.getClass()) {
- return false;
- }
- Card thatCard = (Card)that;
- return rank == thatCard.rank;
- }
- public String toString() {
- return ""+ "-A23456789TJQK".charAt(rank) + "shdc".charAt(suit);
- }
- }
- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- public class Deck {
- private Card[] cards;
- private int size;
- public Deck() {
- cards = new Card[52];
- size = 0;
- for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
- for (int rank = Card.ACE; rank <= Card.KING; rank++) {
- cards [size] = new Card(rank, suit);
- size +=1;
- }
- }
- }
- public Card deal() {
- size--;
- return cards[size];
- }
- public void shuffle() {
- for (int i = size - 1; i>0; i--) {
- swap(i, (int)(Math.random() * (i+1)));
- }
- }
- public void swap (int i, int j) {
- Card temp = cards[i];
- cards[i] = cards[j];
- cards[j] = temp;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public int size() {
- return size;
- }
- }
- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- import java.util.Scanner;
- public class IdiotsDelight {
- public static final Scanner INPUT = new Scanner(System.in);
- private Stack<Card>[] stacks;
- private Deck deck;
- public IdiotsDelight() {
- deck = new Deck();
- deck.shuffle();
- stacks = new Stack[4];
- for (int i = 0; i < 4; i++) {
- stacks[i] = new ArrayStack<Card>();
- }
- deal();
- }
- public void deal() {
- for (Stack<Card> s : stacks) {
- s.push(deck.deal());
- }
- }
- public void play() {
- while (true) {
- System.out.println("\n" + this);
- boolean done = true;
- for (Stack<Card> s : stacks) {
- if (!(s.isEmpty())) {
- done = false;
- break;
- }
- }
- if (done) {
- System.out.println("You win!");
- return;
- }
- System.out.print("Your command (pair, suit, deal, or quit)? " );
- String command = INPUT.nextLine();
- if (command.equals("pair")) {
- removePair();
- } else if (command.equals("suit")) {
- removeLowCard();
- } else if (command.equals("deal")) {
- deal();
- } else {
- return;
- }
- }
- }
- public void removeLowCard() {
- System.out.print("Location (1-4) of low card? ");
- int i = INPUT.nextInt();
- System.out.print("Location (1-4) of high card? ");
- int j = INPUT.nextInt();
- INPUT.nextLine();
- Card lowCard = stacks[i-1].peek();
- Card highCard = stacks[j-1].peek();
- stacks[i-1].pop();
- stacks[j-1].pop();
- }
- public String toString() {
- String result = "";
- for (int i = 0; i < 4; i++) {
- if (stacks[i].isEmpty()) {
- result += "-- ";
- } else {
- result += stacks[i].peek() + " ";
- }
- }
- return result + "\n" + deck.size() + " cards left in the deck.";
- }
- public static void main (String[] args) {
- System.out.println("Welcome to Idiot's Delight.");
- IdiotsDelight game = new IdiotsDelight();
- game.play();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement