5 Replies - 510 Views - Last Post: 03 October 2012 - 11:57 AM Rate Topic: -----

#1 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Try/Catch

Posted 01 October 2012 - 12:53 PM

Game works great. One thing I just want to add is if user enters anything but a number, I want to catch it and print out illegal move.

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 {

}


Is This A Good Question/Topic? 0
  • +

Replies To: Try/Catch

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Try/Catch

Posted 01 October 2012 - 01:22 PM

use nextLine() instead of nextInt() and store the result in a String
then try/catch the conversion to an int

String line = INPUT.nextLine();
int numValue = 0;
try {
    numValue = Integer.parseInt(line);
}
catch(Exception e) {
   .... it was not a valid integer that was entered
}


Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: Try/Catch

Posted 01 October 2012 - 01:45 PM

You are doing IO a lot of places in your code, you could make a method(s) to handle everything that has to do with IO. That would also get rid of all the try catch all over the place.
Was This Post Helpful? 0
  • +
  • -

#4 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Try/Catch

Posted 03 October 2012 - 11:45 AM

I did this, and it said: exception IllegalMoveException is never thrown in body of corresponding try statement


		int i = 0;
		int j = 0;
		System.out.print("Location (1-4) of low card? ");
		String stringi = INPUT.nextLine();
		System.out.print("Location (1-4) of high card? ");
		String stringj = INPUT.nextLine();
		INPUT.nextLine();
		try { i = Integer.parseInt(stringi);
				j = Integer.parseInt(stringj);
		} catch (IllegalMoveException e) {
		System.out.println("Sorry. That was not a valid input");
		}

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: Try/Catch

Posted 03 October 2012 - 11:52 AM

You can only catch exceptions thrown by code inside the try-block

Integer.parseInt can not throw a IllegalMoveException, so you cannot catch it :)
Instead you can catch NumberFormatException

This post has been edited by CasiOo: 03 October 2012 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Try/Catch

Posted 03 October 2012 - 11:57 AM

I just changed IllegalMoveException to Exception and added throw new IllegalMoveException(); in the catch and it worked :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1