大家看看我的
package day42.TestDrive;
import java.util.*;
public class PlayingCard {
public static void main(String[] args){
//创建一副新扑克牌
System.out.println("-------开始创建扑克牌!---------------");
Poker poker = new Poker();
List<Poker> p = poker.CreatAPoker();
System.out.println("-------创建扑克牌成功!---------------");
System.out.println("------------开始洗牌------------");
Collections.shuffle(p);//洗牌
System.out.println("------------洗牌结束!-----------------");
System.out.println("------------创建玩家-------------------");
Player player = new Player();
List<Player> players = player.creatPlayer();
//开始发牌
PlayingCard pc = new PlayingCard();
pc.deal(players, p);
}
//发牌给玩家
public void deal(List<Player> pl, List<Poker> po) {
System.out.println("开始发牌");
List<Integer> numList = new ArrayList<Integer>();
List<Poker> pokerList = new ArrayList<Poker>();
List<String> aStr = new ArrayList<String>();//a玩家拿的牌
List<String> bStr = new ArrayList<String>();//b玩家拿的牌
List<Integer> aNum = new ArrayList<Integer>();//a玩家拿的牌转为点数大小
List<Integer> bNum = new ArrayList<Integer>();//b玩家拿的牌转为点数大小
//轮流各自发两张牌
Random random = new Random();
for(int i = 0; i < pl.size() * 2; i++) {
int num = random.nextInt(po.size());
do {
pokerList.add(po.get(num));
}while(pokerList.size() != new HashSet<Poker>(pokerList).size());
}
//牌的牌名List
for(int i = 0; i < pokerList.size(); i++) {
if(i % 2 == 0) {
System.out.println("【 "+pl.get(0).getPlayerName()+"拿一张! 】");
aStr.add(pokerList.get(i).suit + pokerList.get(i).point);
}else {
System.out.println("【 "+pl.get(1).getPlayerName()+"拿一张! 】");
bStr.add(pokerList.get(i).suit + pokerList.get(i).point);
}
}
//打印出玩家各自手中的牌
System.out.println("***>>> "+pl.get(0).getPlayerName()+" 的牌是"+aStr);
System.out.println("***>>> "+pl.get(1).getPlayerName()+" 的牌是"+bStr);
Poker poker = new Poker();
for(int i = 0; i < pokerList.size(); i++) {
int num = poker.numTransformation(pokerList.get(i));
poker.pokerNum.add(num);
}
//将玩家手里的牌列出最大值
Collections.sort(poker.pokerNum);
int maxPokerNum = poker.pokerNum.get(poker.pokerNum.size()-1);
//牌的点数List
for(int i = 0; i < pokerList.size(); i++) {
if(i % 2 == 0) {
aNum.add(poker.numTransformation(pokerList.get(i)));
}else {
bNum.add(poker.numTransformation(pokerList.get(i)));
}
}
//判断两个人谁手里的牌最大
if(aNum.contains(maxPokerNum)) {
System.out.println("***>>> "+pl.get(0).getPlayerName()+" 赢了!"+" <<<***");
}else if(aNum.contains(maxPokerNum)) {
System.out.println("***>>> "+pl.get(1).getPlayerName()+" 赢了!"+" <<<***");
}
}
}
//创建玩家类
class Player{
private String playerName;//玩家名字
private int playerId;//玩家ID
public List<String> hand = new ArrayList<String>();//手牌:扑克牌的集合
public Player() {
}
//Player的含参构造方法
public Player(String playerName, int playerId) {
this.playerName = playerName;
this.playerId = playerId;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
@Override
public String toString() {
return "Player [playerName=" + playerName + ", playerId=" + playerId + "]";
}
//创建玩家名片
public List<Player> creatPlayer() {
//创建两名玩家
Player player1 = new Player();
Player player2 = new Player();
List<Player> playerList = new ArrayList<Player>();
Player[] players = {player1, player2};
Scanner input = new Scanner(System.in);
int i = 1;
for(Player pl:players ) {
System.out.println("请输入第"+i+"位玩家的姓名:>>>");
pl.setPlayerName(input.next());
for(int j = 0; j < 3; j++) {
try{
System.out.println("请输入第"+i+"位玩家的ID:>>>");
pl.setPlayerId(input.nextInt());
break;
}catch(Exception ex) {
input = new Scanner(System.in);//阻止无限循环
System.out.println("请输入整数ID!");
}
}
i++;
playerList.add(pl);
}
for(Player pl1:players) {
System.out.println("------欢迎玩家:"+pl1.getPlayerName());
}
return playerList;
}
}
//创建扑克类
class Poker{
public String suit;
public String point;
public String[] suitArray = {"方块", "梅花", "红桃", "黑桃"};//花色
public String[] pointArray = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};//点数
public List<String> p = new ArrayList<String>();//打印扑克
public List<Poker> poker = new ArrayList<Poker>();//一副扑克
public List<Integer> pokerNum = new ArrayList<Integer>();//每张牌对象的集合
//Poker的构造方法
public Poker() {
}
public Poker(String suit, String point) {
this.suit = suit;
this.point = point;
}
//创建一副扑克
public List<Poker> CreatAPoker() {
for(int j = 0; j < pointArray.length; j++) {
for(int i = 0; i < suitArray.length; i++) {
Poker pn = new Poker(suitArray[i], pointArray[j]);
poker.add(pn);
}
}
for(int i = 0; i < poker.size(); i++) {//String型的是通过Poker来的
String sb = poker.get(i).suit+poker.get(i).point;
p.add(sb);
}
for(int i = 0; i < poker.size(); i++) {//Integer型的也是通过Poker来的
pokerNum.add(numTransformation(poker.get(i)));
}
//打印出一副新建的扑克牌
System.out.println(p);//poker是String类型所以可以打印出来?
return poker;
}
//点数转换数字比大小
public int numTransformation(Poker c) {
//花权值
int ps = 0;
if(c.suit == "方块") {
ps = 1;
}else if(c.suit == "梅花"){
ps = 2;
}else if(c.suit == "红桃") {
ps = 3;
}else if(c.suit == "黑桃") {
ps = 4;
}
//牌权值
int pp = 0;
if(c.point == "J") {
pp = 11;
}else if(c.point == "Q") {
pp = 12;
}else if(c.point == "K") {
pp = 13;
}else if(c.point == "A") {
pp = 12;
}else if(c.point == "2") {
pp = 13;
}else {
pp = Integer.parseInt(c.point);//数字型字符串转换成数字
}
return pp * 10 + ps;
}
}