Java基础~挖坑纸牌游戏(三人游戏,留底牌)

1,游戏规则:三个人进行挖坑游戏,需要留四张底牌,“A”,“2”,“3”是最大的,然后从小到大依次是“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”,我们需要把A,2,3,放到最后面,然后从大到小依次进行排序,还有,黑桃,红心,梅花,方块,的四种颜色,从大到小依次排序

ArrayList.java(定义牌的可扩充数组)

package com.yan.zhipai;

import java.util.Arrays;

public class ArrayList {
	private Comparable[] arr;
	private int size = 0;

	public ArrayList() {
		this(10);
	}

	public ArrayList(int length) {
		arr = new Comparable[length];
	}

	public void add(Comparable data) {
		arr[size++] = data;
		if (size >= arr.length)
			resize();
	}

	public Comparable delete(int index) {
		if (index >= size || size < 0)
			throw new ArrayIndexOutOfBoundsException();
		Comparable res = arr[index];
		System.arraycopy(arr, index + 1, arr, index, arr.length - index - 1);
		arr[size - 1] = null;
		size--;
		return res;
	}

	public void update(int index, Comparable data) {
		if (index >= size || size < 0)
			throw new ArrayIndexOutOfBoundsException();
		arr[index] = data;
	}

	public Comparable[] getData() {
		return this.arr;
	}

	public String toString() {
		return Arrays.toString(arr);
	}

	private void resize() {
		Comparable[] res = new Comparable[arr.length * 3 / 2];
		System.arraycopy(arr, 0, res, 0, arr.length);
		this.arr = res;
	}

	public int size() {
		return this.size;
	}

	public Comparable[] sort() {
		Comparable[] res = getData();
		Arrays.sort(res);
		return res;
	}

}

HuaSe.java(定义牌的花色)这是引用的枚举类型

package com.yan.zhipai;

public enum HuaSe {
//可以在这个 “这个打上你需要的图标”
	HEI("♠"), HONG("🧡"), MEI("♣"), FANG("🔶");

	private String name;

	private HuaSe(String name) {
		this.name = name;
	}

	public String toString() {
		return this.name;
	}
}

Pai.java(这个是我的牌的方法)

package com.yan.zhipai;

public class Pai implements Comparable<Pai> {
//final 其中所有的方法都不能被重写,这里需要注意的是不能被重写,但是可以被重载
	private final int num;
	private final HuaSe color;

	public Pai(int num, HuaSe color) {
		this.num = num;
		this.color = color;
	}

	public int getNum() {
		return num;
	}

	public HuaSe getColor() {
		return color;
	}

	public String toString() {
		String aa = "" + num;
		if (num > 10) {
			if (num > 10) {
				if (num == 11)
					aa = "J";
				if (num == 12)
					aa = "Q";
				if (num == 13)
					aa = "K";
				if (num == 14)
					aa = "A";
				if (num == 15)
					aa = "2";
				if (num == 16)
					aa = "3";
			}
		}
		String res = "(" + aa + ":" + color + ")";
		return res;
	}

	@Override
	public int compareTo(Pai o) {
		// TODO Auto-generated method stub
		return 0;
	}

}

PaiHe.java(这是我的牌盒方法,用来定义牌的数量和颜色还有留的底牌数量)

package com.yan.zhipai;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class PaiHe {
	private ArrayList pais = new ArrayList(53);

	public PaiHe() {
		for (HuaSe tmp : HuaSe.values()) {
			for (int k = 4; k < 17; k++) {
				pais.add(new Pai(k, tmp));
			}
		}
	}

	public Pai[] faPai() {
// 每个人发16张,也可以进行改变
		Random r = new Random();
		Pai[] res = new Pai[16];
		for (int i = 0; i < res.length; i++) {
			int pos = r.nextInt(pais.size());
			Object p = pais.delete(pos);
			if (p != null && p instanceof Pai) {
				Pai pp = (Pai) p;
				res[i] = pp;
			}
		}
		return res;
	}

	public static void main(String[] args) {
		PaiHe ph = new PaiHe();
// 3个人玩,也可以改变游戏玩法
		for (int i = 0; i < 3; i++) {
			Pai[] arr = ph.faPai();
			Arrays.sort(arr, new Comparator<Pai>() {
				public int compare(Pai o1, Pai o2) {
					int res = o1.getNum() - o2.getNum();
					if (res == 0) {
						res = o1.getColor().compareTo(o2.getColor());
					}
					return res;
				}
			});
			System.out.println(Arrays.toString(arr));
		}
//需要留的底牌数量
		System.out.println("底牌:");
		Comparable[] brr = ph.getPais().getData();
		for (Comparable tmp : brr) {
			if (tmp != null)
				System.out.print(tmp);
		}
	}

	public ArrayList getPais() {
		return pais;
	}
}

运行及截图:

🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡这个程序的可变性很大,如果对你有帮助帮忙留个赞🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值