18 March 2017

Chương trình chia bài Tú Radom cho bốn người trên JAVA

Card.Java
Java Advance 2016

public class Card {

    private String rank;
    private String suit;

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

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public String toString() {
        return rank + " - " + suit;
    }
}
Deck.Java
Java Advance  2016

import java.util.Random;

public class Deck {

    private final String rank[] = {"AT", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    private final String suit[] = {"Co", "Ro", "Bich", "Nhep"};
    private Card deck[];
    private final int TOTAL_CARDS = 52;
    private Random randNum;

    public Deck() {
        deck = new Card[TOTAL_CARDS];
        randNum = new Random();
        for (int i = 0; i < deck.length; i++) {
            deck[i] = new Card(rank[i % 13], suit[i / 13]);
        }
    }

    public void shuffle() {
        for (int i = 0; i < deck.length; i++) {
            int j = randNum.nextInt(TOTAL_CARDS);
            Card c = deck[i];
            deck[i] = deck[j];
            deck[j] = c;
        }
    }

    public Card getCard(int index) {
        return deck[index];
    }
}
DemoStack.Java
Java Advance  2016

public class Game {

    private Player[] players;
    private Deck deck;

    public Game() {
        deck = new Deck();
        players = new Player[4];
        players[0] = new Player();
        players[1] = new Player();
        players[2] = new Player();
        players[3] = new Player();
        deck.shuffle();
    }

    public void dealCards() {
        int count = 0;
        for (int i = 0; i < players[0].getCards().length; i++) {
            for (int j = 0; j < players.length; j++) {
                players[j].setCardAtIndex(deck.getCard(count++), i);
            }
        }
    }

    public void showCards() {
        for (int i = 0; i < players.length; i++) {
            System.out.print("Player " + (i + 1) + ": ");
            for (int j = 0; j < players[0].getCards().length; j++) {
                System.out.print("{" + players[i].getCardAtIndex(j).toString()
                        + "} ");
            }
            if (players[i].countPair() > 0) {
                System.out.print("\nCo " + players[i].countPair() + " Doi ");
            }
            if (players[i].isFlush()) {
                System.out.print("FLUSH!!\n");
            }
            System.out.println("\n------------------------------------");
        }
    }
}
Player.Java
Java Advance  2016

public class Player {

    public final static int MAX_CARD = 5;
    private Card cards[];

    public Player() {
        cards = new Card[MAX_CARD];
    }

    public Card[] getCards() {
        return cards;
    }

    public Card getCardAtIndex(int index) {
        if (index >= 0 && index < MAX_CARD) {
            return cards[index];
        } else {
            return null;
        }
    }

    public void setCardAtIndex(Card c, int index) {
        if (index >= 0 && index < MAX_CARD) {
            cards[index] = c;
        }
    }

    public int countPair() {
        int count = 0;
        for (int i = 0; i < cards.length; i++) {
            for (int j = i + 1; j < cards.length; j++) {
                if (cards[i].getRank().equals(cards[j].getRank())) {
                    count++;
                }
            }
        }
        return count;
    }

    public boolean isFlush() {
        int count = 0;
        for (int i = 0; i < cards.length; i++) {
            for (int j = i + 1; j < cards.length; j++) {
                if (cards[i].getSuit().equals(cards[j].getSuit())) {
                    count++;
                }
            }
        }
        if (count == 5) {
            return true;
        } else {
            return false;
        }
    }
}
GameApp.Java
Java Advance  2016

public class GameApp {

    public static void main(String[] args) {
        Game game = new Game();
        game.dealCards();
        game.showCards();
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang