/**
* @author showball
* 1、用List类型来创建一副全新的扑克牌
* 2、用Set类型来创建打乱无序的扑克牌
* 3、将Set类型数据赋给List类型
* 4、从List类型数据按照顺序发牌
* 5、使用comparator确立比较派大小的规则
*/
建立扑克类
public class Card {
public int cardNum;
public int cardCol;
public String getcardNum(){
if(cardNum<=10&&cardNum>1){
return cardNum+"";
}
else if (cardNum==1) {
return "A";
}
else if(cardNum==11){
return "J";
}
else if(cardNum==12){
return "Q";
}
else {
return "K";
}
}
public String getcardCol(){
if(this.cardCol==0){
return "黑桃";
}
else if(this.cardCol==1){
return "红桃";
}
else if(this.cardCol==2){
return "梅花";
}
else {
return "方片";
}
}
public Card(){}
public Card(int cardCol,int cardNum){
this.cardNum=cardNum;
this.cardCol=cardCol;
}
}
使用CardList 来有序存储和发牌
public class CardList {
private List<Card> cardList;
int colourNum=4;
int cardNum=13;
public CardList(){
this.cardList=new ArrayList<Card>();
}
// 创建一套扑克牌
public void createCard() {
for (int i = 0; i < colourNum; i++) {
for (int j = 1; j <=cardNum; j++) {
cardList.add(new Card(i, j));
}
}
}
// 展示创建的扑克牌
public void cardForEach() {
System.out.print("为:");
for (Card card: cardList) {
System.out.print(card.getcardCol() +card.getcardNum());
}
System.out.println("");
}
// 删除创建的扑克牌
public void deletecard() {
cardList.clear();
}
// 存储打乱的扑克牌
public void saveshuffleCard(Set<Card>cardSet)
{
for (Card card: cardSet) {
cardList.add(card);
}
}
public List<Card> getcardList(){
return this.cardList;
}
}
使用Cardset来无序存储,实现打乱牌
public class CardSet {
public Set<Card> cardSet;
public Card card;
int colourNum = 4;
int cardNum = 13;
public CardSet() {
this.cardSet = new HashSet<Card>();
}
// 打乱扑克牌的顺序
public void shuffleCard() {
for (int i = 0; i < colourNum; i++) {
for (int j = 1; j <= cardNum; j++) {
this.cardSet.add(new Card(i, j));
}
}
}
public void cardForEach() {
System.out.print("为:");
for (Card card: cardSet) {
System.out.print(card.getcardCol() +card.getcardNum());
}
System.out.println("");
}
public Set<Card> getcardSet() {
return this.cardSet;
}
}
建立玩家类
public class Player {
private int ID;
private String name;
private Card card;
public int getID(){
return this.ID;
}
public String getName() {
return this.name;
}
public Player(int ID,String name){
this.ID=ID;
this.name=name;
}
public void setCard(Card card){
this.card=card;
}
public Card getCard(){
return this.card;
}
}
重写牌类比大小规则
public class CardComparator implements Comparator<Card> {
public int compare(Card o1, Card o2) {
// TODO Auto-generated method stub
if (o1.cardCol < o2.cardCol) {
return 1;
} else if (o1.cardCol == o2.cardCol) {
if (o1.cardNum > o2.cardNum) {
return 1;
} else if (o1.cardNum == o2.cardNum) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}
}
主程序
public class PlayCard {
private static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
CardList cardList = new CardList();
System.out.println("-------------创建扑克牌--------");
cardList.createCard();
System.out.println("-----创建成功-----");
cardList.cardForEach();
System.out.println("----开始洗牌----");
CardSet cardSet = new CardSet();
cardSet.shuffleCard();
// System.out.println("cardSet洗牌展示如下:");
// cardSet.cardForEach();
// System.out.println("cardList删除之后展示");
cardList.getcardList().clear();
// cardList.cardForEach();
// System.out.println("cardList存储Set展示如下:");
cardList.saveshuffleCard(cardSet.getcardSet());
// cardList.cardForEach();
System.out.println("-----洗牌结束----");
System.out.println("----创建玩家----");
System.out.println("请输入第1位玩家的ID和姓名:");
int ID1 =-1;
while(true){
ID1=inputCommand();
if(ID1==-1){
continue;
}
else {
break;
}
}
System.out.println("请输入姓名:");
String name1 = console.next();
Player player1 = new Player(ID1, name1);
System.out.println("请输入第2位玩家的ID和姓名:");
int ID2=-1;
while(true){
ID2=inputCommand();
if(ID2==-1){
continue;
}
else {
break;
}
}
System.out.println("请输入姓名:");
String name2 = console.next();
Player player2 = new Player(ID2, name2);
System.out.println("----开始发牌----");
System.out.println("玩家" + player1.getName() + "拿牌");
System.out.println("玩家" + player2.getName() + "拿牌");
System.out.println("玩家" + player1.getName() + "拿牌");
System.out.println("玩家" + player2.getName() + "拿牌");
Card[] card1={cardList.getcardList().get(0),cardList.getcardList().get(2)};
Card[] card2={cardList.getcardList().get(1),cardList.getcardList().get(3)};
System.out.println("----发牌结束----");
System.out.println("----开始游戏----");
Card maxCard1=compareCard(card1[0],card1[1]);
System.out.println("玩家:"+player1.getName()+"最大的手牌为:"+maxCard1.getcardCol()+maxCard1.getcardNum());
Card maxCard2=compareCard(card2[0],card2[1]);
System.out.println("玩家:"+player2.getName()+"最大的手牌为:"+maxCard2.getcardCol()+maxCard2.getcardNum());
Card maxCard=compareCard(maxCard1,maxCard2);
if((maxCard.getcardCol()==maxCard1.getcardCol())&&(maxCard.getcardNum()==maxCard1.getcardNum())){
System.out.println("-----玩家"+player1.getName()+"获胜!------");
}
else {
System.out.println("-----玩家"+player2.getName()+"获胜!------");
}
System.out.println("玩家各自的手牌为:");
System.out.println(player1.getName()+":"+cardList.getcardList().get(0).getcardCol()+cardList.getcardList().get(0).getcardNum()
+cardList.getcardList().get(2).getcardCol()+cardList.getcardList().get(2).getcardNum());
System.out.println(player2.getName()+":"+cardList.getcardList().get(1).getcardCol()+cardList.getcardList().get(1).getcardNum()
+cardList.getcardList().get(3).getcardCol()+cardList.getcardList().get(3).getcardNum());
}
public static Card compareCard(Card card1,Card card2){
CardComparator cardComparator=new CardComparator();
if(cardComparator.compare(card1, card2)>=0)
{
return card1;
}
else {
return card2;
}
}
private static int inputCommand(){
int command;
try {
System.out.println("请输入ID:");
command = console.nextInt();
return command;
} catch (Exception e) {
//若输入字符型或者字符串,则抛出异常,捕获该异常,抛出”错误命令异常“
console = new Scanner(System.in);
System.out.println("命令输入错误!请根据提示输入数字命令!");
//返回-1
return -1;
}
}
}
运行结果
-------------创建扑克牌--------
-----创建成功-----
为:黑桃A黑桃2黑桃3黑桃4黑桃5黑桃6黑桃7黑桃8黑桃9黑桃10黑桃J黑桃Q黑桃K红桃A红桃2红桃3红桃4红桃5红桃6红桃7红桃8红桃9红桃10红桃J红桃Q红桃K梅花A梅花2梅花3梅花4梅花5梅花6梅花7梅花8梅花9梅花10梅花J梅花Q梅花K方片A方片2方片3方片4方片5方片6方片7方片8方片9方片10方片J方片Q方片K
----开始洗牌----
-----洗牌结束----
----创建玩家----
请输入第1位玩家的ID和姓名:
请输入ID:
m
命令输入错误!请根据提示输入数字命令!
请输入ID:
1
请输入姓名:
xiaoming
请输入第2位玩家的ID和姓名:
请输入ID:
2
请输入姓名:
xiaohua
----开始发牌----
玩家xiaoming拿牌
玩家xiaohua拿牌
玩家xiaoming拿牌
玩家xiaohua拿牌
----发牌结束----
----开始游戏----
玩家:xiaoming最大的手牌为:红桃6
玩家:xiaohua最大的手牌为:红桃3
-----玩家xiaohua获胜!------
玩家各自的手牌为:
xiaoming:梅花A红桃6
xiaohua:红桃3方片3
点击查看更多内容
10人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦