【player】类:
package player;
import card.Card;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Card> myCards;
public Player(int id, String name){
this.id = id;
this.name = name;
this.myCards = new ArrayList<Card>();
}
public void getCard(Card c){
System.out.println("-----玩家"+this.name+ '-'+ "拿牌");
myCards.add(c);
}
// 获取最大权值
public int getMaxWeight() {
Card item = Collections.max(myCards);
System.out.println("玩家:"+ this.name + "最大手牌:"+ item.name);
return item.weight;
}
}
【card类】
package card;
public class Card implements Comparable<Card>{
// 扑克牌权值 = 黑/红/梅/方(1/2/3/4) * 100 + 点数(A为14)
public int weight;
// 卡牌名称 如 '红桃A'
public String name;
public Card(int w, String name) {
this.weight = w;
this.name = name;
}
@Override
public int compareTo(Card c) {
if (this.weight > c.weight) return 1;
if (this.weight == c.weight) return 0;
else return -1;
}
}
【Main 】
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import card.Card;
import player.Player;
public class Main {
// 花色对应权值的Map
public Map<String, Integer> codeMap;
// 点数对应权值的Map
public Map<String, Integer> numMap;
// 扑克牌
public List<Card> cards;
// 玩家
public List<Player> players;
public Main() {
this.codeMap = new HashMap<String, Integer>();
this.numMap = new HashMap<String, Integer>();
this.cards = new ArrayList<Card>();
this.players = new ArrayList<Player>();
}
public static void main(String[] args) {
Main m = new Main();
m.genNumMap();
m.genCodeMap();
m.genCards();
// 生成玩家
int i = 0;
while (i<2) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入玩家ID:");
try {
int id = sc.nextInt();
System.out.println("请输入玩家名称");
String name = sc.next();
m.players.add(new Player(id, name));
i++;
} catch (InputMismatchException e) {
System.out.println("类型错误,请重新输入");
}
}
// 发牌
Player p1 = m.players.get(0);
Player p2 = m.players.get(1);
p1.getCard(m.playerGetCard(52));
p2.getCard(m.playerGetCard(51));
p1.getCard(m.playerGetCard(50));
p2.getCard(m.playerGetCard(49)); // 取了牌 牌会变少
// 计算结果
int p1Weight = p1.getMaxWeight();
int p2Weight = p2.getMaxWeight();
// 输出结果
System.out.println(p1Weight);
System.out.println(p2Weight);
if(p1Weight > p2Weight) {System.out.println(p1.name+"获胜!");}
if(p1Weight == p2Weight) {System.out.println("平局!");}
if(p1Weight < p2Weight); {System.out.println(p2.name+"获胜!");}
System.out.println("---------------双方手牌公示------------");
System.out.print(p1.name+":");
for(Card c:p1.myCards) {
System.out.print(c.name+ ",");
}
System.out.println("\n--- ");
System.out.print(p2.name+":");
for(Card c:p2.myCards) {
System.out.print(c.name+ ",");
}
System.out.println("\n--- ");
System.out.println("---------------底牌公示------------");
for (Card c : m.cards) {
System.out.print(c.name + ',');
}
}
public void genCodeMap() {
codeMap.put("黑桃", 400);
codeMap.put("红桃", 300);
codeMap.put("梅花", 200);
codeMap.put("方块", 100);
System.out.println("生成花色权重表:");
System.out.println(codeMap);
System.out.println("------------------------------------------------------------");
}
public void genNumMap() {
for (int i = 2; i <= 10; i++) {
numMap.put(Integer.toString(i), i);
}
numMap.put("J", 11);
numMap.put("Q", 12);
numMap.put("K", 13);
numMap.put("A", 14);
System.out.println("生成点数权重表:");
System.out.println(numMap);
System.out.println("------------------------------------------------------------");
}
// 生成扑克牌
public void genCards() {
System.out.println("生成扑克牌并把牌洗整齐");
for (Entry<String, Integer> numItem : numMap.entrySet()) {
for (Entry<String, Integer> codeItem : codeMap.entrySet()) {
cards.add(new Card(numItem.getValue() + codeItem.getValue(), codeItem.getKey() + numItem.getKey()));
}
}
Collections.sort(cards); // 把牌洗整齐
for (Card c : cards) {
System.out.print(c.name + ',');
}
System.out.println("\n------------------------------------------------------------");
System.out.println("把牌顺序打乱");
Collections.shuffle(cards); // 把牌打乱
for (Card c : cards) {
System.out.print(c.name + ',');
}
System.out.println("\n------------------------------------------------------------");
}
public Card playerGetCard(int valume){
int random = (int) (Math.random() * valume);
Card temp = cards.get(random);
cards.remove(random);
return temp;
}
}
这个不对,他这个是按照花色排序,要改一下
兄弟,你这个平局咋回事啊