SCAU 扑克牌 OOP java 作业

1. 编写程序,项目名和类名均为PokerGame。
2. 实现功能:
(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个人:
......

3. 实现要求:
(1) 使用数组存放发牌情况。
(2) 编写不同方法完成不同功能。

提交:打包为可以执行的JAR文档,其中要包含源程序文件。
 

/*
1. 编写程序,项目名和类名均为PokerGame。
2. 实现功能:
(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个人:
......

3. 实现要求:
(1) 使用数组存放发牌情况。
(2) 编写不同方法完成不同功能。

提交:打包为可以执行的JAR文档,其中要包含源程序文件。
 */

package HHJ;

import java.util.Arrays;
import java.util.Scanner;

/*
 *  程序思想:建立一个三维数组存放了 人-符号-牌的点数 ,均匀发牌实现方法是每个人轮流从牌库抽牌。
 *  程序缺陷:是1、要提前建立个一个三维数组,第三维是用来装牌,因为无法知道他们到底能拿到多少张所以要把第三维
 *  的长度定的很多来适应多幅牌,十分浪费空间。2、就是均匀发牌实现靠着牌库抽牌来实现,由于数组不能删除数据,所
 *  以导致抽到后面废牌 *  很多,抽到有效牌概率很低,十分浪费时间。希望发到网上后有人能修改下。
 */
public class HHJ{

	public static void hhj(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.println("输入人数:");
		int numberofPeople = input.nextInt();
		int[][][] people = new int[numberofPeople][4][20];  //第三维的长度要尽量大点,,不让后面牌副数多了就装不下了

		System.out.println("输入扑克牌副数:");
		int numberofCard = input.nextInt();

		dealCard(numberofPeople, people, numberofCard);

		// 重新排序 这里是从小到大排的
		sortCard(people,numberofPeople);
	
		printCard(numberofPeople, people);
	}

	//dealCard 包含了 创建牌库和均匀发牌的功能
	public static void dealCard(int numberofPeople, int[][][] people, int numberofCard) {
		// 正常一副牌的数量
		int[][] playcard = new int[][] { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 },
				{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 },
				{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 } };

		// 重新构建一副新的牌,把多幅牌融合在一起
		int[][] newplaycard = new int[4][13 * numberofCard];
		int lastnumber = 0;
		
		for (int i = 1; i <= numberofCard; i++) {
			for (int j = 0; j < 4; j++) {
				System.arraycopy(playcard[j], 0, newplaycard[j], lastnumber, 13);  // 实现效果 每组符号都有若干个 13*n  1 2 3 ``` 13  1 2 3 ```13
			}
			lastnumber += 13;
		}
		
		// 开始发牌 
		int j = 0;
		int colour = (int) (Math.random() * 4); // 符号随机
		int cardnumber = (int) (Math.random() * 13); // 点数随机
		
		for (int i = 0; i < 52 * numberofCard; ) {   //抽出的牌数不能大过牌库的数量
			j = i % numberofPeople;  //利用求余实现每个人都循环抽一张牌,实现均匀发牌
			colour = (int) (Math.random() * 4); // 符号随机
			cardnumber = (int) (Math.random() * (13*numberofCard)); // 点数随机
			if (newplaycard[colour][cardnumber] != 0) // 作废牌0不要
			{
				people[j][colour] = insertElement(people[j][colour], newplaycard[colour][cardnumber], 0); // 每个人轮流从牌库里面抽一张牌,放到自己对应的符号颜色库里面
				newplaycard [colour][cardnumber] = 0; // 抽到的牌置0作废
				i++; //抽牌成功次数加1
			}

		}

	}

	
	//打印输出他们的牌 
	public static void printCard(int numberofPeople, int[][][] people) {
		System.out.println("输出如下:");
		for (int i = 0; i < numberofPeople; i++) {  //i从0开始为了方便后面的数组寻数
			System.out.println("第" + (i + 1) + "个人:");
			
			System.out.print("黑桃:");
			for (int j = people[i][0].length - 1; j >= 0; j--) {  //用sort是从小到大 所以只能倒着输出了 不想重载sort了
				if (people[i][0][j] == 1)
					System.out.print("A ");
				else if (people[i][0][j] == 11)
					System.out.print("J ");
				else if (people[i][0][j] == 12)
					System.out.print("Q ");
				else if (people[i][0][j] == 13)
					System.out.print("k ");
				else if (people[i][0][j] != 0)  //因为我的people数组人手中的牌容器是做大了很多 里面有很多无效数 所以要把0去掉
					System.out.print(people[i][0][j] + " ");
			}
			System.out.println("");
			
			System.out.print("红心:");
			for (int j = people[i][1].length - 1; j >= 0; j--) {
				if (people[i][1][j] == 1)
					System.out.print("A ");
				else if (people[i][1][j] == 11)
					System.out.print("J ");
				else if (people[i][1][j] == 12)
					System.out.print("Q ");
				else if (people[i][1][j] == 13)
					System.out.print("k ");
				else if (people[i][1][j] != 0)
					System.out.print(people[i][1][j] + " ");
			}
			System.out.println("");
			
			System.out.print("草花:");
			for (int j = people[i][2].length - 1; j >= 0; j--) {
				if (people[i][2][j] == 1)
					System.out.print("A ");
				else if (people[i][2][j] == 11)
					System.out.print("J ");
				else if (people[i][2][j] == 12)
					System.out.print("Q ");
				else if (people[i][2][j] == 13)
					System.out.print("k ");
				else if (people[i][2][j] != 0)
					System.out.print(people[i][2][j] + " ");
			}
			System.out.println("");
			
			System.out.print("方块:");
			for (int j = people[i][3].length - 1; j >= 0; j--) {
				if (people[i][3][j] == 1)
					System.out.print("A ");
				else if (people[i][3][j] == 11)
					System.out.print("J ");
				else if (people[i][3][j] == 12)
					System.out.print("Q ");
				else if (people[i][3][j] == 13)
					System.out.print("k ");
				else if (people[i][3][j] != 0)
					System.out.print(people[i][3][j] + " ");
			}
			System.out.println("");
		}

	}
	
	
	//这个网上找的,int[]数组追加元素 方便抽牌后插入
	private static int[] insertElement(int original[], int element, int index) {

		int length = original.length;

		int destination[] = new int[length + 1];

		System.arraycopy(original, 0, destination, 0, index);

		destination[index] = element;

		System.arraycopy(original, index, destination, index + 1, length - index);

		return destination;
	}

	//把牌重新排序
	private static int[][][] sortCard(int[][][] people,int numberofPeople ){
		for (int i = 0; i < numberofPeople; i++) {
			for (int j = 0; j < 4; j++) {
				Arrays.sort(people[i][j]);
			}
		}
		return people;
	}
}

 

你可能感兴趣的:(SCAU 扑克牌 OOP java 作业)