Java 斗地主发牌

 1.先定义一个Card类来存储扑克牌的信息

package com.collection.cases.ddz_game;public class Card {private String size; // 牌的点数private String color; // 牌的花色private int index; // 牌的真正大小public Card() {}public Card(String size, String color, int index) {this.size = size;this.color = color;this.index = index;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}// 记得重写toString()方法,我们只需要输出牌的点数和花色@Overridepublic String toString() {return size + color;}
}

 2.开始写具体功能了

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class DdzGame {/*1.定义一个静态的集合,这样这个集合只加载一个,因为当前房间只需要一张派*/public static List cards = new ArrayList<>();/*2.在程序真正运行main方法前,把54张牌放进去,后续游戏就可以直接只用了*/static {// 3.正式做做牌,放到集合中去// a.定义一个数组储存全部点数,类型确定了,个数确定了String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};// b.定义一个数组存储全部的花色,类型确定了,个数确定了String[] colors = {"♥", "♦", "♣", "♠"};//  c.遍历点数int index = 0; // 记录牌的大小for (String size : sizes) {// 每遍历一个点数,index就得自增,来体现每个点数的大小index++;// d.遍历花色for (String color : colors) {// 将牌放进牌集合里去cards.add(new Card(size, color, index));}}// 别漏了大小王,这里使用的是Collections工具类,Collections.addAll() 快速添加多个数据Collections.addAll(cards, new Card("大","👑", ++index), new Card("小","👑", ++index));// 将集合内容打乱(洗牌)Collections.shuffle(cards);}public static void main(String[] args) {System.out.println("洗牌牌后:" + cards);// 定义三个玩家集合,存储他们各自的牌List gamer1 = new ArrayList<>();List gamer2 = new ArrayList<>();List gamer3 = new ArrayList<>();// 开始发牌(从牌集合中发出51张牌给三个玩家,剩余3张当底牌for (int i = 0; i < cards.size() - 3; i++) {// 拿到当前牌对象Card c = cards.get(i);// 对3取余 轮循算法if (i % 3 == 0) {gamer1.add(c);} else if (i % 3 == 1) {gamer2.add(c);} else {gamer3.add(c);}}// 拿到最后三张牌(将最后三张牌截取成一个子集合)List lastThreeCards = cards.subList(cards.size() - 3, cards.size());// 给玩家的牌排序sortCards(gamer1);sortCards(gamer2);sortCards(gamer3);// 输出玩家的怕牌System.out.println("玩家1:" + gamer1);System.out.println("玩家2:" + gamer2);System.out.println("玩家3:" + gamer3);}private static void sortCards(List gamer) {//        gamer.sort(new Comparator() {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return o1.getIndex() - o2.getIndex();
//            }
//        });//        优化后
//        gamer.sort((o1, o2) -> o1.getIndex() - o2.getIndex());
//        通过Comparator接口,它代表比较器
//        static  Comparator comparingInt(ToIntFunction keyExtractor)
//        接受提取的一个函数int从类型分类键T ,并返回一个Comparator ,通过该排序关键字进行比较。如果指定的功能也可串行化,则返回的比较器是可序列化的。gamer.sort(Comparator.comparingInt(Card::getIndex));//        通过Collections工具类提供的sort()方法进行排序
//        Collections.sort(gamer, new Comparator() {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return o1.getIndex() - o2.getIndex();
//            }
//        });//        优化后
//        Collections.sort(gamer, (o1, o2) -> o1.getIndex() - o2.getIndex());}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部