public class Game {
int results[][] = new int[6][5];//记录骰子结果
int spare[][] = new int[6][7];//计数各点数出现次数
Card[] card = new Card[6];
private static class kindOfCard
{
static int baozi =8;
static int shunzi =7;
static int tiezhi =6;
static int hulu =5;
static int santiao =4;
static int liangdui =3;
static int yidui =2;
static int sanpai =1;
}
class Card
{
public int player;
public int kind;
public int score;
}
public Game()
{
play();
initSpare();
}
private void play()
{
for(int i=0;i<6;i++)
{
FiveDice fivedice = new FiveDice();
results[i] = fivedice.getDices();
}
}
private void initSpare()
{
for(int i=0; i<6;i++)
for(int j=0;j<7;j++)
spare[i][j]=0;
}
private void getSpare()
{
for(int i=0;i<6;i++)
{
for(int j=0;j<5;j++)
{
this.spare[i][this.results[i][j]]++;
}
}
}
private void judge()
{
again:
for(int i=0;i<6;i++)
{
card[i] = new Card();
card[i].player =i+1;
for(int j=1;j<7;j++)// 根据累计的点数归类
{
if(this.spare[i][j]==5)//豹子
{
card[i].kind = kindOfCard.baozi;
card[i].score = getRankScore(j);
continue again;
}
else if(this.spare[i][j]==4)//铁支
{
card[i].kind= kindOfCard.tiezhi ;
for(int k=1;k<7;k++)
if(this.spare[i][k]==1)
{
card[i].score = getRankScore(j)*10
+getRankScore(k);
continue again;
}
}
else if(this.spare [i][j]==3)//三个同点数
{
for(int k=1;k<7;k++)
if(this.spare[i][k]==2)//葫芦
{
card[i].kind = kindOfCard.hulu ;
card[i].score = getRankScore(j)*10
+getRankScore(k);
continue again;
}
else if(this.spare[i][k]==1)//三条
{
for(int l=1;l<7;l++)
if(l!=k&&this.spare[i][l]==1)
{
card[i].kind = kindOfCard.santiao;
int a = getRankScore(k),b=getRankScore(l);
if(a>b)
card[i].score = getRankScore(j)*100+a*10+b;
else
card[i].score = getRankScore(j)*100+b*10+a;
continue again;
}
}
}
else if(this.spare[i][j]==2)//两个点数相同
{
for(int k=1;k<7;k++)
if(k!=j&&this.spare[i][k]==2)//两对
{
for(int l=1;l<7;l++)
if(this.spare[i][l]==1)
{
card[i].kind = kindOfCard.liangdui ;
int a = getRankScore(j),b=getRankScore(k);
if(a>b)
card[i].score = a*100+b*10+getRankScore(l);
else
card[i].score = b*100+a*10+getRankScore(l);
continue again;
}
}
else if(this.spare[i][k]==1)//一对
{
for(int l=1;l<7;l++)
if(l!=k&&this.spare [i][l]==1)
for(int m=1;m<7;m++)
if(m!=k&&m!=l&&this.spare [i][m]==1)
{
card[i].kind =kindOfCard.yidui;
card[i].score = getRankScore(j)*1000
+getRankScore(k)*100+getRankScore(l)*10
+getRankScore(m);
continue again;
}
}
}
else if(onlyOne(this.spare [i]))//处理每个点数只有一个的情况
{
if(this.spare[i][1]==0)
{
card[i].kind = kindOfCard.shunzi;
card[i].score =0;
continue again;
}
else if(this.spare[i][6]==0)
{
card[i].kind = kindOfCard.shunzi;
card[i].score =1;
continue again;
}
else
{
for(int k=1;k<7;k++)
if(this.spare[i][k]==0)
{
int score;//按缺失的牌计分
switch(k)
{
case 5:score=1;
case 4:score=2;
case 3:score=3;
case 2:score=4;
default:score=0;
}
card[i].kind = kindOfCard.sanpai ;
card[i].score =score;
continue again;
}
}
}
}
}
}
private int getRankScore(int num)
{
switch(num)
{
case 1:return 6;
case 6:return 5;
case 5:return 4;
case 4:return 3;
case 3:return 2;
case 2:return 1;
default: return 0;
}
}
private boolean onlyOne(int a[])
{
int nums=0;
for(int i=1;i<7;i++)
if(a[i]==1)
nums++;
if(nums==5)
return true;
else
return false;
}
private String getName(int kind)
{
switch(kind)
{
case 8:return "豹子";
case 7:return "顺子";
case 6:return "铁支";
case 5:return "葫芦";
case 4:return "三条";
case 3:return "两对";
case 2:return "一对";
case 1:return "散牌";
default:return "";
}
}
private void printRollResults()
{
for(int i=0;i<6;i++)
{
System.out.printf("Player%d:",card[i].player);
for(int j=0;j<5;j++)
System.out.print(this.results[i][j]+" ");
System.out.println(getName(card[i].kind));
}
}
private void compare()
{
for(int i=0;i<6-1;i++)
for(int j=i+1;j<6;j++)
if(card[i].kind <card[j].kind )
{
Card temp;
temp=card[i];
card[i]=card[j];
card[j]=temp;
}
else if(card[i].kind == card[j].kind )
{
if(card[i].score<card[j].score )
{
Card temp;
temp=card[i];
card[i]=card[j];
card[j]=temp;
}
}
}
private void printGameResults()
{
System.out.println();
for(int i=0;i<6;i++)
{
System.out.printf("No.%d Player%d:%s\n",i+1,card[i].player ,getName(card[i].kind) );
}
}
public void playTheGame()
{
play();
initSpare();
getSpare();
judge();
printRollResults();
compare();
printGameResults();
}
}