首先生成一副扑克牌(这里不包含大小王,自己练习的时候可以加上),然后根据用户输入的玩家人数和发给每名玩家的牌数进行发牌。
提示:可以使用shuffle()实现“洗牌”操作。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package test; import java.util.*; import static java.util.Collection.*; public class test3 { public static void main(String[] args) { System.out.println("请输入玩家人数:"); int numHands = new Scanner(System.in).nextInt(); System.out.println("请输入每手牌的牌数:"); int num_card = new Scanner(System.in).nextInt(); List<String> huase = Arrays.asList("♠","♥","♣","♦"); List<String> numb = Arrays.asList("A","2","3","4","5","6","7","8","9","10","J","Q","K"); List<String> deck = new ArrayList<>(); for(int i=0;i<huase.size();i++) for(int j=0;j<numb.size();j++) deck.add(numb.get(j)+" of "+huase.get(i)); Collections.shuffle(deck); for(int i = 0;i<numHands;i++) System.out.println(dealHand(deck,num_card)); } public static List dealHand(List<String>deck,int n){ int deckSize = deck.size(); List<String> handView = deck.subList(deckSize-n,deckSize); List<String> hand = new ArrayList<String>(handView); handView.clear(); return hand; } }
结果:
复制代码
1
2
3
4
5
6
7
8
9
10
11请输入玩家人数: 5 请输入每手牌的牌数: 5 [K of ♥, 9 of ♥, K of ♠, Q of ♦, 4 of ♦] [Q of ♥, 4 of ♠, 2 of ♠, 9 of ♠, Q of ♠] [8 of ♣, 3 of ♠, 3 of ♣, 6 of ♣, 3 of ♥] [10 of ♣, 3 of ♦, 10 of ♠, 6 of ♠, 9 of ♣] [Q of ♣, 10 of ♥, 7 of ♣, 6 of ♦, J of ♠] 进程已结束,退出代码0
好了,一个简单的发牌程序就写成了。如果同学想提升自己的话可以升级一下,例如在控制台下进行出牌等操作,就像斗地主那种。
同学们,看完来个免费的点赞和关注吧!!谢谢了!!!
最后
以上就是飘逸往事最近收集整理的关于java编写扑克牌程序的全部内容,更多相关java编写扑克牌程序内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复