java实现斗地主
文章目錄
- 前言
- 二、代碼
- 1.代碼展示:
- 2.結(jié)果展示:
前言
java簡(jiǎn)單實(shí)現(xiàn)斗地主功能
話不多說,直接上代碼;
二、代碼
1.代碼展示:
代碼如下(示例):
public class PlayCard {public static void main(String[] args) {//存儲(chǔ)玩家及底牌Map<String, List<String>> map = new LinkedHashMap<>();//存儲(chǔ)紙牌及花色List<String> list = new ArrayList<>();//初始紙牌String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};//花色String[] colors = {"?", "?", "?", "?"};list.add("大王");list.add("小王");for (String number : numbers) {for (String color : colors) {list.add(number + color);}}System.out.println("list = " + list);//打亂紙牌順序Collections.shuffle(list);System.out.println("list = " + list);//玩家一List<String> list1 = new ArrayList<>();//玩家二List<String> list2 = new ArrayList<>();//玩家三List<String> list3 = new ArrayList<>();//底牌List<String> cards = new ArrayList<>();for (int i = 0; i < list.size(); i++) {if (i < 3) {cards.add(list.get(i));} else if (i % 3 == 0) {list1.add(list.get(i));} else if (i % 3 == 1) {list2.add(list.get(i));} else if (i % 3 == 2) {list3.add(list.get(i));}}map.put("底牌", cards);map.put("玩家一", list1);map.put("玩家二", list2);map.put("玩家三", list3);for (Map.Entry<String, List<String>> entry : map.entrySet()) {System.out.println(entry.getKey() + ":" + entry.getValue());}} }2.結(jié)果展示:
代碼如下(示例):
底牌:[7?, 4?, 3?] 玩家一:[Q?, K?, 10?, 2?, 9?, Q?, 9?, 6?, 5?, 10?, 6?, 小王, K?, 7?, K?, 3?, 9?] 玩家二:[6?, Q?, 2?, 7?, 8?, 6?, A?, 10?, 8?, 5?, A?, 大王, 4?, 2?, Q?, 8?, 8?] 玩家三:[2?, A?, 4?, 9?, 10?, K?, 3?, J?, J?, 4?, A?, 3?, J?, 5?, 7?, J?, 5?]總結(jié)
- 上一篇: 2020电子设计大赛E题复盘
- 下一篇: OpenCV车辆追踪学习笔记