继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第三季——简易扑克游戏

慕函数8614728
关注TA
已关注
手记 2
粉丝 0
获赞 7

简易扑克游戏

一、主要想法

1.创建了3个类,分别用来创建扑克,创建玩家,以及实现主函数

2.用list储存扑克牌,用Map储存玩家,用list储存玩家的手牌

3.扑克牌大小比较的方法

·创建两个字符串数组存放花色(decor)和(points),里面的元素按大小排列好,将它们赋给扑克牌

.返回要比较的属性在字符串中的位置,比较他们的大小

·写一个查找数组中元素位置的方法(一直没有找到好一些的,自己写了一个for循环)

二、问题

1.扑克牌大小比较的方法一直纠结了好久,想到了可以用数组的位置比较,或者将数组转换成list集合比较,但我觉得这样效率低,看了其他人的方法,有个人字符串储存的花色和点数,没用数组,感觉比较方便一些

2.储存手牌的集合我定义成list,但我感觉手牌应该是无序的,应该用set集合,由于我的比较方法需要输入的形参是两个扑克牌,用list比较方便,想改成set

3.获取数组中元素的位置的方法自己写的,想知道java中有什么好用的方法

4.第一次学编程,经历少,要是有什么习惯上的错误可以提

三、实现效果

整体流程图片描述

类型不匹配错误图片描述

次数太多错误图片描述

四、代码

//Poker类,用来创建扑克牌
public class Poker {
	private String decor;
	private String points;
	public Poker(String decor,String points){
		this.decor=decor;
		this.points=points;
	}		
	public String getDecor() {
		return decor;
	}
	public String getPoints() {
		return points;
	}
}
//Player类,用来创建玩家
import java.util.ArrayList;
import java.util.List;
public class Player {
	private int id;
	private String name;
	private List<Poker> pokerHand;
	public Player(int id,String name){
		this.id=id;
		this.name=name;
		pokerHand=new ArrayList<Poker>();
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public List<Poker> getPokerHand() {
		return pokerHand;
	}
	public void addPokerHand(Poker poker) {
		pokerHand.add(poker);
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Player))
			return false;
		Player other = (Player) obj;
		if (id != other.id)
			return false;
		return true;
	}
}
//Game类,用来实现游戏的各个功能
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Game {
	private Scanner console;
	private StringBuilder sb;
	private List<Poker> pokerList;
	private Map<Integer,Player> players;
	private Set<Entry<Integer,Player>> entrys;
	private String[] decor={"","","",""};
	private String[] points={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
	public Game(){
		sb=new StringBuilder();
		pokerList=new ArrayList<Poker>();
		console=new Scanner(System.in);
		players=new HashMap<Integer,Player>();
		entrys=players.entrySet();
	}	
	private void createPokerCards(){
		System.out.println("-------------创建扑克牌--------------");
		for (int i = 0; i < decor.length; i++) {
			for (int j = 0; j < points.length; j++) {
				pokerList.add(new Poker(decor[i],points[j]));
			}
		}
		System.out.println("-------------扑克牌创建成功!--------------");
		for (Poker poker : pokerList) {
			sb.append(poker.getDecor()+poker.getPoints()+",");
		}
		sb.deleteCharAt(sb.length()-1);
		System.out.println("为:["+sb+"]");
	}	
	private void shuffleCards(){
		System.out.println("-------------开始洗牌...--------------");
		Collections.shuffle(pokerList);
		System.out.println("-------------洗牌结束!--------------");
	}
	private void createPlayers(){
		int id;
		int limit=1;
		System.out.println("-------------创建玩家...--------------");
		for (int i = 0; i < 2; i++) {
			System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
			while(true){
				try{
					System.out.println("输入ID(最多输入3次):");
					//每次循环都需要重新new一个Scanner对象
					console=new Scanner(System.in);
					id=console.nextInt();
					if(players.containsValue(new Player(id,null)))
						System.out.println("该ID已被占用!");
					else
						break;
				}catch(InputMismatchException e){
					if(limit<3){
						System.out.println("请输入正确的ID!(整数)");
						limit++;
					}else{
						System.out.println("输入次数达到上限!账户被锁死,无法输入,游戏结束!");
						System.exit(1);
					}
				}
			}
			System.out.println("输入姓名");
			String name=console.next();
			players.put(i, new Player(id,name));
		}
		//用EntrySet方法输出Map类型变量
		for (Entry<Integer,Player> entry : entrys) {
			System.out.println("----玩家:"+entry.getValue().getName());
		}
	}
	private void dealCards(){
		System.out.println("-------------开始发牌--------------");
		int dealCardsTimes=2;
		int k=0;
		//将pokerList中的牌(对象)按顺序放到Map类型的players(value值)的pokerHand中	
		for (int i = 0; i < dealCardsTimes; i++) {
			for (Entry<Integer, Player> entry : entrys) {
				entry.getValue().addPokerHand(pokerList.get(k));
				System.out.println("----玩家:"+entry.getValue().getName()+"-拿牌");
				k++;
			}
		}
		System.out.println("-------------发牌结束!--------------");
	}
	private void handCardsView(){
		System.out.println("玩家各自的手牌为:");
		//和老师展示的不同,尝试用StringBuilder方法
		for (Entry<Integer, Player> entry : entrys) {
			sb.delete(0, sb.length());
			for (Poker poker : entry.getValue().getPokerHand()) {
				sb.append(poker.getDecor()+poker.getPoints()+",");
			}
			sb.deleteCharAt(sb.length()-1);
			System.out.println(entry.getValue().getName()+":["+sb+"]");
		}
	}	
	private void compareGame(){
		System.out.println("-------------开始游戏...--------------");
		Poker[] pokers=new Poker[2];
		int i=0;
		for (Entry<Integer, Player> entry : entrys) {
			pokers[i]=comparePoker(entry.getValue().getPokerHand().get(0),entry.getValue().getPokerHand().get(1));
			System.out.println("玩家:"+entry.getValue().getName()+"最大的手牌为:"+pokers[i].getDecor()+pokers[i].getPoints());
			i++;
		}
		Poker newPoker=comparePoker(pokers[0],pokers[1]);
		int winner=arraySort(pokers,newPoker);
		System.out.println("-------------玩家:"+players.get(winner).getName()+"获胜--------------");
	}
	/**
	 * 比较扑克牌的大小
	 * @param pok1
	 * @param pok2
	 * @return
	 */
	private Poker comparePoker(Poker pok1,Poker pok2){
		int point1=arraySort(points, pok1.getPoints());
		int point2=arraySort(points, pok2.getPoints());
		int decor1=arraySort(decor, pok1.getDecor());
		int decor2=arraySort(decor, pok2.getDecor());		
		if(point1>point2)
			return pok1;
		if(point1<point2)
			return pok2;
		if(point1==point2){
			if(decor1>decor2)
				return pok1;
			if(decor1<decor2)
				return pok2;
			if(decor1==decor2)
				return pok1;
		}
		return pok1;
	}
	/**
	 * 获取数组中元素的位置
	 * @param obj
	 * @param o
	 * @return
	 */
	private int arraySort(Object[] obj,Object o){
		int j=-1;
		if(o instanceof String){
			for (int i = 0; i < obj.length; i++) {
				if(obj[i].equals(o))
					j=i;
			}
		}	
		if(o instanceof Poker){
			Poker[] other=(Poker[])obj;
			Poker oth=(Poker)o;
			for (int i = 0; i < obj.length; i++) {
				if(other[i].getDecor().equals(oth.getDecor())
						&&other[i].getPoints().equals(oth.getPoints()))
					j=i;
			}
		}		
		return j;
	}
	public static void main(String[] args) {
		Game ga=new Game();
		ga.createPokerCards();
		ga.shuffleCards();
		ga.createPlayers();
		ga.dealCards();
		ga.compareGame();
		ga.console.close();
		ga.handCardsView();
	}
}

打开App,阅读手记
7人推荐
发表评论
随时随地看视频慕课网APP