package com.imooc.cardsdemo;
public class Cards implements Comparable<Cards>{
// 牌的属性,花色和点数
private String huaSe;
private String dianShu;
// 对扑克牌的点数取值,用于排序
public int getDianshuValue(String dianshu) {
String num = dianShu;
int numValue = 0;
switch (num) {
case "3":numValue = 1;break;
case "4":numValue = 2;break;
case "5":numValue = 3;break;
case "6":numValue = 4;break;
case "7":numValue = 5;break;
case "8":numValue = 6;break;
case "9":numValue = 7;break;
case "10":numValue = 8;break;
case "J":numValue = 9;break;
case "Q":numValue = 10;break;
case "K":numValue = 11;break;
case "A":numValue = 12;break;
}
return numValue;
}
// 对扑克牌的花色取值,用于排序
public int getHuaseValue(String huase) {
int huaValue = 0;
switch (hua) {
case "黑桃":huaValue = 4;break;
case "红桃":huaValue = 3;break;
case "方块":huaValue = 2;break;
case "草花":huaValue = 1;break;
}
return huaValue;
}
public String getHuaSe() {
return huaSe;
}
public void setHuaSe(String huaSe) {
this.huaSe = huaSe;
}
public String getDianShu() {
return dianShu;
}
public void setDianShu(String dianShu) {
this.dianShu = dianShu;
}
this.huaSe = huaSe;
this.dianShu = dianShu;
}
public Cards() {
// TODO Auto-generated constructor stub
}
// 重写比较的方法,先判断点数大小,相同再判断花色
@Override
public int compareTo(Cards o) {
// TODO Auto-generated method stub
if (this.getDianshuValue(dianShu) > o.getDianshuValue(dianShu)) {
return -1;
}
else if(this.getDianshuValue(dianShu)==o.getDianshuValue(dianShu)) {
if (this.getHuaseValue(huaSe) >= o.getHuaseValue(huaSe)) {
return -1;
}
else {
return 1;
}
}
else {
return 1;
}
}
}
package com.imooc.cardsdemo;
import java.util.ArrayList;
import java.util.List;
public class Player {
// 玩家的属性,编号,姓名以及持有牌的集合
private int playerId;
private String playerName;
public List<Cards> cardsOnHand;
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public Player(int playerId,String playerName) {
this.playerId = playerId;
this.playerName = playerName;
this.cardsOnHand = new ArrayList<Cards>();
}
public Player() {
}
}
package com.imooc.cardsdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Poker {
// pokers为生成所有牌的序列
public static List<Cards> pokers;
public Poker () {
this.pokers = new ArrayList<Cards>();
}
public static void main(String[] args) {
Poker pk = new Poker();
// 生成扑克牌
String[] huase = new String[]{"黑桃","红桃","方块","草花"};
String[] dianshu = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0;i < huase.length;i ++) {
for (int j = 0;j < dianshu.length;j ++) {
Cards card =new Cards(huase[i],dianshu[j]);
pokers.add(card);
}
}
System.out.println("-----扑克牌初始化成功!-----");
System.out.println("成功创建的扑克牌:");
for (Cards cards : pokers) {
System.out.print(cards.getHuaSe() + cards.getDianShu() + " ");
}
System.out.println("");
// 打断pokers序列
Collections.shuffle(pokers);
System.out.println("-----扑克牌洗牌成功!-----");
System.out.println("洗牌后的扑克牌:" );
for (Cards cards : pokers) {
System.out.print(cards.getHuaSe() + cards.getDianShu() + " ");
}
System.out.println("");
System.out.println("创建玩家:");
// 初始化玩家p1和p2
Player p1 = new Player();
Player p2 = new Player();
// 对玩家p1进行编号和姓名的获取,编号输入错误时获取异常并要求重新输入
while (true) {
try {
Scanner console1 = new Scanner(System.in);
System.out.println("请输入第一位玩家的编号:");
int num1 = console1.nextInt();
System.out.println("请输入第一位玩家的名字:");
String nam1 = console1.next();
p1 = new Player(num1,nam1);
break;
}catch(Exception e) {
System.out.println("请输入整数!");
continue;
}
}
// 对玩家p2进行编号和姓名的获取,编号输入错误时获取异常并要求重新输入 while (true) {
try {
Scanner console2 = new Scanner(System.in);
System.out.println("请输入第二位玩家的编号:");
int num2 = console2.nextInt();
System.out.println("请输入第二位玩家的名字:");
String nam2 = console2.next();
p2 = new Player(num2,nam2);
break;
}catch(Exception e) {
System.out.println("请输入整数!");
continue;
}
}
// 输出玩家编号和姓名并开始游戏
System.out.println("两位玩家创建成功!");
System.out.println("玩家1:" + p1.getPlayerId() + " " + p1.getPlayerName());
System.out.println("玩家2:" + p2.getPlayerId() + " " + p2.getPlayerName());
System.out.println("-----开始游戏!-----");
System.out.println("开始发牌...");
// 采用乱序pokers,顺序发牌,按要求每人发2张牌
for (int i = 0;i < 4;i ++) {
Cards x = new Cards();
x = pokers.get(0);
if(i%2 == 0) {
p1.cardsOnHand.add(x);
pokers.remove(x);
System.out.println("玩家" + p1.getPlayerName() + "得到手牌");
}
if (i%2 == 1) {
p2.cardsOnHand.add(x);
pokers.remove(x);
System.out.println("玩家" + p2.getPlayerName() + "得到手牌");
}
}
System.out.println("-----发牌完毕!-----");
// 对各自手牌进行排序,从大到小排列
Collections.sort(p1.cardsOnHand);
Collections.sort(p2.cardsOnHand);
// 展示各自手牌
System.out.print("玩家" + p1.getPlayerName() + "的手牌为:");
System.out.print(p1.cardsOnHand.get(0).getHuaSe()+p1.cardsOnHand.get(0).getDianShu());
System.out.println(p1.cardsOnHand.get(1).getHuaSe()+p1.cardsOnHand.get(1).getDianShu());
System.out.print("玩家" + p2.getPlayerName() + "的手牌为:");
System.out.print(p2.cardsOnHand.get(0).getHuaSe()+p2.cardsOnHand.get(0).getDianShu());
System.out.println(p2.cardsOnHand.get(1).getHuaSe()+p2.cardsOnHand.get(1).getDianShu());
System.out.println("------比赛判断:--------");
// 取各自的最大的牌进行比较,比较过程与排序类似
if (p1.cardsOnHand.get(0).getDianshuValue(p1.cardsOnHand.get(0).getDianShu()) > p2.cardsOnHand.get(0).getDianshuValue(p2.cardsOnHand.get(0).getDianShu())) {
System.out.println("玩家" + p1.getPlayerName() + "的手牌点数比玩家" + p2.getPlayerName() + "的大,获胜的是:" + p1.getPlayerName());
}else if (p1.cardsOnHand.get(0).getDianshuValue(p1.cardsOnHand.get(0).getDianShu()) == p2.cardsOnHand.get(0).getDianshuValue(p2.cardsOnHand.get(0).getDianShu())) {
if (p1.cardsOnHand.get(0).getHuaseValue(p1.cardsOnHand.get(0).getHuaSe()) > p2.cardsOnHand.get(0).getHuaseValue(p2.cardsOnHand.get(0).getHuaSe())) {
System.out.println("玩家" + p1.getPlayerName() + "的手牌花色比玩家" + p2.getPlayerName() + "的大,获胜的是:" + p1.getPlayerName());
}
else {
System.out.println("玩家" + p2.getPlayerName() + "的手牌花色比玩家" + p1.getPlayerName() + "的大,获胜的是:" + p2.getPlayerName());
}
}
else {
System.out.println("玩家" + p2.getPlayerName() + "的手牌点数比玩家" + p1.getPlayerName() + "的大,获胜的是:" + p2.getPlayerName());
}
}
}