纸牌类
package com.imooc_pokergame.dengdi;
public class PokerCard implements Comparable<PokerCard> {
private String cardName;//纸牌花色
private String cardNumber;//纸牌数字
public PokerCard() {
super();
// TODO Auto-generated constructor stub
}
public PokerCard(String cardName, String cardNumber) {
super();
this.cardName = cardName;
this.cardNumber = cardNumber;
}
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.cardName + this.cardNumber;
}
/*
* 最大纸牌的比较方法
*/
@Override
public int compareTo(PokerCard o) {
if (this.cardNumber.charAt(0) > o.cardNumber.charAt(0)) {
return 1;
} else if (this.cardNumber.charAt(0) < o.cardNumber.charAt(0)) {
return -1;
} else {
return this.getCardName().compareTo(o.cardNumber);
}
}
}
玩家类
package com.imooc_pokergame.dengdi;
import java.util.ArrayList;
import java.util.List;
public class Gamer {
private String id;
private String name;
private List<PokerCard> handCards= new ArrayList<PokerCard>();//手牌
public Gamer() {
super();
// TODO Auto-generated constructor stub
}
public Gamer(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<PokerCard> getHandCards() {
return handCards;
}
public void setHandCards(List<PokerCard> handCards) {
this.handCards = handCards;
}
}
手牌中最大牌的比较方法
package com.imooc_pokergame.dengdi;
import java.util.Comparator;
public class HandCardsCompare implements Comparator<PokerCard>{
/*
* 手牌最大的比较方法
*/
@Override
public int compare(PokerCard o1, PokerCard o2) {
if(o1.getCardNumber().compareTo(o2.getCardNumber())==0){
return o1.getCardName().compareTo(o2.getCardName());
}
return o1.getCardNumber().compareTo(o2.getCardNumber());
}
}
测试类
package com.imooc_pokergame.dengdi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PokerDemo {
private List<PokerCard> pokerCards;//牌组
public PokerDemo() {
this.pokerCards = new ArrayList<PokerCard>();
}
// 创建纸牌
public void setCards() {
System.out.println("-------------创建扑克牌---------------------");
String[] cardName = { "黑桃", "红桃", "梅花", "方片" };
String[] cardNumber = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A" };
for (int i = 0; i < cardName.length; i++) {
for (int j = 0; j < cardNumber.length; j++) {
pokerCards.add(new PokerCard(cardName[i], cardNumber[j]));
}
}
System.out.println("-------------扑克牌创建成功-----------------");
}
// 洗牌方法
public void washCards() {
Random random = new Random();
System.out.println("------------开始洗牌---------------------");
/*
* 随机将牌组的任意一张放在最上面,重复任意次数
*/
for (int i = 0; i < random.nextInt(52) + 1; i++) {
PokerCard temp = pokerCards.get(i);
pokerCards.remove(i);
pokerCards.add(0, temp);
}
System.out.println("------------洗牌结束----------------------");
}
// 发牌方法
public void dealCard(Gamer gamer01, Gamer gamer02) {
int i = 1;
Iterator<PokerCard> it = pokerCards.iterator();
System.out.println("-------------开始发牌---------------------");
/*
* 运用迭代器来遍历牌组,设置i数值来限制两个玩家手牌有几张牌
*/
while (it.hasNext()) {
System.out.println("玩家" + gamer01.getName() + "拿牌");
gamer01.getHandCards().add(it.next());
System.out.println("玩家" + gamer02.getName() + "拿牌");
gamer02.getHandCards().add(it.next());
if (i == 2)
break;
i++;
}
System.out.println("--------------发牌结束---------------------");
}
// 开始游戏
public void beginGame(Gamer gamer01, Gamer gamer02) {
System.out.println("---------------开始游戏---------------------");
//将两个玩家手牌排序
Collections.sort(gamer01.getHandCards(), new HandCardsCompare());
Collections.sort(gamer02.getHandCards(), new HandCardsCompare());
//取他们手牌最后一张,也就是最大的牌
System.out.println("玩家:" + gamer01.getName() + "最大手牌为:"
+ gamer01.getHandCards().get(1));
System.out.println("玩家:" + gamer02.getName() + "最大手牌为:"
+ gamer02.getHandCards().get(1));
/*
* 比较两个玩家的最大牌,返回的数值来选择输出
*/
switch (gamer01.getHandCards().get(1)
.compareTo(gamer02.getHandCards().get(1))) {
case 1:
System.out.println("------------玩家" + gamer01.getName()
+ "获胜-----------------");
break;
case -1:
System.out.println("------------玩家" + gamer02.getName()
+ "获胜-----------------");
break;
default:
System.out.println("------------平手------------------------------");
break;
}
}
public void showHandCards(Gamer gamer01, Gamer gamer02){
System.out.println("玩家各自的手牌为:");
System.out.println(gamer01.getName() + "["
+ gamer01.getHandCards() + "]");
System.out.println(gamer02.getName() + "["
+ gamer02.getHandCards() + "]");
}
public static void main(String[] args) {
PokerDemo pd = new PokerDemo();
//创建牌组
pd.setCards();
//循环遍历
for (PokerCard pc : pd.pokerCards) {
System.out.print(pc);
}
System.out.println();
pd.washCards();//洗牌
Scanner input = new Scanner(System.in);
List<Gamer> gamers = new ArrayList<Gamer>();
for (int i = 1; i < 3; i++) {
System.out.println("请输入第" + i + "位玩家的ID和名字");
System.out.println("请输入ID");
String id = input.next();
System.out.println("请输入名字");
String name = input.next();
gamers.add(new Gamer(id, name));
}
pd.dealCard(gamers.get(0), gamers.get(1));//发牌
pd.beginGame(gamers.get(0), gamers.get(1));//开始游戏
pd.showHandCards(gamers.get(0), gamers.get(1));//展示手牌
}
}
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦