实验6:发牌程序
题目类别: 实验
关键字: 掌握Java数组、方法的基本定义
内容要求:
- 编写程序,项目名和类名均为PokerGame。
- 实现功能:
(1) 共有m幅扑克牌,每幅扑克牌不包括大王和小王共52张牌。
(2) 可能有n个人参与扑克游戏,2<=n<=52。
(3) 程序运行时输入扑克牌幅数m和人数n,然后所有牌分别依次分发给n个人。不能整除时,每个人的牌数可以不同,如3个人1幅牌,则第1个人18张,第2个和第3个人17张牌。
(4) 发牌完成后按花色(顺序为黑桃、红心、草花、方块)和牌面大小输出每个人得到的牌。
例如:
输入扑克牌幅数:1
输入人数:3
输出如下:
第1个人:
黑桃:K 10 5 A
红心:10 3 2
草花:K 10 8 6 3 A
方块:Q J 5 2
第2个人:
…
第3个人:
…
- 实现要求:
(1) 使用数组存放发牌情况。
(2) 编写不同方法完成不同功能。
提交:打包为可以执行的JAR文档,其中要包含源程序文件。
package main;
import java.util.Scanner;
public class PokerGame {
public static void main(String[] args){
Scanner reader=new Scanner(System.in);
int m,n;
System.out.print("输入扑克牌幅数:");
m=reader.nextInt();
System.out.print("输入人数:");
n=reader.nextInt();
System.out.println("输出如下:\n");
int[][] poker=new int[m][52];
int[][] man=new int[n][52*m+5];
for(int i=0;i<m;i++)
for(int j=1;j<=52;j++){
poker[i][j-1]=j;
}
PokerGame.sell(poker,man,m,n);
int[][] manPoker=new int[4][13];
for(int i=0;i<n;i++){
System.out.println("第"+(i+1)+"个人:");
for(int x=0;x<4;x++){
for(int y=0;y<13;y++){
manPoker[x][y]=0;
}
}
sortMan(man[i],manPoker,n);
for(int a=3;a>=0;a--){
if(a==3){
System.out.print("黑桃:");
}else if(a==2){
System.out.print("红心:");
}else if(a==1){
System.out.print("草花:");
}else{
System.out.print("方块:");
}
for(int b=12;b>=0;b--){
if(manPoker[a][b]!=0)
if(b==0){
System.out.print("A"+" ");
}else if(b==10){
System.out.print("J"+" ");
}else if(b==11){
System.out.print("Q"+" ");
}else if(b==12){
System.out.print("K"+" ");
}else{
System.out.print((b+1)+" ");
}
}
System.out.println();
}
}
}
static void sortMan(int[] man,int[][] manPoker,int n){
for(int i=0;i<man.length&&man[i]!=0;i++){
man[i]--;
int color=man[i]/13;
int num=man[i]%13;
manPoker[color][num]=1;
}
}
static void sell(int[][] poker,int[][] man,int m,int n){
int manLabel=0;
int random;
int pokerLabel1;
int pokerLabel2;
int sellTime=0;
for(int i=0;i<m*52;i++){
if(manLabel>=n){
manLabel=0;
sellTime++;
}
do{
random=(int) (Math.random()*52*m);
pokerLabel1=random%52;
pokerLabel2=random/52;
if(poker[pokerLabel2][pokerLabel1]!=0){
break;
}
}while (poker[pokerLabel2][pokerLabel1]==0);
poker[pokerLabel2][pokerLabel1]=0;
man[manLabel][sellTime]=pokerLabel1+1;
manLabel++;
}
}
}