jacks or better video poker (java console version)

Card类

public class Card {
	private int value;
	private String color;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Card(String color, int value) {
		this.color = color;
		this.value = value;
	}

	public String toString() {
        String strValue = new String();
        switch (value){
            case 14:
            	strValue = "A";
                break;
            case 11:
            	strValue = "J";
                break;
            case 12:
            	strValue = "Q";
                break;
            case 13:
            	strValue = "K";
                break;
            default:
            	strValue = String.valueOf(value);
        }
        return color + strValue;
	}
}

Poker类



import java.util.*;
public class Poker {
    String[] colors = {"红桃", "黑桃", "方片", "梅花"};
    Card[] cards = new Card[24];

    public Poker() {
        int index = 0;
        for (int i = 0; i < colors.length; i++) {
            for (int j = 9; j <= 14; j++) {
                cards[index] = new Card(colors[i], j);
                index++;
            }
        }
    }

    public void output() {
    	int count = 0;
        for (int i = 0; i < cards.length; i++) {
            if (count % 6 == 0 && count != 0) {
                System.out.println();
            }
            System.out.print(cards[i]+" ");
            count++;
        }
    }

    public void shuffle() {
        Random random = new Random();
        for (int i = 0; i < cards.length; i++) {
            Card tmp = cards[i];
            int tmpIndex = random.nextInt(cards.length);
            cards[i] = cards[tmpIndex];
            cards[tmpIndex] = tmp;
        }
    }

    public List<Card> getOneHand(){
        this.shuffle();
        List<Card> lst = new ArrayList<>();
        for(int i = 0; i < 5; i++){
            lst.add(cards[i]);
        }
        return lst;
    }
    public Card getOneCard(int index){
        return cards[index];
    }
    public String getHandType(List<Card> hand){
        Set<String> colorSet = new HashSet<>();
        List<Integer> valueLst = new ArrayList<>();
        Set<Integer> valueSet = new HashSet<>();
        Map<Integer,Integer> mapCard = new HashMap<>();

        for(Card card : hand){
            Integer valueTmp = card.getValue();
            colorSet.add(card.getColor());
            valueLst.add(valueTmp);
            valueSet.add(valueTmp);

            if(mapCard.containsKey(valueTmp)){
                Integer count = mapCard.get(valueTmp);
                count++;
                mapCard.put(valueTmp,count);
            }
            else{
                mapCard.put(valueTmp,1);
            }
        }

        Collections.sort(valueLst);
        boolean bIsSameColor = false;
        boolean bIsStraight = false;

        if(valueLst.get(4) - valueLst.get(0) == 4
                && valueSet.size() == 5){
            bIsStraight = true;
//            return "顺子";
        }

        if(colorSet.size() == 1){
            bIsSameColor = true;
//            return "同花";
        }

        if(bIsStraight && bIsSameColor){
            return "同花顺";
        }
        if(bIsSameColor){
            return "同花";
        }
        if(bIsStraight){
            return "顺子";
        }
        if(valueSet.size() == 4){
            return "一对";
        }
        if(valueSet.size() == 5){
            return "杂牌";
        }
 
        if(valueSet.size() == 2){
            if(mapCard.containsValue(4)){
                return "四条";
            }
            else{
                return "满堂红";
            }
        }
        if(valueSet.size() == 3){
            if(mapCard.containsValue(3)){
                return "三带一";
            }
            else{
                return "两对";
            }
        }
        return "";
    }
}

Main类

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

//        poker.output();
//        System.out.println();
//        System.out.println();
//        poker.shuffle();
//        poker.output();
//        System.out.println();
//        System.out.println();
//        List<Card> hand = poker.getOneHand();
//        System.out.println(hand);
//        String type = poker.getHandType(hand);
//        System.out.println(type);
        Poker poker = new Poker();
        Map<String, Integer> mapPrize = new HashMap<>();
        mapPrize.put("同花顺",100);
        mapPrize.put("四条",25);
        mapPrize.put("满堂红",10);
        mapPrize.put("同花",5);
        mapPrize.put("顺子",4);
        mapPrize.put("三带一",3);
        mapPrize.put("两对",2);
        mapPrize.put("一对",0);
        mapPrize.put("杂牌",-1);
//        System.out.println(mapPrize.get(type));

        Scanner scanner = new Scanner(System.in);
        int score = 10;
        while(score > 0){
            System.out.println("-----------------------");
            System.out.println("your current score: " + score);

            System.out.println("please bet your score(1-5):");
            int bet = Integer.valueOf(scanner.nextLine());
            score -= bet;
            List<Card> hand = poker.getOneHand();
            System.out.println(hand);

            System.out.println("please input card no what you want to change(0 for exit)\n, input enter key if you do not change any card");
            String info = scanner.nextLine();
            if(info.equals("0")){
                break;
            }
            else if(info.isEmpty()){
                //一张牌也不换
                ;
            }
            else// 1 3 5
            {
                String[] arr = info.split(" ");
                for(int i = 0;  i < arr.length; i++){
                    int card_no = Integer.parseInt(arr[i]);
                    card_no--;
                    hand.remove(card_no);
                    hand.add(card_no, poker.getOneCard(5+i));
                }
            }

            System.out.println(hand);
            String type = poker.getHandType(hand);
            System.out.println(type);
            int prize = mapPrize.get(type);
            System.out.println(prize*bet);
            score += prize*bet;
        }
    }
}


输出

you current score is 5
[方片A, 梅花Q, 红桃K, 黑桃Q, 方片K]
please input card no what you want to change (0 to exit)
, if you do not want to change any card, pleas input enter 
1 2 4
[方片Q, 红桃Q, 红桃K, 红桃J, 方片K]
两对
you win 2 score
you current score is 6
[黑桃A, 红桃J, 方片10, 黑桃Q, 梅花10]
please input card no what you want to change (0 to exit)
, if you do not want to change any card, pleas input enter 
1 2 4
[红桃K, 红桃A, 方片10, 红桃10, 梅花10]
三带一
you win 3 score
you current score is 8
[方片9, 梅花K, 梅花10, 黑桃J, 红桃A]
please input card no what you want to change (0 to exit)
, if you do not want to change any card, pleas input enter 
0
you got max score is 8
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值