import java.util.Scanner;
public class IdiotsDelight {
public static final Scanner INPUT = new Scanner(System.in);
private Stack<Card>[] stacks;
private Deck deck;
public static void main(String[] args) {
System.out.println("\n************************************************************");
System.out.println("Welcome to Idiot's Delight.");
IdiotsDelight game = new IdiotsDelight();
game.play();
}
public IdiotsDelight() {
deck = new Deck();
deck.shuffle();
stacks = new Stack[4];
for (int i = 0; i < 4; i++) {
stacks[i] = new ArrayStack<Card>();
}
try {
deal();
} catch (IllegalMoveException e) {
e.printStackTrace();
System.exit(1);
}
}
// Deals cards to stacks
public void deal() throws IllegalMoveException {
if (deck.isEmpty()) {
throw new IllegalMoveException();
}
for (Stack<Card> s : stacks) {
s.push(deck.deal());
}
}
// Game continues until user quits, win, or loses
public void play() {
while (true) {
try { System.out.println("\n" + this);
boolean done = true;
for (Stack<Card> s : stacks) {
if (!(s.isEmpty())) {
done = false;
break;
}
}
if (done) {
System.out.println("Yon win!");
return;
}
System.out.print("Your command (p(air), s(uit), d(eal), m(ove), or q(uit))? ");
String command = INPUT.nextLine();
if (command.equals("pair") || command.equals("p")) {
removePair();
} else if (command.equals("suit") || command.equals("s")) {
removeLowCard();
} else if (command.equals("deal") || command.equals("d")) {
deal();
} else if (command.equals("move") || command.equals("m")) {
move();
} else {
return;
}
} catch (IllegalMoveException e) {
System.out.println("I'm sorry, that's not a legal move.");
}
}
}
// Removes the lowest card of the same suit
public void removeLowCard() throws IllegalMoveException {
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();
System.out.println("\n************************************************************");
if ((i > 4) || (j > 4) || (i == j) || (i < 1) || (j < 1) || (stacks[i - 1].isEmpty())
|| (stacks[j - 1].isEmpty())) {
throw new IllegalMoveException();
}
Card lowCard = stacks[i - 1].peek();
Card highCard = stacks[j - 1].peek();
if ((lowCard.getSuit() != highCard.getSuit()) || (lowCard.getRank() > highCard.getRank())
|| (i == j)) {
throw new IllegalMoveException();
}
stacks[i - 1].pop();
}
// Removes two cards of the same rank
public void removePair() throws IllegalMoveException {
System.out.print("Location (1-4) of first card? ");
int i = INPUT.nextInt();
System.out.print("Location (1-4) of second card? ");
int j = INPUT.nextInt();
INPUT.nextLine();
System.out.println("\n************************************************************");
if ((i > 4) || (j > 4) || (i < 1) || (j < 1)) {
throw new IllegalMoveException();
}
if ((stacks[i - 1].isEmpty()) || (stacks[j - 1].isEmpty())) {
throw new IllegalMoveException();
}
Card card1 = stacks[i - 1].peek();
Card card2 = stacks[j - 1].peek();
if (!(card1.equals(card2)) || (i == j)) {
throw new IllegalMoveException();
}
stacks[i - 1].pop();
stacks[j - 1].pop();
}
// Moves the top card to an empty stack
public void move() throws IllegalMoveException {
System.out.print("Location (1-4) of card? ");
int i = INPUT.nextInt();
System.out.print("Location (1-4) of empty stack? ");
int j = INPUT.nextInt();
INPUT.nextLine();
System.out.println("\n************************************************************");
if ((i > 4) || (j > 4) || (i < 1) || (j < 1)) {
throw new IllegalMoveException();
}
if ((!(stacks[j - 1].isEmpty())) || (stacks[i - 1].isEmpty())) {
throw new IllegalMoveException();
}
stacks[j - 1].push(stacks[i - 1].pop());
}
// Displays when stack is empty
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 class IllegalMoveException extends Exception {
}
public class EmptyStructureException extends RuntimeException {
}

New Topic/Question
Reply



MultiQuote



|