欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

扑克牌比较 (Java)

程序员文章站 2022-03-06 08:07:14
...

扑克牌比较 (Java)

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

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

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

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

说明:此问题是无意中看到的,写的时候比较随意,所以代码可能有些混乱,有疑问请留言^_^

代码:

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<Card> list = new ArrayList<Card>();
        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<Card> 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;
    }

}

结果截图:

扑克牌比较 (Java)

扑克牌比较 (Java)