Java poker implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rank.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) Chris 'MD87' Smith, 2007. All rights reserved.
  3. *
  4. * This code may not be redistributed without prior permission from the
  5. * aforementioned copyright holder(s).
  6. */
  7. package com.md87.cardgame;
  8. /**
  9. * Defines the standard ranks in a pack of cards.
  10. *
  11. * @author Chris
  12. */
  13. public enum Rank {
  14. ACE,
  15. KING,
  16. QUEEN,
  17. JACK,
  18. TEN,
  19. NINE,
  20. EIGHT,
  21. SEVEN,
  22. SIX,
  23. FIVE,
  24. FOUR,
  25. THREE,
  26. DEUCE;
  27. /**
  28. * Retrieves the rank that is one lower than this one, or null if this is
  29. * the lowest rank (deuce).
  30. *
  31. * @return The next lower rank
  32. */
  33. public Rank getLower() {
  34. for (Rank target : values()) {
  35. if (target.compareTo(this) == 1) {
  36. return target;
  37. }
  38. }
  39. return null;
  40. }
  41. /**
  42. * Returns a nicely capitalised version of this rank's name.
  43. *
  44. * @return A capitalised version of this rank's name
  45. */
  46. public String capitalise() {
  47. return this.toString().substring(0, 1) + this.toString().substring(1).toLowerCase();
  48. }
  49. public int compareTo(final Rank o, final boolean acesLow) {
  50. // ^ is the XOR operator
  51. if (acesLow && (this == ACE ^ o == ACE)) {
  52. int thisPos = ordinal();
  53. int thatPos = o.ordinal();
  54. if (this == ACE) {
  55. thisPos = DEUCE.ordinal() + 1;
  56. } else if (o == ACE) {
  57. thatPos = DEUCE.ordinal() + 1;
  58. }
  59. return thisPos - thatPos;
  60. } else {
  61. return compareTo(o);
  62. }
  63. }
  64. }