今日java小练习之整牌

题目描述:

  1. 给出四种牌面花色,和13种大小牌面,将花色与数字整合初始化牌面,整合除52张牌
  2. 再整理牌面,即乱序
  3. 将整好的牌面发给四个人
  4. 四人手中的牌面按顺序整理

实现思路1,如下:

主要通过数组完成

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class 整牌 {
	public static void main(String[] args) {
		//所有牌面的四种花色
		char[] flags= {'♥','♣','♦','♠'};	
		//所有牌面的数字
		String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		String[] ret=new String[52];
		//1.初始化所有牌面
		int index=0;
		for(char a:flags) {
			for(String b:numbers) {
				ret[index++]=a+b;
			}
		}
		//初始化后的牌面
		System.out.println("牌面初始化后:"+Arrays.toString(ret));
		//2.乱序
		for(int i=0;i<10;i++) {
			for(int k=ret.length-1;k>0;k--) {
				int randIndex=(int)(Math.random()*k);
				String temp=ret[k];
				ret[k]=ret[randIndex];
				ret[randIndex]=temp;
			}
		}
		System.out.println("牌面洗10次后:"+Arrays.toString(ret));
		//3.发牌
		String[][] players=new String[4][13];
		for(int i=0,n=0;i=0) {
								switch(array[x]) {
								case "A":
									array[x]="1";
									break;
								case"J":
									array[x]="11";
									break;
								case"Q":
									array[x]="12";
									break;
								case"K":
									array[x]="13";
									break;
								}
							}
						}
						int o1=Integer.parseInt(array[0]);						
						int o2=Integer.parseInt(array[1]);
						if(o1>o2) {
							String temp=ret3[k];
							ret3[k]=ret3[k+1];
							ret3[k+1]=temp;
						}
					}
				}
			}
			System.out.println();
			System.out.println("玩家整排后:");
			for(String[] ret3:players) {
				System.out.println(Arrays.toString(ret3));
			}
	}
}

代码运行结果:

牌面初始化后:[♥A, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♥J, ♥Q, ♥K, ♣A, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♦A, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10, ♦J, ♦Q, ♦K, ♠A, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K]
牌面洗10次后:[♦7, ♣6, ♦5, ♦4, ♥K, ♣3, ♦Q, ♣7, ♠6, ♣J, ♦2, ♥9, ♠2, ♣A, ♠8, ♥8, ♥2, ♣8, ♥4, ♠J, ♠K, ♦K, ♣K, ♣9, ♦8, ♣Q, ♠3, ♦A, ♥J, ♠10, ♦J, ♠7, ♠9, ♠Q, ♥7, ♣4, ♥10, ♥Q, ♥5, ♠4, ♣10, ♦3, ♠5, ♣5, ♥3, ♣2, ♦9, ♥6, ♠A, ♥A, ♦10, ♦6]
依次发牌后:
玩家:[♦7, ♥K, ♠6, ♠2, ♥2, ♠K, ♦8, ♥J, ♠9, ♥10, ♣10, ♥3, ♠A]
玩家:[♣6, ♣3, ♣J, ♣A, ♣8, ♦K, ♣Q, ♠10, ♠Q, ♥Q, ♦3, ♣2, ♥A]
玩家:[♦5, ♦Q, ♦2, ♠8, ♥4, ♣K, ♠3, ♦J, ♥7, ♥5, ♠5, ♦9, ♦10]
玩家:[♦4, ♣7, ♥9, ♥8, ♠J, ♣9, ♦A, ♠7, ♣4, ♠4, ♣5, ♥6, ♦6]

玩家整排后:
[♠A, ♠2, ♥2, ♥3, ♠6, ♦7, ♦8, ♠9, ♥10, ♣10, ♥J, ♥K, ♠K]
[♣A, ♥A, ♣2, ♣3, ♦3, ♣6, ♣8, ♠10, ♣J, ♣Q, ♠Q, ♥Q, ♦K]
[♦2, ♠3, ♥4, ♦5, ♥5, ♠5, ♥7, ♠8, ♦9, ♦10, ♦J, ♦Q, ♣K]
[♦A, ♦4, ♣4, ♠4, ♣5, ♥6, ♦6, ♣7, ♠7, ♥8, ♥9, ♣9, ♠J]

 实现思路2,如下:

主要通过集合实现

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class 整牌2 {
	public static void main(String[] args) {
		//所有牌面的四种花色
		char[] flags= {'♥','♣','♦','♠'};	
		//所有牌面的数字
		String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		//1.初始化所有牌面
		ArrayList ret=new ArrayList();
		for(char a:flags) {
			for(String b:numbers) {
				ret.add(a+b);
			}
		}
		//初始化后的牌面
		System.out.println("牌面初始化后:"+ret);
		//2.乱序洗牌
		for(int i=0;i<10;i++) {
			Collections.shuffle(ret);
		}
		System.out.println("洗牌后:"+ret);
		//3.发牌
		ArrayList> ret2=new ArrayList>();
		for(int i=0;i<4;i++) {
			ret2.add(new TreeSet(new Comparator() {
				@Override
				public int compare(String o1, String o2) {
					String s1=o1.substring(1);
					String s2=o2.substring(1);
					String[] array= {s1,s2};	
					for(int x=0;x player:ret2) {
			System.out.println(player);
		}
	}
}

 代码运行结果:

牌面初始化后:[♥A, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♥J, ♥Q, ♥K, ♣A, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♦A, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10, ♦J, ♦Q, ♦K, ♠A, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K]
洗牌后:[♦4, ♠K, ♦Q, ♥5, ♣7, ♠4, ♦J, ♣K, ♣A, ♣3, ♥Q, ♦10, ♦8, ♥2, ♠9, ♣2, ♥10, ♥3, ♠3, ♥K, ♠5, ♣4, ♦6, ♥7, ♦3, ♣Q, ♥8, ♦9, ♠2, ♣6, ♦A, ♥9, ♦2, ♥6, ♦7, ♠J, ♣J, ♣9, ♠8, ♥A, ♣5, ♠7, ♠Q, ♦5, ♦K, ♣8, ♠6, ♣10, ♠10, ♥4, ♠A, ♥J]
发牌后:
[♣A, ♠2, ♦2, ♦3, ♦4, ♠5, ♣5, ♣7, ♦8, ♠10, ♥10, ♣J, ♦K]
[♥2, ♣3, ♥3, ♠4, ♣4, ♥4, ♣6, ♥6, ♠7, ♣8, ♣9, ♣Q, ♠K]
[♠A, ♦A, ♠3, ♠6, ♦6, ♦7, ♠8, ♥8, ♠9, ♦J, ♠Q, ♥Q, ♦Q]
[♥A, ♣2, ♥5, ♦5, ♥7, ♥9, ♦9, ♣10, ♦10, ♠J, ♥J, ♣K, ♥K]

你可能感兴趣的:(java,算法,开发语言)