根据描述,将抽象出Card类(一张扑克),Poker类(扑克牌类),玩家类(Player)和Game类(主类)
package com.starlet.poker;
/**
* 卡片类(一张扑克)
* @author 刘佳星 [email protected]
*
*/
public class Card implements Comparable {
private String suie; //花色
private int face; //点数
public Card(String suie, int face) {
this.suie = suie;
this.face = face;
}
/**
* 重写toString方法,处理AJQK,把当前对象要显示的信息格式设置成“花色+点数”的形式
*/
@Override
public String toString() {
String faceStr = "";
switch(face) {
case 1 : faceStr="A"; break;
case 11 : faceStr="J"; break;
case 12 : faceStr="Q"; break;
case 13 : faceStr="K"; break;
default : faceStr=String.valueOf(face);
}
return suie + faceStr ;
}
public String getSuie() {
return suie;
}
public void setSuie(String suie) {
this.suie = suie;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
/**
* 卡片比较规则。花色的比较可能有问题...
*/
@Override
public int compareTo(Card o) {
if(this.face == o.face) {
return this.suie.compareTo(o.suie);
}
return this.face - o.face;
}
}
package com.starlet.poker;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 扑克类(一副扑克)
* @author 刘佳星 [email protected]
*
*/
public class Poker {
private String[] colors = {"黑桃","红桃","梅花","方片"};
private List cards;
/**
* 在构造方法中生成一副扑克牌,存储在cards中
*/
public Poker() {
cards = new ArrayList();
for(int i=0; ifor(int j=0; j<13; j++) {
cards.add(i*13+j, new Card(colors[i],j+1));
}
}
}
/**
* 洗牌
*/
public void shuffle() {
// 扑克牌列表长度
int length = cards.size();
Random random = new Random();
for(int i=0; i// 交换index位置和i位置的Card,实现洗牌
int index = random.nextInt(length);
Card temp = cards.get(index);
cards.set(index, cards.get(i));
cards.set(i, temp);
}
}
/**
* 发牌
* @param index 发牌的位置
* @return index位置的牌
*/
public Card deal(int index) {
return cards.get(index);
}
public String[] getColors() {
return colors;
}
public void setColors(String[] colors) {
this.colors = colors;
}
public List getCards() {
return cards;
}
public void setCards(List cards) {
this.cards = cards;
}
}
package com.starlet.poker;
import java.util.List;
/**
* 玩家
* @author 刘佳星 [email protected]
*
*/
public class Player {
private String id;//玩家ID
private String name;//玩家姓名
private List cards;//玩家手牌
public Player(String id, String name) {
this.id = id;
this.name = name;
}
public Player() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getCards() {
return cards;
}
public void setCards(List cards) {
this.cards = cards;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package com.starlet.poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("----------------创建扑克牌----------------");
//在Poker的构造方法中创建扑克牌
Poker poker = new Poker();
System.out.println("----------------创建扑克牌成功!----------------");
//重写了Card类的toString方法,可以输出形如:[梅花A, 梅花2....]
System.out.println("为:" + poker.getCards());
System.out.println("----------------开始洗牌...----------------");
//洗牌就是随机交换List中元素的位置
poker.shuffle();
System.out.println("----------------洗牌结束----------------");
System.out.println("----------------创建玩家...----------------");
//玩家列表
List playerList = new ArrayList();
for(int i=0; i<2; i++) {
System.out.println("请输入第" + (i+1) + "位玩家的ID和姓名:");
int id=0;
boolean isCatch = false;
do {
try{
System.out.println("输入ID:");
isCatch = false;
//这里要重新创建Scanner,经过断点调试发现如果不重新创建,下一次循环程序不会停下。
Scanner console = new Scanner(System.in);
id = console.nextInt();
} catch(InputMismatchException e) {
System.out.println("请输入整数类型ID:");
isCatch = true;
}
} while(isCatch);
System.out.println("输入姓名:");
Scanner console = new Scanner(System.in);
String name = console.next();
Player player = new Player(id, name);
//把创建的玩家添加到玩家列表
playerList.add(player);
System.out.println("----欢迎玩家:" + player.getName());
}
System.out.println("-------------------开始发牌-----------------------");
Player player1 = playerList.get(0);
Player player2 = playerList.get(1);
//存储玩家1的牌,由于只有两个玩家,两张牌,就直接按逻辑写了。
List player1Cards = new ArrayList();
//存储玩家2的牌
List player2Cards = new ArrayList();
//玩家依次抓牌,抓了前4张牌
System.out.println("----玩家:" + player1.getName() + "-拿牌");
player1Cards.add(poker.deal(0));
System.out.println("----玩家:" + player2.getName() + "-拿牌");
player2Cards.add(poker.deal(1));
System.out.println("----玩家:" + player1.getName() + "-拿牌");
player1Cards.add(poker.deal(2));
System.out.println("----玩家:" + player2.getName() + "-拿牌");
player2Cards.add(poker.deal(3));
//把牌交给玩家
player1.setCards(player1Cards);
player2.setCards(player2Cards);
//玩家自己的牌进行升序排列
Collections.sort(player1.getCards());
Collections.sort(player2.getCards());
System.out.println("玩家:" + player1.getName() + "最大的手牌为:" + player1.getCards().get(1));
System.out.println("玩家:" + player2.getName() + "最大的手牌为:" + player2.getCards().get(1));
//把每个玩家最大的牌取出来,放到winPlay列表中。之后进行排序
List winPlay = new ArrayList();
winPlay.add(player1.getCards().get(1));
winPlay.add(player2.getCards().get(1));
Collections.sort(winPlay);
if(winPlay.get(1).equals(player2.getCards().get(1))) {
System.out.println("------------------玩家:" + player2.getName() + "获胜!----------------------");
} else{
System.out.println("------------------玩家:" + player1.getName() + "获胜!----------------------");
}
System.out.println("玩家各自的手牌为:");
System.out.println(player1.getName()+":"+player1.getCards());
System.out.println(player2.getName()+":"+player2.getCards());
}
}