1.创建Poker类(属性:花色,点数;方法:创建扑克牌())和Player类(属性:ID,姓名,手牌集合;方法:获取玩家最大手牌()),再创建PlayGame类,实现1.创建扑克牌(调用Poker类的方法,增加一些友好的提示信息) 2.创建玩家 3.洗牌 4.发牌 5.游戏
2.其中创建整副扑克集合时按牌面规则从小到大的顺序创建,在comparetTo方法中判断大小时,先获取扑克牌在集合中的index值,判断index即可判断牌面大小
3.另外实现了玩家个数的灵活选择,定义count设置玩家人数
Poker.java代码如下:
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 扑克牌类,具有花色和点数两个属性
*/
public class Poker implements Comparable{
private String pattern; //花色
private String point; //点数
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
//构造函数
public Poker(){}
public Poker(String pattern, String point) {
this.pattern = pattern;
this.point = point;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Poker poker = (Poker) o;
if (!pattern.equals(poker.pattern)) return false;
return point.equals(poker.point);
}
@Override
public int hashCode() {
int result = pattern.hashCode();
result = 31 * result + point.hashCode();
return result;
}
@Override
/**
* 重写compareTo(),比较两张牌,点数较大者大,点数相同时,花色排在前面的大
*/
public int compareTo(Object o) {
List<Poker> pokerList = createPoker();
Poker temp = (Poker) o; //将传参强制转换为Poker对象
int index1 = pokerList.indexOf(this);
int index2 = pokerList.indexOf(temp);
if (index1 > index2) return 1;
else if (index1 == index2)return 0;
else return -1;
}
/**
* 创建扑克牌,返回扑克牌List
* 按小从到大的顺序创建扑克,方便在compareTo中进行比较
*/
public List<Poker> createPoker(){
List<Poker> pokerList = new ArrayList<Poker>(); //整副扑克牌集合
String[] patterns = {"方片","梅花","红桃","黑桃"};
String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A",};
int len1 = patterns.length;
int len2 = points.length;
for (int i = 0; i < len2; i++) {
for (int j = 0; j < len1; j++) {
Poker newPoker = new Poker(patterns[j],points[i]);
pokerList.add(newPoker);
}
}
return pokerList;
}
}
Player.java
package com.imooc;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Player {
private Integer id;
private String name;
private Set<Poker> handCards;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Poker> getHandCards() {
return handCards;
}
public void setHandCards(Set<Poker> handCards) {
this.handCards = handCards;
}
public Player(Integer id, String name) {
this.id = id;
this.name = name;
this.handCards = new HashSet<Poker>(); //需要在构造函数中初始化,否则在主函数中调用getHandCards().add()时会报空指针异常
}
/**
* 判断玩家的最大手牌,返回Poker对象
*/
public Poker getMaxPoker(){
//由于HashSet类型无法取到集合中第一张排,先定义一张最小的牌
Poker maxPoker = new Poker("方片","2");
if(handCards.isEmpty()){
System.out.println("玩家手上无牌");
return null;
}
for (Poker poker: handCards) {
int result = poker.compareTo(maxPoker);
if(result > 0){
maxPoker.setPattern(poker.getPattern());
maxPoker.setPoint(poker.getPoint());
}
}
return maxPoker;
}
}
PlayGame.java
package com.imooc;
import java.util.*;
/**
* 实现功能
* 简易扑克牌游戏 游戏介绍:
* 1. 创建一副扑克牌,不考虑大小王
* 2. 创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
* 3. 洗牌,将之前创建的扑克牌顺序打乱(说明是有序的)
* 4. 发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
* 5. 开始游戏,比大小,取两人各自的点数最大的牌进行比较,点数大的赢,若大小相同比花色(黑红梅方)
**/
public class PokerGame {
private Scanner input;
private List<Poker> pokerList; //整副扑克牌集合
private List<Player> playersList; //玩家集合
private int count = 4; //玩家个数
public PokerGame() {
this.input = new Scanner(System.in);
this.playersList = new ArrayList<Player>();
this.pokerList = new ArrayList<Poker>();
}
/**
* 1.创建扑克牌,直接调用Poker类中的createPoker()方法创建
*/
public void createPoker(){
System.out.println("------------创建扑克牌-----------------");
Poker pk = new Poker();
pokerList = pk.createPoker();
for (int i = 0; i < pokerList.size(); i++) {
System.out.print(pokerList.get(i).getPattern() + pokerList.get(i).getPoint() + " ");
}
System.out.println("\n");
System.out.println("-----------创建扑克牌完成,共" + pokerList.size() + "张-----------------");
}
/**
* 2.创建玩家
* @param args
*/
public void createPlayer(){
System.out.println("-------------创建玩家--------------");
for (int i = 0; i < count; i++) {
System.out.println("请输入第" + (i+1) + "位玩家的ID和姓名:");
Integer id;
while (true) {
try {
System.out.println("请输入ID:");
id = Integer.parseInt(input.next());
} catch (Exception e){
System.out.println("请输入整数ID");
continue;
}
System.out.println("请输入姓名:");
String name = input.next();
playersList.add(i, new Player(id, name));
break;
}
}
//提示欢迎信息
for (Player player:playersList) {
System.out.println("欢迎玩家:" + player.getName());
}
System.out.println("----------------创建玩家结束------------");
}
/**
* 3.洗牌
* @param args
*/
public void pokerShuffle(){
System.out.println("-------------开始洗牌--------------");
Collections.shuffle(pokerList);
for (Poker poker:
pokerList) {
System.out.print(poker.getPattern() + poker.getPoint() + " ");
}
System.out.print("\n");
System.out.println("-------------洗牌结束--------------");
}
/**
* 4.发牌
* @param args
*/
public void pokerDeal(){
System.out.println("-------------开始发牌--------------");
for (int i = 0; i < 2; i++) { //每人两张牌
for (int j = 0; j < count; j++) {
System.out.println("玩家:" + playersList.get(j).getName() + "拿牌");
//index计算当前应发第几张牌,temp获取该张牌,并加入到该玩家的handCards集合中
int index = j + i*count;
Poker temp = new Poker(pokerList.get(index).getPattern(),pokerList.get(index).getPoint());
playersList.get(j).getHandCards().add(temp);
}
}
System.out.println("-------------发牌结束--------------");
}
/**
* 5.游戏,比大小
* 先计算每个玩家的最大手牌,放到一个集合中,再计算这个集合中的最大牌,通过最大牌的索引可知胜出玩家
* @param args
*/
public void game() {
System.out.println("---------------开始游戏--------------");
//计算每个玩家的最大手牌,放到集合中
List<Poker> maxPokerList = new ArrayList<Poker>();
for (Player player : playersList) {
Poker temp = player.getMaxPoker();
maxPokerList.add(temp);
System.out.println("玩家" + player.getName() + "的最大手牌为:" + temp.getPattern() + temp.getPoint());
}
Poker maxPoker = maxPokerList.get(0); //临时对象,存放玩家最大手牌,先赋值为第一个玩家的最大手牌
int maxIndex = 0; //临时变量,存放拥有最大手牌的玩家索引值
for (Poker poker : maxPokerList) {
int result = poker.compareTo(maxPoker);
if (result > 0) {
maxPoker = poker;
maxIndex = maxPokerList.indexOf(poker);
}
}
//输出最终输赢结果
System.out.println("----------玩家" + playersList.get(maxIndex).getName() + "获胜--------------");
//输出玩家手牌信息以便验证结果
System.out.println("玩家各自的手牌为");
for (Player player:playersList) {
System.out.print("玩家" + player.getName() + ":");
for (Poker poker:player.getHandCards()) {
System.out.print(poker.getPattern() + poker.getPoint() + " ");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
PokerGame pg = new PokerGame();
pg.createPoker();
pg.createPlayer();
pg.pokerShuffle();
pg.pokerDeal();
pg.game();
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦