纸牌排序

import java.util.*;

public class Test {

public static void main(String[] args) {

Random rnd = new Random();

int Cards[][] = new int[13][4];//检测牌是否会重复

int Cardss[][] = new int[13][4];//按牌的大小给每张牌赋值,例如方块3等于1,黑桃2等于52,

int t = 1;

//赋值

for (int m = 0; m < 13; m++)

{

for (int n = 0; n < 4; n++)

{

Cardss[m][n] = t;

t++;

}

}

 

//原牌

for (int m = 0; m < 4; m++)

{

for (int n = 0; n < 13; n++)

{

Card Ca = new Card(n, m);

System.out.print(Ca.display() + "\t");

}

System.out.println();

}

System.out.println();

System.out.println();

 

 

int a, b, sum = 0,r=0,l=0;

String[][] str = new String[4][53];

while (sum!= 52) //牌的数量到52张的时候就退出循环

{

a = rnd.nextInt(13);//控制点数

b = rnd.nextInt(4);//控制花色

if (Cards[a][b] == 0)//当二维数组这个位置没有出现对应的牌时,才可进入下步操做

{

Cards[a][b]=1;//标记这张牌已出现过了

Card Ca = new Card(a, b);//生成一个新对象

System.out.print(Ca.display() + " ");

sum++;//统计牌数

l=Cardss[a][b]; //把这张牌对应的值赋值给l

str[r][l]=Ca.display();

if (sum % 13 == 0)

{

System.out.println();

r++;

}

 

 

}

}

//整理牌

System.out.println();

System.out.println();

for(r=0;r<4;r++)

{

for(l=0;l<53;l++)

{

if(str[r][l]!=null)

{

System.out.print(str[r][l]+" ");

}

}

System.out.println();

}

}

}

 

class Card

{

int face;// 点数 3(0) 4 5 6 7 8 9 10 J(8) Q(9) K(10) A(11) 2(12)

int suit;// 花色 方块(0) 梅花(1) 红桃(2) 黑桃(3)

public Card(int f, int s)

{

face = f;

suit = s;

}

public String display()

{

String s = ""; // 花色

if (suit == 0)

s = "♦️";

else if (suit == 1)

s = "♣️";

else if (suit == 2)

s = "♥️";

else if (suit == 3)

s = "♠️";

String f = ""; // 点数

if (face >= 0 && face <= 7)

f = face + 3 + "";

else if (face == 8)

f = "J";

else if (face == 9)

f = "Q";

else if (face == 10)

f = "K";

else if (face == 11)

f = "A";

else if (face == 12)

f = "2";

return s + f;

}

}

 

posted on 2018-04-21 11:30  苏打水不好喝  阅读(144)  评论(0编辑  收藏  举报

导航