中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

有心學(xué)做網(wǎng)站東莞網(wǎng)站設(shè)計排行榜

有心學(xué)做網(wǎng)站,東莞網(wǎng)站設(shè)計排行榜,做qq頭像的網(wǎng)站,phpnow 搭建網(wǎng)站目錄 前言: 1.初始化牌 2.洗牌 3.揭牌 總代碼: Card類: CardGame類: Main類: 結(jié)語: 前言: 斗地主是全國范圍內(nèi)的一種桌面游戲,本節(jié)我們來實現(xiàn)一下斗地主中的簡單初始化牌、…

目錄

前言:

1.初始化牌

?2.洗牌

3.揭牌

總代碼:

Card類:

CardGame類:

Main類:

結(jié)語:


前言:

斗地主是全國范圍內(nèi)的一種桌面游戲,本節(jié)我們來實現(xiàn)一下斗地主中的簡單初始化牌、發(fā)牌和看牌功能。按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。具體規(guī)則為使用 52?張牌(不含大小王)打亂順序,3 個玩家參與游戲,3 人交替摸牌,每人 5(可以自己修改)?張牌。

實現(xiàn)思路步驟:

1.初始化牌

2.洗牌

3.揭牌

4.剩余牌

效果展示:

實現(xiàn)如下:

1.初始化牌

牌有數(shù)字和花色,故我們要自己創(chuàng)建一個類來進(jìn)行操作。toString方法要記得重寫不然后面println不能直接輸出。

public class Card{public String suit;//花色public int num;public Card(String suit,int num){this.suit = suit;this.num = num;}@Overridepublic String toString(){return suit+" "+num;}
}

用suits來存儲花色。List<Card>可以看成是一個存儲Card的一維數(shù)組。利用java自帶的ArrayLIst的add函數(shù)實現(xiàn)尾插。?

public static final String[] suits = {"?","?","?","?"};//初始化牌public List<Card> buyCard(){List<Card> cardList = new ArrayList<>();for(int i = 0;i < 4;i++){for(int j = 1;j <= 13;j++){String suit = suits[i];Card card = new Card(suit,j);cardList.add(card);}}return cardList;}

?效果如下:

?2.洗牌

利用random來生成隨機數(shù),這里有個小細(xì)節(jié)i是從最后一個數(shù)來進(jìn)行遍歷的利用rand來生成i之前的隨機數(shù)進(jìn)行交換這樣就能保證每個牌都有被洗到。

//洗牌public void shuffle(List<Card> cardList){Random random = new Random();for(int i = cardList.size()-1;i > 0;i--){swap(cardList,i,random.nextInt(i));}}public void swap(List<Card> cardList,int i,int j){Card temp = cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,temp);}

效果如下:

3.揭牌

這里解釋一下List<List<Card>>,可以看成一個二維數(shù)組如圖,List<Card>是一個泛型,最外面的List里面存儲了一個List<Card>。

//揭牌public List<List<Card>> getCard(List<Card> cardList){List<Card> hand1 = new ArrayList<>();List<Card> hand2 = new ArrayList<>();List<Card> hand3 = new ArrayList<>();List<List<Card>> hand = new ArrayList<>();hand.add(hand1);hand.add(hand2);hand.add(hand3);for(int i = 0;i < 5;i++){for(int j = 0;j < 3;j++){Card card = cardList.remove(0);hand.get(j).add(card);}}return hand;}

總代碼:

Card類:

public class Card{public String suit;//花色public int num;public Card(String suit,int num){this.suit = suit;this.num = num;}@Overridepublic String toString(){return suit+" "+num;}
}

CardGame類:

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class CardGame{public static final String[] suits = {"?","?","?","?"};//初始化牌public List<Card> buyCard(){List<Card> cardList = new ArrayList<>();for(int i = 0;i < 4;i++){for(int j = 1;j <= 13;j++){String suit = suits[i];Card card = new Card(suit,j);cardList.add(card);}}return cardList;}//洗牌public void shuffle(List<Card> cardList){Random random = new Random();for(int i = cardList.size()-1;i > 0;i--){swap(cardList,i,random.nextInt(i));}}public void swap(List<Card> cardList,int i,int j){Card temp = cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,temp);}//揭牌public List<List<Card>> getCard(List<Card> cardList){List<Card> hand1 = new ArrayList<>();List<Card> hand2 = new ArrayList<>();List<Card> hand3 = new ArrayList<>();List<List<Card>> hand = new ArrayList<>();hand.add(hand1);hand.add(hand2);hand.add(hand3);for(int i = 0;i < 5;i++){for(int j = 0;j < 3;j++){Card card = cardList.remove(0);hand.get(j).add(card);}}return hand;}
}

Main類:

import java.util.List;
public class Main {public static void main(String[] args){CardGame cardgame = new CardGame();List<Card> ret = cardgame.buyCard();System.out.println("初始化牌");System.out.println(ret.subList(0,13));System.out.println(ret.subList(13,26));System.out.println(ret.subList(26,39));System.out.println(ret.subList(39,52));System.out.println("洗牌");cardgame.shuffle(ret);System.out.println(ret.subList(0,13));System.out.println(ret.subList(13,26));System.out.println(ret.subList(26,39));System.out.println(ret.subList(39,52));System.out.println("揭牌");List<List<Card>> hand = cardgame.getCard(ret);for(int i = 0;i < 3;i++){System.out.println("第 "+(i+1)+"個人的牌:"+hand.get(i));}System.out.println("剩下的牌:");System.out.println(ret);}
}

結(jié)語:

其實寫博客不僅僅是為了教大家,同時這也有利于我鞏固自己的知識點,和一個學(xué)習(xí)的總結(jié),由于作者水平有限,對文章有任何問題的還請指出,接受大家的批評,讓我改進(jìn),如果大家有所收獲的話還請不要吝嗇你們的點贊收藏和關(guān)注,這可以激勵我寫出更加優(yōu)秀的文章。

http://www.risenshineclean.com/news/65878.html

相關(guān)文章:

  • 黃石手機網(wǎng)站建設(shè)東莞營銷網(wǎng)站建設(shè)優(yōu)化
  • 南海網(wǎng)站智能推廣線上推廣的渠道和方法
  • 怎樣用網(wǎng)站模板做網(wǎng)站高傭金app軟件推廣平臺
  • 坪山網(wǎng)站建設(shè)信息房地產(chǎn)銷售
  • 知名設(shè)計網(wǎng)站公司站長之家域名查詢排行
  • 網(wǎng)站關(guān)鍵詞排名沒有了平臺推廣怎么做
  • 游樂場網(wǎng)站開發(fā)百度快速收錄網(wǎng)站
  • 怎么做屬于自己的售卡網(wǎng)站鄭州網(wǎng)絡(luò)營銷策劃
  • 唐山專業(yè)網(wǎng)站建設(shè)公司活動推廣方案怎么寫
  • 成品網(wǎng)站建設(shè)咨詢seo必備軟件
  • 深圳市專業(yè)制作網(wǎng)站公司嗎互聯(lián)網(wǎng)廣告代理
  • 南京個人做網(wǎng)站百度指數(shù)專業(yè)版app
  • ppt可以做網(wǎng)站嗎合肥網(wǎng)絡(luò)推廣優(yōu)化公司
  • 城關(guān)區(qū)建設(shè)局網(wǎng)站自媒體營銷方式有哪些
  • 西安做網(wǎng)站優(yōu)化教育培訓(xùn)機構(gòu)十大排名
  • 網(wǎng)展企業(yè)網(wǎng)站系統(tǒng) 免費沈陽網(wǎng)站推廣優(yōu)化
  • 濟(jì)源做網(wǎng)站公司bt螞蟻磁力搜索天堂
  • 學(xué)院網(wǎng)站建設(shè)工作總結(jié)網(wǎng)絡(luò)銷售是做什么的
  • 企業(yè)網(wǎng)站建設(shè)多長時間seo網(wǎng)站推廣專員招聘
  • 商務(wù)部網(wǎng)站建設(shè)情況匯報360搜索引擎推廣
  • 網(wǎng)站建設(shè)受眾軟文營銷案例分析
  • 商務(wù)網(wǎng)站開發(fā)設(shè)計搜什么關(guān)鍵詞能搜到好片
  • wordpress如何修改評論北京seo網(wǎng)站優(yōu)化培訓(xùn)
  • 東莞做網(wǎng)站優(yōu)化哪家好app開發(fā)自學(xué)教程
  • 福州市建設(shè)廳網(wǎng)站谷歌搜索指數(shù)查詢
  • 怎么做免費網(wǎng)站如何讓百度收錄資源最全的網(wǎng)盤搜索引擎
  • 國內(nèi)網(wǎng)站建設(shè)費用聯(lián)盟谷粉搜索谷歌搜索
  • 網(wǎng)站的表單關(guān)鍵字優(yōu)化用什么系統(tǒng)
  • 宣城市網(wǎng)站建設(shè)平臺推廣營銷
  • 網(wǎng)站開發(fā)工程師應(yīng)聘書范文1000有沒有永久免費crm