這篇文章主要為大家展示了Java如何實(shí)現(xiàn)斗地主,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到桃江網(wǎng)站設(shè)計(jì)與桃江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋桃江地區(qū)。
import java.util.ArrayList; import java.util.Collections; public class DemoPoker { public static void main(String[] args) { /** *一、準(zhǔn)備牌 普通牌:2 A K...3 花色:♥ ♠ ♣ ♦ 王牌:大王 小王 創(chuàng)建一個(gè)集合,把牌組裝之后存進(jìn)去 */ String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; String[] colors = {"♥", "♠", "♣", "♦"}; ArrayList<String> pokerBox = new ArrayList<>(); for (String color : colors) { for (String number : numbers) { pokerBox.add(color + number); } } pokerBox.add("大王"); pokerBox.add("小王"); /** * 二、洗牌 static void shuffle​(List<?> list) 使用默認(rèn)的隨機(jī)源隨機(jī)置換指定的列表。 此處為了輸出結(jié)果工整所以沒(méi)有直接輸出集合 */ Collections.shuffle(pokerBox); for (int i = 0; i < pokerBox.size(); i++) { System.out.print(pokerBox.get(i)+"\t"); if (i==26) { System.out.println(); } } System.out.println(); /** * 三、發(fā)牌 遍歷集合,用索引%3發(fā)牌,余0給玩家1,余1給玩家2,余2給玩家3 索引0-50是玩家的牌,51-53是底牌 */ //玩家一 ArrayList<String> player01 = new ArrayList<>(); //玩家二 ArrayList<String> player02 = new ArrayList<>(); //玩家三 ArrayList<String> player03 = new ArrayList<>(); //底牌 ArrayList<String> diPai = new ArrayList<>(); for (int i = 0; i < pokerBox.size(); i++) { String faces = pokerBox.get(i); if (i>=51) { diPai.add(faces); } else if (i%3==0) { player01.add(faces); } else if (i%3==1) { player02.add(faces); } else if (i%3==2) { player03.add(faces); } } /** * 四、看牌 直接輸出每位玩家的集合 */ System.out.println("張無(wú)忌"+player01); System.out.println("張翠山"+player02); System.out.println("殷素素"+player03); System.out.println("底牌"+diPai); } }
帶排序版的
package com.demo_2.poker; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; public class Poker { /** *一共要四步。一、備牌 二、洗牌 三、發(fā)牌 四、看牌 目的:練習(xí)集合的用法 */ public static void main(String[] args) { /** * 第一步:備牌 使用List接口中的of()方法添加并分別創(chuàng)建numbers和colors集合 */ //numbers:存儲(chǔ)普通牌 2、A、K...3從大到小 List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"); //colors:存儲(chǔ)四種花色 ♥、♠、♦、♣ List<String> colors = List.of("♥", "♠", "♦", "♣"); //創(chuàng)建一個(gè)Map集合存儲(chǔ)索引和組裝好的牌 HashMap<Integer, String> pokerBox = new HashMap<>(); //創(chuàng)建一個(gè)List集合儲(chǔ)存牌的索引 LinkedList<Integer> pokerIndex = new LinkedList<>(); //先把大王、小王和下標(biāo)分別放進(jìn)Map的鍵和值里面,再向LinkedList里面存儲(chǔ)下標(biāo),下標(biāo)增加1 int index = 0; pokerBox.put(index, "大王"); pokerIndex.add(index); index++; pokerBox.put(index, "小王"); pokerIndex.add(index); index++; //組裝牌:遍歷兩個(gè)List集合,使用Map接口中的put()方法給pokerBox添加鍵和值,并給LinkedList傳下標(biāo) for (String number : numbers) { for (String color : colors) { pokerBox.put(index, color + number); pokerIndex.add(index); index++; } } /** * 第二步:洗牌 使用Collocations類中的shuffler方法,傳遞參數(shù)pokerIndex */ Collections.shuffle(pokerIndex); /** * 第三步:發(fā)牌 創(chuàng)建四個(gè)List集合,分別存儲(chǔ)三位玩家和底牌 使用for循環(huán)遍歷pokerIndex,i%3結(jié)果為0的給玩家1,1的給玩家2,2的給玩家3 */ LinkedList<Integer> player01 = new LinkedList<>(); LinkedList<Integer> player02 = new LinkedList<>(); LinkedList<Integer> player03 = new LinkedList<>(); LinkedList<Integer> diPai = new LinkedList<>(); for (int i = 0; i < pokerIndex.size(); i++) { Integer in = pokerIndex.get(i); if (i >= 51) { diPai.add(in); } else if (i % 3 == 0) { player01.add(in); } else if (i % 3 == 1) { player02.add(in); } else if (i % 3 == 2) { player03.add(in); } } //給玩家的牌排序,使用Collocations接口中的sort()方法排序 Collections.sort(player01); Collections.sort(player02); Collections.sort(player03); Collections.sort(diPai); /** *第四步:看牌 遍歷排過(guò)序的List集合作為Map集合的鍵值獲取對(duì)應(yīng)的值 為提高代碼復(fù)用性定義一個(gè)方法代替 */ print("令狐沖",player01,pokerBox); print("諸葛瑾",player02,pokerBox); print("司馬懿",player03,pokerBox); print("底牌",diPai,pokerBox); } /** *看牌的方法: 參數(shù): String name LinkedList<Integer> list HashMap<Integer, String> map */ public static void print(String name,LinkedList<Integer> list,HashMap<Integer, String> map){ System.out.print(name+":"); for (Integer key : list) { System.out.print(map.get(key)+" "); } System.out.println(); } }
以上就是關(guān)于Java如何實(shí)現(xiàn)斗地主的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:Java如何實(shí)現(xiàn)斗地主
文章URL:http://aaarwkj.com/article8/ihhdip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、全網(wǎng)營(yíng)銷推廣、做網(wǎng)站、服務(wù)器托管、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)