package com.game;
/**
* 扑克牌类
*/
public class Card {
String num; // 卡牌标号 2-10 A J Q K
String type; // 花色 黑桃 红桃 梅花 方片
Card(String num, String type) {
this.num = num;
this.type = type;
}
@Override
public String toString() {
return type + num;
}
}
package com.game;
import java.util.ArrayList;
import java.util.List;
/**
* 玩家角色
*/
public class Role {
int id; // 玩家id
String name; // 玩家姓名
List<Card> cards; // 玩家手牌
Role(int id, String name) {
this.id = id;
this.name = name;
cards = new ArrayList<>();
}
}
package com.game;
import java.util.*;
/**
* 比较两名玩家的手牌,首先比较点数、若点数相同则比较花色
*/
public class Game {
// 一副扑克牌 共52张
private List<Card> cards = new ArrayList<>();
// 玩家角色 共两人
private Role[] roles = new Role[2];
// 玩家持有的手牌数量
private int num = 2;
public void run() {
System.out.println("----------------------------------开始游戏!!!----------------------------------");
initCards();
showCards();
shuffle();
initRole();
send();
compare();
showRoleCards();
System.out.println("----------------------------------游戏结束!!!----------------------------------");
}
// 对扑克牌进行初始化
private void initCards() {
System.out.println("-----------------创建扑克牌!!!-----------------");
String[] type = {"黑桃", "红桃", "梅花", "方片"};
for (int i = 0; i < 4; i++) {
cards.add(new Card("A", type[i]));
for (int j = 2; j <= 10; j++) {
cards.add(new Card(j + "", type[i]));
}
cards.add(new Card("J", type[i]));
cards.add(new Card("Q", type[i]));
cards.add(new Card("K", type[i]));
}
System.out.println("-----------------创建扑克牌成功!!!-----------------");
}
// 初始化角色
private void initRole() {
System.out.println("-----------------创建玩家!!!-----------------");
Scanner sc = new Scanner(System.in);
int id; // 玩家id
String name; // 玩家姓名
for (int i = 0; i < roles.length; i++) {
System.out.println("请输入玩家" + (i + 1) + "的id:");
id = sc.nextInt();
System.out.println("请输入玩家" + (i + 1) + "的name:");
name = sc.next();
roles[i] = new Role(id, name);
}
for (Role role : roles) {
System.out.println("-------欢迎玩家:" + role.name);
}
}
// 展示扑克牌
private void showCards() {
System.out.println(cards);
}
// 展示玩家手牌
private void showRoleCards() {
System.out.println("玩家各自的手牌为:");
for (Role role : roles) {
System.out.println(role.name + ":" + role.cards);
}
}
// 洗牌
private void shuffle() {
System.out.println("-----------------开始洗牌!!!-----------------");
Collections.shuffle(cards);
System.out.println("-----------------洗牌结束!!!-----------------");
}
// 游戏规则
private void send() {
System.out.println("-----------------开始发牌!!!-----------------");
Iterator<Card> iter = cards.iterator();
for (int i = 0; i < num; i++) {
for (Role role : roles) {
System.out.println("玩家:" + role.name + "-拿牌");
role.cards.add(iter.next());
}
}
System.out.println("-----------------发牌结束!!!-----------------");
}
// 比较
private void compare() {
Map<Card, String> map = new HashMap<>();
for (Role role : roles) {
Collections.sort(role.cards, new CardComparator());
map.put(role.cards.get(0), role.name);
}
for (Map.Entry<Card, String> entry : map.entrySet()) {
System.out.println("玩家:" + entry.getValue() + " 最大的手牌为:" + entry.getKey());
}
List<Card> list = new ArrayList<>(map.keySet());
Collections.sort(list, new CardComparator());
System.out.println("-----------------玩家:" + map.get(list.get(0)) + " 获胜!!!-----------------");
}
}
package com.game;
import java.util.Comparator;
public class CardComparator implements Comparator<Card> {
@Override
public int compare(Card c1, Card c2) {
if (!c1.num.equals(c2.num)) {
return c2.num.compareTo(c1.num);
} else {
return c2.type.compareTo(c1.type);
}
}
}
package com.game;
public class Client {
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}