diff --git a/out/production/sp21-cs242-assignment1/CardManager.class b/out/production/sp21-cs242-assignment1/CardManager.class index bc11383ba8d1990ad64464166c8f003d107eeb1e..3b553b8e9cce60d30360c982042f9805d8b9eeae 100644 Binary files a/out/production/sp21-cs242-assignment1/CardManager.class and b/out/production/sp21-cs242-assignment1/CardManager.class differ diff --git a/out/production/sp21-cs242-assignment1/Game.class b/out/production/sp21-cs242-assignment1/Game.class index cc388d7240516f9285c00e9039b390411bff60f3..d5c853fa460b7ce595a9af05c64a4e50bd7a828d 100644 Binary files a/out/production/sp21-cs242-assignment1/Game.class and b/out/production/sp21-cs242-assignment1/Game.class differ diff --git a/out/test/sp21-cs242-assignment1/RuleControllerTest.class b/out/test/sp21-cs242-assignment1/RuleControllerTest.class index d81b897b07ecfc6a709c8c887add8725d235eada..9999d71a7bba788c9c2c9c63cffae4034f977b94 100644 Binary files a/out/test/sp21-cs242-assignment1/RuleControllerTest.class and b/out/test/sp21-cs242-assignment1/RuleControllerTest.class differ diff --git a/src/Test/RuleControllerTest.java b/src/Test/RuleControllerTest.java index be37cfc13a5fc658cceef6fc09c63f7c37b45414..ddae6d42ab23f69c801c2ff4c408014be80eae33 100644 --- a/src/Test/RuleControllerTest.java +++ b/src/Test/RuleControllerTest.java @@ -8,7 +8,12 @@ import java.util.ArrayList; import java.util.Random; -// Test for judging whether a play is legal +/** + * A comprehensive test for RuleController class, Including + * 1. validating each card in given contexts (including behavior of wildDraw4) + * 2. updating the gameState (matchable color, number, symbol) when player plays a specific card + * 3. Initialization of initial game state + */ class RuleControllerTest { private static CardParser parser = new CardParser(); @@ -256,28 +261,34 @@ class RuleControllerTest { ArrayList<Integer> groundTruth = groundTruthGenerator(ruler, player, ruler.getMatchableColor(), ruler.getMatchableNumber(), ruler.getMatchableSymbol()); - ruler.isValidPlay(player, 25, true); // check behavior of red 0 + // current player plays red 0 + ruler.isValidPlay(player, 25, true); assert(checkStateUpdatedCorrectly(ruler, "red", "none", "0", 0)); + // current player plays red skip card setCurrentState(ruler, "red", "none", "8", 0); - ruler.isValidPlay(player, 20,true); // red skip card + ruler.isValidPlay(player, 20,true); assert(checkStateUpdatedCorrectly(ruler, "red", "skip", "none", 3)); + // current player plays red reverse card setCurrentState(ruler, "red", "none", "8", 0); - ruler.isValidPlay(player, 22,true); // red reverse card + ruler.isValidPlay(player, 22,true); assert(checkStateUpdatedCorrectly(ruler, "red", "reverse", "none", 0)); assert(!ruler.getIsClockwise()); + // current player plays red draw 2 ruler.changeGameOrder(); setCurrentState(ruler, "red", "none", "8", 0); - ruler.isValidPlay(player, 24,true); // red draw2 card + ruler.isValidPlay(player, 24,true); assert(checkStateUpdatedCorrectly(ruler, "red", "draw2", "none", 1)); assert(ruler.getPenaltyDraw() == 2); // next player should draw 2 + // current player plays blue 8 setCurrentState(ruler, "red", "none", "8", 0); - ruler.isValidPlay(player, 58, true); // blue 8 + ruler.isValidPlay(player, 58, true); assert(checkStateUpdatedCorrectly(ruler, "blue", "none", "8", 0)); + // current player plays wild card setCurrentState(ruler, "red", "none", "8", 0); ruler.isValidPlay(player, 102, true); // wild card, color should be NA, a special state assert(checkStateUpdatedCorrectly(ruler, "NA", "none", "none", 0)); @@ -319,8 +330,37 @@ class RuleControllerTest { } + /** + * 12. Test initialization of beginning game state is done properly + * As this is a random process, it should be repeated many times using for loop + */ + @Test + void testInitialization() { + ArrayList<String> colors = new ArrayList<>(); + colors.add("red"); + colors.add("green"); + colors.add("blue"); + colors.add("yellow"); + + ArrayList<String> numbers = new ArrayList<>(); + for(int i = 0; i <= 9; i++) { + numbers.add((Integer.toString(i))); + } + + for (int i = 0; i < 500; i++) { + RuleController ruler = new RuleController(); + assert(colors.contains(ruler.getMatchableColor())); + assert(numbers.contains(ruler.getMatchableNumber())); + assert(ruler.getMatchableSymbol().equals("none")); + } + + } + + + /** + * helper function: * extract all the cards that the ruler think as legal */ private ArrayList<Integer> getAllLegalCardsByRuler(RuleController ruler, Player player) { diff --git a/src/UNO/CardManager.java b/src/UNO/CardManager.java index 4dcf7bf573898c429317ecbd1b18729e98f65de7..949e5888392a6fd820b865342b24e17c5560cc27 100644 --- a/src/UNO/CardManager.java +++ b/src/UNO/CardManager.java @@ -51,21 +51,24 @@ public class CardManager { return null; } - ArrayList<Integer> cardsDrawnFromDiscard = new ArrayList<Integer>(); + ArrayList<Integer> cardsDrawnFromDiscard = new ArrayList<>(); if (numCardLeft() < numToDraw) { int numToDrawFromDiscard = numToDraw - cardPile.size(); + numToDraw -= numToDrawFromDiscard; cardsDrawnFromDiscard = drawCardsfromDiscardPile(numToDrawFromDiscard); + if (numToDraw == 0) return cardsDrawnFromDiscard; // we don't need to check cardPile anymore } - ArrayList<Integer> drawnCards = new ArrayList<Integer>(cardPile.subList(0, numToDraw)); + + ArrayList<Integer> drawnCards = new ArrayList<>(cardPile.subList(0, numToDraw)); drawnCards.addAll(cardsDrawnFromDiscard); cardPile.subList(0, numToDraw).clear(); // erase the drawn cards return drawnCards; } private ArrayList<Integer> drawCardsfromDiscardPile(int numToDraw) { - ArrayList<Integer> drawnCards = new ArrayList<Integer>(discardPile.subList(0, numToDraw)); - cardPile.subList(0, numToDraw).clear(); // erase the drawn cards + ArrayList<Integer> drawnCards = new ArrayList<>(discardPile.subList(0, numToDraw)); + discardPile.subList(0, numToDraw).clear(); // erase the drawn cards return drawnCards; } diff --git a/src/UNO/Game.java b/src/UNO/Game.java index 9009612e6474858a406bc2728df8b87ba207376b..7d5ec00f0540eb62d3018d436273b86cdb28f726 100644 --- a/src/UNO/Game.java +++ b/src/UNO/Game.java @@ -21,6 +21,7 @@ public class Game { * @param playerNum */ public Game(int playerNum) { + assert(playerNum >= 1 && playerNum <= 10); // UNO is intended for 1-10 players gameCardManager = new CardManager(); ruler = new RuleController(); decideFirstPlayerID(playerNum); @@ -46,7 +47,6 @@ public class Game { currentPlayerID = rand.nextInt(playerNum); } - /** * find the next player in the sequence, regardless whether he or not should be skipped */