Java實(shí)現(xiàn)斗地主小游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)斗地主小游戲的具體代碼,供大家參考,具體內(nèi)容如下
原理圖:
斗地主過(guò)程:
* 1、組合牌
* 2、洗牌
* 3、發(fā)牌
* 4、看牌
代碼實(shí)現(xiàn):
package itcast.demo6; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class DouDiZhu { public static void main(String[] args) { //1、組合牌 //創(chuàng)建Map集合,鍵:編號(hào) 值:牌 HashMap<Integer,String> pooker = new HashMap<Integer,String>(); //創(chuàng)建List集合,存儲(chǔ)編號(hào) ArrayList<Integer> pookerNumber = new ArrayList<Integer>(); //定義13個(gè)點(diǎn)數(shù)的數(shù)組 String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; //定義4個(gè)花色組合 String[] colors = {"♠","♣","♥","♦"}; //定義一個(gè)整數(shù)變量,作為Map的鍵 int index = 2; //遍歷數(shù)組,用花色+點(diǎn)數(shù)的組合,存儲(chǔ)到Map集合中 for(String number : numbers) { for(String color : colors) { pooker.put(index, color + number); pookerNumber.add(index); index++; } } //System.out.println(pooker); //System.out.println(pookerNumber); //單獨(dú)存儲(chǔ)大王和小王 pooker.put(0, "大王"); pookerNumber.add(0); pooker.put(1, "小王"); pookerNumber.add(1); //洗牌,將牌的編號(hào)打亂 Collections.shuffle(pookerNumber); //System.out.println(pookerNumber); //發(fā)牌,將牌編號(hào),發(fā)給3個(gè)玩家集合+1個(gè)底牌集合 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> buttom = new ArrayList<Integer>(); //發(fā)牌,采用的是集合的索引%3 for(int i = 0; i < pookerNumber.size();i++) { //現(xiàn)將底牌做好 if(i<3) { //存到底牌去 buttom.add(pookerNumber.get(i)); //對(duì)索引%3判斷 }else if(i % 3 == 0) { //索引上的編號(hào),發(fā)給玩家1 player1.add(pookerNumber.get(i)); }else if(i % 3 == 1) { //發(fā)給玩家2 player2.add(pookerNumber.get(i)); }else if(i % 3 == 2) { //發(fā)給玩家3 player3.add(pookerNumber.get(i)); } } //對(duì)玩家手中的編號(hào)進(jìn)行排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); //看牌,就是將玩家手中的編號(hào),到Map集合中查找,根據(jù)鍵找值 //定義實(shí)現(xiàn)方法 look("包身工 ",player1,pooker); look("清潔工 ",player2,pooker); look("洗碗工 ",player3,pooker); look("底牌 ",buttom,pooker); } public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker) { //遍歷ArrayList集合,獲取元素,作為鍵,到集合Map中找值 System.out.print(name+" "); for(Integer key : player) { String value = pooker.get(key); System.out.print(value+" "); } System.out.println(); } }
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot數(shù)據(jù)訪問(wèn)和數(shù)據(jù)視圖的使用方式詳解
這篇文章主要為大家介紹了springboot數(shù)據(jù)訪問(wèn)和數(shù)據(jù)視圖的使用方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(17)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07開(kāi)發(fā)者在Idea 中常見(jiàn)的配置,你都了解嗎
idea這款java開(kāi)發(fā)工具真是好用無(wú)比,不僅好用而且界面也很好看,有黑白主題,功能強(qiáng)大配置簡(jiǎn)單,好了不多說(shuō)了,今天給大家羅列下Idea 中常見(jiàn)的配置,喜歡的朋友一起看看吧2021-06-06關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn),依賴注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說(shuō)你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07idea使用easyCode生成代碼(根據(jù)mybatis-plus模板創(chuàng)建自己的模板)
本文主要介紹了idea使用easyCode生成代碼,easyCode代碼生成器可以減少低價(jià)值搬磚,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10