@Test public void testSetHand() { Player player1 = new Player("Player 1", GameColor.BLACK); Hand newHand = new Hand(); newHand.addCard(new NegotiateCard()); Assert.assertNotEquals(newHand, player1.getHand()); player1.setHand(newHand); Assert.assertEquals(newHand, player1.getHand()); }
private static void testEquity() { long iterations = 1; long p1Wins = 0; long p2Wins = 0; while (iterations < 2_000_000) { Player p1 = new Player("p1"); Player p2 = new Player("p2"); Deck deck = new Deck(); deck.shuffle(); p1.addCard(deck.getCard(Rank.ACE, Suit.CLUB)); p1.addCard(deck.getCard(Rank.ACE, Suit.SPADE)); p2.addCard(deck.getCard(Rank.FIVE, Suit.DIAMOND)); p2.addCard(deck.getCard(Rank.TWO, Suit.HEART)); List<Card> community = new ArrayList<>(); for (int i = 0; i < 5; i++) { community.add(deck.deal()); } List<Card> p1Cards = new ArrayList<>(); p1Cards.addAll(p1.getHand()); p1Cards.addAll(community); List<Card> p2Cards = new ArrayList<>(); p2Cards.addAll(p2.getHand()); p2Cards.addAll(community); List<Card> p1BestHand = HandRankUtils.findBestHand(p1Cards); List<Card> p2BestHand = HandRankUtils.findBestHand(p2Cards); int result = HandRankUtils.compare(p1BestHand, p2BestHand); if (result == 1) { p1Wins++; } else if (result == -1) { p2Wins++; } else { p1Wins++; p2Wins++; } if (iterations % 10_000 == 0) { System.out.println("Player 1 equity: " + ((float) p1Wins / iterations) * 100f); System.out.println("Player 2 equity: " + ((float) p2Wins / iterations) * 100f); System.out.println(); } iterations++; } }
private void trade( Player recieve, Player send) // trades the best card of the scum(send) with the worst card of the prez(recieve) { ScumCard c = send.getHand().get(send.findIndexMax()); // System.out.print("Trading: " + c + " For: "); send.removeCard(c); recieve.addCard(c); c = recieve.getHand().get(recieve.findIndexMin()); recieve.removeCard(c); // System.out.println(c); send.addCard(c); }
@Override public void makeChoices(Game game, Event event, java.util.Map<Parameter, Set> parameters) { int numberOfCards = 1; if (parameters.containsKey(Parameter.NUMBER)) numberOfCards = Sum.get(parameters.get(Parameter.NUMBER)); if (numberOfCards < 0) numberOfCards = 0; java.util.Set<Card> cardsInHand = null; boolean specificChoices = parameters.containsKey(Parameter.CHOICE); if (specificChoices) cardsInHand = parameters.get(Parameter.CHOICE).getAll(Card.class); for (Player player : parameters.get(Parameter.PLAYER).getAll(Player.class)) { if (!specificChoices) cardsInHand = new Set(player.getHand(game.actualState).objects).getAll(Card.class); java.util.Collection<Card> choices = player.sanitizeAndChoose( game.actualState, numberOfCards, cardsInHand, PlayerInterface.ChoiceType.OBJECTS, PlayerInterface.ChooseReason.DISCARD); if (choices.size() != numberOfCards) event.allChoicesMade = false; event.putChoices(player, choices); } }
@Override public boolean attempt(Game game, Event event, java.util.Map<Parameter, Set> parameters) { int successes = 0; Set cause = parameters.get(Parameter.CAUSE); int required = 1; if (parameters.containsKey(Parameter.NUMBER)) required = Sum.get(parameters.get(Parameter.NUMBER)); for (Player player : parameters.get(Parameter.PLAYER).getAll(Player.class)) { java.util.Set<Card> cards = null; if (parameters.containsKey(Parameter.CHOICE)) cards = parameters.get(Parameter.CHOICE).getAll(Card.class); else cards = new Set(player.getHand(game.actualState).objects).getAll(Card.class); successes = 0; for (Card thisCard : cards) { java.util.Map<Parameter, Set> newParameters = new java.util.HashMap<Parameter, Set>(); newParameters.put(Parameter.CAUSE, cause); newParameters.put(Parameter.CARD, new Set(thisCard)); if (createEvent( game, thisCard.getOwner(game.actualState) + " discards " + thisCard + ".", DISCARD_ONE_CARD, newParameters) .attempt(event)) successes++; if (successes == required) break; } if (successes != required) return false; } return true; }
@Override public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) { Set cards = new Set(); for (Player player : parameters.get(Parameter.PLAYER).getAll(Player.class)) cards.addAll(player.getHand(game.actualState).objects); java.util.Map<Parameter, Set> discardParameters = new java.util.HashMap<Parameter, Set>(); discardParameters.put(EventType.Parameter.CAUSE, parameters.get(Parameter.CAUSE)); discardParameters.put(EventType.Parameter.CARD, cards); Event discardEvent = createEvent( game, "Each player discards his or her hand", EventType.DISCARD_CARDS, discardParameters); discardEvent.perform(event, true); java.util.Map<Parameter, Set> drawParameters = new java.util.HashMap<Parameter, Set>(); drawParameters.put(EventType.Parameter.CAUSE, parameters.get(Parameter.CAUSE)); drawParameters.put(EventType.Parameter.PLAYER, parameters.get(Parameter.PLAYER)); drawParameters.put(EventType.Parameter.NUMBER, new Set(4)); Event drawEvent = createEvent(game, "and draws four cards.", EventType.DRAW_CARDS, drawParameters); drawEvent.perform(event, true); event.setResult(Empty.set); return true; }
/* * print player's hand to screen */ static void showHand(Player player, String msg) { int i = 0; System.out.print(msg); for (Card card : player.getHand().getHighestToLowestRankCards(true)) { System.out.print(String.valueOf(++i) + ") " + card.toString() + " "); } System.out.println(); }
public void showTable() { System.out.println("========================"); System.out.println("Game ID: " + gid); for (Player p : playerlist.values()) { System.out.println(); System.out.println(p.getName()); System.out.println(p.getHand()); } System.out.println(); System.out.println("Top card:" + cardOnTable); }
@Override public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) { event.setResult(Empty.set); MagicSet cause = parameters.get(Parameter.CAUSE); Player you = parameters.get(Parameter.PLAYER).getOne(Player.class); Zone library = you.getLibrary(game.actualState); Map<Parameter, MagicSet> zombieParameters = new HashMap<Parameter, MagicSet>(); zombieParameters.put(Parameter.CAUSE, cause); zombieParameters.put(Parameter.PLAYER, new MagicSet(you)); zombieParameters.put(Parameter.NUMBER, ONE); zombieParameters.put(Parameter.CARD, new MagicSet(library)); zombieParameters.put(Parameter.TYPE, new MagicSet(HasSubType.instance(SubType.ZOMBIE))); Event zombieSearch = createEvent(game, "Search your library for a Zombie card", SEARCH, zombieParameters); zombieSearch.perform(event, false); Map<Parameter, MagicSet> swampParameters = new HashMap<Parameter, MagicSet>(zombieParameters); swampParameters.put(Parameter.TYPE, new MagicSet(HasSubType.instance(SubType.SWAMP))); Event swampSearch = createEvent(game, "Search your library for a Swamp card", SEARCH, swampParameters); swampSearch.perform(event, false); MagicSet thoseCards = new MagicSet(); thoseCards.addAll(zombieSearch.getResult()); thoseCards.addAll(swampSearch.getResult()); Map<Parameter, MagicSet> moveParameters = new HashMap<Parameter, MagicSet>(); moveParameters.put(Parameter.CAUSE, cause); moveParameters.put(Parameter.TO, new MagicSet(you.getHand(game.actualState))); moveParameters.put(Parameter.OBJECT, thoseCards); Event move = createEvent(game, "Put those cards into your hand", MOVE_OBJECTS, moveParameters); move.perform(event, true); Map<Parameter, MagicSet> shuffleParameters = new HashMap<Parameter, MagicSet>(); shuffleParameters.put(Parameter.CAUSE, cause); shuffleParameters.put(Parameter.PLAYER, new MagicSet(you)); Event shuffle = createEvent(game, "Shuffle your library", SHUFFLE_LIBRARY, shuffleParameters); shuffle.perform(event, true); return true; }
public void init() { setupDeck(); Collections.shuffle(deck); for (Player p : playerlist.values()) { for (int j = 0; j < 7; j++) { p.addCard(deck.get(0)); deck.remove(0); } } System.out.println(); System.out.println("Game ID: " + gid); for (Player p : playerlist.values()) { System.out.println(); System.out.println(p.getName()); System.out.println(p.getHand()); } cardOnTable = dealTopCard(); System.out.println(); System.out.println("Top card:" + cardOnTable); }
@Test public void testGetHand() { Player player1 = new Player("Player 1", GameColor.BLACK); Assert.assertNotNull(player1.getHand()); }
/* * prompt for and return cards to discard based off their index position */ static ArrayList<Integer> getDiscardedCards(Player player) { System.out.println(); int aceCount = player.getHand().getAceCount(); ArrayList<Integer> list = new ArrayList<Integer>(); // control to keep asking for user input until valid input received Boolean isInvalid = true; while (isInvalid) { int discardLimit = NORMAL_DISCARD_LIMIT; if (aceCount > 0) { discardLimit = ACE_DISCARD_LIMIT; System.out.println( "Since you have an Ace you can keep the Ace and discard the other four cards."); } isInvalid = false; System.out.print( "List the card numbers you wish to discard (Enter 0 to keep existing hand). > "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { Boolean isFirstCard = true; String[] indices = br.readLine().split(" "); for (String index : indices) { try { int idx = Integer.parseInt(index); // user opt to keep current hand if (isFirstCard && idx == 0) { return list; } isFirstCard = false; // get card in hand that user index input corresponds to Card card = player.getHand().getCardAtIndex(idx - 1); if (card == null) { // index out of range - card not found System.out.println("Card not found... Try again."); isInvalid = true; list.clear(); break; } list.add(idx); discardLimit--; // if user discards an Ace, confirm they have another // Ace so they maintain a valid discard limit if ((card.getRank() == Rank.ACE) && (aceCount == 1)) { // user discarded their only Ace, reduce discard // limit discardLimit--; } if (discardLimit < 0) { // user discarded too many cards System.out.println("Cannot exceed your discard limit... Try again."); isInvalid = true; list.clear(); break; } } catch (NumberFormatException ex) { System.out.println("Invalid number received... Try again."); isInvalid = true; list.clear(); break; } } } catch (IOException ex) { System.out.println("Invalid number received... Try again."); isInvalid = true; list.clear(); break; } } return list; }
public static void main(String[] args) { Boolean didUserWin = false; CardPile deck; ArrayList<Player> players = new ArrayList<Player>(); players.add(new User("Human")); printGreeting(); int numComps; if (DEBUG) { numComps = 3; } else { /* * ask for number of player */ numComps = getNumComps(); } /* * Create computer players */ for (int i = 0; i < numComps; i++) { players.add(new Opponent("Comp " + String.valueOf(i + 1))); } /* * get our deck of cards */ deck = new CardPile(); Boolean isContinuePlay = true; while (isContinuePlay) { /* * randomizes position of cards within deck */ deck.shuffle(); System.out.println("Cards have been shuffled. Let's play!"); pause(); System.out.println("Dealer is dealing cards to players..."); pause(); /* * draw cards for each player */ if (DEBUG) { players.get(0).getHand().addCard(new Card(Suit.SPADES, Rank.ACE)); players.get(0).getHand().addCard(new Card(Suit.HEARTS, Rank.TEN)); players.get(0).getHand().addCard(new Card(Suit.HEARTS, Rank.TWO)); players.get(0).getHand().addCard(new Card(Suit.SPADES, Rank.FIVE)); players.get(0).getHand().addCard(new Card(Suit.CLUBS, Rank.FOUR)); players.get(1).getHand().addCard(new Card(Suit.HEARTS, Rank.ACE)); players.get(1).getHand().addCard(new Card(Suit.SPADES, Rank.NINE)); players.get(1).getHand().addCard(new Card(Suit.SPADES, Rank.THREE)); players.get(1).getHand().addCard(new Card(Suit.CLUBS, Rank.KING)); players.get(1).getHand().addCard(new Card(Suit.SPADES, Rank.FOUR)); players.get(2).getHand().addCard(new Card(Suit.SPADES, Rank.QUEEN)); players.get(2).getHand().addCard(new Card(Suit.CLUBS, Rank.TEN)); players.get(2).getHand().addCard(new Card(Suit.CLUBS, Rank.JACK)); players.get(2).getHand().addCard(new Card(Suit.HEARTS, Rank.SIX)); players.get(2).getHand().addCard(new Card(Suit.CLUBS, Rank.FOUR)); players.get(3).getHand().addCard(new Card(Suit.DIAMONDS, Rank.TWO)); players.get(3).getHand().addCard(new Card(Suit.SPADES, Rank.NINE)); players.get(3).getHand().addCard(new Card(Suit.DIAMONDS, Rank.THREE)); players.get(3).getHand().addCard(new Card(Suit.SPADES, Rank.TEN)); players.get(3).getHand().addCard(new Card(Suit.HEARTS, Rank.FOUR)); } else { for (int c = 1; c <= MAX_HAND_SIZE; c++) { for (Player player : players) { player.getHand().addCard(deck.drawCard()); } } } /* * show user their hand in descending rank */ showHand(players.get(USER_INDEX_POSITION), "The cards in your hand are: "); pause(); System.out.println("It is time now to discard unwanted cards."); /* * get user's unwanted cards */ ArrayList<Integer> discardIndices = getDiscardedCards(players.get(USER_INDEX_POSITION)); /* * physically discard the user's cards */ ArrayList<Card> discardCards = new ArrayList<Card>(); for (int discardIndex : discardIndices) { Card card = players.get(USER_INDEX_POSITION).getHand().getCardAtIndex(discardIndex - 1); if (card != null) { discardCards.add(card); } else { System.out.println("Failed to get card at index " + discardIndex); } } for (Card discardCard : discardCards) { players.get(USER_INDEX_POSITION).getHand().discardCard(discardCard); } if (!DEBUG) { /* * opponents discard their non-valuable cards */ for (int i = 0; i < players.size(); i++) { // we loop through all index values of array not assuming // which // index position user is at... if index is user, continue if (i == USER_INDEX_POSITION) continue; // now we are in index position related to opponent // Cast player as opponent Opponent opponent = (Opponent) players.get(i); opponent.discardCards(); int discarded = MAX_HAND_SIZE - players.get(i).getHand().getCardCount(); if (discarded > 0) { System.out.println( players.get(i).getName() + " has discarded " + discarded + " card" + (discarded > 1 ? "s" : "")); } else { System.out.println(players.get(i).getName() + " has chosen to keep their hand"); } } } pause(); /* * loop through each player and draw new cards */ for (Player player : players) { int drawn = 0; for (int i = player.getHand().getCardCount() + 1; i <= MAX_HAND_SIZE; i++) { player.getHand().addCard(deck.drawCard()); drawn++; } if (drawn > 0) System.out.println( player.getName() + " has drawn " + drawn + " card" + (drawn > 1 ? "s" : "")); } pause(); /* * show everyone's hand and determine who has best */ int highestScore = -1; for (Player player : players) { int playerScore = player.getHand().evalHand(); if (highestScore < playerScore) { highestScore = playerScore; } System.out.println(player.getName() + " has a " + player.getHand().getHandHas()); showHand(player, player.getName() + "'s hand: "); System.out.println(); } // We need ArrayList of who has highest score in case of ties ArrayList<Player> winners = new ArrayList<Player>(); for (Player player : players) { int playerScore = player.getHand().evalHand(); if (highestScore == playerScore) { winners.add(player); } } System.out.println(); if (winners.size() == 1) { System.out.println( winners.get(0).getName() + " wins this hand with a " + winners.get(0).getHand().getHandHas() + "."); didUserWin = (winners.get(0).getName() == players.get(USER_INDEX_POSITION).getName()); } else { // we need to break ties for (int i = 0; i < winners.size() - 1; i++) for (int j = i + 1; j < winners.size(); j++) { Player player1 = winners.get(i); Player player2 = winners.get(j); if (player1.equals(player2)) continue; int result = player1.getHand().willBeat(player2.getHand()); if (result == 1) { // player 2 lost this match, so remove from winners // list winners.remove(player2); j--; } else if (result == 0) { // player 1 lost this match, so remove from winners // list winners.remove(player1); j--; } } if (winners.size() == 1) { System.out.println( winners.get(0).getName() + " wins this hand with a " + winners.get(0).getHand().getHandHas() + "."); didUserWin = (winners.get(0).getName() == players.get(USER_INDEX_POSITION).getName()); } else { System.out.println("There was an unbreakable tie."); } } // Placeholder - hardcode to end game isContinuePlay = false; } if (didUserWin) System.out.println("Thanks for playing. You rock!"); else System.out.println("Thanks for playing. Better luck next time."); }