案例分析:
模拟洗牌发牌游戏
1.创建一副扑克牌
包括四种花色 2-10 JQKA 不考虑大小王
2.创建两名玩家
玩家至少要有ID、姓名、手牌等属性、手牌为扑克牌的集合
3.洗牌
将之前创建的一副扑克牌 打乱顺序
4.发牌
将洗牌之后的扑克牌集合,
从第一张开始,一人一张,每人发两张
5.游戏 *游戏规则
取两人各自手中点数最大的牌进行比较,点数大的赢;
若两人各自的点数最大的牌相等 则比较花色
花色大小的顺序:方块 梅花 红桃 黑桃的顺序
使用知识点:
1:类的抽象,2,集合框架的增加,以及使用Collections工具类
3:比较的方法 实现有多种,這里使用 String类的indexOf根据索引大小判断。
Poker (扑克类)
/**
* 简单的扑克类
*/
public class Poker {
private String color;//花色
private String number;//数字
public Poker(String color, String number) {
this.color = color;
this. number = number;
}
@Override
public String toString() {
return color+number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
Player(玩家类)
package com.poker;
import java.util.List;
/**
* 简单的玩家类
*/
public class Player {
private String id;
private String name;
private List<Poker> poker;
public Player(String id, String name) {
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> getPoker() {
return poker;
}
public void setPoker(List poker) {
this.poker = poker;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Player player = (Player) o;
if (id != null ? !id.equals(player.id) : player.id != null) return false;
return name != null ? name.equals(player.name) : player.name == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Player{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
public void showPoker(){
System.out.print("id: "+id+ "name:"+ name +"手持牌为:");
for (int i=0;i<poker.size();i++){
System.out.println(poker.get(i).toString());
}
}
}
Compare (零时比较类)
package com.poker;
import java.util.Comparator;
public class Compare implements Comparator<Poker> {
@Override
public int compare(Poker o1, Poker o2) {
// String[] colors ={"黑桃","红桃","梅花","方块"};
// String[] numbers ={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String colors = "方块 梅花 红桃 黑桃";
String numbers = "3 4 5 6 7 8 9 10 J Q K A 2";
System.out.println("牌数: "+o1.getNumber()+" -- " + o2.getNumber());
System.out.println("牌色: "+o1.getColor()+" -- " + o2.getColor());
if( numbers.indexOf(o1.getNumber()) == numbers.indexOf(o2.getNumber())){
return colors.indexOf(o1.getColor()) - colors.indexOf(o2.getColor());
}else {
return numbers.indexOf(o1.getNumber()) - numbers.indexOf(o2.getNumber());
}
}
}
PokerTest(游戏主程序)
package com.poker;
import java.util.*;
public class PokerTest {
public static void main(String[] args) {
System.out.println("============创建扑克牌===========");
List pokers = CreatePoker();
showPoker(pokers);
// 洗牌
System.out.println("=============开始洗牌===========");
shuffleTheDeck(pokers);
// showPoker(pokers);
// 创建玩家:
System.out.println("=========请输入几名玩家:");
int playerNum = getNumberByScanner();
List<Player> players =createPlayer(playerNum);
System.out.println("=====玩家信息=====");
showPlayer(players);
// 发牌
System.out.println("=========开始发牌===============");
dealPoker(players,pokers,2);
// 查看下牌
players.get(0).showPoker();
players.get(1).showPoker();
// 比较玩家的牌的方法咯
comparePoker(players);
}
public static int getNumberByScanner(){
int result = 0;
Scanner sc = new Scanner(System.in);
boolean correct = false;
do {
correct = false;
String playerNum = sc.next();
try {
result = Integer.parseInt(playerNum);
} catch (NumberFormatException e) {
// e.printStackTrace();//错误信息注释掉
System.out.println("输入错误,请输入数字!");
correct =true;
}
}while (correct);
return result;
}
public static void comparePoker(List<Player> players){
Compare compare = new Compare();
List<Poker> apokers = players.get(0).getPoker();
List<Poker> bpokers = players.get(1).getPoker();
int a = compare.compare(apokers.get(0),apokers.get(1));
//玩家手中的大牌
Poker amax = null;
Poker bmax = null;
amax= a<0?apokers.get(1):apokers.get(0);
int b = compare.compare(bpokers.get(0),bpokers.get(1));
bmax = b<0?bpokers.get(1):bpokers.get(0);
System.out.println("玩家A的最大牌:"+amax.toString() +" B的最大牌:"+bmax);
// 赢家
Player win = null;
int result = compare.compare(amax,bmax);
win = result<0?players.get(1):players.get(0);
System.out.println("赢家是: "+ win.toString());
// compare.compare(players.get(0).getPoker().get(0));
}
/**
*
* @param players
* @param pokers
* @param n 发几张
*/
public static void dealPoker(List<Player> players,List<Poker> pokers,int n){
List poker1 = new ArrayList<Poker>();
List poker2 = new ArrayList<Poker>();
Iterator it = pokers.iterator();
int count = 0;
while (it.hasNext() && (count<2*n)){
// 2个人---- 当作2人吧
if(count%players.size()==0){
poker1.add(it.next());
}else{
poker2.add(it.next());
}
count++;
}
players.get(0).setPoker(poker1);
players.get(1).setPoker(poker2);
}
/**
* 遍历玩家信息
* @param players
*/
public static void showPlayer(List<Player> players){
Iterator it = players.iterator();
while (it.hasNext()){
System.out.println( it.next().toString());
}
}
/***
* 创建玩家的方法
* @param n
* @return
*/
public static List<Player> createPlayer(int n){
List players = new ArrayList();
Scanner sc = new Scanner(System.in);
for (int i=0;i<n;i++){
System.out.print("请输出玩家" + (i + 1) + "的id: ");
String id = String.valueOf(getNumberByScanner());
System.out.print("请输出玩家" + (i + 1) + "的姓名: ");
String name = sc.next();
players.add(new Player(id, name));
}
return players;
}
/**
* 洗牌的方法
* @param pokers
*/
public static void shuffleTheDeck(List<Poker> pokers){
System.out.println("====开始洗牌===");
Collections.shuffle(pokers);
System.out.println("洗牌结束");
}
/**创建一副Poker*/
public static List<Poker> CreatePoker(){
String[] colors ={"黑桃","红桃","梅花","方块"};
String[] numbers ={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
List pokers = new ArrayList();
for(int i=0;i<colors.length;i++){
for (int j=0;j<numbers.length;j++){
pokers.add(new Poker(colors[i],numbers[j]));
}
}
return pokers;
}
public static void showPoker(List<Poker> pokers){
int count=0;
for (int i=0;i<pokers.size();i++){
System.out.print(pokers.get(i)+",");
count++;
if(count%13==0){
System.out.println();
}
}
}
}
运行结果