麻将一组有1-9筒,1-9条,1-9万,中发白,东南西北,34张牌,共有四组,共136张牌。
可以通过一个数组来表示则一组牌,代码如下:
private final static String[] mahjong = { "1T", "2T", "3T", "4T", "5T",
"6T", "7T", "8T", "9T", "1S", "2S", "3S", "4S", "5S", "6S", "7S",
"8S", "9S", "1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W",
"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI" };
将一组麻将循环4次,共得到136张麻将,麻将随机洗牌算法如下:
/**
* pengweikang 20180716 随机洗牌次数
* @param cards 总牌数
* @param times 洗牌次数
*/
public static void shuffle(int [] cards,int times){
for(int j = 0; j <= times; j ++)
for(int i = cards.length; i > 1; i --){
int temp_i = rd.nextInt(i);
int value = cards[temp_i];
cards[temp_i] = cards[i - 1];
cards[i - 1] = value;
}
}
获取一副洗好牌后的麻将代码如下:
/**
* pengweiakng 20180714 获取已经洗好牌的麻将
*/
private static int[] getMajiang(){
int majiang [] = new int[136];
for(int i = 0; i < 4; i++){
for(int j = 0; j < mahjong.length; j++){
majiang[j+(i*mahjong.length)] = j;
}
}
/**
* 默认洗牌5次
*/
shuffle(majiang,5);
return majiang;
}
按照用户人数进行洗麻将代码如下:
/**
* pengweikang 20180714 通过用户数量进行洗牌
* @param users 用户数
* @return
*/
public static List getBlocks(int users) throws Exception{
if(users > 4){
throw new Exception("用户数量不能大于4人");
}
List blockList = new ArrayList<>();
int [] majiang = getMajiang();
/**
* blockSize为每个人分配的麻将数量
*/
int blockSize = majiang.length/users;
int [] leftData = new int[blockSize];
for(int i = 0; i < majiang.length; i++){
if((i+1)%blockSize == 0){
leftData[i%blockSize] = majiang[i];
Block block = new Block();
block.setLeftData(leftData);
blockList.add(block);
leftData = new int[blockSize];
}else{
leftData[i%blockSize] = majiang[i];
}
}
return blockList;
}
执行结果如下:
初始化麻将:
2W,9W,9T,1W,6W,1W,2S,FA,1S,9T,5T,2T,3S,4T,9T,3W,6W,4S,5W,FA,2T,8T,XI,9S,BEI,6S,8W,7S,XI,XI,5T,9S,1S,5T,
8W,5T,5W,5W,8T,8T,6T,7T,4W,6S,XI,1T,4W,9W,BAI,FA,5S,1T,BEI,8S,1W,4W,2W,4S,6W,8W,9W,6T,7S,9W,DONG,BAI,8T,2T,
9S,2S,BEI,2S,9T,ZHONG,8S,5W,NAN,BAI,ZHONG,1W,6W,5S,7T,NAN,NAN,1T,7S,DONG,7W,DONG,6T,8S,5S,1S,4W,FA,DONG,6T,6S,ZHONG,3T,7T,
2S,7W,3T,NAN,BAI,8S,2T,5S,8W,1S,3S,9S,3W,7W,6S,7T,3W,7W,3T,1T,4T,2W,4T,4T,4S,3S,3T,ZHONG,2W,3W,4S,3S,BEI,7S,
振骰子玩法:庄家通过自己振骰子的点数找到第二次骰筛子的人,例如庄家振骰子点数为7,从庄家自己开始顺时针数到7的用户为第二次振骰子的人,显然是庄家对面的玩家了,然后第二个人开始振骰子。
抓拍玩法:第二个人振完骰子后,将两次振骰子的点数进行相加,然后庄家从第二次振骰子的玩家麻将区从左到右数完两次振的骰子点数后开始抓拍,庄家抓完后有庄家下家进行抓拍,没人每次抓4张。抓3次,每人一共抓12张牌,之后庄家抓两张牌,其他玩家依次抓1张牌,抓完后庄家14张牌,其他玩家13张牌。
代码执行结果如下:
-1代码空的麻将位,表示已经抓过的麻将位。
初始化麻将:
2W,9W,9T,1W,6W,1W,2S,FA,1S,9T,5T,2T,3S,4T,9T,3W,6W,4S,5W,FA,2T,8T,XI,9S,BEI,6S,8W,7S,XI,XI,5T,9S,1S,5T,
8W,5T,5W,5W,8T,8T,6T,7T,4W,6S,XI,1T,4W,9W,BAI,FA,5S,1T,BEI,8S,1W,4W,2W,4S,6W,8W,9W,6T,7S,9W,DONG,BAI,8T,2T,
9S,2S,BEI,2S,9T,ZHONG,8S,5W,NAN,BAI,ZHONG,1W,6W,5S,7T,NAN,NAN,1T,7S,DONG,7W,DONG,6T,8S,5S,1S,4W,FA,DONG,6T,6S,ZHONG,3T,7T,
2S,7W,3T,NAN,BAI,8S,2T,5S,8W,1S,3S,9S,3W,7W,6S,7T,3W,7W,3T,1T,4T,2W,4T,4T,4S,3S,3T,ZHONG,2W,3W,4S,3S,BEI,7S,
庄家是:3
第一次摇撒子点数:3
第二次摇撒子点数:11
宝为:2W
剩余麻将:
2W,9W,9T,1W,6W,1W,2S,FA,1S,9T,5T,2T,3S,4T,9T,3W,6W,4S,5W,FA,2T,8T,XI,9S,BEI,6S,8W,7S,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,5S,7T,NAN,NAN,1T,7S,DONG,7W,DONG,6T,8S,5S,1S,4W,FA,DONG,6T,6S,ZHONG,3T,7T,
2S,7W,3T,NAN,BAI,8S,2T,5S,8W,1S,3S,9S,3W,7W,6S,7T,3W,7W,3T,1T,4T,2W,4T,4T,4S,3S,3T,ZHONG,2W,3W,4S,3S,BEI,7S,
用户洗牌麻将:
-1,8T,8T,2S,2S,8S,9S,1W,4W,5W,5W,BEI,BEI,ZHONG,
-1,6T,7T,9T,4S,6S,8S,1W,2W,4W,5W,6W,8W,ZHONG,
1T,5T,6T,7S,9S,4W,6W,9W,9W,9W,NAN,XI,XI,XI,
-1,1T,2T,5T,5T,8T,1S,5S,8W,DONG,FA,BAI,BAI,BAI,
算法如下:
/**
* pengweikang 20180716 初始化麻将
* @param blockList 洗好的麻将
* @param banker 庄家索引
*/
public static void matchCard(List blockList,int banker) {
System.out.println("庄家是:"+(banker+1));
/**
* 摇一摇撒子,进行摇点数,第一次
*/
int random1 = rd.nextInt(12) + 1;
System.out.println("第一次摇撒子点数:"+random1);
/**
* 摇一摇撒子,进行摇点数,第二次
*/
int random2 = rd.nextInt(12) + 1;
System.out.println("第二次摇撒子点数:"+random2);
/**
* 第一次摇骰子选中的用户序号
*/
int usernum = (random1 + banker - 1) % blockList.size() ;
/**
* 正骰子总点数
*/
int randomSum = random1 + random2;
int start = banker; //庄家所属位置
int times = 0; //抓取次数问题
int usertimes = 0; //用户抓取次数抓取次数问题
int bao = -1;
int[] leftData = blockList.get(usernum).getLeftData(); //从哪个用户麻将区域进行抓牌
if (randomSum >= (leftData.length / 2)) { //判断骰子点数是否大于该人的麻将总数
randomSum = randomSum - leftData.length / 2;
blockList.get(usernum).setGhostIndex(randomSum*2); //设置宝的索引位置
bao = leftData[randomSum*2];
usernum++;
if (usernum > 3) {
usernum = 0;
}
}else{
int tempindex = usernum;
tempindex--;
if(tempindex == -1){
tempindex = blockList.size() - 1;
}
blockList.get(tempindex).setGhostIndex(randomSum*2); //设置宝的索引位置
leftData = blockList.get(tempindex).getLeftData();
bao = leftData[randomSum*2];
}
System.out.println("宝为:"+mahjong[bao]);
while (true) {
leftData = blockList.get(usernum).getLeftData(); //从哪个用户麻将区域进行抓牌
if(usertimes == 2){ //一个用户抓取两次
usertimes = 0;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
if(start == banker){
times++;
}
}
if(times != 3){
int[] data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2];
data[times * 4 + usertimes*2 + 1] = leftData[randomSum * 2 + 1];
leftData[randomSum * 2] = -1;
leftData[randomSum * 2 + 1] = -1;
randomSum++;
usertimes++;
}else{
if(randomSum == 15){
int[] data = blockList.get(start).getData();
int tempusernum = usernum + 1;
if (tempusernum > blockList.size() - 1) {
tempusernum = 0;
}
int[] leftData1 = blockList.get(tempusernum).getLeftData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2];
leftData[randomSum * 2] = -1;//抓牌后麻将设置成-1
data[times * 4 + usertimes*2 + 1] = leftData1[0];
leftData1[0] = -1;//抓牌后麻将设置成-1
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 1];
leftData[randomSum * 2 + 1] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 2];
leftData[randomSum * 2 + 2] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 3];
leftData[randomSum * 2 + 3] = -1;
blockList.get(tempusernum).setCardIndex(1); //设置抓拍索引
}else if(randomSum == 16){
int[] data = blockList.get(start).getData();
int tempusernum = usernum + 1;
if (tempusernum > blockList.size() - 1) {
tempusernum = 0;
}
int[] leftData1 = blockList.get(tempusernum).getLeftData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2];
leftData[randomSum * 2] = -1;
data[times * 4 + usertimes*2 + 1] = leftData1[2];
leftData1[2] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 1];
leftData[randomSum * 2 + 1] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData1[0];
leftData1[0] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData1[1];
leftData1[1] = -1;
blockList.get(tempusernum).setCardIndex(3); //设置抓拍索引
}else{
int[] data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2];
leftData[randomSum * 2] = -1;
data[times * 4 + usertimes*2 + 1] = leftData[randomSum * 2 + 4];
leftData[randomSum * 2 + 4] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 1];
leftData[randomSum * 2 + 1] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 2];
leftData[randomSum * 2 + 2] = -1;
start++;
if (start > blockList.size() - 1) {
start = 0;
}
data = blockList.get(start).getData();
data[times * 4 + usertimes*2 ] = leftData[randomSum * 2 + 3];
leftData[randomSum * 2 + 3] = -1;
blockList.get(usernum).setCardIndex(randomSum * 2 + 5);//设置抓牌的索引
}
break;
}
if (randomSum > 16) { //判断当前用户的麻将是否分配完,如果分配完了就进行下一个人麻将进行分配
randomSum = 0;
usernum++;
if (usernum > 3) {
usernum = 0;
}
}
}
for(Block block : blockList){
Arrays.sort(block.getData());
}
}