久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java實現(xiàn)簡單的斗地主游戲

瀏覽:37日期:2022-09-02 14:51:21

本文實例為大家分享了Java實現(xiàn)斗地主游戲的具體代碼,供大家參考,具體內容如下

原理圖:

Java實現(xiàn)簡單的斗地主游戲

斗地主過程:

1、組合牌 * 2、洗牌 * 3、發(fā)牌 * 4、看牌

代碼實現(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集合,鍵:編號 值:牌 HashMap<Integer,String> pooker = new HashMap<Integer,String>(); //創(chuàng)建List集合,存儲編號 ArrayList<Integer> pookerNumber = new ArrayList<Integer>(); //定義13個點數(shù)的數(shù)組 String[] numbers = {'2','A','K','Q','J','10','9','8','7','6','5','4','3'}; //定義4個花色組合 String[] colors = {'♠','♣','♥','♦'}; //定義一個整數(shù)變量,作為Map的鍵 int index = 2; //遍歷數(shù)組,用花色+點數(shù)的組合,存儲到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); //單獨存儲大王和小王 pooker.put(0, '大王'); pookerNumber.add(0); pooker.put(1, '小王'); pookerNumber.add(1); //洗牌,將牌的編號打亂 Collections.shuffle(pookerNumber); //System.out.println(pookerNumber); //發(fā)牌,將牌編號,發(fā)給3個玩家集合+1個底牌集合 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)); //對索引%3判斷 }else if(i % 3 == 0) { //索引上的編號,發(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)); } } //對玩家手中的編號進行排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); //看牌,就是將玩家手中的編號,到Map集合中查找,根據(jù)鍵找值 //定義實現(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(); }}

運行結果:

包身工 大王 ♣2 ♥2 ♦2 ♦A ♦K ♣J ♥10 ♠9 ♣9 ♥9 ♦9 ♣5 ♦5 ♦4 ♠3 ♦3 清潔工 小王 ♠K ♥K ♠Q ♣Q ♦Q ♦J ♠10 ♦10 ♥8 ♠7 ♥7 ♦7 ♥5 ♠4 ♣3 ♥3 洗碗工 ♠2 ♥A ♣K ♥Q ♠J ♥J ♣10 ♠8 ♦8 ♣7 ♠6 ♣6 ♥6 ♦6 ♠5 ♣4 ♥4 底牌 ♠A ♣A ♣8

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關文章:
主站蜘蛛池模板: 弋阳县| 阳山县| 河北区| 青海省| 宁武县| 枣庄市| 延庆县| 琼中| 革吉县| 麻江县| 遂川县| 晋城| 仁化县| 乌拉特后旗| 合作市| 青州市| 京山县| 旬邑县| 德州市| 南川市| 南投市| 连江县| 刚察县| 阜新| 秦安县| 巢湖市| 闵行区| 晋江市| 岳普湖县| 广元市| 蓝山县| 阳西县| 平江县| 湘乡市| 崇明县| 忻城县| 响水县| 张北县| 平远县| 志丹县| 光泽县|