定义Poker类,属性有花色和点数
定义Player类,属性有ID、姓名和Poker的List
定义ComparePoker类,继承Comparator接口,覆写compare接口(通过比较扑克牌的花色和点数索引比较大小,索引越大的牌越大)
定义PlayPokerTest类,测试游戏功能
下面是各个类的源码信息:
Poker类
package com.imooc.poker;
public class Poker{
private String suit;
private String point;
public Poker(String suit, String point) {
super();
this.suit = suit;
this.point = point;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public void PrintPokerInfo(){
System.out.println(this.getSuit()+this.getPoint());
}
public void PrintPokerInfo(int i){
System.out.print(this.getSuit()+this.getPoint()+" ");
}
}
Player类
package com.imooc.poker;
import java.util.List;
public class Player {
private String id;
private String name;
private List<Poker> pokerlist;
public Player(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Poker> getPokerlist() {
return pokerlist;
}
public void setPokerlist(List<Poker> pokerlist) {
this.pokerlist = pokerlist;
}
public void PrintPlayerInfo(){
System.out.println(this.getId()+"\t"+this.getName());
}
public void PrintPlayerPokerInfo(){
System.out.print(this.getName()+":");
for(Poker poker:this.getPokerlist()){
poker.PrintPokerInfo(1);
}
System.out.println();
}
}
ComparePoker类
package com.imooc.poker;
import java.util.Comparator;
public class ComparePoker implements Comparator<Poker>{
private static final String[] suits = {"方片","梅花","红桃","黑桃"};
private static final String[] points = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
public static String[] getSuits() {
return suits;
}
public static String[] getPoints() {
return points;
}
public int indexOfArray(String[] array,String key){
int index = 0;
for(String str : array){
index ++;
if(str.equals(key)){
return index;
}
}
return -1;
}
//若p1<p2则返回-1,相等则返回0,否则返回1
@Override
public int compare(Poker p1, Poker p2) {
// TODO Auto-generated method stub
if(indexOfArray(points, p1.getPoint()) > indexOfArray(points, p2.getPoint())){
return 1;
}
else if(indexOfArray(points, p1.getPoint()) < indexOfArray(points, p2.getPoint())){
return -1;
}
else{
if(indexOfArray(suits, p1.getPoint()) > indexOfArray(suits, p2.getSuit())){
return 1;
}
else if(indexOfArray(suits, p1.getPoint()) < indexOfArray(suits, p2.getSuit())){
return -1;
}else{
return 0;
}
}
}
}
PlayPokerTest类
package com.imooc.poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PlayPokerTest {
private static final int POKER_NUM = 52;
private static final int HANDCARD_NUM = 4;
private static String PLAYER1_ID;
private static String PLAYER2_ID;
private static String PLAYER1_NAME;
private static String PLAYER2_NAME;
public static void main(String[] args) {
PlayPokerTest game = new PlayPokerTest();
try{
Scanner scanner = new Scanner(System.in);
System.out.println("输入第1位玩家的ID和姓名");
System.out.println("输入ID:");
PLAYER1_ID = scanner.next();
System.out.println("输入姓名:");
PLAYER1_NAME = scanner.next();
System.out.println("输入第2位玩家的ID和姓名");
System.out.println("输入ID:");
PLAYER2_ID = scanner.next();
System.out.println("输入姓名:");
PLAYER2_NAME = scanner.next();
}
catch(Exception e){
System.out.println("输入有问题");
}
Player player1 = new Player(PLAYER1_ID,PLAYER1_NAME);
Player player2 = new Player(PLAYER2_ID,PLAYER2_NAME);
System.out.println("玩家信息:\n"+"玩家ID\t玩家姓名");
player1.PrintPlayerInfo();
player2.PrintPlayerInfo();
System.out.println("开始创建扑克牌...");
List<Poker> pokerlist = game.createPokerList();
System.out.println("结束创建扑克牌!\n扑克牌为:");
for(Poker poker : pokerlist){
System.out.print(""+poker.getSuit()+poker.getPoint()+",");
}
System.out.println("\n开始洗牌...");
List<Integer> handcardlist = new ArrayList<>();
handcardlist = game.genRandomArray(handcardlist, POKER_NUM, POKER_NUM);
System.out.println("洗牌结束!");
List<Poker> p1List = new ArrayList<>();
List<Poker> p2List = new ArrayList<>();
System.out.println("开始发牌...");
int flag = 1;
for(int i = 0 ; i < HANDCARD_NUM ; i++){
if(flag > 0){
p1List.add(pokerlist.get(handcardlist.get(i)));
System.out.println("发牌给:玩家"+player1.getName());
}else{
p2List.add(pokerlist.get(handcardlist.get(i)));
System.out.println("发牌给:玩家"+player2.getName());
}
flag = -flag;
}
player1.setPokerlist(p1List);
player2.setPokerlist(p2List);
System.out.println("结束发牌!");
Collections.sort(p1List,new ComparePoker());
Collections.sort(p2List,new ComparePoker());
System.out.println("玩家各自的手牌为:");
player1.PrintPlayerPokerInfo();
player2.PrintPlayerPokerInfo();
System.out.println("玩家最大的手牌为:");
Poker poker1_max = game.getMaxPoker(p1List);
Poker poker2_max = game.getMaxPoker(p2List);
System.out.print(player1.getName()+":");
poker1_max.PrintPokerInfo();
System.out.print(player2.getName()+":");
poker2_max.PrintPokerInfo();
ComparePoker comparePoker = new ComparePoker();
if(comparePoker.compare(poker1_max, poker2_max) == 1){
System.out.println(player1.getName()+"获胜");
}else if(comparePoker.compare(poker1_max, poker2_max) == -1){
System.out.println(player2.getName()+"获胜");
}else{
System.out.println("这是个Bug");
}
}
/**
* 创建扑克牌
* @return
*/
public List<Poker> createPokerList(){
List<Poker> pokerlist = new ArrayList<>();
for(String suit : ComparePoker.getSuits()){
for(String point : ComparePoker.getPoints()){
pokerlist.add(new Poker(suit, point));
}
}
return pokerlist;
}
/**
* 洗牌
* @param list
* @param handcardnum
* @param range
* @return
*/
public List<Integer> genRandomArray(List<Integer> list,int handcardnum, int range){
Random random = new Random();
int number;
for(int i = 0 ; i < handcardnum ; ++i){
do{
number = random.nextInt(range);
}
while(list.contains(number));
list.add(number);
}
return list;
}
/**
* 获取最大的扑克牌
* @param list
* @return
*/
public Poker getMaxPoker(List<Poker> list){
return list.get(list.size()-1);
}
}
下面是输出信息:
输入第1位玩家的ID和姓名
输入ID:
1
输入姓名:
jack
输入第2位玩家的ID和姓名
输入ID:
2
输入姓名:
rose
玩家信息:
玩家ID 玩家姓名
1 jack
2 rose
开始创建扑克牌...
结束创建扑克牌!
扑克牌为:
方片3,方片4,方片5,方片6,方片7,方片8,方片9,方片10,方片J,方片Q,方片K,方片A,方片2,梅花3,梅花4,梅花5,梅花6,梅花7,梅花8,梅花9,梅花10,梅花J,梅花Q,梅花K,梅花A,梅花2,红桃3,红桃4,红桃5,红桃6,红桃7,红桃8,红桃9,红桃10,红桃J,红桃Q,红桃K,红桃A,红桃2,黑桃3,黑桃4,黑桃5,黑桃6,黑桃7,黑桃8,黑桃9,黑桃10,黑桃J,黑桃Q,黑桃K,黑桃A,黑桃2,
开始洗牌...
洗牌结束!
开始发牌...
发牌给:玩家jack
发牌给:玩家rose
发牌给:玩家jack
发牌给:玩家rose
结束发牌!
玩家各自的手牌为:
jack:红桃6 方片7
rose:方片10 黑桃A
玩家最大的手牌为:
jack:方片7
rose:黑桃A
rose获胜