Java简单实现斗地主洗牌、发牌
程序员文章站
2022-07-12 09:08:57
...
本文摘自:https://funyan.cn/p/403.html
需求分析
按照斗地主的规则,完成洗牌发牌的动作。
具体规则:
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
代码实现
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
/**
*
**/
public class Poker
{
public static void main(String[] args) {
//准备牌,54张牌,其中两张大小王,其他为不同花色的牌
//一盒牌的集合
ArrayList<String> PokerBox=new ArrayList<>();
//把大小王加进去
PokerBox.add("大王");
PokerBox.add("小王");
//将其他牌加进来
//定义两个数组
String[] colors={"♥","♠","♦","♣"};
String[] num={"A","2","K","Q","J","10","9","8","7","6","5","4","3"};
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < colors.length; j++) {
PokerBox.add(colors[j]+num[i]);
}
}
//洗牌
Collections.shuffle(PokerBox);
//发牌
//发51张牌,剩余当做底牌不发
//新建四个集合,用于接收牌
ArrayList<String> zhangsan=new ArrayList<>();
ArrayList<String> lisi=new ArrayList<>();
ArrayList<String> wangwu=new ArrayList<>();
ArrayList<String> bottomPoker=new ArrayList<>();
for (int i = 0; i < PokerBox.size(); i++) {
if(i<51){
//正常发牌
switch (i%3){
case 0: zhangsan.add(PokerBox.get(i));break;
case 1: lisi.add(PokerBox.get(i));break;
case 2: wangwu.add(PokerBox.get(i));break;
}
}else{
bottomPoker.add(PokerBox.get(i));
}
}
//看牌
System.out.println("张三的牌:"+zhangsan);
System.out.println("李四的牌:"+lisi);
System.out.println("王五的牌:"+wangwu);
System.out.println("底牌:"+bottomPoker);
}
}
结果
张三的牌:[♣6, ♠2, ♣10, ♥10, ♠3, ♠5, ♥7, ♥J, ♦9, ♠J, ♦10, ♥A, ♠10, ♥Q, ♣9, ♣K, ♠Q]
李四的牌:[♥9, ♦K, ♥8, ♠4, ♦Q, ♥2, ♦7, ♦2, ♦J, ♠A, ♦8, ♥4, 小王, ♥3, ♠9, ♣8, ♥6]
王五的牌:[♣Q, ♣3, ♣A, ♦A, ♠K, ♦4, ♠6, ♣J, ♦5, ♥5, ♦3, ♣4, ♦6, ♥K, 大王, ♣2, ♣5]
底牌:[♣7, ♠7, ♠8]
本文摘自:https://funyan.cn/p/403.html
下一篇: Java实现--模拟斗地主的洗牌和发牌