java使用HashMap實(shí)現(xiàn)斗地主(有序版)
本文實(shí)例為大家分享了java使用HashMap實(shí)現(xiàn)斗地主的具體代碼,供大家參考,具體內(nèi)容如下
案例介紹
按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。 具體規(guī)則:
使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。
案例分析
1、準(zhǔn)備牌:
每張撲克牌牌由花色和數(shù)字兩部分組成??梢允褂没ㄉ吓c數(shù)字集合嵌套迭代完成撲克牌的組裝。
2、發(fā)牌
撲克牌組轉(zhuǎn)完畢后由Collections類的shuffle方法打亂重排,最后3張當(dāng)作底牌,剩余牌通過對3取模依次發(fā)牌。
3、看牌
打印集合。
代碼演示
import java.util.*;
/**
* @author layman
*/
public class Poker2 {
// 牌堆
private static Map<Integer, String> pokerMap = new HashMap<>();
// 花色
private static ArrayList<String> colors = new ArrayList<>();
// 數(shù)字
private static ArrayList<String> numbers = new ArrayList<>();
// 撲克牌的編號集合
private static ArrayList<Integer> numberList = new ArrayList<>();
// 玩家編號集合
private static ArrayList<Integer> noP1 = new ArrayList<>();
private static ArrayList<Integer> noP2 = new ArrayList<>();
private static ArrayList<Integer> noP3 = new ArrayList<>();
// 底牌編號集合
private static ArrayList<Integer> diPaiNo = new ArrayList<>();
// 三個玩家
private static ArrayList<String> playerOne = new ArrayList<String>();
private static ArrayList<String> playerTwo = new ArrayList<String>();
private static ArrayList<String> playerThree = new ArrayList<String>();
// 底牌
private static ArrayList<String> diPai = new ArrayList<String>();
/**
* 創(chuàng)建撲克牌并洗牌
*/
public static void createPoker(){
Collections.addAll(colors, "♦", "♣", "♥", "♠");
Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
// 設(shè)置存儲編號
int count = 1;
pokerMap.put(count++, "大王");
pokerMap.put(count++, "小王");
// 創(chuàng)建撲克牌
for (String number : numbers) {
for (String color : colors) {
String card = color + number;
pokerMap.put(count++, card);
}
}
// 先取編號
Set<Integer> numberSet = pokerMap.keySet();
numberList.addAll(numberSet);
// 然后隨機(jī)洗牌
Collections.shuffle(numberList);
}
/**
* 發(fā)牌
*/
public static void faPai(){
for (int i = 0; i < numberList.size(); i++) {
Integer no = numberList.get(i);
// 留出底牌
if (i >= 51) {
diPaiNo.add(no);
} else {
if (i % 3 == 0) {
noP1.add(no);
} else if (i % 3 == 1) {
noP2.add(no);
} else {
noP3.add(no);
}
}
}
}
/**
* 發(fā)牌并排序
*/
public static void sortCards(){
// 對編號進(jìn)行排序
Collections.sort(noP1);
Collections.sort(noP2);
Collections.sort(noP3);
Collections.sort(diPaiNo);
// 進(jìn)行牌面的轉(zhuǎn)換
for (Integer i : noP1) {
// 根據(jù)編號獲取牌面,并發(fā)給對應(yīng)的玩家
String card = pokerMap.get(i);
playerOne.add(card);
}
for (Integer i : noP2) {
String card = pokerMap.get(i);
playerTwo.add(card);
}
for (Integer i : noP3) {
String card = pokerMap.get(i);
playerThree.add(card);
}
for (Integer i : diPaiNo) {
String card = pokerMap.get(i);
diPai.add(card);
}
}
/**
* 看牌
*/
public static void showCards(){
System.out.println("賭圣:" + playerOne);
System.out.println("賭俠:" + playerTwo);
System.out.println("賭王:" + playerThree);
System.out.println("底牌:" + diPai);
}
public static void main(String[] args) {
createPoker();
faPai();
sortCards();
showCards();
}
}
補(bǔ)充:
使用ArrayList實(shí)現(xiàn)斗地主案例(無序版):傳送門
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動報錯問題
這篇文章主要介紹了解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的
這篇文章主要介紹了Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
MyBatisPlus中使用or()和and()遇到的問題及細(xì)節(jié)處理
這篇文章主要介紹了MyBatisPlus中使用or()和and()遇到的問題,本文通過多種寫法實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
解決Idea報錯There is not enough memory
在使用Idea開發(fā)過程中,可能會遇到因內(nèi)存不足導(dǎo)致的閃退問題,出現(xiàn)"There is not enough memory to perform the requested operation"錯誤時,可以通過調(diào)整Idea的虛擬機(jī)選項(xiàng)來解決,方法是在Idea的Help菜單中選擇Edit Custom VM Options2024-11-11

