java內(nèi)的Collections類(lèi)自帶了一個(gè)shuffle洗牌算法。
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)注于成都網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),樺南網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:樺南等地區(qū)。樺南做網(wǎng)站價(jià)格咨詢:18982081108
static void shuffle(List? list)
使用默認(rèn)隨機(jī)源對(duì)指定列表進(jìn)行置換。
static void shuffle(List? list, Random rnd)
使用指定的隨機(jī)源對(duì)指定列表進(jìn)行置換。
現(xiàn)在你可以把52張牌放進(jìn)一個(gè)List里,調(diào)用他的shuffle算法打亂順序。
1.定義牌類(lèi)
public class Card{
public int id;//牌大小
public String number;//牌值名稱
public String color;//花色
}
2.定義牌組,用ArrayList存放所有牌
ArrayListCard cardGroup =new ArrayListCard();
3.定義花色枚舉
public static final String[] COLORS=new String[]{"方塊","梅花","紅桃","黑桃"};
4.定義牌值大小枚舉
public static fina String[] CARDVALUES=new String[]{"A","1","2","3",
"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};
5.初始化牌組,這就是一副新牌,沒(méi)切過(guò),己經(jīng)排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i13;i++){
for (int j=0;j4;j++){
//生成一張牌的對(duì)象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//將對(duì)象加入牌組
cardGroup.add(card);
}
}
//加入大小王
Card card_k1=new Card();
card_k1.id=13;
card_k1.color="King_Small";
card_k1.number="King_Small";
cardGroup.add(card_k1);
Card card_k2=new Card();
card_k2.id=14;
card_k2.color="King_big";
card_k2.number="King_big";
cardGroup.add(card_k2);
}
6. 洗牌,將原牌組打亂,得到新的牌組
public ArrayListCard flushCardsGroup(ArrayListCard src){
ArrayListCard result=new ArrayListCard();
while(src.size0){
int size=src.size();
//以size為限,生成0~size之間的隨機(jī)數(shù)
Random r=new Random();
int index=r.nextInt(size);
//對(duì)index做個(gè)檢查
if(index0){index=0;}
if(indexsize-1){index=size-1;}
//index就是撿出來(lái)的牌,加到新的數(shù)組中,從原來(lái)數(shù)組中刪除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.HashMap;
import?java.util.TreeSet;
/*
*?思路:
*? A:創(chuàng)建一個(gè)HashMap集合
*? B:創(chuàng)建一個(gè)ArrayList集合
*? C:創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組
*? D:從0開(kāi)始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對(duì)應(yīng)的牌
*????????同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。
*??????E:洗牌(洗的是編號(hào))
*??????F:發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)
*??????G:看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對(duì)應(yīng)的牌)
*/
public?class?PokerDemo?{
public?static?void?main(String[]?args)?{
//?創(chuàng)建一個(gè)HashMap集合
HashMapInteger,?String?hm?=?new?HashMapInteger,?String();
//?創(chuàng)建一個(gè)ArrayList集合
ArrayListInteger?array?=?new?ArrayListInteger();
//?創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組
//?定義一個(gè)花色數(shù)組
String[]?colors?=?{?"?",?"?",?"?",?"?"?};
//?定義一個(gè)點(diǎn)數(shù)數(shù)組
String[]?numbers?=?{?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",?"J",?"Q",
"K",?"A",?"2",?};
//?從0開(kāi)始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對(duì)應(yīng)的牌,同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。
int?index?=?0;
for?(String?number?:?numbers)?{
for?(String?color?:?colors)?{
String?poker?=?color.concat(number);
hm.put(index,?poker);
array.add(index);
index++;
}
}
hm.put(index,?"小王");
array.add(index);
index++;
hm.put(index,?"大王");
array.add(index);
//?洗牌(洗的是編號(hào))
Collections.shuffle(array);
//?發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)
TreeSetInteger?fengQingYang?=?new?TreeSetInteger();
TreeSetInteger?linQingXia?=?new?TreeSetInteger();
TreeSetInteger?liuYi?=?new?TreeSetInteger();
TreeSetInteger?diPai?=?new?TreeSetInteger();
for?(int?x?=?0;?x??array.size();?x++)?{
if?(x?=?array.size()?-?3)?{
diPai.add(array.get(x));
}?else?if?(x?%?3?==?0)?{
fengQingYang.add(array.get(x));
}?else?if?(x?%?3?==?1)?{
linQingXia.add(array.get(x));
}?else?if?(x?%?3?==?2)?{
liuYi.add(array.get(x));
}
}
//?看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對(duì)應(yīng)的牌)
lookPoker("風(fēng)清揚(yáng)",?fengQingYang,?hm);
lookPoker("林青霞",?linQingXia,?hm);
lookPoker("劉意",?liuYi,?hm);
lookPoker("底牌",?diPai,?hm);
}
//?寫(xiě)看牌的功能
public?static?void?lookPoker(String?name,?TreeSetInteger?ts,
HashMapInteger,?String?hm)?{
System.out.print(name?+?"的牌是:");
for?(Integer?key?:?ts)?{
String?value?=?hm.get(key);
System.out.print(value?+?"?");
}
System.out.println();
}
}
import?java.util.*;
public?class?Main?{
public?static?void?main(String[]?args)?{
Scanner?scanner?=?new?Scanner(System.in);
ListString?list?=?new?ArrayListString();
while?(scanner.hasNextLine())?{
String?string?=?scanner.nextLine();
list.add(string);
if?(list.size()?==?5)?{
int?r?=?getCard(list);
System.out.println(r);
list.clear();
}
}
}
private?static?int?getCard(ListString?list)?{
int?temp?=?7;
int?size?=?list.size();
ListString?numList?=?new?ArrayListString();
SetString?numSet?=?new?TreeSetString();
ListString?colourList?=?new?ArrayListString();
SetString?colourSet?=?new?TreeSetString();
for?(int?i?=?0;?i??list.size();?i++)?{
String?num?=?list.get(i).split("?")[0];
String?colour?=?list.get(i).split("?")[1];
if?(num.equals("J"))?{
numList.add("11");
numSet.add("11");
}?else?if?(num.equals("Q"))?{
numList.add("12");
numSet.add("12");
}?else?if?(num.equals("K"))?{
numList.add("13");
numSet.add("13");
}?else?if?(num.equals("A"))?{
numList.add("14");
numSet.add("14");
}?else?{
numList.add(num);
numSet.add(num);
}
colourList.add(colour);
colourSet.add(colour);
}
//?同花順,或者同花
if?(colourSet.size()?==?1)?{
Collections.sort(numList);
for?(int?i?=?0;?i??numList.size()?-?1;?i++)?{
if?(Integer.parseInt(numList.get(i?+?1))?-?Integer.parseInt(numList.get(i))?==?1)?{
if?(i?==?numList.size()?-?2)?{
temp?=?1;
}
continue;
}?else?{
if?(temp??4)?{
temp?=?4;
}
}
}
}
//?四條或者葫蘆
if?(numSet.size()?==?2)?{
Collections.sort(numList);
//?四條
if?(!numList.get(0).equals(numList.get(1))?||?!numList.get(size?-?1).equals(numList.get(size?-?2)))?{
if?(temp??2)?{
temp?=?2;
}
}?else?{
if?(temp??3)?{
temp?=?3;
}
}
}
//?三條
if?(numSet.size()?==?3)?{
if?(temp??6)?{
temp?=?6;
}
}
//?順子
if?(colourSet.size()??1)?{
Collections.sort(numList);
for?(int?i?=?0;?i??numList.size()?-?1;?i++)?{
if?(Integer.parseInt(numList.get(i?+?1))?-?Integer.parseInt(numList.get(i))?==?1)?{
if?(i?==?numList.size()?-?2)?{
if(temp5){
temp?=?5;
}
}
}?else?{
break;
}
}
}
return?temp;
}
}
一:import java.util.Scanner;
public class Play {
/**
* 玩牌
*/
public static void main(String[] args) {
Poker poker = new Poker();
boolean over = false;
Scanner cin=new Scanner(System.in);
while(!over){
System.out.println("打牌輸入1,展示剩余牌輸入其他字符:");
String c=cin.nextLine();
if("1".equals(c)){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
}else{
poker.ShowPages();
}
}
/*for(int i = 0;i54;i++){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
poker.ShowPages();
}
*/
}
}二://牌
public class Page { private String huase = "";
private String haoma = "";
public Page(String huase,String haoma){
this.huase = huase;
this.haoma = haoma;
}
public String show(){
return this.huase+this.haoma;
}
}三:import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一副撲克
*/
public class Poker {
private List pages = new ArrayList();
public Poker(){
String huase[] = new String[]{"黑桃","紅桃","草花","方塊"};
String haoma[] = new String[]{"A","2","3","4","5","6","7","8","9","10"
,"J","Q","K"};
for(int i = 0;ihaoma.length;i++){
for(int j = 0;jhuase.length;j++){
pages.add(new Page(huase[j],haoma[i]));
}
}
pages.add(new Page("","大王"));
pages.add(new Page("","小王"));
}
public void ShowPages(){
System.out.println("展示剩余牌:");
for(Iterator i = pages.iterator();i.hasNext();){
System.out.println(((Page)i.next()).show());
}
}
//打牌
public void remove(int index){
pages.remove(index);
}
//剩余牌數(shù)
public int getSize(){
return pages.size();
}
}
public class Test {
public static void main(String args[]){
int puke[][] = new int[52][2];
for(int i = 0 ; i 52; i++){
puke[i][0] = i+1;//牌面
puke[i][1] = 0;//0表示正面,1表示背面
}
for(int i = 2; i = 52 ;i++){
for(int j = 0 ; j 52; j++){
if(puke[j][0]%i==0){
puke[j][1]=Math.abs(puke[j][1]-1);//1為0,0為1;
}
}
}
for(int i = 0 ; i 52; i++){
if(puke[i][1]==0)//為正面打印
System.out.println(puke[i][0]);
}
}
}
結(jié)果為:1,4,9,16,25,36,49
網(wǎng)頁(yè)標(biāo)題:java代碼實(shí)現(xiàn)紙牌算法 java麻將算法
文章來(lái)源:http://www.rwnh.cn/article18/hiihgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、服務(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)