- 下面是这个扑克牌游戏的代码。
-
由于每个类的代码不多,集中在一起写比较方便来的快,没有创建多个文件,通通包装在一起。
-
个人认为这个扑克牌游戏重点在于如何创建扑克牌序列,是用数组承装还是用集合框架。我这里用的是List来装这个扑克牌对象,将花色和点数放在两个数据成员中,便于分别比较点数或者花色。并重写了在PokerCard类中的toString语句,可以直接把装在List里面的52个PokerCard对象用toString输出。
- 第二个重点在于扑克牌的比较,这里是让PokerCard实现了比较的接口并重写compareTo方法。利用数组的下标,首先点数不相等的情况下,让两个待比较的扑克牌的点数从数组的第一个开始遍历(数组有顺序,2,3,4……A这样排列)。最先在数组里找到的那个点数对应的扑克牌就是更小的。
- PS:玩家的数量可以通过改变定义的常量来改变。
- 集合框架都是用的List(扑克牌,玩家的手牌),用来承装玩家对象的是数组。
- Player类中重载的比较方法,其实也就是调用的PokerCard里面的compareTo,这样就能直接比较Player对象。
下面是运行的结果:
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//主体类
public class Poker {
public static void main(String[] args) {
final int playerCount = 2; // 玩家数量
final int giveCount = 2; // 发牌次数
Poker p = new Poker();
p.washPoker();
Player[] player = new Player[playerCount];
for (int i = 0; i < playerCount; i++) {
System.out.println("请输入第" + (i + 1) + "个玩家的信息");
player[i] = new Player();
}
System.out.println("*********开始发牌");
for (int i = 0; i < giveCount; i++) {
p.givePoker(player);
}
System.out.println("*********发牌结束");
for (int i = 0; i < playerCount; i++) {
player[i].getMax();
}
p.Winner(player);
System.out.println("*********游戏结束");
for (int i = 0; i < playerCount; i++) {
player[i].printPoker();
}
}
public Poker() {
aPoker = new ArrayList<PokerCard>();
System.out.println("开始游戏");
System.out.println("创建扑克牌");
String[] color = { "黑桃", "红桃", "梅花", "方片" };
String[] num = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < num.length; j++) {
aPoker.add(new PokerCard(color[i], num[j]));
}
}
System.out.println(aPoker.toString());
}
public void washPoker() {
List<PokerCard> listPoker = new ArrayList<PokerCard>();
int num;
for (int i = 0; i < 52; i++) {
do {
num = (int) (Math.random() * 52);
} while (listPoker.contains(aPoker.get(num)));
listPoker.add(aPoker.get(num));
}
aPoker = listPoker;
System.out.println("洗牌完成");
}
public void givePoker(Player[] p) {
for (int i = 0; i < p.length; i++) {
System.out.println("玩家:" + p[i].name + "拿牌");
p[i].playerPoker.add(aPoker.get(0));
aPoker.remove(0);
}
}
public void Winner(Player[] p) {
Player winner = p[0];
for (int i = 1; i < p.length; i++) {
if (winner.compareTo(p[i]) < 0) {
winner = p[i];
}
}
System.out.println(winner.name + "获胜!");
}
public List<PokerCard> aPoker;
}
//扑克牌类
class PokerCard implements Comparable<PokerCard> {
public String color;
public String nums;
public PokerCard(String s, String num) {
color = s;
nums = num;
}
@Override
public String toString() {
return color + nums;
}
@Override
public int compareTo(PokerCard o) {
// TODO Auto-generated method stub
String[] color = { "黑桃", "红桃", "梅花", "方片" };
String[] num = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
if (!this.nums.equals(o.nums)) {
for (String str : num) {
if (this.nums.equals(str)) { //先找到的那个点数就是小的那个
return -1;
} else if (o.nums.equals(str)) {
return 1;
}
}
} else {
for (String str : color) {
if (this.color.equals(str)) { //同理
return 1;
} else if (o.nums.equals(str)) {
return -1;
}
}
}
return 0;
}
}
//玩家类
class Player implements Comparable<Player> {
public int id;
public String name;
public PokerCard maxPoker; //保存手中最大的手牌
public List<PokerCard> playerPoker;
public Player() {
playerPoker = new ArrayList<PokerCard>();
System.out.println("输入玩家id:");
Scanner input = new Scanner(System.in);
this.id = input.nextInt();
System.out.println("输入玩家名称:");
this.name = input.next();
}
public void getMax() {
Collections.sort(playerPoker);
maxPoker = playerPoker.get(playerPoker.size() - 1);
System.out.println("玩家:" + name + "最大手牌是:" + maxPoker.toString());
}
public void printPoker() {
System.out.println("玩家" + name + "的手牌:" + playerPoker.toString());
}
@Override
public int compareTo(Player o) {
// TODO Auto-generated method stub
return this.maxPoker.compareTo(o.maxPoker);
}
}
热门评论
演示的照片里,黑桃A不比红桃A大?