package poker;
/*
* 玩家类
*/
public class paly {
// Id
public String id;
// 姓名
public String name;
public paly() {
}
public paly(String id, String name) {
this.id = id;
this.name = name;
}
}package poker;
/*
* 扑克类
*/
public class poker {
Integer i;
String num;
String num2;
// 牌
public poker(Integer i, String num, String num2) {
this.num = num;
this.num2 = num2;
this.i = i;
}
public poker() {
}
// @Override
// public int compareTo(poker o) {
// // TODO Auto-generated method stu
// return this.i.compareTo(o.i);
// }
}package poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class run {
Random random = new Random();
int a ;
private static Scanner console = new Scanner(System.in);
// 手牌1
public Map<Integer, poker> Paly1Poker;
// 手牌2
public Map<Integer, poker> Paly2Poker;
// 牌库
public Map<Integer, poker> PokerMap;
// 用于给玩家1手牌排序
static List<Integer> a1 = new ArrayList<Integer>();
// 用于给玩家2手牌排序
static List<Integer> a2 = new ArrayList<Integer>();
public run() {
this.PokerMap = new HashMap<Integer, poker>();
}
// 加载牌库内容,完成
public void pokerMap() {
// TODO Auto-generated method stub
// 牌的大小总是有顺序的,可以用int类来代替
// 3
PokerMap.put(0, new poker(0, "3", "方块"));
PokerMap.put(1, new poker(1, "3", "梅花"));
PokerMap.put(2, new poker(2, "3", "红桃"));
PokerMap.put(3, new poker(3, "3", "黑桃"));
// 4
PokerMap.put(4, new poker(4, "4", "方块"));
PokerMap.put(5, new poker(5, "4", "梅花"));
PokerMap.put(6, new poker(6, "4", "红桃"));
PokerMap.put(7, new poker(7, "4", "黑桃"));
// 5
PokerMap.put(8, new poker(8, "5", "方块"));
PokerMap.put(9, new poker(9, "5", "梅花"));
PokerMap.put(10, new poker(10, "5", "红桃"));
PokerMap.put(11, new poker(11, "5", "黑桃"));
// 6
PokerMap.put(12, new poker(12, "6", "方块"));
PokerMap.put(13, new poker(13, "6", "梅花"));
PokerMap.put(14, new poker(14, "6", "红桃"));
PokerMap.put(15, new poker(15, "6", "黑桃"));
// 7
PokerMap.put(16, new poker(16, "7", "方块"));
PokerMap.put(17, new poker(17, "7", "梅花"));
PokerMap.put(18, new poker(18, "7", "红桃"));
PokerMap.put(19, new poker(19, "7", "黑桃"));
// 8
PokerMap.put(20, new poker(20, "8", "方块"));
PokerMap.put(21, new poker(21, "8", "梅花"));
PokerMap.put(22, new poker(22, "8", "红桃"));
PokerMap.put(23, new poker(23, "8", "黑桃"));
// 9
PokerMap.put(24, new poker(24, "9", "方块"));
PokerMap.put(25, new poker(25, "9", "梅花"));
PokerMap.put(26, new poker(26, "9", "红桃"));
PokerMap.put(27, new poker(27, "9", "黑桃"));
// 10
PokerMap.put(28, new poker(28, "10", "方块"));
PokerMap.put(29, new poker(29, "10", "梅花"));
PokerMap.put(30, new poker(30, "10", "红桃"));
PokerMap.put(31, new poker(31, "10", "黑桃"));
// J
PokerMap.put(32, new poker(32, "J", "方块"));
PokerMap.put(33, new poker(33, "J", "梅花"));
PokerMap.put(34, new poker(34, "J", "红桃"));
PokerMap.put(35, new poker(35, "J", "黑桃"));
// Q
PokerMap.put(36, new poker(36, "Q", "方块"));
PokerMap.put(37, new poker(37, "Q", "梅花"));
PokerMap.put(38, new poker(38, "Q", "红桃"));
PokerMap.put(39, new poker(39, "Q", "黑桃"));
// K
PokerMap.put(40, new poker(40, "K", "方块"));
PokerMap.put(41, new poker(41, "K", "梅花"));
PokerMap.put(42, new poker(42, "K", "红桃"));
PokerMap.put(43, new poker(43, "K", "黑桃"));
// A
PokerMap.put(44, new poker(44, "A", "方块"));
PokerMap.put(45, new poker(45, "A", "梅花"));
PokerMap.put(46, new poker(46, "A", "红桃"));
PokerMap.put(47, new poker(47, "A", "黑桃"));
// 2
PokerMap.put(48, new poker(48, "2", "方块"));
PokerMap.put(49, new poker(49, "2", "梅花"));
PokerMap.put(50, new poker(50, "2", "红桃"));
PokerMap.put(51, new poker(51, "2", "黑桃"));
System.out.println("牌库加载中" + "\n" + "----------洗牌中----------");
System.out.println("牌库加载完毕" + "\n" + "----------洗牌完成---------");
// 假的,没洗
}
// 给玩家1随机发牌,完成
public void getpoker() {
// 玩家1抽2张牌,每获取一张牌则将其从牌库删除,就不需要检查玩家的牌是否重复
for (int i = 1; i < 3; i++) {
System.out.print("玩家1,第" + i + "次抽牌:");
// 为了防止第二个随机数与第一个相同,需要检查牌库中是否有此牌,如果没有则继续抽
do {
a = random.nextInt(51);
}
// 检查牌库中是否有此牌,有则取出来,没有则继续抽牌
while (PokerMap.get((Integer)a)==null);
//将牌加入玩家手牌
Paly1Poker.put(a, PokerMap.get(a));
// 打印玩家抽到的牌
System.out.println(Paly1Poker.get(a).num2 + Paly1Poker.get(a).num);
//将Map中的牌传进list再排序
a1.add(Paly1Poker.get(a).i);
// 将抽出的牌从牌库删除
PokerMap.remove(a);
}
}
// 给玩家2随机发牌,完成
public void getpoker2() {
// 玩家2抽2张牌牌,每获取一张牌则将其从牌库删除,就不需要检查牌是否重复
for (int i = 1; i < 3; i++) {
System.out.println("玩家2第" + i + "抽牌:");
// 为了防止第二个随机数与第一个相同,需要检查牌库中是否有此牌,如果没有则继续抽
do {
a = random.nextInt(51);
}
// 检查牌库中是否有此牌,有则取出来,没有则继续抽牌
while (PokerMap.get(a) == null);
Paly2Poker.put(a, PokerMap.get(a));//运行结果指出这里!!!空指针异常!!!
System.out.println(Paly2Poker.get(a).num2 + Paly2Poker.get(a).num);
//将Map中的牌传进list再排序
a1.add(Paly1Poker.get(a).i);
// 将抽出的牌从牌库删除
PokerMap.remove(a);
}
}
// 比较两个玩家中牌面大小判定输赢,打印玩家手中的牌
public static void main(String[] args) {
// TODO Auto-generated method stub
run n1 = new run();
// 加载牌库
n1.pokerMap();
// 创建玩家
System.out.println("请输入第一个玩家的id");
String p1id = console.next();
System.out.println("请输入第一个玩家的姓名");
String p1name = console.next();
System.out.println("请输入第二个玩家的id");
String p2id = console.next();
System.out.println("请输入第二个玩家的姓名");
String p2name = console.next();
paly p1 = new paly(p1id, p1name);
paly p2 = new paly(p2id, p2name);
// 发牌
n1.getpoker();
n1.getpoker2();
Collections.sort(a1);
Collections.sort(a2);
int b1 = a1.get(1);
int b2 = a2.get(1);
if (b1 > b2) {
System.out.println("玩家" + p1.id + "." + p1.name + "获得胜利");
} else {
System.out.println("玩家" + p2.id + "." + p2.name + "获得胜利");
}
}
}
添加回答
举报
0/150
提交
取消