package pukegame;
public class Card implements Comparable<Card>{
private String color;
private String number;
public Card(String color, String number) {
this.color = color;
this.number = number;
}
public String getColor() {
return color;
}
public String getNumber() {
return number;
}
@Override
public String toString() {
return this.color+this.number;
}
@Override
public int compareTo(Card o) {
if (Game.numberList.indexOf(this.getNumber()) == Game.numberList.indexOf(o.getNumber())) {
return Integer.valueOf(Game.colorList.indexOf(this.getColor())).compareTo(Integer.valueOf(Game.colorList.indexOf(o.getColor())));
}else {
return Integer.valueOf(Game.numberList.indexOf(this.getNumber())).compareTo(Integer.valueOf(Game.numberList.indexOf(o.getNumber())));
}
}
}
package pukegame;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
private List<Card> handCard;
public Player(int id, String name) {
this.id = id;
this.name = name;
handCard = new ArrayList<Card>();
}
public String getName() {
return name;
}
public void setHandCard(Card handCard) {
this.handCard.add(handCard);
}
public List<Card> getHandCard() {
return handCard;
}
}
package pukegame;
import java.util.Comparator;
public class CardComparator implements Comparator<Card> {
@Override
public int compare(Card o1, Card o2) {
if (Game.numberList.indexOf(o1.getNumber()) == Game.numberList.indexOf(o2.getNumber())) {
return Integer.valueOf(Game.colorList.indexOf(o1.getColor())).compareTo(Integer.valueOf(Game.colorList.indexOf(o2.getColor())));
}else {
return Integer.valueOf(Game.numberList.indexOf(o1.getNumber())).compareTo(Integer.valueOf(Game.numberList.indexOf(o2.getNumber())));
}
}
}
package pukegame;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
public static final int COLORTYPE = 4;//4种花色
public static final int NUMTYPE = 13;//13种牌面数字
public static final String[] COLORS = {"黑桃","红桃","梅花","方片"};
public static final String[] NUMBERS = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//用于比牌
public static final List<String> colorList = new ArrayList<>(Arrays.asList(COLORS));
public static final List<String> numberList = new ArrayList<>(Arrays.asList(NUMBERS));
//创建一副扑克牌
public List<Card> creatCards() {
List<Card> cards = new ArrayList<Card>();
for (int i = 0; i < COLORTYPE; i++) {
for (int j = 0; j < NUMTYPE; j++) {
Card card = new Card(COLORS[i], NUMBERS[j]);
cards.add(card);
}
}
return cards;
}
//展示扑克牌列表
public void showCards(List<Card> cards) {
int length = cards.size();//扑克牌个数
String[] cardStrings = new String[length];
for (int i = 0; i < length; i++) {
cardStrings[i] = cards.get(i).toString();
}
System.out.println(Arrays.toString(cardStrings));//打印牌列表
}
//创建玩家,玩家的个数由参数决定
public List<Player> creatPlayers(int playersNum) {
List<Player> players = new ArrayList<Player>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < playersNum; i++) {
System.out.println("输入第"+(i+1)+"位玩家的ID和姓名:");
System.out.println("输入ID:");
int id = scanner.nextInt();
System.out.println("输入姓名:");
String name = scanner.next();
Player player = new Player(id, name);
players.add(player);
}
if (scanner != null) {
scanner.close();
}
return players;
}
//给玩家发牌,每个玩家发两张牌
public void giveCards(List<Player> players, List<Card> cards) {
int count = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < players.size(); j++) {
Player player = players.get(j);
System.out.println("******玩家:"+player.getName()+"拿牌");
player.setHandCard(cards.get(count));
count++;
}
}
}
//自定义比牌的方法
public Card cardPK(Card card1, Card card2) {
if (numberList.indexOf(card1.getNumber()) == numberList.indexOf(card2.getNumber())) {
return colorList.indexOf(card1.getColor())>colorList.indexOf(card2.getColor())? card1:card2;
}else {
return numberList.indexOf(card1.getNumber())>numberList.indexOf(card2.getNumber())? card1:card2;
}
}
//程序入口
public static void main(String[] args) {
Game game = new Game();
//创建扑克牌
System.out.println("***************创建扑克牌***************");
List<Card> cards = game.creatCards();
//创建扑克牌成功
System.out.println("**************扑克牌创建成功*************");
//输出创建的扑克牌
game.showCards(cards);
//开始洗牌
System.out.println("****************开始洗牌***************");
Collections.shuffle(cards);
//洗牌结束
System.out.println("****************洗牌结束***************");
// game.showCards(cards);//展示洗牌后的结果,测试用
//创建玩家
System.out.println("****************创建玩家***************");
List<Player> players = game.creatPlayers(2);//创建2名玩家
//欢迎玩家
for (Player player : players) {
System.out.println("******欢迎玩家"+player.getName());
}
//开始发牌
System.out.println("****************开始发牌***************");
game.giveCards(players, cards);
//发牌结束
System.out.println("****************发牌结束***************");
//开始游戏
System.out.println("****************开始游戏***************");
//比牌,可以有三种方式
//第一种:通过Collections.sort(List<T> list)方法实现每个玩家的手牌排序,此时Card类需要实现Comparable接口
//第二种:通过Collections.sort(List<T> list, Comparator<?super?> c),此时需要实现Comparator接口
//第三种:自己自定义比牌的方法
//第一种比牌实现
System.out.println("\n\n***************************第一种比牌方式的结果***************************");
List<Card> maxCard = new ArrayList<>();//记录每个玩家的最大手牌
for (Player player : players) {
Card tempCard = new Card("方片", "2");
for (Card card : player.getHandCard()) {
tempCard = game.cardPK(tempCard, card);
}
System.out.println("玩家:"+player.getName()+"最大的手牌为:"+tempCard);
maxCard.add(tempCard);
}
Card tempCard1 = new Card("方片", "2");
for (Card card : maxCard) {
tempCard1 = game.cardPK(tempCard1, card);//各玩家最大手牌进行比较
}
String winner = players.get(maxCard.indexOf(tempCard1)).getName();
System.out.println("***************玩家:"+winner+"获胜**************");
//第二种比牌实现
System.out.println("\n\n***************************第二种比牌方式的结果***************************");
List<Card> maxCard1 = new ArrayList<>();
for (Player player : players) {
Collections.sort(player.getHandCard());
Collections.reverse(player.getHandCard());
System.out.println("玩家:"+player.getName()+"最大的手牌为:"+player.getHandCard().get(0));
maxCard1.add(player.getHandCard().get(0));
}
Card tempCard2 = maxCard1.get(0);
for (Card card : maxCard1) {
if (tempCard2.compareTo(card)==-1) {
tempCard2 = card;
}
}
String winner1 = players.get(maxCard1.indexOf(tempCard2)).getName();
System.out.println("***************玩家:"+winner1+"获胜**************");
//第三种比牌实现
System.out.println("\n\n***************************第三种比牌方式的结果***************************");
List<Card> maxCard2 = new ArrayList<>();
for (Player player : players) {
Collections.sort(player.getHandCard());
Collections.reverse(player.getHandCard());
System.out.println("玩家:"+player.getName()+"最大的手牌为:"+player.getHandCard().get(0));
maxCard2.add(player.getHandCard().get(0));
}
Card tempCard3 = maxCard2.get(0);
for (Card card : maxCard2) {
if (tempCard3.compareTo(card)==-1) {
tempCard3 = card;
}
}
String winner2 = players.get(maxCard2.indexOf(tempCard3)).getName();
System.out.println("***************玩家:"+winner2+"获胜**************\n\n");
//输出玩家的手牌信息
System.out.println("玩家各自的手牌为:");
for (Player player : players) {
System.out.print(player.getName()+":");
game.showCards(player.getHandCard());
}
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦