package com.lovo;
/**
* 枚举牌的花色
* @author 李睿
*
*/
public enum PaiSuite {
Wanzi,Tongzi,Tiaozi // 万字、筒子、条子
}
package com.lovo;
/**
* 牌类
* @author 李睿
*
*/
public class Pai {
private int num;
private PaiSuite suites;
/**
* 构造器
* @param num 牌的点数
* @param suites 牌的花色
*/
public Pai(int num, PaiSuite suites) {
this.num = num;
this.suites = suites;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public PaiSuite getSuites() {
return suites;
}
public void setSuites(PaiSuite suites) {
this.suites = suites;
}
@Override
public String toString() {
String str = "" + num;
switch(suites){
case Wanzi: str += "万"; break;
case Tiaozi: str += "条"; break;
case Tongzi: str += "筒"; break;
}
return str;
}
}
package com.lovo;
/**
* 麻将类
* @author 李睿
*
*/
public class Mahjong {
Pai[] pai = new Pai[108];
private int currentPosition = 0; // 记录牌的位置
/**
* 生成一副新的麻将
*/
public Mahjong(){
int[] faces = {1, 2, 3, 4, 5, 6, 7, 8, 9};
PaiSuite[] suites = {PaiSuite.Wanzi, PaiSuite.Tiaozi, PaiSuite.Tongzi};
for(int i = 0; i < suites.length; i++){ //麻将只有条子、万字和筒子
for(int j = 0; j < faces.length; j++){ // 每门牌有9种
for(int k = 0; k < 4; k++){ // 每种牌有相同的4张
pai[i * 36 + j * 4 + k] = new Pai(faces[j],suites[i] );
}
}
}
}
/**
* 洗牌
*/
public void shuffle() {
for (int i = 0; i < pai.length; i++){
int index = (int) (Math.random() * pai.length);
Pai temp = pai[i];
pai[i] = pai[index];
pai[index] = temp;
}
}
/**
* 发牌
* @return
*/
public Pai deal() {
return currentPosition < pai.length? pai[currentPosition++]: null;
}
}
package com.lovo;
/**
* 测试运行麻将类(洗牌和发牌方法)
* @author 李睿
*
*/
public class Text01 {
public static void main(String[] args) {
Mahjong m = new Mahjong();
m.shuffle(); // 洗牌
Pai p = null;
while((p = m.deal()) != null){
System.out.println(p + "\t");
}
}
}
麻将类
最新推荐文章于 2023-02-25 10:35:30 发布