Java 堆栈-数组仿真扑克牌洗牌发牌过程
设计一个Java程序,用数组仿真扑克牌洗牌发牌过程。请用随机数来生产扑克牌后压入堆栈,放满52张牌后开始发牌,使用堆栈的弹出功能来给四个人发牌。
package basic;
import java.io.IOException;
public class test2 {
static int top=-1;
public static void push(int stack[],int MAX,int val) {
if(top>=MAX-1) {
System.out.println("堆栈已经满了");
}else {
top++;
stack[top]=val;
}
}
public static int pop(int stack[]) {
if(top<0)
System.out.println("堆栈已经空了");
else
top--;
return stack[top];
}
public static void main(String[] args) throws IOException {
int card[]=new int[52];
int stack[]=new int[52];
int i,j,k=0,test;
char ascVal=5;
int style = 0;
for(i=0;i<52;i++)
card[i]=i;
while(k<30) {
for(i=0;i<51;i++) {
for(j=i+1;j<52;j++){
if(((int)(Math.random()*5))==2) {
test=card[i];
card[i]=card[j];
card[j]=test;
}
}
}
k++;
}
i=0;
while(i!=52) {
push(stack,52,card[i]);
i++;
}
System.out.println("逆时针发牌");
System.out.println("显示各家的牌 \n 东家 \t 北家 \t 西家 \t 南家");
while(top>=0) {
style=stack[top]/13;
switch(style) {
case 0:
ascVal='C';
break;
case 1:
ascVal='D';
break;
case 2:
ascVal='H';
break;
case 3:
ascVal='S';
break;
}
System.out.print("["+ascVal+(stack[top]%13+1)+"]");
System.out.print('\t');
if(top%4==0)
System.out.println();
top--;
}
}
}