当前位置: 主页 > JAVA语言

java 扑克牌比较五张-比较嗨的酒桌游戏扑克

发布时间:2023-03-24 09:13   浏览次数:次   作者:佚名

扑克牌比较 (Java)

描述:本程序主要实现52张扑克牌(除去大小王)的洗牌,发牌,比较功能。首先建立两个用户java 扑克牌比较五张,输入用户名,各分发两张扑克牌,而后从两用户中各自拿出手中最大的扑克牌比较,大的获胜。java

扑克牌的比较规则:先按数值比较:2>A>K>Q>J>10>9>8>7>6>5>4>3;若数值相同:则比较花色:红桃 > 黑桃 > 方片 > 梅花。web

输入:两用户名,以后洗牌、发牌、比较自动执行。dom

输出:洗牌、发牌、比较过程。svg

说明:此问题是无心中看到的,写的时候比较随意,因此代码可能有些混乱java 扑克牌比较五张,有疑问请留言^_^this

代码:spa

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/** * 扑克牌 * * 分两个玩家,分别发两张牌,比较两者两牌中最大牌的大小,大的胜, * 2>A>K>Q>J>10>9>8>7>6>5>4>3 * * 若相同,则按花色大小比较:红桃 > 黑桃 > 方片 > 梅花 * */
public class Card {
    static final String[] flo = {"梅花","方片","黑桃","红桃"};
    static final String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    public int flower; // 花色
    public int number;  // 大小 
    public Card() {}
    public Card(int flower, int number) {
        super();
        this.flower = flower;
        this.number = number;
    }
    public static void main(String[] args) throws InterruptedException {
        Role[] role = new Role[2];
        // 按顺序存牌
        List list = new ArrayList();
        for (int i = 0; i < flo.length; i++) {
            for (int j = 0; j < num.length; j++) {
                list.add(new Card(i,j));
            }
        }
        // 建立角色
        for (int i = 0; i < 2; i++) {
            System.out.println("角色"+(i+1));
            System.out.print("姓名:");
            role[i] = new Role();
            role[i].setName(new Scanner(System.in).nextLine());
            System.out.println("******************************");
        }
        // 洗牌
        System.out.println("------------开始洗牌------------");
        washCard(list);
        Thread.sleep(2000);
        System.out.println("------------洗牌完毕------------");
        System.out.println();
        Thread.sleep(1000);
        // 发牌
        System.out.println("------------开始发牌------------");
        Thread.sleep(2000);
        for (int i = 0; i < 4; i++) {
            Card pos = list.remove((int)(Math.random()*list.size()));
            role[i/2].getCard()[i%2].flower = pos.flower;
            role[i/2].getCard()[i%2].number = pos.number;
        }
        System.out.println("------------发牌完毕------------");
        System.out.println();
        Thread.sleep(2000);
        // 比较
        System.out.println("------------开始比较------------");
        Thread.sleep(1000);
        for (int i = 0; i < role.length; i++) {
            role[i].setMaxCard(Role.compare(role[i].getCard()[0], role[i].getCard()[1]));
            Card c = role[i].getMaxCard();
            System.out.println("玩家"+role[i].getName()+"的最大牌为:"+flo[c.flower]+""+num[c.number]);
            Thread.sleep(3000);
        }
        Card card = Role.compare(role[0].getMaxCard(), role[1].getMaxCard());
        if(role[0].getMaxCard().flower==card.flower && role[0].getMaxCard().number==card.number){
            System.out.println("玩家"+role[0].getName()+"获胜");
        }else {System.out.println("玩家"+role[1].getName()+"获胜");}
        Thread.sleep(1000);
        System.out.println("------------比较完毕------------");
        System.out.println();
        Thread.sleep(2000);
        // 查看结果
        System.out.println("------------查看结果------------");
        Thread.sleep(1000);
        for (int i = 0; i < role.length; i++) {
            System.out.print("玩家"+role[i].getName()+":");
            System.out.print(flo[role[i].getCard()[0].flower]+""+num[role[i].getCard()[0].number]+",");
            System.out.println(flo[role[i].getCard()[1].flower]+""+num[role[i].getCard()[1].number]);
            Thread.sleep(1000);
        }
        System.out.println("剩余牌:");
        for (int i = 0; i < list.size(); i++) {
            Card c = list.get(i);
            System.out.print(flo[c.flower]+""+num[c.number]);
            if(i!=list.size()-1) System.out.print(",");
        }
        System.out.println();
    }
    // 洗牌
    public static void washCard(List list){
        int size = list.size();
        for (int i = size-1; i > 0; i--) {
            int random = Math.abs(new Random().nextInt(i));
            Card temp1 = list.get(random), temp2 = list.get(i);
            list.remove(random); list.add(random, temp2);
            list.remove(i); list.add(i, temp1);
        }
    }
}
// 玩家角色类
class Role{
    private String name;  // 姓名
    private Card[] card;  // 两张牌
    private Card maxCard; // 最大牌
    public Role() {
        card = new Card[2];
        for (int i = 0; i < card.length; i++) { card[i] = new Card(); }
    }
    // 比较最大张牌
    public static Card compare(Card c1, Card c2){
        if(c1.number == c2.number)
            return c1.flower > c2.flower ? c1 : c2;
        return c1.number > c2.number ? c1 : c2;
    }
    public Card getMaxCard(){
        return this.maxCard;
    }
    public void setMaxCard(Card c){
        this.maxCard = c;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Card[] getCard() {
        return card;
    }
    public void setCard(Card[] card) {
        this.card = card;
    }
}

结果截图:code

这里写图片描述

这里写图片描述