Java发牌游戏代码

仅供参考,不足的地方欢迎指点:

import java.util.Scanner;

import java.util.Random;


public class PokerGame {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] poker = new int[52];
for (int i = 0; i < poker.length; i++) {
poker[i] = i + 1;
}


String c;
char ch = 'y';
while (true) {
if (ch == 'y' || ch == 'Y') {
while (true) {
System.out.print("请输入参加者的数量(2~6):");
int num = input.nextInt();
if (num >= 2 && num <= 6) {
int[][] gamer = new int[4 * num][13];
Shuffle(poker);
Deal(poker, gamer, num);
Print(gamer, num);
break;
} else {
System.out.print("您的输入有误!输入的不是2到6的一个阿拉伯数字。");
}
}
System.out.print("是否继续新的游戏?(y/n):");
c = input.next();
if (c.length() == 1) {
ch = c.charAt(0);
} else {
ch = 'w';
}
} else if (ch == 'n' || ch == 'N') {
System.out.println("游戏结束!");
break;
} else {
System.out.print("你的输入有误!是否继续新的游戏?(y/n):");
c = input.next();
if (c.length() == 1) {
ch = c.charAt(0);
} else {
ch = 'w';
}
}


}


}


public static void Shuffle(int[] poker) {
Random rand = new Random();
for (int i = 0; i < poker.length; i++) {
int i1 = rand.nextInt(poker.length);
int temp = poker[i];
poker[i] = poker[i1];
poker[i1] = temp;
}
}


public static int Length(int[] gamer) {
int length = 0;
while (gamer[length] != 0)
length++;
return length;
}


public static void Deal(int[] poker, int[][] gamer, int num) {
for (int i = 0; i < gamer.length; i++) {
for (int j = 0; j < gamer[i].length; j++) {
gamer[i][j] = 0;
}


}


for (int i = 0, j = 0; i < poker.length; i++) {
int k = poker[i] / 13, m = 0;
if (poker[i] % 13 == 0) {
for (int l = Length(gamer[j + k - 1]); l > 0; l--) {
gamer[j + k - 1][l] = gamer[j + k - 1][l - 1];
}
gamer[j + k - 1][0] = poker[i];
} else {
for (; poker[i] < gamer[j + k][m]; m++)
;
for (int l = Length(gamer[j + k]); l > m; l--) {
gamer[j + k][l] = gamer[j + k][l - 1];
}
gamer[j + k][m] = poker[i];
}


if ((j / 4 + 1) == num) {
j = 0;
continue;
}
j = j + 4;
}
}


public static void Print(int[][] gamer, int num) {
for (int i = 1; i <= num; i++) {
System.out.println("第" + i + "个人:");
for (int j = 0; j < 4; j++) {
if (j == 0)
System.out.print("黑桃:");
else if (j == 1)
System.out.print("红心:");
else if (j == 2)
System.out.print("草花:");
else
System.out.print("方块:");
for (int k = 0; gamer[j + 4 * (i - 1)][k] != 0; k++) {
int l = gamer[j + 4 * (i - 1)][k] % 13;
if (l >= 2 && l <= 10) {
System.out.print(l + " ");
} else if (l == 1) {
System.out.print("A ");
} else if (l == 11) {
System.out.print("J ");
} else if (l == 12) {
System.out.print("Q ");
} else {
System.out.print("K ");
}
}
System.out.println();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值