手记

java入门第三季—扑克牌游戏

package poker;
/*
创建扑克牌类
/
public class Poker {
private String design;//花色
private String number;//点数
public Poker(String design,String number){//初始化成员变量
this.design=design;
this.number=number;
}
/

  • 用getter和setter方法使用被封装的成员变量
    */
    public String getDesign() {
    return design;
    }
    public void setDesign(String design) {
    this.design = design;
    }
    public String getNumber() {
    return number;
    }
    public void setNumber(String number) {
    this.number = number;
    }
    /**
  • 自定义方法比较点数的大小,规定A最小,K最大
  • 当手牌中的点数大于参数扑克牌的点数时,返回1;相等时返回0;小于时返回-1
    */
    public int numberSize(Poker poker){//传入参数扑克牌
    String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    int thisNumber=0;//手牌点数
    int pokerNumber=0;//参数扑克牌点数
    for(int i=0;i<numbers.length;i++){
    if(this.number.equals(numbers[i])&&poker.number.equals(numbers[i]))
    thisNumber=pokerNumber=i;
    else{
    if(this.number.equals(numbers[i]))
    thisNumber=i;
    else{
    if(poker.number.equals(numbers[i]))
    pokerNumber=i;
    }
    }
    }
    if(thisNumber>pokerNumber)
    return 1;
    else{
    if(thisNumber==pokerNumber)
    return 0;
    else
    return -1;
    }
    }
    /**
  • 自定义方法比较花色的大小,规定方块最小,黑桃最大
  • 当手牌中的花色大于参数扑克牌的花色时,返回1;小于时返回-1;这里不定义相等是因为相等的话在点数比较时就有结果了
    */
    public int designSize(Poker poker){
    String[] designs={"方块","梅花","红心","黑桃"};
    int thisDesign=0;//手牌花色
    int pokerDesign=0;//参数扑克牌花色
    for(int i=0;i<designs.length;i++){
    if(this.design.equals(designs[i]))
    thisDesign=i;
    else{
    if(poker.design.equals(designs[i]))
    pokerDesign=i;
    else
    continue;
    }
    }
    if(thisDesign>pokerDesign)
    return 1;
    else
    return -1;
    }
    }
    package poker;
    import java.util.ArrayList;
    import java.util.List;
    /
    创建玩家类
    /
    public class Player {
    private int id;//玩家ID
    private String name;//玩家姓名
    public List<Poker> pokers;//玩家手牌
    public Player(int id,String name){//初始化成员变量
    this.id=id;
    this.name=name;
    this.pokers=new ArrayList<Poker>();
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    package poker;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.Map.Entry;
    /

    测试扑克牌游戏类
    /
    public class GameTest {
    public List pokerList;//扑克牌集合
    public List<Player> players;//玩家集合
    public GameTest(){//初始化成员变量
    this.pokerList=new ArrayList();
    this.players=new ArrayList<Player>();
    }
    /**
  • 创建扑克牌
    */
    public void addPoker(){
    Poker[] poker={new Poker("方块","A"),new Poker("方块","2"),new Poker("方块","3"),new Poker("方块","4"),new Poker("方块","5"),new Poker("方块","6"),new Poker("方块","7"),new Poker("方块","8"),new Poker("方块","9"),new Poker("方块","10"),new Poker("方块","J"),new Poker("方块","Q"),new Poker("方块","K"),
    new Poker("梅花","A"),new Poker("梅花","2"),new Poker("梅花","3"),new Poker("梅花","4"),new Poker("梅花","5"),new Poker("梅花","6"),new Poker("梅花","7"),new Poker("梅花","8"),new Poker("梅花","9"),new Poker("梅花","10"),new Poker("梅花","J"),new Poker("梅花","Q"),new Poker("梅花","K"),
    new Poker("红心","A"),new Poker("红心","2"),new Poker("红心","3"),new Poker("红心","4"),new Poker("红心","5"),new Poker("红心","6"),new Poker("红心","7"),new Poker("红心","8"),new Poker("红心","9"),new Poker("红心","10"),new Poker("红心","J"),new Poker("红心","Q"),new Poker("红心","K"),
    new Poker("黑桃","A"),new Poker("黑桃","2"),new Poker("黑桃","3"),new Poker("黑桃","4"),new Poker("黑桃","5"),new Poker("黑桃","6"),new Poker("黑桃","7"),new Poker("黑桃","8"),new Poker("黑桃","9"),new Poker("黑桃","10"),new Poker("黑桃","J"),new Poker("黑桃","Q"),new Poker("黑桃","K")};
    pokerList.addAll(Arrays.asList(poker));
    System.out.println("-----创建扑克牌-----");
    }
    /**
  • 循环打印扑克牌
    */
    public void forEach(){
    System.out.println("-----扑克牌创建成功-----");
    System.out.print("扑克牌:");
    for(Object obj:pokerList){
    Poker p=(Poker)obj;
    System.out.print(p.getDesign()+p.getNumber()+",");
    }
    System.out.println();//跳到下一行
    }
    /**
  • 打乱扑克牌顺序
    */
    public void disOrganize(){
    System.out.println("-----开始洗牌-----");
    Collections.shuffle(pokerList);
    System.out.println("-----洗牌结束!-----");
    }
    /**
  • 创建玩家
    */
    public void creatPlayer(){
    Scanner sc=new Scanner(System.in);
    int i=0;
    while(i<2){
    System.out.println("输入玩家"+(i+1)+"ID:");
    int ID=sc.nextInt();
    System.out.println("输入姓名:");
    String name=sc.next();
    Player newPlayer=new Player(ID,name);
    players.add(i, newPlayer);
    i++;
    }
    }
    /**
  • 遍历玩家
    */
    public void playerList(){
    for(Player player:players){
    System.out.println("---欢迎玩家:"+player.getName());
    }
    }
    /**
  • 开始发牌
    */
    public void deal(){
    System.out.println("-----开始发牌-----");
    List<Integer> integerList=new ArrayList<Integer>();
    Random random=new Random();
    int k;
    for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
    do{
    k=random.nextInt(52);//取得随机数,去掉重复
    }while(integerList.contains(k));
    integerList.add(k);
    Poker pl=(Poker)pokerList.get(k);//以随机数为索引位置,获取对应的扑克牌
    players.get(j).pokers.add(pl);//将该扑克牌存入玩家的手牌中
    System.out.println("玩家"+players.get(j).getName()+"-拿牌");
    }
    }
    System.out.println("-----发牌结束-----");
    }
    /**
  • 开始游戏
    /
    public void gameBegin(){
    System.out.println("-----开始游戏-----");
    List<Poker> maxPokers=new ArrayList<Poker>();//存放两个玩家的最大手牌
    for(Player player:players){
    Poker maxPoker=null;//定义玩家的最大手牌变量
    Poker p1=player.pokers.get(0);//获取玩家的第一张手牌
    Poker p2=player.pokers.get(1);//获取玩家的第二张手牌
    switch(p1.numberSize(p2)){
    case 1:
    maxPoker=p1;
    break;
    case -1:
    maxPoker=p2;
    break;
    case 0:{
    switch(p1.designSize(p2)){
    case 1:
    maxPoker=p1;
    break;
    case -1:
    maxPoker=p2;
    break;
    }
    }
    }
    System.out.println("玩家"+player.getName()+"的最大手牌是:"+maxPoker.getDesign()+maxPoker.getNumber());
    maxPokers.add(maxPoker);//将最大手牌添加入最大手牌集合
    }
    Poker mp1=maxPokers.get(0);//获取玩家1的最大手牌
    Poker mp2=maxPokers.get(1);//获取玩家2的最大手牌
    switch(mp1.numberSize(mp2)){
    case 1:
    System.out.println("-----玩家"+players.get(0).getName()+"获胜-----");
    break;
    case -1:
    System.out.println("-----玩家"+players.get(1).getName()+"获胜-----");
    break;
    case 0:{
    switch(mp1.designSize(mp2)){
    case 1:
    System.out.println("-----玩家"+players.get(0).getName()+"获胜-----");
    break;
    case -1:
    System.out.println("-----玩家"+players.get(1).getName()+"获胜-----");
    break;
    }
    }
    }
    /

    • 循环打印玩家的手牌
      */
      for(int i=0;i<2;i++){
      System.out.print("玩家"+players.get(i).getName()+"的牌:");
      for(Poker p:players.get(i).pokers){
      System.out.print(p.getDesign()+p.getNumber()+" ");
      }
      System.out.println();
      }
      }

    public static void main(String[] args) {
    GameTest gt=new GameTest();//创建对象
    gt.addPoker();//创建扑克牌
    gt.forEach();//循环打印扑克牌
    gt.disOrganize();//打乱扑克牌顺序
    gt.creatPlayer();//创建玩家
    gt.playerList();//遍历玩家
    gt.deal();//发牌
    gt.gameBegin();//开始游戏
    }

}

0人推荐
随时随地看视频
慕课网APP