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

【Java入门第三季】项目实战:简易扑克牌

qq_晓木珊子_0
关注TA
已关注
手记 5
粉丝 4
获赞 14
// 类:Player
package com.imooc;

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

public class Player {
	//属性
	private String id;
	private String name;
	public List<Card> cardsList;
	//构造器
	public Player(String id,String name) {
		this.id=id;
		this.name=name;
		this.cardsList=new ArrayList<Card>();
	}
	public String getName() {
		return name;
	}
	public List<Card> getCardsList() {
		return cardsList;
	}
	public String getId() {
		return id;
	}

	
}
//类:Card
package com.imooc;

import java.util.Comparator;

public class Card implements Comparator<Card>{
	//属性
	private String color;
	private String point;
	private String cardString;
	//构造器
	public Card(String color,String point) {
		this.color=color;
		this.point=point;
		this.cardString=color+point;
	}
	public Card() {
		
	}
	public String getColor() {
		return color;
	}
	public String getPoint() {
		return point;
	}
	public String getCardString() {
		return cardString;
	}
	@Override
	public int compare(Card o1, Card o2) {
		int pO1=0;
		int pO2=0;
		int cO1=0;
		int cO2=0;
//		System.out.println(o1.point);
//		System.out.println(o2.point);
		String[] stringArray= {"3","4","5","6","7","8","9",
				"10","J","Q","K","A","2"};
		//o1
		for(int i=0;i<stringArray.length;i++) {
			if(stringArray[i].equals(o1.point)) {
				pO1=i;
				break;
			}else {
				continue;
			} 		
		}
		//o2
		for(int i=0;i<stringArray.length;i++) {
			if(stringArray[i].equals(o2.point)) {
				pO2=i;
				break;
			}else {
				continue;
			} 		
		}
		if(pO1>pO2) {
			return 1;
		}else if(pO1<pO2) {
			return -1;
		}else {
//			System.out.println(o1.color);
//			System.out.println(o2.color);
			String[] stringColorArray= {"方片","梅花","红桃","黑桃"};
			//o1
			for(int j=0;j<stringColorArray.length;j++) {
				if(stringColorArray[j].equals(o1.color)) {
					cO1=j;
					break;
				}else {
					continue;
				} 		
			}
			//o2
			for(int j=0;j<stringColorArray.length;j++) {
				if(stringColorArray[j].equals(o2.color)) {
					cO2=j;
					break;
				}else {
					continue;
				} 		
			}
			if(cO1>cO2) {
				return 1;
			}else {
				return -1;
			}
		}
	} 


}
//类:PlayCards,主函数
package com.imooc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class PlayCards {
	//属性
	public Scanner console;
	public static final String[] points= {"3","4","5","6","7","8","9",
			"10","J","Q","K","A","2"};
	public static final String[] colors= {"方片","梅花","红桃","黑桃"};
	//方法
	public PlayCards() {
		console=new Scanner(System.in);
	}
    /**
     * 创建扑克牌
     * @param args
     */
	public List<Card> constrCards() {
		System.out.println("-------创建扑克牌-------");
		List<Card> cardsFinalList=new ArrayList<Card>();
		for (String color : colors) {
			for (String point : points) {
				cardsFinalList.add(new Card(color,point));
			}
		}
		System.out.println("扑克牌创建成功,共创建"+
		                  cardsFinalList.size()+"张牌,分别为: ");
		String[] cardsFinalStringArrays=castToStringArray(cardsFinalList);
		System.out.println(Arrays.toString(cardsFinalStringArrays));
		return cardsFinalList;
		
	} 
	/**
	 * 将List<Card>转换成String[],每个元素是卡片信息
	 */
	public String[] castToStringArray(List<Card> cardsList) {
		String[] cardsString=new String[cardsList.size()];
		for(int i=0;i<cardsList.size();i++) {
			cardsString[i]=cardsList.get(i).getCardString();
		}
		return cardsString;
	}
	/**
	 * 洗牌:将一组扑克牌序列打乱顺序
	 * @param args
	 */
	public List<Card> cardsMix(List<Card> cardsToMix){
		System.out.println("-------开始洗牌--------");
		Collections.shuffle(cardsToMix);
		System.out.println("------洗牌结束---------");
//		String[] cardsToMixString=castToStringArray(cardsToMix);
//		System.out.println("洗牌之后的扑克牌为: ");
//		System.out.println(Arrays.toString(cardsToMixString));
		return cardsToMix;
	}
	/**
	 * 创建玩家,玩家个数playerNum,每个玩家的牌数cardsNum
	 * @param args
	 */
	public List<Player> constrPlayers(List<Card> cardsAfterMix) {
		System.out.println("------创建玩家-----");
		int playerNum=2;
		int cardsNum=2;
		String id=null;
		String name=null;
		List<Player> playerList=new ArrayList<Player>();
		for(int i=0;i<playerNum;i++) {
		        System.out.println("请输入第"+(i+1)+"位玩家的ID: ");
			id=console.next();
			System.out.println("请输入第"+(i+1)+"位玩家的姓名: ");
			name=console.next();
			playerList.add(new Player(id, name));			
			}
		for(int j=0;j<playerNum;j++) {
			System.out.println("欢迎玩家"+playerList.get(j).getId()+": "+
		                     playerList.get(j).getName());
		}
		//发牌
		System.out.println("------开始发牌-------");
		for(int k=0;k<cardsAfterMix.size();k++) {
			if(playerList.get(k%playerNum).cardsList.size()<cardsNum) {
				playerList.get(k%playerNum).cardsList.add(cardsAfterMix.get(k));
				System.out.println("----玩家"
				+playerList.get(k%playerNum).getName()+"拿牌");
			}else {
				break;
			}
		}
		System.out.println("----发牌结束-----");
//		for (Player player : playerList) {
//			 System.out.println("玩家牌为: ");
//			 System.out.println(Arrays.toString(castToStringArray(player.getCardsList())));
//		}
		return playerList;
	}
	/**
	 * 游戏
	 * @param args
	 */
	public void playStarter(List<Player> playerList) {
		List<Card> cardsListForEachPlayer=new ArrayList<Card>();
		List<Card> maxCardsListForAllPlayers=new ArrayList<Card>();
		for(int i=0;i<playerList.size();i++) {
			cardsListForEachPlayer=playerList.get(i).getCardsList();
			Collections.sort(cardsListForEachPlayer, new Card());
			maxCardsListForAllPlayers.add(cardsListForEachPlayer.
					get(cardsListForEachPlayer.size()-1));
			System.out.println("玩家"+playerList.get(i).getName()+"的最大牌为: "
	                    +cardsListForEachPlayer.
	                    get(cardsListForEachPlayer.size()-1).
	                    getCardString());
		}
		Collections.sort(maxCardsListForAllPlayers, new Card());
		Card maxCard=maxCardsListForAllPlayers.get(playerList.size()-1);
//		// 输出最大牌
//		String maxCardString=maxCardsListForAllPlayers.
//	             get(playerList.size()-1).getCardString();
//		System.out.println("最大牌为:");
//		System.out.println(maxCardString);
		// 判断赢家并输出
		for (int j=0;j<playerList.size();j++) {
			if(playerList.get(j).getCardsList().contains(maxCard)) {
				System.out.println("玩家"+playerList.get(j).getName()+"获胜!");
				break;
			}
		}
	}
	/**
	 * 展示玩家各自的手牌
	 * @param args
	 */
	public void showCardsForEachPlayer(List<Player> playerList) {
		System.out.println("玩家各自的手牌为: ");
		for (int i=0;i<playerList.size();i++) {
			 System.out.println(playerList.get(i).getName()+": "
		               +Arrays.toString(castToStringArray(playerList.get(i).getCardsList())));
		}
	}
	
	public static void main(String[] args) {
		PlayCards pc=new PlayCards();
		//创建扑克牌
		List<Card> cardsInitial=pc.constrCards();
		//洗牌
		List<Card> cardsAfterMix=new ArrayList<Card>();
		cardsAfterMix=pc.cardsMix(cardsInitial);
		//创建玩家并发牌
		List<Player> playerList=pc.constrPlayers(cardsAfterMix);
		//开始游戏
		pc.playStarter(playerList);
		//展示玩家各自的手牌
		pc.showCardsForEachPlayer(playerList);
	}

}


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