用Java实现扑克牌的洗牌再排序

package pkp;
public class Puke implements Comparable<Puke>{
	private String color;
	private int points;
	
	public Puke(){
		
	}
	
	public Puke(String color,int points){
		this.color=color;
		this.points=points;
	}
	
	public int compareTo(Puke p){
		return this.points - p.points;
		/*if (this.points > p.points) {
            return 1;
        } else if (this.points > p.points) {
            return 0;
        } else {
            return -1;
        }*/
	}
	
	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}
	
	public boolean equals(Object obj){
		Puke p = (Puke) obj;
		if(this == obj){
			return true;
		}
		if(obj == null){
			return false;
		}
		if(obj instanceof Puke){
			return this.color.equals(p.getColor())&&this.points==p.points;
		}
		return false;
	}
	
	public String toString(){
		return color+points;
	}
}
package pkp;

import java.util.Comparator;

public class ColorComparator implements Comparator<Puke> {

	public int compare(Puke p1, Puke p2) {
		//黑桃 红桃 方片 梅花 同等花色按点数比
		String [] arr = {"梅花","方片","红桃","黑桃"};
		int index1 = 0,index2 = 0;
		for(int i = 0;i<arr.length;i++){
			if(p1.getColor().equals(arr[i])){
				index1 = i;
				break;
			}
		}
		for(int j = 0;j<arr.length;j++){
			if(p2.getColor().equals(arr[j])){
				index2 = j;
				break;
			}
		}
		 if(index1 != index2){
			return index1-index2;
		}else {
			return p1.getPoints()-p2.getPoints();
		}
	}
	
}
package pkp;
import java.util.ArrayList;
import java.util.Collections;
public class TestPuke {
	public static void show(ArrayList<Puke> list) {
		for(int i=0;i<list.size();i++){
			System.out.print(list.get(i)+"  "); 
			if((i+1)%13 == 0){
				System.out.print("\n");
			}
		}
	}
	public static void main(String[] args) {
		ArrayList<Puke> list = new ArrayList<Puke>();
		String [] color = {"黑桃","梅花","方片","红桃"};
		int [] points = {1,2,3,4,5,6,7,8,9,10,11,12,13};
		for (int i = 0; i < color.length; i++) {
			for (int j = 0; j < points.length; j++) {
				list.add(new Puke(color[i], points[j]));
			}
		}
		//list.add(new Puke("红","大王"));
		//list.add(new Puke("黑","小王"));
		show(list);
		Collections.shuffle(list);
		System.out.println("\n洗牌后:");
		show(list);
		Collections.sort(list);
		System.out.println("\n排序后:");
		show(list);
		Puke p1 = new Puke("黑桃",5);
		Puke p2 = new Puke("黑桃",5);
		if(p1.compareTo(p2) == 0){
			System.out.println("\n相等");
		}else{
			System.out.println("\n不相等");
		}
		Collections.sort(list,new ColorComparator());
		System.out.println("\n再次排序后:");
		show(list);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值