All pastes #912474 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-02-21 12:14 UTC
rendered paste body
package oving5.card;

import java.util.Comparator;

public class CardComparator implements Comparator<Card> {

	/*
	 * @return	The index (value) of the suit as an Integer. 
	 */
	private int getSuitValue(Card c) {
		return "CDSH".indexOf(c.getSuit());
	}

	/*
	 * Compares two cards.
	 * 
	 * @param	c1	The first card.
	 * @param	c2	The second card.
	 * @return		A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second, respectively.
	 */
	public int compare(Card c1, Card c2) {
		if (c1.getFace() == c2.getFace()) {
			if ((getSuitValue(c1) - getSuitValue(c2)) < 0) return -1;
			if ((getSuitValue(c1) - getSuitValue(c2)) > 0) return 1;
			return 0;
		} else {
			if ((c1.getFace() - c2.getFace()) > 0) return -1;
			if ((c1.getFace() - c2.getFace()) < 0) return 1;
			return 0;
		}
	}
	
}