总过四个类:
第一个类:Poker
public class Poker {
//扑克类
public String desgin;// 花色
public String num;// 牌值
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((desgin == null) ? 0 : desgin.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Poker))
return false;
Poker other = (Poker) obj;
if (desgin == null) {
if (other.desgin != null)
return false;
} else if (!desgin.equals(other.desgin))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
return true;
}
public Poker(String desgin, String num) {
this.desgin = desgin;
this.num = num;
}
}
第二个类:Player
import java.util.ArrayList;
import java.util.List;
public class Player {
//玩家类
public String name;
public int id;
// 新建一个可以装Poker的集合
public List<Poker> getcards = new ArrayList<Poker>();
public Player(int id2, String name) {
this.id = id2;
this.name = name;
}
}
第三个类:PlayerComparator
import java.util.Comparator;
//比较类
public class PlayerComparator implements Comparator<Poker> {
public int compare(Poker o1, Poker o2) {
// 花色、牌值从小到大排列
String[] cardDesign = { "方片", "梅花", "红桃", "黑桃" };
String[] cardNum = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K", "A" };
int temp1 = 0;
int temp2 = 0;
for (int i = 0; i < cardNum.length; i++) {
if (o1.num.equals(cardNum[i]))
temp1 = 1 + i;
if (o2.num.equals(cardNum[i]))
temp2 = 1 + i;
}
if (temp1 > temp2) {// 牌值左边大返回-1
return -1;
} else if (temp1 == temp2) {// 牌值如果大小相同,比较花色
for (int i = 0; i < cardDesign.length; i++) {
if (o1.desgin.equals(cardDesign[i]))
temp1 = temp1 + i;
if (o2.desgin.equals(cardDesign[i]))
temp2 = temp2 + i;
}
if (temp1 > temp2) {// 花色左边大返回-1
return -1;
} else {// 花色右边大返回1
return 1;
}
} else {// 牌值右边大返回1
return 1;
}
}
}
第四个类:GameTest(主测试)
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 GameTest {
public List<Poker> pokerList;
public Map<Integer, Player> playerMap;
private Scanner input;
public GameTest() {
this.pokerList = new ArrayList<Poker>();
this.playerMap = new HashMap<Integer, Player>();
input = new Scanner(System.in);
}
/*
* 创建扑克并洗牌
*/
public void CreatePoker() {
String[] desgin = new String[] { "黑桃", "红桃", "梅花", "方块" };
String[] num = new String[] { "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A" };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
Poker pk = new Poker(desgin[i], num[j]);
pokerList.add(pk);
}
}
System.out.println("----------创建扑克牌----------");
System.out.println("----------扑克牌创建成功!----------");
for (Poker poker : pokerList) {
System.out.print("[" + poker.desgin + poker.num + "]");
}
System.out.println("----------开始洗牌----------");
Collections.shuffle(pokerList);// 集合中的乱序方法
for (Poker poker : pokerList) {
System.out.print("[" + poker.desgin + poker.num + "]");
}
System.out.println("----------洗牌结束!----------");
}
/*
* 创建玩家
*/
public void CreatePlayer() {
System.out.println("----------创建玩家----------");
int i = 0;
while (i < 2) {
System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名:");
System.out.println("输入ID:");
int id = 0;
try {
id = scanf();
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
Player p = playerMap.get(id);
if (p == null) {
System.out.println("请输入玩家姓名:");
String name = input.next();
playerMap.put(id, new Player(id, name));
i++;
} else {
System.out.println("该ID已被占用!");
continue;// 跳出当前回到循环
}
}
}
/*
* 防止输入非整数类型的ID
*/
private int scanf() throws Exception {
try {
int in = input.nextInt();
return in;
} catch (Exception e) {
input = new Scanner(System.in);
throw new Exception("输入异常,请输入整数类型的ID!");
}
}
/*
* 开始比赛
*/
public void StarGame() {
System.out.println("---欢迎玩家:" + playerMap.get(1).name);
System.out.println("---欢迎玩家:" + playerMap.get(2).name);
System.out.println("----------开始发牌...----------");
System.out.println("---欢迎玩家:" + playerMap.get(1).name + "拿牌"
+ pokerList.get(0).desgin + pokerList.get(0).num);
playerMap.get(1).getcards.add(pokerList.get(0));
System.out.println("---欢迎玩家:" + playerMap.get(2).name + "拿牌"
+ pokerList.get(1).desgin + pokerList.get(1).num);
playerMap.get(2).getcards.add(pokerList.get(1));
System.out.println("---欢迎玩家:" + playerMap.get(1).name + "拿牌"
+ pokerList.get(2).desgin + pokerList.get(2).num);
playerMap.get(1).getcards.add(pokerList.get(2));
System.out.println("---欢迎玩家:" + playerMap.get(2).name + "拿牌"
+ pokerList.get(3).desgin + pokerList.get(3).num);
playerMap.get(2).getcards.add(pokerList.get(3));
System.out.println("----------发牌结束!----------");
System.out.println("----------开始游戏...----------");
Collections.sort(playerMap.get(1).getcards, new PlayerComparator());
Collections.sort(playerMap.get(2).getcards, new PlayerComparator());
System.out.println("玩家:" + playerMap.get(1).name + " " + "最大的手牌为:"
+ playerMap.get(1).getcards.get(0).desgin
+ playerMap.get(1).getcards.get(0).num);
System.out.println("玩家:" + playerMap.get(2).name + " " + "最大的手牌为:"
+ playerMap.get(2).getcards.get(0).desgin
+ playerMap.get(2).getcards.get(0).num);
// 比较两者最大值
List<Poker> stringList = new ArrayList<Poker>();
stringList.add(new Poker("playerMap.get(1).getcards.get(0).desgin",
" playerMap.get(1).getcards.get(0).num"));
stringList.add(new Poker("playerMap.get(2).getcards.get(0).desgin",
" playerMap.get(2).getcards.get(0).num"));
Collections.sort(stringList, new PlayerComparator());
String name=playerMap.get(1).name;
if (stringList.get(0).equals("playerMap.get(1).getcards.get(0).desgin+playerMap.get(1).getcards.get(0).num"))
playerMap.get(1).name = playerMap.get(1).name;
else
playerMap.get(1).name = playerMap.get(2).name;
System.out.println("----------玩家:" + playerMap.get(1).name + "获胜!----------");
playerMap.get(1).name=name;
System.out.println("玩家各自手牌为:");
System.out.println(playerMap.get(1).name+":"+playerMap.get(1).getcards.get(0).desgin+playerMap.get(1).getcards.get(0).num
+","+playerMap.get(1).getcards.get(1).desgin+playerMap.get(1).getcards.get(1).num);
System.out.println(playerMap.get(2).name+":"+playerMap.get(2).getcards.get(0).desgin+playerMap.get(2).getcards.get(0).num
+","+playerMap.get(2).getcards.get(1).desgin+playerMap.get(2).getcards.get(1).num);
}
public static void main(String[] args) {
GameTest gt = new GameTest();
gt.CreatePoker();
gt.CreatePlayer();
gt.StarGame();
}
}
运行结果:(为了便于验证代码的正确性,将洗好的顺序和发牌时玩家得到的牌顺便一起打印出来了!)
异常处理:
(还有就是输入非整型的ID,以及重复输入相同的ID时能处理异常)
点击查看更多内容
29人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦