继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java入门三—扑克牌简易游戏

我是你的皇阿玛
关注TA
已关注
手记 3
粉丝 1
获赞 6

 - Player类

import java.awt.List;
import java.util.LinkedList;

public class Player {
    int id;
    String name;
    LinkedList<Object> handcard = new LinkedList<>();

    public Player(int id,String name) {
        this.id=id;
        this.name=name;
        //System.out.println("欢迎"+name);
    }
}

 - 自定义异常类

import javax.management.RuntimeOperationsException;

public class Playerex extends RuntimeException {

public Playerex(RuntimeException a, String b) {
    super(b);
}

public Playerex(String me) {
    super(me);
}
// private static final long serialVersionUID = 1L;

}


 - PukerCards类

package cards;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.text.Caret;

import pukercard.Card;
import shiyan.sadsad;

public class PukerCards implements Comparable<PukerCards> {
List<PukerCards> card = new LinkedList<>();
List<PukerCards> set;
Set<PukerCards> pcCards = new TreeSet<>();
String color;
String num;
String color1[] = new String[] { "", "", "", "" };
String num1[] = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
Player player1 = null;
Player player2 = null;

public PukerCards(String color, String num) {
    this.color = color;
    this.num = num;
}

public PukerCards() {
    // TODO Auto-generated constructor stub
}

public void printCard() {// 创建一副扑克牌
    for (String cString : color1) {
        for (String nstring : num1) {
            card.add(new PukerCards(cString, nstring));
        }
    }
    for (int i = 0; i < card.size(); i++) {
        if (i % 13 == 0 && i != 0) {
            System.out.println("\n");
        }
        System.out.print(card.get(i) + " ");
    }
}

public void shuffleCard() {
    set = new LinkedList<>(card);// 获取一个无序的扑克牌
    // Random a = new Random();//一开始的想法是修改他的hashcode,后面发现有一个方法;
    Collections.shuffle(set);
    for (int i = 0; i < set.size(); i++) {
        if (i % 13 == 0 && i != 0) {
            System.out.println("\n");
        }
        System.out.print(set.get(i) + " ");
    }
}
// System.out.println(set);

public void takeCard(Player player1, Player player2) {// 玩家拿牌

    Iterator it = set.iterator();
    int i = 0;
    System.out.println("\n" + "---------------开始游戏------------------");
    while (it.hasNext() && i < 2) {
        System.out.println("----------" + "玩家" + player1.name + ":拿牌" + "-----------");
        player1.handcard.add(it.next());
        System.out.println("----------" + "玩家" + player2.name + ":拿牌" + "-----------");
        player2.handcard.add(it.next());
        i++;
    }
}

public void playGame(Player player1, Player player2) {// 玩家手牌比较
    this.player1 = player1;
    this.player2 = player2;
    List<PukerCards> pcCards1 = new LinkedList<>();
    List<PukerCards> pcCards2 = new LinkedList<>();
    Set<PukerCards> pcCards3 = new TreeSet<>();
    pcCards1.add((PukerCards) player1.handcard.get(0));
    pcCards1.add((PukerCards) player1.handcard.get(1));
    pcCards2.add((PukerCards) player2.handcard.get(0));
    pcCards2.add((PukerCards) player2.handcard.get(1));
    System.out.println("----------拿牌结束--------------");
    // System.out.println(""+pcCards1.get(0)+pcCards1.get(1));// 玩家一手牌
    // System.out.println(""+pcCards2.get(0)+pcCards2.get(1));//玩家二手牌

    pcCards = new TreeSet<>(pcCards1);
    pcCards3 = pcCards;
    Iterator it = pcCards.iterator();
    System.out.println("请玩家展示最大手牌");
    System.out.print("玩家" + player1.name + "最大手牌为:");
    while (it.hasNext()) {
        it.next();
        System.out.println(it.next());
    }
    // System.out.println("\n" + );
    pcCards = new TreeSet<>(pcCards2);
    pcCards3.add(pcCards2.get(0));
    pcCards3.add(pcCards2.get(1));
    ((TreeSet<PukerCards>) pcCards3).last();
    // System.out.println("\n" + pcCards);// 玩家排序后的扑克牌。
    Iterator it2 = pcCards.iterator();
    System.out.print("玩家" + player2.name + "最大手牌为:");
    String cString;
    while (it2.hasNext()) {
        it2.next();
        cString = it2.next().toString();
        System.out.println(cString);
        if (cString.equals(((TreeSet<PukerCards>) pcCards3).last())) {
            System.out.println("玩家" + player2.name + "胜利");
        } else {
            System.out.println("玩家" + player2.name + "胜利");
        }
    }

    // System.out.println("\n" + pcCards);// 玩家排序后的扑克牌。
    System.out.println("玩家" + player2.name + "手牌为:" + pcCards1.get(0) + " " + pcCards1.get(1));// 玩家一手牌
    System.out.println("玩家" + player2.name + "手牌为:" + pcCards2.get(0) + " " + pcCards2.get(1));// 玩家二手牌
}

@Override
public String toString() {// 改写tostring方法
    // Iterator it=card.iterator();
    return color + num;
}

@Override
public int compareTo(PukerCards o) {// 集合里面的条件排序,重写的方法
    int i = 0;
    int m = 0;
    for (i = 0; i < this.num1.length; i++) {
        if (this.num.equals(this.num1[i])) {
            break;
        }
    }
    for (m = 0; m < o.num1.length; m++) {
        if (o.num.equals(o.num1[m])) {
            break;
        }
    }
    if (i > m) {
        return 1;
    }
    if (i < m) {
        return -1;
    }
    if (i == m) {
        for (i = 0; i < this.color1.length; i++) {
            if (this.color.equals(this.color1[i])) {
                break;
            }
        }
        for (m = 0; m < this.color1.length; m++) {
            if (o.color.equals(o.color1[m])) {
                break;
            }
        }
        if (i > m) {
            return -1;
        }
        if (i < m) {
            return 1;
        }

    }
    return 0;
}

}


 - 测试类

import java.util.Scanner;

public class Text {
public static void main(String[] args) {
PukerCards cards1=new PukerCards();
System.out.println("\n" + "-------创建扑克牌成功!-------");
cards1.printCard();
try {
Scanner input=new Scanner(System.in);
System.out.println("\n"+"请输入第一个玩家的id:");
int playerid=input.nextInt();
System.out.println("请输入第一个玩家的昵称:");
Scanner input2=new Scanner(System.in);
String playname=input2.nextLine();
System.out.println("---------------------");
Player player1=new Player(playerid,playname);
System.out.println("请输入第二个玩家的id:");
int playerid2=input.nextInt();
try {
if(playerid2==playerid){
throw new Playerex("id一样异常");
}
} catch (Playerex e) {
System.out.println("id有误从新来过");
System.exit(0);
}
System.out.println("请输入第二个玩家的昵称:");
Scanner input3=new Scanner(System.in);
String playname2=input3.nextLine();
Player player2=new Player(playerid2,playname2);
System.out.println("\n" + "-------开始洗牌-------");
System.out.println("-------洗牌结束-------");
cards1.shuffleCard();
PukerCards card = null;
cards1.takeCard( player1, player2);
cards1.playGame(player1, player2);
} catch (Exception e) {
System.out.println("输入id只能是数字!!!!!");
}

    System.out.println();

}

}

![图片描述][1]
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP