Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Refactor Card class
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangerCoug committed Oct 21, 2023
1 parent bca4df4 commit 14566fa
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@
* @author Jeffrey Hope <strangercoug@hotmail.com>
*/
public enum CardRank {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING,
ACE, JOKER
TWO("Two"),
THREE("Three"),
FOUR("Four"),
FIVE("Five"),
SIX("Six"),
SEVEN("Seven"),
EIGHT("Eight"),
NINE("Nine"),
TEN("Ten"),
JACK("Jack"),
QUEEN("Queen"),
KING("King"),
ACE("Ace"),
JOKER("Joker");

private final String name;

CardRank(String name){
this.name = name;
}

@Override
public String toString() {
return name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,21 @@
* @author Jeffrey Hope <strangercoug@hotmail.com>
*/
public enum CardSuit {
CLUBS, DIAMONDS, HEARTS, SPADES, BLACK, RED
CLUBS("Clubs"),
DIAMONDS("Diamonds"),
HEARTS("Hearts"),
SPADES("Spades"),
BLACK("Black"),
RED("Red");

private final String name;

CardSuit(String name){
this.name = name;
}

@Override
public String toString() {
return name;
}
}
11 changes: 3 additions & 8 deletions src/main/java/com/github/strangercoug/freecasino/objs/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@
* @author Jeffrey Hope <strangercoug@hotmail.com>
*/
public record Card(CardRank rank, CardSuit suit) implements Comparable<Card> {
private static final String[] rankNames = {"Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace",
"Joker"};
private static final String[] suitNames = {"Clubs", "Diamonds", "Hearts", "Spades",
"Black", "Red"};

/**
* This method is intended only for baccarat and variants of twenty-one. Use {@code getRank()} if the tens and face
Expand Down Expand Up @@ -110,9 +105,9 @@ public int compareTo(Card other) {

@Override
public String toString() {
if (rank == CardRank.JOKER)
return suitNames[suit.ordinal()] + " " + rankNames[rank.ordinal()];
if (suit == CardSuit.BLACK || suit == CardSuit.RED)
return suit + " " + rank.toString();

return rankNames[rank.ordinal()] + " of " + suitNames[suit.ordinal()];
return rank.toString() + " of " + suit.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018-2023, Jeffrey Hope
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.strangercoug.freecasino.enums;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class CardRankTest {

@ParameterizedTest
@MethodSource("cardRankToString")
void testPokerRank(CardRank cardRank, String string) {
assertThat(cardRank.toString(), equalTo(string));
}

private static Stream<Arguments> cardRankToString() {
return Stream.of(
arguments(CardRank.TWO, "Two"),
arguments(CardRank.THREE, "Three"),
arguments(CardRank.FOUR, "Four"),
arguments(CardRank.FIVE, "Five"),
arguments(CardRank.SIX, "Six"),
arguments(CardRank.SEVEN, "Seven"),
arguments(CardRank.EIGHT, "Eight"),
arguments(CardRank.NINE, "Nine"),
arguments(CardRank.TEN, "Ten"),
arguments(CardRank.JACK, "Jack"),
arguments(CardRank.QUEEN, "Queen"),
arguments(CardRank.KING, "King"),
arguments(CardRank.ACE, "Ace"),
arguments(CardRank.JOKER, "Joker")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2018-2023, Jeffrey Hope
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.strangercoug.freecasino.enums;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class CardSuitTest {

@ParameterizedTest
@MethodSource("cardSuitToString")
void testCardSuit(CardSuit cardSuit, String string) {
assertThat(cardSuit.toString(), equalTo(string));
}

private static Stream<Arguments> cardSuitToString() {
return Stream.of(
arguments(CardSuit.CLUBS, "Clubs"),
arguments(CardSuit.DIAMONDS, "Diamonds"),
arguments(CardSuit.HEARTS, "Hearts"),
arguments(CardSuit.SPADES, "Spades"),
arguments(CardSuit.BLACK, "Black"),
arguments(CardSuit.RED, "Red")
);
}
}

0 comments on commit 14566fa

Please sign in to comment.