package com.puke;
public class Poker {
/**
* 扑克类
*/
private String num; //扑克点
private String flower; //扑克花
private String poker; //一张扑克
public Poker(String pnum,String pflower) {
this.num = pnum;
this.flower = pflower;
this.poker = pflower+pnum;
}
/**
* 获取扑克点数
* @return
* 由于根据默认字符串的比较规则 10是比2 . 3 . 4 ....9 要小的
* 字符串的默认排序规则是 先排数字第一位 如果相等比较第二位 .. 然后在大写字母 小写字母 依次类推
* 这里的 10 J Q K A 要返回一个数字类型的字符串 用来给扑克排序
*/
public String getPokerNum(){
if (this.num == "10") {
this.num = "91";
}else if (this.num == "J") {
this.num = "92";
}else if (this.num == "Q") {
this.num = "93";
}else if (this.num == "K") {
this.num = "94";
}else if (this.num == "A") {
this.num = "1";
}
return this.num;
}
/**
* 获取扑克的花
* @return
*/
public String getPokerFlower(){
return this.flower;
}
@Override
public String toString() {
return poker;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((flower == null) ? 0 : flower.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Poker))
return false;
Poker other = (Poker) obj;
if (flower == null) {
if (other.flower != null)
return false;
} else if (!flower.equals(other.flower))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
return true;
}
}
==================================================================
package com.puke;
import java.util.Comparator;
public class PokerComparator implements Comparator<Poker> {
/**
* 比较规则类
*/
@Override
//比较优先级 先比较点数 点数相同再比较花色
public int compare(Poker arg0, Poker arg1) {
String num0 = ((Poker) arg0).getPokerNum();
String num1 = ((Poker) arg1).getPokerNum();
int ret = 0;
if((ret = num0.compareTo(num1)) == 0){
String flw0 = ((Poker) arg0).getPokerFlower();
String flw1 = ((Poker) arg1).getPokerFlower();
ret = flw0.compareTo(flw1);
}
return ret;
}
}
===============================================================
package com.puke;
import java.util.ArrayList;
import java.util.List;
/**
* 玩家类
* @author momo1a@qq.com
*
*
*/
public class Player {
private int ID; //玩家ID
private String name; //玩家名字
private List<Poker> pokerCard; //玩家手牌集合
/**
* 无参构造
*/
public Player() {
}
/**
* 初始化玩家
* @param ID 玩家ID
* @param name 玩家用户名
* 初始化手牌类
*/
public Player(int ID,String name) {
this.pokerCard = new ArrayList<Poker>();
this.ID = ID;
this.name = name;
}
/**
* 获取用户ID
* @return 用户ID
*/
public int getPlayerID(){
return this.ID;
}
/**
* 设置用户ID
* @param ID
*/
public void setPlayerID(int ID) {
this.ID = ID;
}
/**
* 获取用户名
* @return 用户名
*/
public String getPlayerName(){
return this.name;
}
/**
* 设置用户名
* @param name
*/
public void setPlayerName(String name) {
this.name = name;
}
/**
* 设置玩家手牌
* @param poker
*
*/
public void setPokerCard(Poker poker){
this.pokerCard.add(poker);
}
/**
* 获取玩家手牌
* @return 玩家手牌集合
*/
public List<Poker> getPokerCard(){
return this.pokerCard;
}
@Override
public String toString() {
return name;
}
}
===============================================================
package com.puke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PlayGame {
private String numbers[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private String flowers[]={"方块","梅花","红桃","黑桃"};
private List<Poker> pokerList; //一副扑克的集合
private List<Player> players; //玩家集合
private List<Poker> maxPokerList; //出牌的集合 (也就是各自拿来比较那张大牌的集合)
/**
* 无参构造方法 初始化一副扑克
*/
public PlayGame(){
players = new ArrayList<Player>();
pokerList = new ArrayList<Poker>();
maxPokerList = new ArrayList<Poker>();
System.out.println("-----------创建扑克牌----------");
for (int i = 0; i < flowers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
Poker poker = new Poker(numbers[j], flowers[i]);
this.pokerList.add(poker); //创建一张扑克就添加到扑克的集合
}
}
System.out.println("-----------扑克牌创建成功----------");
System.out.println("为:"+this.pokerList);
}
/**
* 洗牌功能
*/
public void washPoker() {
System.out.println("---------开始洗牌------------");
for (int i = 0; i < pokerList.size(); i++) {
Random random = new Random();
int j = random.nextInt(pokerList.size());
Poker temp = pokerList.get(i); //用一个变量保存 i位置的一张扑克
pokerList.set(i, pokerList.get(j)); //用j位置(随机长生)的扑克替换i位置的扑克
pokerList.set(j, temp); //j位置的扑克就替换成原来i位置的扑克
}
System.out.println("---------洗牌结束!-----------");
}
/**
* 创建玩家 并把玩家添加到玩家集合
* 输入玩家的id必须为数字类型
*
*/
public void createPlayer(){
Scanner input = new Scanner(System.in);
this.washPoker(); //洗牌
System.out.println("请输入两位玩家的ID和用户名");
for (int i = 0; i < 2; i++) {
System.out.println("输入第"+(i+1)+"位玩家ID:");
int ID = 0;
try {
ID = input.nextInt();
System.out.println("输入第"+(i+1)+"位玩家用户名:");
} catch (Exception e) {
System.out.println("请输入整数类型ID!!!");
this.createPlayer();
}
String name = input.next();
players.add(new Player(ID, name)); //添加用户到用户集合中
}
//输出欢迎字样
for (int i = 0; i < players.size(); i++) {
System.out.println("----欢迎玩家:"+players.get(i));
}
}
/**
* 发牌功能
* 从第一张开始发牌 轮流拿牌 各自发两张牌
*/
public void perPokerCard(){
System.out.println("-----开始发牌------");
for (int i = 0, n=0; i < 2; i++) {
for (int j = 0; j < players.size(); j++,n++) {
this.players.get(j).setPokerCard(this.pokerList.get(n));
System.out.println("----玩家:"+players.get(j)+"-拿牌");
}
}
System.out.println("------发牌结束!!!------");
}
/**
* 开始玩游戏 比大小
* 比手牌大小的规则来自PokerComparator类
* 先比玩家的点数 A<2<3<4<5<6<7<8<9<10<J<Q<K
* 如果点数相同 在比较花色 方块<梅花<红桃<黑桃
*/
public void playGame(){
System.out.println("----开始游戏...-----");
for (int i = 0; i < players.size(); i++) {
System.out.println("玩家:"+players.get(i)+"最大的手牌为:"+ Collections.max(this.players.get(i).getPokerCard(),new PokerComparator()));
maxPokerList.add(Collections.max(this.players.get(i).getPokerCard(),new PokerComparator())); //添加各自的大牌到一个List
}
Poker winPoker = Collections.max(maxPokerList, new PokerComparator()); //比较大牌的List集合 决出 最大的一张扑克
//判断最大那张扑克属于谁的 谁就是胜利者
for (int i = 0; i < players.size(); i++) {
if (players.get(i).getPokerCard().contains(winPoker)) {
System.out.println("---------玩家:"+players.get(i)+"获胜 !---------");
}
}
System.out.println("各自玩家的手牌为:");
for (int i = 0; i < players.size(); i++) {
System.out.println(players.get(i)+":"+players.get(i).getPokerCard());
}
}
}
==================================================================
package com.puke;
public class ShowPlay {
/**
* main()方法
* @param args
*/
public static void main(String[] args) {
PlayGame showpoker = new PlayGame(); //初始化游戏 新建52张扑克
showpoker.createPlayer(); //新建玩家
showpoker.perPokerCard(); //发牌
showpoker.playGame(); //玩游戏 比大小
}
}