Java实现扑克牌游戏的示例代码

想不想带上好朋友来上一局三人扑克呢?这篇文章教你如何利用Java语言实现一个简单的扑克牌游戏,文中的示例代码讲解详细,感兴趣的可以了解一下

一、三人扑克

想不想带上好朋友来上一局三人扑克呢。

二、具体实现

Card类

定义一个花色color变量和一个大小rank变量。

public class Card {
    public int rank;
    public String color;

    @Override
    public String toString() {
        return String.format("[%s,%d]",color,rank);
    }
}

生成52张牌

我们先买一副新牌

public class Cards {
    public static final String[] COLORS = {"♦","♥","♣","♠"};
    //输入52张牌
    public static List<Card> buyCard() {
        List<Card> list = new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String color = COLORS[i];
                int rank = j;
                Card card = new Card();
                card.color = color;
                card.rank =rank;
                list.add(card);
            }
        }
        return list;
    }
}    

我截取了部分图片,我们可以发现这是一副新牌,按照花色大小放着,我们玩扑克的时候肯定要打乱扑克牌。

打乱顺序

这个方法负责将扑克牌打乱

 public static void mixed(List<Card> cards) {
        for (int i = 51; i > 0; i--) {
            Random random = new Random();
            int j = random.nextInt(i);
            Card c = cards.get(i);
            cards.set(i,cards.get(j));
            cards.set(j,c);
        }
    }

发牌

public static void main(String[] args) {
        List<Card> list = buyCard();//获取新牌
        mixed(list);//把牌打乱
        List<List<Card>> lists = new ArrayList<>();
        lists.add(new ArrayList<Card>());
        lists.add(new ArrayList<Card>());
        lists.add(new ArrayList<Card>());
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                lists.get(j).add(list.remove(0));
            }
        }
        System.out.println("三个人的牌分别是: ");
        System.out.println(lists.get(0));
        System.out.println(lists.get(1));
        System.out.println(lists.get(2));
    }

三、完整代码

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Card {
    public int rank;
    public String color;

    @Override
    public String toString() {
        return String.format("[%s,%d]",color,rank);
    }
}
public class Cards {
    public static final String[] COLORS = {"♦","♥","♣","♠"};
    //输入52张牌
    public static List<Card> buyCard() {
        List<Card> list = new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String color = COLORS[i];
                int rank = j;
                Card card = new Card();
                card.color = color;
                card.rank =rank;
                list.add(card);
            }
        }
        return list;
    }
    public static void mixed(List<Card> cards) {
        for (int i = 51; i > 0; i--) {
            Random random = new Random();
            int j = random.nextInt(i);
            Card c = cards.get(i);
            cards.set(i,cards.get(j));
            cards.set(j,c);
        }
    }

    public static void main(String[] args) {
        List<Card> list = buyCard();//获取新牌
        mixed(list);//把牌打乱
        List<List<Card>> lists = new ArrayList<>();
        lists.add(new ArrayList<Card>());
        lists.add(new ArrayList<Card>());
        lists.add(new ArrayList<Card>());
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                lists.get(j).add(list.remove(0));
            }
        }
        System.out.println("三个人的牌分别是: ");
        System.out.println(lists.get(0));
        System.out.println(lists.get(1));
        System.out.println(lists.get(2));
    }
}

到此这篇关于Java实现扑克牌游戏的示例代码的文章就介绍到这了,更多相关Java扑克牌游戏内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

本文标题为:Java实现扑克牌游戏的示例代码

基础教程推荐