炸金花实践练习

Poker类:表示一张扑克牌,包括花色(suit)和数字(rank)。提供了相应的构造方法、getter和setter。

java 复制代码
public class PokersPK {
	public class Poker {
	    private String suit;//花色
	    private int rank;//数字
	 
	    public Poker(String suit, int rank) {
	        this.suit = suit;
	        this.rank = rank;
	    }
	 
	    public String getSuit() {
	        return suit;
	    }
	 
	    public void setSuit(String suit) {
	        this.suit = suit;
	    }
	 
	    public int getRank() {
	        return rank;
	    }
	 
	    public void setRank(int rank) {
	        this.rank = rank;
	    }
	 
	    @Override
	    public String toString() {
	        return "{ "+suit+" "+rank+"}";
	    }
	}
	
	private static final String[] suits = {"♥","♣","♦","♠"};

buyPoker()方法:生成一副完整的扑克牌,包括四种花色("♥","♣","♦","♠")和数字(1 到 13)的组合,总共52张牌。

java 复制代码
public List<Poker> buyPoker(){
        List<Poker> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Poker poker = new Poker(suits[i], j);
                pokers.add(poker);
            }
        }
        return pokers;
    }

shuffle(List<Poker> pokers)方法:对传入的扑克牌进行洗牌操作,即打乱牌的顺序。

java 复制代码
 public void shuffle(List<Poker> pokers){
        for (int i = pokers.size()-1; i > 0; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(pokers,i,index);
        }
    }

swap(List<Poker> pokers, int i, int j)方法:交换扑克牌集合中的两张牌的位置。

java 复制代码
 private void swap(List<Poker> pokers, int i, int j){
        Poker tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));
        pokers.set(j,tmp);
    }

game(List<Poker> pokers)方法:进行发牌操作,将洗好的扑克牌分成三份,每份五张。

java 复制代码
public List<List<Poker>> game(List<Poker> pokers){
        List<List<Poker>> hand = new ArrayList<>();
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
 
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Poker removePoker = pokers.remove(0);
                hand.get(j).add(removePoker);
            }
        }
        return hand;
    }

main()方法:在主函数中实例化了一个PokersPK对象,购买了一副扑克牌,并展示了洗牌和发牌的过程。

java 复制代码
public static void main(String[] args) {
    	  PokersPK game = new PokersPK();
          List<Poker> pokers = game.buyPoker();
          System.out.println(pokers);
   
          //洗牌
          game.shuffle(pokers);
          System.out.println("洗牌:");
          System.out.println(pokers);
   
          //揭牌
          List<List<Poker>> hand = game.game(pokers);
          System.out.println("揭牌:");
          for (int i = 0; i < hand.size(); i++) {
              System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));
          }
          System.out.println("剩下的牌");
          System.out.println(pokers);
      }

运行结果:

相关推荐
真上帝的左手13 分钟前
7. 网络安全-网关-认证鉴权实战-Spring Boot+SpringSecurity+JWT登录认证
java·spring boot·web安全
1-1=019 分钟前
Vue TodoList
前端·javascript·vue.js
Resean022310 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设10 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
feixing_fx12 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
无奈笑天下12 小时前
Linux上kdump故障标准处理流程
linux·运维·服务器·github
Flynt12 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
前端架构师-老李13 小时前
html教程:header、nav、main、footer 四个标签各管哪一块?
前端·html
烟雨空灵14 小时前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
wangruofeng14 小时前
怎么让你的项目更好看?用 UI Lab 练设计原则,给界面去 AI 味
前端·aigc·ai编程