-------Android培訓、java培訓
、期待與您交流!---------
鬥地主是人們經常上網玩的遊戲,玩這個遊戲通常需要先給每個玩家發牌,並且要留下三張撲克牌作爲底牌。那麼程序是如何實現發牌的呢?
思想:
實現發牌的話首先需要定義兩個數組,一個數組存儲花色,一個數組存儲牌的大小,一個HashMap集合對象用於存儲所有的撲克牌;
HashMap<Integer,String> hm = new HashMap<Integer,String>(); //定義HashMap變量用於存儲每張排的編號以及牌型
String[] colors = {"♤","♥","♣","♢"}; //定義數組存儲排的花色
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//定義數組存儲牌值
其次是 撲克牌生成之後需要做的是再定義一個ArrayList集合對象,用於存儲每張牌的編號並將編號與生成的牌裝入到HashMap集合對象中,在發牌時實際上發的是編號,再通過編號獲取對應的撲克牌;
ArrayList<Integer> array = new ArrayList<Integer>();//定義ArrayList變量存儲排的編號
for(String number : numbers){ //遍歷排值數組
for(String color : colors){ //遍歷花色
hm.put(index, color.concat(number));//將花色與牌值拼接,並將編號與拼接後的結果存儲到hm中
array.add(index); //將編號存儲到array中
index++;
}
}
/*
* 將小王和大王存儲到hm中
*/
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
再次是定義四個TreeMap集合的對象存儲三個玩家的牌以及底牌,使用TreeMap是因爲TreeMap是實現了排序;
TreeSet<Integer> playerOne = new TreeSet<Integer>();
TreeSet<Integer> PlayerTwo = new TreeSet<Integer>();
TreeSet<Integer> playerThree = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
接着就是發牌,當存儲好編號時,打亂ArrayList集合對象存儲的編號,再遍歷ArrayList集合獲得,在遍歷的時候,用編號的下標去除3取餘,餘數爲0,就將牌發給玩家1,餘數爲1就發給玩家2,餘數爲3就將牌發給玩家3,當下標超過51的時候就將牌作爲底牌;
Collections.shuffle(array); //調用Collections集合的shuffle()方法,將array中存儲的編號進行隨機的置換,即打亂順序
//遍歷編號的集合,實現發牌
for(int x = 0; x < array.size(); x++){
if(x >= array.size() - 3){
dipai.add(array.get(x));
}else if( x % 3 == 0){
playerOne.add(array.get(x));
}else if(x % 3 == 1){
PlayerTwo.add(array.get(x));
}else if(x % 3 == 2){
playerThree.add(array.get(x));
}
}
最後就是看牌,遍歷每個玩家的TreeMap對象集合以及底牌的集合。
定義一個看牌的方法,因爲每個玩家都需要看牌,所以爲了提高代碼的複用性,所以單獨定義一個看牌的方法
/**
* 遍歷每個玩家的牌以及底牌
* @param name
* @param ts
* @param hm
*/
public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.print(name+":\t"); //打印玩家名稱
for(Integer key : ts){ //遍歷玩家TreeSet集合,獲得玩家的牌的編號
String value = hm.get(key);//根據玩家牌編號獲取具體的牌值
System.out.print(value+" ");//打印
}
System.out.println();
}
}
以上就是分析的過程和主要代碼的實現,這個發牌的代碼主要就是利用了java中的集合去實現,HashMap是基於哈希表的,無序且唯一,TreeMap則是一種自平衡的紅黑樹,實現了排序的功能,而ArrayList是基於數組的,有序,可重複,效率高,線程不安全。另外就是利用了Collections類的方法。
完整代碼:
package cn.itheima03;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class PokerDemo {
public static void main(String[] args) {
HashMap<Integer,String> hm = new HashMap<Integer,String>(); //定義HashMap變量用於存儲每張排的編號以及牌型
ArrayList<Integer> array = new ArrayList<Integer>();//定義ArrayList變量存儲排的編號
String[] colors = {"♤","♥","♣","♢"}; //定義數組存儲排的花色
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//定義數組存儲牌值
int index = 0; //定義編號
for(String number : numbers){ //遍歷排值數組
for(String color : colors){ //遍歷花色
hm.put(index, color.concat(number));//將花色與牌值拼接,並將編號與拼接後的結果存儲到hm中
array.add(index); //將編號存儲到array中
index++;
}
}
/*
* 將小王和大王存儲到hm中
*/
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
Collections.shuffle(array); //調用Collections集合的shuffle()方法,將array中存儲的編號進行隨機的置換,即打亂順序
/*
* 定義四個TreeSet集合的變量用於存儲底牌編號以及三個玩家的牌的編號
* 採用TreeSet集合是因爲TreeSet集合可以實現自然排序
*/
TreeSet<Integer> playerOne = new TreeSet<Integer>();
TreeSet<Integer> PlayerTwo = new TreeSet<Integer>();
TreeSet<Integer> playerThree = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
//遍歷編號的集合,實現發牌
for(int x = 0; x < array.size(); x++){
if(x >= array.size() - 3){
dipai.add(array.get(x));
}else if( x % 3 == 0){
playerOne.add(array.get(x));
}else if(x % 3 == 1){
PlayerTwo.add(array.get(x));
}else if(x % 3 == 2){
playerThree.add(array.get(x));
}
}
lookPoker("底牌",dipai,hm);
lookPoker("huangshen",playerOne,hm);
lookPoker("xiequn",PlayerTwo,hm);
lookPoker("diannao",playerThree,hm);
}
/**
* 遍歷每個玩家的牌以及底牌
* @param name
* @param ts
* @param hm
*/
public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.print(name+":\t"); //打印玩家名稱
for(Integer key : ts){ //遍歷玩家TreeSet集合,獲得玩家的牌的編號
String value = hm.get(key);//根據玩家牌編號獲取具體的牌值
System.out.print(value+" ");//打印
}
System.out.println();
}
}