最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)斗地主案例

 更新時(shí)間:2020年04月22日 10:00:26   作者:-_Coco_  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)斗地主案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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();
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一篇文章搞定數(shù)據(jù)庫(kù)連接池

    一篇文章搞定數(shù)據(jù)庫(kù)連接池

    數(shù)據(jù)庫(kù)連接池在編寫(xiě)應(yīng)用服務(wù)是經(jīng)常需要用到的模塊,太過(guò)頻繁的連接數(shù)據(jù)庫(kù)對(duì)服務(wù)性能來(lái)講是一個(gè)瓶頸,使用緩沖池技術(shù)可以來(lái)消除這個(gè)瓶頸,本文就來(lái)介紹Java常見(jiàn)的幾種,感興趣的可以了解一下
    2021-07-07
  • 詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問(wèn)題

    詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問(wèn)題

    本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • MybatisPlus中的save方法詳解

    MybatisPlus中的save方法詳解

    save方法是Mybatis-plus框架提供的一個(gè)添加記錄的方法,它用于將一個(gè)實(shí)體對(duì)象插入到數(shù)據(jù)庫(kù)表中,這篇文章主要介紹了MybatisPlus中的save方法,需要的朋友可以參考下
    2023-11-11
  • java性能優(yōu)化四種常見(jiàn)垃圾收集器匯總

    java性能優(yōu)化四種常見(jiàn)垃圾收集器匯總

    這篇文章主要介紹了java性能優(yōu)化四種常見(jiàn)垃圾收集器匯總,每種垃圾收集器都有其不同的算法實(shí)現(xiàn)和步驟,下面我們簡(jiǎn)單描述下我們常見(jiàn)的四種垃圾收集器的算法過(guò)程,感興趣的同學(xué)們最好先看下以下的兩篇文章去增加理解
    2022-07-07
  • java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

    java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

    這篇文章主要為大家詳細(xì)介紹了java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Spring處理@Async導(dǎo)致的循環(huán)依賴失敗問(wèn)題的方案詳解

    Spring處理@Async導(dǎo)致的循環(huán)依賴失敗問(wèn)題的方案詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot中的@Async導(dǎo)致循環(huán)依賴失敗的原因及其解決方案,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2022-07-07
  • Java8 Lambda表達(dá)式詳解及實(shí)例

    Java8 Lambda表達(dá)式詳解及實(shí)例

    這篇文章主要介紹了Java8 Lambda表達(dá)式詳解的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • J2SE基礎(chǔ)之在Eclipse中運(yùn)行hello world

    J2SE基礎(chǔ)之在Eclipse中運(yùn)行hello world

    本文的內(nèi)容非常的簡(jiǎn)單,跟隨世界潮流,第一個(gè)Java程序輸出“Hell World!”。希望大家能夠喜歡
    2016-05-05
  • Java中各種集合判空方法總結(jié)

    Java中各種集合判空方法總結(jié)

    最近接觸集合比較多,經(jīng)常對(duì)于集合是否為空做判斷,下面這篇文章主要給大家介紹了關(guān)于Java中各種集合判空方法總結(jié)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • IDEA控制臺(tái)日志中文亂碼解決方案(好用!)

    IDEA控制臺(tái)日志中文亂碼解決方案(好用!)

    這篇文章主要給大家介紹了關(guān)于IDEA控制臺(tái)日志中文亂碼解決的相關(guān)資料,平常的開(kāi)發(fā)中,我們通常會(huì)用到日志打印進(jìn)行開(kāi)發(fā),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07

最新評(píng)論

嘉鱼县| 墨江| 台南市| 阆中市| 句容市| 宁蒗| 独山县| 若羌县| 连山| 绥中县| 梧州市| 资源县| 德钦县| 宣威市| 广灵县| 鲁山县| 青州市| 维西| 夏河县| 平乐县| 桂阳县| 杂多县| 连山| 信阳市| 获嘉县| 迁安市| 鹤岗市| 林周县| 上蔡县| 岳阳市| 财经| 外汇| 新平| 呈贡县| 当涂县| 荆州市| 普兰店市| 梅河口市| 丘北县| 恭城| 苏尼特右旗|