Write a poker game in 5 hours-golden hook fishing

Source: Internet
Author: User
Tags addall

Luo Dayou has a song cloud: "boring days always write boring songs ...... ", I am not a singer, I am a programmer, so I always write boring programs on boring days. The program cannot be too large, or there is no time to complete; the program should be interesting, or it will not achieve the purpose of killing time; the program should be so challenging, or it will not make progress even if it is finished.

The golden hook phishing game is a poker game that I used to play when I was a child. The rule is very simple. After two players send cards to their hands, the next rule is basically settled, players do not have the opportunity to make their own decisions, so this game can be easily simulated automatically using a program.

 

(1) about golden hook phishing games

Basic Rules (simplified version): two players (players), one pair of poker (Deck), and the size of the King (Joker) can be required or not. Our game assumes that it contains the size of the king, after Shuffle, each player gets the same number of cards (27 cards). Players cannot view the cards in their hands at any time. players play cards one by one each time, when it is your turn to play a card, pull out the bottom card in your hand and put it on the Board. The cards on the Board are arranged into a long chain in the order of playing the card. J (Hook) is the most special card. When a player reaches J, all the cards on the card are owned, and put it at the top of the card pool (the opposite to when the card is played). This is the so-called "golden hook fishing". At this time, the card table is cleared, and then the player plays the card again. In addition, when your cards are the same as a card number on the card table, then, the cards in the card table and their subsequent cards are all owned by the players (including the cards they just issued), and then the players play the cards again. For example, the cards on the card table are 3, 7, 8, 4, 9. When a player goes out of 8, the 8, 4, and 9 on the card table will be taken back together with the 8 that he just got out, and there will be 3, 7 on the table. At last, whoever wins the first card will lose.

 

(2) modeling of a deck

Color (Suit) is not important for this game, so the modeling of color is omitted for playing card modeling. Similarly, because you do not need to compare the size, the number of cards (Rank) it can be represented by a String (in which the king is represented by "W ).

Card. java

package com.thoughtworks.davenkin.simplefishinggame;

public class Card {
private String rank;

public Card(String rank) {
this.rank = rank;
}

public String getRank() {
return rank;
}
}

A pair of poker (Deck) consists of 54 cards:

Deck. java

package com.thoughtworks.davenkin.simplefishinggame;

import java.util.ArrayList;
import java.util.Collections;

public class Deck {
ArrayList<Card> cards = new ArrayList<Card>();

public Deck() {
buildDeck();
}

private void buildDeck() {
buildNumberCards();
buildCard("J");
buildCard("Q");
buildCard("K");
buildCard("A");
buildJokerCard();
}

private void buildJokerCard() {
cards.add(new Card("W"));
cards.add(new Card("W"));
}

private void buildNumberCards() {
for (int rank = 2; rank <= 10; rank++) {
buildCard(rank);
}
}

private void buildCard(int rank) {
for (int index = 1; index <= 4; index++) {
cards.add(new Card(String.valueOf(rank)));
}
}

private void buildCard(String rank) {
for (int index = 1; index <= 4; index++) {
cards.add(new Card(rank));
}
}

public ArrayList<Card> getCards() {
return cards;
}

public void shuffle() {
Collections.shuffle(cards);
}
}

Deck not only contains 54 cards, but also defines shuffle and other methods.

 

(3) Modeling of Players

Players have their own names and cards left in their hands. The most important thing is how to play a Player:

Player. java

package com.thoughtworks.davenkin.simplefishinggame;

import java.util.ArrayList;
import java.util.List;

public class Player {
ArrayList<Card> cards = new ArrayList<Card>();
String name;

public Player(String name) {
this.name = name;
}

public String getName() {
return name;
}

public ArrayList<Card> getCards() {
return cards;
}

public void obtainCards(List<Card> cardsToAdd) {
cards.addAll(cardsToAdd);
}


public void playCard(Board board) {
board.addCard(cards.get(0));
System.out.println(name + " played " + cards.get(0).getRank());
board.displayCards();
cards.remove(0);
}

public void displayCards() {
System.out.print("Cards for " + name + ": ");
for (Card card : cards) {
System.out.print(card.getRank() + " ");
}

System.out.println();

}

}

At the beginning of the game, a CardDistributor was defined to authorize each player with the same number of cards. Of course, the licensing action should be completed after shuffling:

CardDistributor. java

package com.thoughtworks.davenkin.simplefishinggame;

import java.util.List;

public class CardDistributor {

public void distributeCards(Deck deck, List<Player> players) {
int cardsPerPlayer = deck.getCards().size() / players.size();
int startIndex = 0;
for (Player player : players) {
player.obtainCards(deck.getCards().subList(startIndex, cardsPerPlayer + startIndex));
startIndex += cardsPerPlayer;
}
}
}

When a Player plays a card, he needs to transfer a card in his hand to the card table. When the Player plays the card, the card table should determine whether there is a card that will be "caught" by the Player, therefore, the getCardsToBeFished method is also defined in Borad:

Board. java

package com.thoughtworks.davenkin.simplefishinggame;

import java.util.ArrayList;
import java.util.List;

public class Board {
ArrayList<Card> cards = new ArrayList<Card>();

public ArrayList<Card> getCards() {
return cards;
}

public void addCard(Card card) {
cards.add(card);
}

public List<Card> getCardsToBeFished() {
if (cards.size() == 1)
return null;

List<Card> cardsToBeFished;
Card lastCard = cards.get(cards.size() - 1);
if (lastCard.getRank().equals("J")) {
cardsToBeFished = cards;
} else {
cardsToBeFished = getCardsOfRangeFishing(lastCard);
}
return cardsToBeFished;
}

public void displayCards() {
System.out.print("Current cards on board:");
for (Card card : cards) {
System.out.print(card.getRank() + " ");
}
System.out.println();
}

public void removeFishedCards(List<Card> cardsToBeFished) {
int endIndex = getCards().indexOf(cardsToBeFished.get(0));
ArrayList<Card> newCards = new ArrayList<Card>();
newCards.addAll(cards.subList(0, endIndex));
cards = newCards;
}

private List<Card> getCardsOfRangeFishing(Card lastCard) {
int startIndex = -1;
for (Card card : cards) {
if (card == lastCard)
break;
if (card.getRank().equals(lastCard.getRank())) {
startIndex = cards.indexOf(card);
}
}

if (startIndex != -1)
return cards.subList(startIndex, cards.indexOf(lastCard) + 1);
return null;
}
}

 

(4) Modeling of the entire game

The entire game defines a FishingManager for centralized management. FishingManager includes member variables such as all players and card tables.

FishingManager. java

package com.thoughtworks.davenkin.simplefishinggame;

import java.util.ArrayList;
import java.util.ListIterator;

public class FishingManager implements FishingRuleChecker, AfterPlayListener {
ArrayList<Player> players = new ArrayList<Player>();
private Player currentPlayer;
Board board;
private ListIterator<Player> iterator;

public FishingManager() {
board = new Board();
}

private void resetPlayerIterator() {
iterator = players.listIterator();
}

public void addPlayers(ArrayList<Player> players) {
this.players.addAll(players);
resetPlayerIterator();
}

@Override
public Player nextPlayer() {
if (iterator.hasNext()) {
return iterator.next();
}
resetPlayerIterator();
return nextPlayer();
}

@Override
public Player whoFailed() {
ListIterator<Player> listIterator = players.listIterator();
while (listIterator.hasNext()) {
Player currentPlayer = listIterator.next();
if (currentPlayer.getCards().size() == 0)
return currentPlayer;
}

return null;
}

@Override
public void afterPlay() {
if (board.getCardsToBeFished() == null)
return;
doFish();
nextPlayer();
}

private void doFish() {
System.out.println(currentPlayer.getName() + " fished cards");
currentPlayer.obtainCards(board.getCardsToBeFished());
board.removeFishedCards(board.getCardsToBeFished());
currentPlayer.displayCards();
board.displayCards();
}

public void start() {
int count = 0;
while (true) {
currentPlayer = nextPlayer();
currentPlayer.displayCards();
currentPlayer.playCard(board);
afterPlay();
count++;
if (whoFailed() != null) {
break;
}
}

System.out.println(whoFailed().getName() + " has failed.");
System.out.println("Total: " + count + " rounds");
}

public static void main(String[] args) {
FishingManager manager = new FishingManager();
Player player1 = new Player("Kayla");
Player player2 = new Player("Samuel");
ArrayList<Player> players = new ArrayList<Player>();
players.add(player1);
players.add(player2);

Deck deck = new Deck();
deck.shuffle();
CardDistributor distributor = new CardDistributor();
distributor.distributeCards(deck, players);

manager.addPlayers(players);
manager.start();
}
}

FishingManager should also contain game rules, such as deciding the order of winning or losing players, so it defines a Game Rule interface FishingRuleChecker and enables FishingManager to implement the FishingRuleChecker interface:

FishingRuleChecker. java

package com.thoughtworks.davenkin.simplefishinggame;

public interface FishingRuleChecker {
Player nextPlayer();
Player whoFailed();
}

At the same time, after each player plays a card, FishingManager should determine whether there is a fish bait and execute the phishing operation. Therefore, it defines an AfterPlayListener interface, and FishingManager also implements
AfterPlayListener interface:

AfterPlayListener. java

package com.thoughtworks.davenkin.simplefishinggame;

public interface AfterPlayListener {
public void afterPlay();
}

 

(5) interesting phenomena

Running FinshingManager can automatically simulate the entire game process. I am interested in the following: how many cards are played by all players before the game ends? As a result, the author conducted 10000 simulated tests and obtained the following results: the maximum number of 14023 hands, the minimum number of 66 hands, and the average number of 1303 hands, ask a mathematical expert to prove whether there is a statistical expectation. The distribution of the number of cards is as follows:

In, the horizontal axis is the game round (a total of 10000 times), and the vertical axis is the number of players corresponding to each game.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.