Java洗牌、发牌实现

package com.softeem.xipaiDemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class XiPai {
	
	private ArrayList<Pai> p;
	private int[] a;
	private int[] a2;
	
	private void innitArr(int[] a){
		
		for (int j = 0; j < a.length;j++) {
			a[j]=j;
		}
	}
	public XiPai(){
		p=new ArrayList<Pai>();
		a=new int[13];
		a2=new int[2];
		for (int j = 0; j < a2.length;j++) {
			a2[j]=j+14;
		}
		innitArr( a);
		initPai();
	}
	public void initPai(){
		addPai("A", a );
		addPai("B", a );
		addPai("C", a );
		addPai("D", a );
		addPai("X", a2 );
		System.out.println("当前一副牌初始化如下:"+"共"+p.size()+"张");
		showInitPai(p);
		System.out.println();
	}
	private void showInitPai(ArrayList<Pai> p) {
		int count=0;
		for (Pai pai : p) {
			count++;
			System.out.print(type(pai.getType())+sZ(pai.getSize())+" ");
			if(count%13==0){
				System.out.println();
			}
		}
	}
	
	public void doXiPai(){
		System.out.println("开始洗牌:.....");
		Collections.shuffle(p);
		showInitPai(p);
		System.out.println();
		System.out.println("洗牌完成!......");
		System.out.println("现在开始发牌");
		doFaPai();
		System.out.println("发牌完成.....");
		System.out.println("输入任意字符,开始下一局!!!");
		System.out.println("*******************************");
		Scanner sc=new Scanner(System.in);
		String s=sc.next();
		if(s!=null){
			doXiPai();
		}
		
	}
	public void doFaPai(){
		
		int i=showDizhu();
		
		ArrayList<Pai> list1 = new ArrayList<Pai>();
		ArrayList<Pai> list2 = new ArrayList<Pai>();
		ArrayList<Pai> list3 = new ArrayList<Pai>();
		for(int j=0;j<p.size();j++){
			if(j<(p.size()-3)){
				
				switch((j+1)%3){
				case 1: list1.add(p.get(j));break;
				case 2: list2.add(p.get(j));break;
				case 0: list3.add(p.get(j));break;
				}
			}else{
				switch(i){
				case 1: list1.add(p.get(j));break;
				case 2: list2.add(p.get(j));break;
				case 0: list3.add(p.get(j));break;
				}
			}
		}
		switch(i){
		case 1: System.out.println("玩家1地主!");break;
		case 2: System.out.println("玩家2地主!");break;
		case 0: System.out.println("玩家3地主!");break;
		}
		System.out.println("玩家1的牌:");
		showFaPai(list1);
		System.out.println();
		System.out.println("玩家2的牌:");
		showFaPai(list2);
		System.out.println();
		System.out.println("玩家3的牌:");
		showFaPai(list3);
		System.out.println();
	}
	
	private void showFaPai(ArrayList<Pai> list) {
		Collections.sort(list,new MyComparator());
		for (Pai pai : list) {
			System.out.print(type(pai.getType())+sZ(pai.getSize())+" ");
		}
	}
	private int showDizhu() {
		Random r=new Random();
		return r.nextInt(3);
	}
	private void addPai(String type,int[] size) {
		
		for (int i = 0; i < size.length; i++) {
			p.add(new Pai(type,size[i])); 
		}
	}
	private String type(String s){
		char c=s.charAt(0);
		switch(c){
		case ‘A‘:s="红桃";break;
		case ‘B‘:s="黑桃";break;
		case ‘C‘:s="方块";break;
		case ‘D‘:s="梅花";break;
		case ‘X‘:s="";	
		}
		return s;
	}
	private String sZ(int i){
		String s=null;
		switch(i){
		case 0	:	s="3";break;
		case 1	:	s="4";break;
		case 2	:	s="5";break;
		case 3	:	s="6";break;
		case 4	:	s="7";break;	
		case 5	:	s="8";break;	
		case 6	:	s="9";break;	
		case 7	:	s="10";break;	
		case 8  :	s="J";break;	
		case 9  :	s="Q";break;	
		case 10 :	s="K";break;	
		case 11 :	s="A";break;	
		case 12 :	s="2";break;	
		case 14 :	s="小王";break;	
		case 15 :	s="大王";break;	
		}
		return s;
	}
	public static void main(String[] args) {
		new XiPai().doXiPai();
	}
}
package com.softeem.xipaiDemo;

public class Pai {
	
	private String type;
	private int size;
	
	public Pai(String type, int size) {
		this.type = type;
		this.size = size;
	}

	

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
package com.softeem.xipaiDemo;

import java.util.Comparator;

public class MyComparator implements Comparator<Object>{

	@Override
	public int compare(Object paramT1, Object paramT2) {
		Pai p1=(Pai) paramT1;
		Pai p2=(Pai) paramT2;
		
		return (p1.getSize()>p2.getSize()?1:p1.getSize()<p2.getSize()?-1:0);
	}

}

 

}

 

 

Java洗牌、发牌实现,布布扣,bubuko.com

Java洗牌、发牌实现

上一篇:基于.NET的程序读取Excel文件的解决方案


下一篇:python知识点(07-08)