簡易撲克牌遊戲簡單實現,歡迎指正

post200發表於2021-09-09

1.建立Poker類(屬性:花色,點數;方法:建立撲克牌())和Player類(屬性:ID,姓名,手牌集合;方法:獲取玩家最大手牌()),再建立PlayGame類,實現1.建立撲克牌(呼叫Poker類的方法,增加一些友好的提示資訊) 2.建立玩家 3.洗牌 4.發牌 5.遊戲
2.其中建立整副撲克集合時按牌面規則從小到大的順序建立,在comparetTo方法中判斷大小時,先獲取撲克牌在集合中的index值,判斷index即可判斷牌面大小
3.另外實現了玩家個數的靈活選擇,定義count設定玩家人數

Poker.java程式碼如下:

package com.imooc;

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

/**
 * 撲克牌類,具有花色和點數兩個屬性
 */
public class Poker implements Comparable{
    private String pattern;     //花色
    private String point;       //點數

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    //建構函式
    public Poker(){}
    public Poker(String pattern, String point) {
        this.pattern = pattern;
        this.point = point;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Poker poker = (Poker) o;

        if (!pattern.equals(poker.pattern)) return false;
        return point.equals(poker.point);
    }

    @Override
    public int hashCode() {
        int result = pattern.hashCode();
        result = 31 * result + point.hashCode();
        return result;
    }

    @Override
    /**
     * 重寫compareTo(),比較兩張牌,點數較大者大,點數相同時,花色排在前面的大
     */
    public int compareTo(Object o) {
        List pokerList = createPoker();

        Poker temp = (Poker) o;     //將傳參強制轉換為Poker物件
        int index1 = pokerList.indexOf(this);
        int index2 = pokerList.indexOf(temp);
        if (index1 > index2) return 1;
        else if (index1 == index2)return 0;
        else return -1;

    }

    /**
     * 建立撲克牌,返回撲克牌List
     * 按小從到大的順序建立撲克,方便在compareTo中進行比較
     */
    public List createPoker(){
        List pokerList = new ArrayList();  //整副撲克牌集合
        String[] patterns = {"方片","梅花","紅桃","黑桃"};
        String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A",};

        int len1 = patterns.length;
        int len2 = points.length;
        for (int i = 0; i 

Player.java

package com.imooc;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Player {
    private Integer id;
    private String name;
    private Set handCards;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set getHandCards() {
        return handCards;
    }

    public void setHandCards(Set handCards) {
        this.handCards = handCards;
    }

    public Player(Integer id, String name) {
        this.id = id;
        this.name = name;
        this.handCards = new HashSet();      //需要在建構函式中初始化,否則在主函式中呼叫getHandCards().add()時會報空指標異常
    }

    /**
     * 判斷玩家的最大手牌,返回Poker物件
     */
    public Poker getMaxPoker(){
        //由於HashSet型別無法取到集合中第一張排,先定義一張最小的牌
        Poker maxPoker = new Poker("方片","2");

        if(handCards.isEmpty()){
            System.out.println("玩家手上無牌");
            return null;
        }

        for (Poker poker: handCards) {
            int result = poker.compareTo(maxPoker);
            if(result > 0){
                maxPoker.setPattern(poker.getPattern());
                maxPoker.setPoint(poker.getPoint());
            }
        }
        return maxPoker;
    }
}

PlayGame.java

package com.imooc;

import java.util.*;

/**
 * 實現功能
 * 簡易撲克牌遊戲 遊戲介紹:
 * 1. 建立一副撲克牌,不考慮大小王
 * 2. 建立兩名玩家,玩家至少要有ID、姓名、手牌等屬性,手牌為撲克牌的集合
 * 3. 洗牌,將之前建立的撲克牌順序打亂(說明是有序的)
 * 4. 發牌,將洗牌之後的撲克牌集合,從第一張開始,發給兩名玩家,按照一人一張的方式,每人發兩張
 * 5. 開始遊戲,比大小,取兩人各自的點數最大的牌進行比較,點數大的贏,若大小相同比花色(黑紅梅方)
 **/
public class PokerGame {
    private Scanner input;
    private  List pokerList;     //整副撲克牌集合
    private List playersList;   //玩家集合
    private int count = 4;          //玩家個數

    public PokerGame() {
        this.input = new Scanner(System.in);
        this.playersList = new ArrayList();
        this.pokerList = new ArrayList();
    }

    /**
     * 1.建立撲克牌,直接呼叫Poker類中的createPoker()方法建立
     */
    public void createPoker(){
        System.out.println("------------建立撲克牌-----------------");
        Poker pk = new Poker();
        pokerList = pk.createPoker();
        for (int i = 0; i  maxPokerList = new ArrayList();
        for (Player player : playersList) {
            Poker temp = player.getMaxPoker();
            maxPokerList.add(temp);
            System.out.println("玩家" + player.getName() + "的最大手牌為:" + temp.getPattern() + temp.getPoint());
        }

        Poker maxPoker = maxPokerList.get(0); //臨時物件,存放玩家最大手牌,先賦值為第一個玩家的最大手牌
        int maxIndex = 0; //臨時變數,存放擁有最大手牌的玩家索引值
        for (Poker poker : maxPokerList) {
            int result = poker.compareTo(maxPoker);
            if (result > 0) {
                maxPoker = poker;
                maxIndex = maxPokerList.indexOf(poker);
            }
        }

        //輸出最終輸贏結果
        System.out.println("----------玩家" + playersList.get(maxIndex).getName() + "獲勝--------------");

        //輸出玩家手牌資訊以便驗證結果
        System.out.println("玩家各自的手牌為");
        for (Player player:playersList) {
            System.out.print("玩家" + player.getName() + ":");
            for (Poker poker:player.getHandCards()) {
                System.out.print(poker.getPattern() + poker.getPoint() + " ");
            }
            System.out.print("n");
        }
    }

    public static void main(String[] args) {
        PokerGame pg = new PokerGame();
        pg.createPoker();
        pg.createPlayer();
        pg.pokerShuffle();
        pg.pokerDeal();
        pg.game();
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2249/viewspace-2799595/,如需轉載,請註明出處,否則將追究法律責任。

相關文章