Implement compareHands method, add compareHands tests and fix UserIO Test

This commit is contained in:
Mitch Peck
2022-02-18 19:35:54 -06:00
parent db1e32a314
commit 5f28cd03fe
3 changed files with 128 additions and 6 deletions

View File

@@ -198,15 +198,28 @@ public class Game {
} }
/** /**
* Compares two hands accounting for natural blackjacks * Compares two hands accounting for natural blackjacks using the
* java.lang.Comparable convention of returning positive or negative integers
* *
* @param handA hand to compare * @param handA hand to compare
* @param handB other hand to compare * @param handB other hand to compare
* @return a negative integer, zero, or a positive integer as handA is less than, equal to, or greater than handB. * @return a negative integer, zero, or a positive integer as handA is less than, equal to, or greater than handB.
*/ */
private int compareHands(LinkedList<Card> handA, LinkedList<Card> handB) { protected int compareHands(LinkedList<Card> handA, LinkedList<Card> handB) {
// TODO implement compareHands int scoreA = this.scoreHand(handA);
return 0; int scoreB = this.scoreHand(handB);
if(scoreA == 21 && scoreB == 21){
if(handA.size() == 2 && handB.size() != 2){
return 1; //Hand A wins
} else if (handA.size() != 2 && handB.size() == 2) {
return -1; //Hand B wins
} else {
return 0; //Tie
}
} else {
return Integer.compare(scoreA, scoreB);
}
} }
/** /**

View File

@@ -213,4 +213,113 @@ public class GameTest {
// Then // Then
assertEquals(13, result); assertEquals(13, result);
} }
@Test
@DisplayName("compareHands should return 1 meaning A beat B, 20 to 12")
public void compareHandsAWins() {
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(10, Card.Suit.SPADES));
handA.add(new Card(10, Card.Suit.CLUBS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(1, Card.Suit.SPADES));
handB.add(new Card(1, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(1, result);
}
@Test
@DisplayName("compareHands should return -1 meaning B beat A, 18 to 4")
public void compareHandsBwins() {
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(2, Card.Suit.SPADES));
handA.add(new Card(2, Card.Suit.CLUBS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(5, Card.Suit.SPADES));
handB.add(new Card(6, Card.Suit.HEARTS));
handB.add(new Card(7, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(-1, result);
}
@Test
@DisplayName("compareHands should return 1 meaning A beat B, natural Blackjack to Blackjack")
public void compareHandsAWinsWithNaturalBlackJack() {
//Hand A wins with natural BlackJack, B with Blackjack
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(10, Card.Suit.SPADES));
handA.add(new Card(1, Card.Suit.CLUBS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(6, Card.Suit.SPADES));
handB.add(new Card(7, Card.Suit.HEARTS));
handB.add(new Card(8, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(1, result);
}
@Test
@DisplayName("compareHands should return -1 meaning B beat A, natural Blackjack to Blackjack")
public void compareHandsBWinsWithNaturalBlackJack() {
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(6, Card.Suit.SPADES));
handA.add(new Card(7, Card.Suit.HEARTS));
handA.add(new Card(8, Card.Suit.CLUBS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(10, Card.Suit.SPADES));
handB.add(new Card(1, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(-1, result);
}
@Test
@DisplayName("compareHands should return 0, hand A and B tied with a Blackjack")
public void compareHandsTieBothBlackJack() {
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(11, Card.Suit.SPADES));
handA.add(new Card(10, Card.Suit.CLUBS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(10, Card.Suit.SPADES));
handB.add(new Card(11, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(0, result);
}
@Test
@DisplayName("compareHands should return 0, hand A and B tie without a Blackjack")
public void compareHandsTieNoBlackJack() {
givenStubGame();
LinkedList<Card> handA = new LinkedList<>();
handA.add(new Card(10, Card.Suit.DIAMONDS));
handA.add(new Card(10, Card.Suit.HEARTS));
LinkedList<Card> handB = new LinkedList<>();
handB.add(new Card(10, Card.Suit.SPADES));
handB.add(new Card(10, Card.Suit.CLUBS));
int result = game.compareHands(handA,handB);
assertEquals(0, result);
}
} }

View File

@@ -70,7 +70,7 @@ public class UserIoTest {
@DisplayName("promptInt should print an error and reprompt if given a non-numeric response") @DisplayName("promptInt should print an error and reprompt if given a non-numeric response")
public void testPromptIntRepromptsOnNonNumeric() { public void testPromptIntRepromptsOnNonNumeric() {
// Given // Given
Reader in = new StringReader("foo\n1"); // word, then number Reader in = new StringReader("foo" + System.lineSeparator() +"1"); // word, then number
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
UserIo userIo = new UserIo(in, out); UserIo userIo = new UserIo(in, out);
@@ -78,7 +78,7 @@ public class UserIoTest {
int result = userIo.promptInt("TEST"); int result = userIo.promptInt("TEST");
// Then // Then
assertEquals("TEST? !NUMBER EXPECTED - RETRY INPUT LINE\n? ", out.toString()); assertEquals("TEST? !NUMBER EXPECTED - RETRY INPUT LINE" + System.lineSeparator() +"? ", out.toString());
assertEquals(1, result); assertEquals(1, result);
} }
} }