簡易撲克遊戲---各位爸爸求指點指點

dav2100發表於2021-09-09

新手的一批,寫了好久,功能都有了,就是寫的有點多,不夠精簡,還請多多指教,沒有用到contains ,腦子不夠用了。

Poker 紙牌類

package com.qianlian;

import java.util.List;
import java.util.Random;

public class Poker implements Comparable{
    private String color;
    private String point;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public Poker(String color, String point) {
        this.color = color;
        this.point = point;
    }
    @Override
    public int hashCode() {
        Random random = new Random();
        int result =random.nextInt(100);
        return result;
    }
    @Override
    public int compareTo(Poker o) {
        return this.getPoint().compareTo(o.getPoint());
    }

}

重寫了hashcode,因為我的set會自動排序,沒有洗牌的效果,所以重寫不知道有沒有更好的方法。
重寫了compareTo ,因為在後面要到sort,難道用 Comparator,就要用Comparable嗎

PokerPlayer 玩家類

package com.qianlian;

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

public class PokerPlayer {
    private int id;
    private String name;
    public PokerPlayer(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

感覺沒問題

ComparatorPoker Comparator類

package com.qianlian;

import java.util.Comparator;

import javax.naming.spi.DirStateFactory.Result;

public class ComparatorPoker implements Comparator {
    public int pokColor1;
    public int pokColor2;
    public int resultColor;
    public int resultPoint;
    public int pokPoint1;
    public int pokPoint2;
    public String pokStrPoint1;
    public String pokStrPoint2;

    @Override
    public int compare(Poker o1, Poker o2) {
        switch (o1.getColor()) {
        case "梅花":
            pokColor1 = 1;
            break;
        case "方塊":
            pokColor1 = 2;
            break;
        case "紅心":
            pokColor1 = 3;
            break;
        case "黑桃":
            pokColor1 = 4;
            break;
        default:
            System.out.println("不存在!");
            break;
        }
        switch (o2.getColor()) {
        case "梅花":
            pokColor2 = 1;
            break;
        case "方塊":
            pokColor2 = 2;
            break;
        case "紅心":
            pokColor2 = 3;
            break;
        case "黑桃":
            pokColor2 = 4;
            break;
        default:
            System.out.println("你是真的皮!");
            break;
        }
        switch (o1.getPoint()) {
        case "J":
            pokStrPoint1="11";
            break;
        case "Q":
            pokStrPoint1="12";
            break;
        case "K":
            pokStrPoint1="13";
            break;
        case "A":
            pokStrPoint1="14";
            break;
        default:
            pokStrPoint1 = o1.getPoint();
            break;
        }
        switch (o2.getPoint()) {
        case "J":
            pokStrPoint2="11";
            break;
        case "Q":
            pokStrPoint2="12";
            break;
        case "K":
            pokStrPoint2="13";
            break;
        case "A":
            pokStrPoint2="14";
            break;
        default:
            pokStrPoint2 = o2.getPoint();
            break;
        }

        pokPoint1 = Integer.parseInt(pokStrPoint1);
        pokPoint2 = Integer.parseInt(pokStrPoint2);

        resultPoint = pokPoint1 - pokPoint2;
        resultColor = pokColor1 - pokColor2;

        if(resultPoint!=0){
            return resultPoint;
        }else{
            return resultColor;
        }

    }
}

有點囉嗦,把JQKA和數字轉成統一型別比較,再去規定花色的大小。最後先比較點數,相同在比較花色。

PlayPoker 開始遊戲

package com.qianlian;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;

public class PlayPoker {
    public List pTemp;
    public Set pokers;
    public Map mapPlayer;
    public String pokPoint;
    public String color;
    public Poker tempAdd;
    public Scanner console;
    public int ID;
    public String pName;
    public List tempPok;
    public Iterator it;

    public PlayPoker(){
        pokers = new HashSet();

        console = new Scanner(System.in);
        tempPok = new ArrayList();
        mapPlayer =new HashMap();
        pTemp = new ArrayList();
    }

    public void addPoker(){
        System.out.println("======================建立撲克牌======================");
        for(int j=0;j 紅心 > 方塊  > 梅花》===================================");
            System.out.println("勝利者是:  "+winPer+"————————> "+winPok);

        }

    public static void main(String[] args) {
        PlayPoker pp = new PlayPoker();
        pp.addPoker();
        pp.forEachPoker();
        pp.playPoker();

    }

}

怎麼用contains呢 ,map的value是個List,最後比較出來最大的牌,用contiains檢索包含在那個entry,找出人名。思路好像沒錯,但是試了好多次沒有成功。

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

相關文章