欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java语言编写扑克牌小游戏

程序员文章站 2024-02-26 16:06:10
...

2.0版本

此次代码添加加了抛出异常在这里插入代码片的方法

package Java.demoTest01;

public class Poker {
	private String point;
	private String color;
	
	public void setPoint(String point) {
		this.point=point;
	}
	public String getPoint() {
		return point;
	}
	public void setColor(String color) {
		this.color=color;
	}
	public String getColor() {
		return color;
	}
	public Poker(String color,String point) {
		// TODO 自动生成的构造函数存根
		this.point=point;
		this.color=color;
	}
	@Override
	public String toString() {
		return color +" "+ point;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((color == null) ? 0 : color.hashCode());
		result = prime * result + ((point == null) ? 0 : point.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof Poker))
			return false;
		Poker other = (Poker) obj;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (point == null) {
			if (other.point != null)
				return false;
		} else if (!point.equals(other.point))
			return false;
		return true;
	}
	
}

package Java.demoTest01;

import java.util.ArrayList;
import java.util.List;

public class GamePlayer {
	private int id;
	private String name;
	private List<Poker> pokers;

	public GamePlayer(int id, String name) {
		// TODO 自动生成的构造函数存根
		this.id = id;
		this.name = name;
		this.pokers = new ArrayList<Poker>();
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public List<Poker> getPokers() {
		return pokers;
	}

	public void setPokers(List<Poker> pokers) {
		this.pokers = pokers;
	}
}

package Java.demoTest01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class ControlCenter implements Comparator<Poker> {
	private List<Poker> pokerList;
	private List<GamePlayer> playerList;
	private List<Poker> pokerListAfterShuffle;
	private int playerId1;
	private String playerName1;
	private int playerId2;
	private String playerName2;
	private Scanner sc;
	private Poker getPokers;

	public ControlCenter() {
		// TODO 自动生成的构造函数存根
		this.pokerList = new ArrayList<>();
		this.playerList = new ArrayList<>();
		this.pokerListAfterShuffle = new ArrayList<>();
		this.sc = new Scanner(System.in);
	}

	/*
	 * 第一步:创建扑克牌
	 */
	public void poker() {
		String arrayPoint[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
		String arrayColor[] = { "方块", "梅花", "红桃", "黑桃" };
		for (String colors : arrayColor) {
			for (String points : arrayPoint) {
				Poker poker = new Poker(colors, points);
				pokerList.add(poker);
			}
		}
		System.out.print("扑克牌:[");
		for (Poker pokerlist : pokerList) {
			System.out.print(pokerlist + " ");
		}
		System.out.println("]");
	}

	/*
	 * 第二步:洗牌
	 */
	public void shufflePoker() {
		System.out.println("-----洗牌开始-----");
		Collections.shuffle(pokerList);
		for (Poker shuffle : pokerList) {
			pokerListAfterShuffle.add(shuffle);
		}
		System.out.println("-----洗牌完毕-----");
	}

	/*
	 * 第三步:创建玩家
	 */
	public void player() {

		System.out.println("有请第1位玩家输入Id和Name:");
		
		while(true) {
			try {
				System.out.println("请输入Id:(注意:必须输入数字!!!)");
				playerId1 = sc.nextInt();
				break;
			}catch(InputMismatchException e) {
				System.out.println("请重新输入,因为您输入的不是数字");
				sc.nextLine();
			}
		}
		System.out.println("请输入Name:");
		playerName1 = sc.next();
		GamePlayer gp1 = new GamePlayer(playerId1, playerName1);
		playerList.add(gp1);
		
		System.out.println("有请第2位玩家输入Id和Name:");
		while(true) {
			try {
				System.out.println("请输入Id:(注意:必须输入数字!!!)");
				playerId2 = sc.nextInt();
				break;
			}catch(InputMismatchException e) {
				System.out.println("请重新输入,因为您输入的不是数字");
				sc.nextLine();
			}
		}
		System.out.println("请输入Name:");
		playerName2 = sc.next();
		GamePlayer gp2 = new GamePlayer(playerId2, playerName2);
		playerList.add(gp2);

		System.out.println("玩家:" + playerList.get(0).getName() + "和玩家" + playerList.get(1).getName() + "登录成功");
	}

	/*
	 * 第四步:发牌
	 */
	public void Licensing() {
		Random r = new Random();

		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				System.out.println(playerList.get(i).getName() + "拿牌");
				int k = r.nextInt(52);
				getPokers = pokerListAfterShuffle.get(k);
				playerList.get(i).getPokers().add(getPokers);
			}
		}
	}

	/*
	 * 第五步:重写comparator方法
	 */
	public int compare(Poker o1, Poker o2) {
		if (o2.getColor().charAt(0) - o1.getColor().charAt(0) > 1) {
			return 1;
		} else if (o2.getColor().charAt(0) - o1.getColor().charAt(0) < 1) {
			return -1;
		} else {
			if (o2.getPoint().charAt(0) - o1.getPoint().charAt(0) > 1) {
				return 1;
			} else if (o2.getPoint().charAt(0) - o1.getPoint().charAt(0) < 1) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	/*
	 * 第六步:扑克牌比大小
	 */
	public void gameStart() {
		Collections.sort(playerList.get(0).getPokers(), new ControlCenter());
		Collections.sort(playerList.get(1).getPokers(), new ControlCenter());
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				System.out.println(
						"玩家:" + playerList.get(i).getName() + "手中的牌:" + playerList.get(i).getPokers().get(j).getColor()
								+ playerList.get(i).getPokers().get(j).getPoint());
			}
		}

		System.out.println("玩家" + playerList.get(0).getName() + "的牌:" + playerList.get(0).getPokers().get(0));
		System.out.println("玩家" + playerList.get(1).getName() + "的牌:" + playerList.get(1).getPokers().get(0));

		List<Poker> lastList = new ArrayList<>();
		lastList.add(playerList.get(0).getPokers().get(0));
		lastList.add(playerList.get(1).getPokers().get(0));
		Collections.sort(lastList, new ControlCenter());

		if (lastList.get(0).equals(playerList.get(0).getPokers().get(0))) {
			System.out.println("第一位玩家:" + playerList.get(0).getName()+"获胜");
		} else {
			System.out.println("第二位玩家:" + playerList.get(1).getName()+"获胜");
		}
	}

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		ControlCenter cc = new ControlCenter();
		cc.poker();
		cc.shufflePoker();
		cc.player();
		cc.Licensing();
		cc.gameStart();
	}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Java语言编写扑克牌小游戏
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
代码实现效果
本人Java小白,此代码如果有BUG请私信我反馈

相关标签: java