#Java小案例 扑克牌小游戏

知识点:

1.Collections.sort;

2.do while确保不重复;

3.重写compare 比较规则;

主要的对象有

1.玩家 (ID、姓名、手牌列表)
2.卡牌(花色、点数)

1.玩家类

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Player {
	public String name;
	public int id;
	public List cards = new ArrayList();

	public Player(int id, String name) {
		this.name = name;
		this.id = id;
	}

	public Player() {

	}

}

2.卡牌类

import java.util.Arrays;


public class cards implements Comparable  {
public String color;
public String num;
public cards(String color,String num){
	this.color=color;
	this.num=num;
	
}
public cards(){

}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((color == null) ? 0 : color.hashCode());
	result = prime * result + ((num == null) ? 0 : num.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	cards other = (cards) obj;
	if (color == null) {
		if (other.color != null)
			return false;
	} else if (!color.equals(other.color))
		return false;
	if (num == null) {
		if (other.num != null)
			return false;
	} else if (!num.equals(other.num))
		return false;
	return true;
}
@Override
public int compareTo(cards o) {
	String[] a={"黑桃","红桃","梅花","方块"};
	String[] b={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
	int thisn=Arrays.binarySearch(b, this.num);
	int on=Arrays.binarySearch(b, o.num);
	int thisc=Arrays.binarySearch(a, this.color);
	int oc=Arrays.binarySearch(a, o.color);
	int j=0;
	if(thisnon)
		j=1;
	if(thisn==on){
		if(thiscoc)
			j=-1;
	}
	return j;
	
}

}

  3.主程序

import java.util.*;

public class Test implements Comparator {
	public List cardss;
	public List players;
	public int jk = 53;

	public Test() {
		this.cardss = new ArrayList();
		this.players = new ArrayList();
	}

	public static void main(String[] args) {
		/*
		 * 提示信息,提示用户进入。
		 */
		Test test = new Test();
		System.out.println("----------创建扑克牌----------");
		System.out.println("---------创建扑克牌成功---------");
		test.cardAdd();
		System.out.println("");
		System.out.println("---------开始洗牌-------------");
		System.out.println("---------洗牌结束-------------");
		System.out.println("---------设置玩家-------------");
		/*
		 * 创建用户并赋上playerSet方法返回的玩家参数。 用do while 确保用户Id不重复。
		 */
		Player player1 = new Player();
		Player player2 = new Player();
		player1 = test.playerSet();
		do {
			player2 = test.playerSet();
			if (player1.id == player2.id) {
				System.out.println("Id重复请重新输入ID");
			}
		} while (player1.id == player2.id);
		/*
		 * 调用getCard方法每名玩家得到两张卡牌,且无重复卡牌出现。
		 */
		System.out.println("---------欢迎玩家:" + player1.name + ";" + player2.name
				+ "-------");
		player1 = test.getCard(player1);
		System.out.println(player1.name + "拿2张牌");
		player2 = test.getCard(player2);
		;
		System.out.println(player2.name + "拿2张牌");
		/*
		 * 将每个玩家的卡牌排序使卡牌大的靠后.
		 */
		Collections.sort(player2.cards);
		Collections.sort(player1.cards);
		System.out.println("------发牌结束------");
		System.out.println(player1.name + "最大的牌为:" + player1.cards.get(1).color
				+ player1.cards.get(1).num);
		System.out.println(player2.name + "最大的牌为:" + player2.cards.get(1).color
				+ player2.cards.get(1).num);
		/*
		 * 调用compare方法比较两个玩家分别最大牌的大小. 一些单独写成方法更科学,稍微偷下懒。 变成有点面向过程意味。
		 */
		int i = test.compare(player1.cards.get(1), player2.cards.get(1));
		if (i == 1)
			System.out.println("------玩家" + player1.name + "赢------");
		if (i == -1)
			System.out.println("------玩家" + player2.name + "赢------");
		/*
		 * 输出各自玩家的手牌
		 */
		System.out.println("------玩家各自的手牌为-----");
		/*
		 * 玩家一:注意println()和print()的区别
		 */
		System.out.print(player1.name + ":\t");
		for (cards ka : player1.cards) {
			System.out.print(ka.color + ka.num + "\t");
		}
		/*
		 * 玩家二:最后一个是print所以多加一行空白的 System.out.println("");
		 */
		System.out.println("");
		System.out.print(player2.name + ":\t");
		for (cards ka : player2.cards) {
			System.out.print(ka.color + ka.num + "\t");
		}
	}

	/*
	 * 用于创建玩家的Id和姓名; try catch 利用for循环和i--使错误时能多进行一次; 可能会有更方便的方法;
	 */
	public Player playerSet() {
		Player p1 = new Player();
		int i;
		for (i = 0; i < 1; i++) {
			try {
				Scanner input = new Scanner(System.in);
				System.out.println("请输入玩家ID:");
				int Id = input.nextInt();
				p1.id = Id;
				System.out.println("请输入ID为" + Id + "的玩家的名字:");
				String Name = input.next();
				p1.name = Name;
				System.out.println("成功添加" + p1.name);
			} catch (InputMismatchException e) {
				System.out.println("请输入整数");
				i--;
			}
		}
		return p1;
	}

	/*
	 * 应该先看save方法再看这个方法;此方法用于将save方法产生的任意卡牌过滤掉重复卡牌;直到往列表cardss加够52张不重复的卡牌;知识点:
	 * 1.do while2.cardss.contains(ka);
	 */
	public void cardAdd() {
		cards ka;
		System.out.println("-----成功创建了52张卡牌列表如下----");
		for (int i = 0; i < 52; i++) {
			do {
				ka = save();
			} while (cardss.contains(ka));
			System.out.print(ka.color + ka.num);
			cardss.add(ka);
		}
	}

	/*
	 * 用于在数组里随机产生一张卡牌。 无需过多限制。 不得不佩服计算机的计算能力。 虽然这种方法不符合人的逻辑,但是对于计算机来说瞬间试错玩,太简单了。
	 */
	public cards save() {
		Random suiji = new Random();
		String[] a = { "黑桃", "红桃", "梅花", "方块" };
		String[] b = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
				"Q", "K" };
		int i = suiji.nextInt(4);
		int h = suiji.nextInt(b.length);
		cards ka = new cards(a[i], b[h]);

		return ka;
	}

	/*
	 * 用于在cardss里取得卡牌赋予到每个Player对象的cards集合里里。 无需过多限制。 jk-2是防止某些小概率情况下报错;
	 */
	public Player getCard(Player pl) {
		Random suiji = new Random();
		cards ka1;
		cards ka2;

		do {
			ka1 = cardss.get(suiji.nextInt(jk));
			ka2 = cardss.get(suiji.nextInt(jk));
		} while (ka1.color.equals(ka2.color) && ka1.num == ka2.num);
		pl.cards.add(ka1);
		pl.cards.add(ka2);
		cardss.remove(ka1);
		cardss.remove(ka2);
		jk = jk - 2;
		return pl;
	}

	/*
	 * compare方法用于临时比较卡牌大小;理论上是用于sort方法,偷懒直接用其返回值配合if进行判断
	 * 所以理论上可以完全不使用Comparator接口自定义一个临时方法
	 */
	public int compare(cards cards1, cards cards2) {
		String[] a = { "黑桃", "红桃", "梅花", "方块" };
		String[] b = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
				"K", "A" };
		int j = 0;
		/*
		 * 新学到的 Arrays.binarySearch(b,cards1.num); 用于查找具体字符串在数组b中的位置。 上面的数组建立的不好
		 * 一个是按大到小,一个是按小到大 导致下面的返回值得思考得更费精力
		 */
		int ka1num = Arrays.binarySearch(b, cards1.num);
		int ka2num = Arrays.binarySearch(b, cards2.num);
		int ka1color = Arrays.binarySearch(a, cards1.color);
		int ka2color = Arrays.binarySearch(a, cards2.color);
		if (ka1num < ka2num)
			j = -1;
		if (ka1num > ka2num)
			j = 1;
		if (ka1num == ka2num) {
			if (ka1color < ka2color)
				j = 1;
			if (ka1color > ka2color)
				j = -1;
		}
		return j;
	}
}

  

 

 

转载于:https://www.cnblogs.com/wyfstudy/p/6622675.html

你可能感兴趣的:(#Java小案例 扑克牌小游戏)