Java语言编写扑克牌小游戏

扑克牌小游戏

第一版本

我写的这个扑克牌小游戏是一个很基础的java控制台程序。这个扑克牌游戏主要的游戏过程是:首先创建一副扑克牌,创建完扑克牌之后要进行洗牌,牌洗好了,需要玩家来玩,接下来就创建玩家。有洗好的牌,也有玩家了,那么就开始发牌,每一位玩家发两张牌,发完牌后,比较玩家中手牌的大小,大的那一位获胜。(忽略大小王)

第一步:首先我们需要创建一个扑克牌类,扑克牌主要有两个属性,一个是扑克牌的点数(point),一个是扑克牌的花色(color),需要重载构造函数,一个无参的,一个有参的。在最后还重写了equals方法,判断两个扑克牌是否相等。

第二步:我们创建玩家类,玩家拥有三个属性,一个是id,一个name,另一个是手牌,手牌需要用集合存储。在这个游戏中,我们给每名玩家会发两张牌,所以用List来存放手牌。同样是创建无参构造方法和有参构造方法。

第三步:创建游戏的主类了。

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 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;
	}
	
}

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

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

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

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

	public String 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;
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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> pokerListShuffle;
	private String playerId;
	private String playerName;
	private Scanner sc;
	private Poker getPokers;

	public ControlCenter() {
		// TODO 自动生成的构造函数存根
		this.pokerList = new ArrayList<>();
		this.playerList = new ArrayList<>();
		this.pokerListShuffle = 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) {
			pokerListShuffle.add(shuffle);
		}
		System.out.println("-----洗牌完毕-----");
	}

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

		for (int i = 1; i < 3; i++) {
			System.out.println("有请第" + i + "位玩家输入Id和Name:");
			System.out.println("请输入Id:");
			playerId = sc.next();
			System.out.println("请输入Name:");
			playerName = sc.next();
			GamePlayer gp = new GamePlayer(playerId, playerName);
			playerList.add(gp);
		}
		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 = pokerListShuffle.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小白,此代码如果有BUG请私信我反馈
此代码后续会继续优化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值