我所理解的扑克牌游戏是如何开发的

348 查看

package cardgame;
/**

  • @author tf
  • */
    public class Card {
    String pattern;//扑克牌的花色
    String figure;//扑克牌的点数
    public Card(){

    }
    public Card(String figure, String pattern) {
    this.pattern = pattern;
    this.figure = figure;
    }

}
package cardgame;
/**

  • 创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
  • @author tf
  • */
    public class Player {
    public int id;
    public String name;
    public Card card1;
    public Card card2;
    public Player() {

    }
    public Player(int id, String name, Card card1, Card card2) {
    this.id = id;
    this.name = name;
    this.card1 = card1;
    this.card2 = card2;
    }
    }
    package cardgame;

import java.util.ArrayList;
import java.util.List;

/**

  • 定义扑克牌的花色
  • @author tf
  • */
    public class Pattern {
    public List<String> patternList;
    public Pattern(){
    patternList = new ArrayList<String>();
    patternList.add("黑桃");
    patternList.add("红桃");
    patternList.add("梅花");
    patternList.add("方片");
    }
    }
    package cardgame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**

  • 定义扑克牌的点数
  • @author tf
  • */
    public class Figure {
    public List<String> figureList;
    public Figure() {
    figureList = new ArrayList<String>();
    String[] figure = {"2","3","4","5","6","7","8","9","10","J","Q","k","A"};
    figureList.addAll(Arrays.asList(figure));
    }

// public static void main(String[] args) {
// Figure fg = new Figure();
// for (String string : fg.figureList) {
// System.out.println(string);
//}
// }
}
package cardgame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**

  • 定义扑克牌的操作
  • @author tf
  • */
    public class CardList {
    public List<Card> cardList;
    public CardList() {
    cardList = new ArrayList<Card>();
    }

    /**

    • 创建一副扑克牌
    • */
      public void creatCard() {
      Pattern pattern = new Pattern();
      Figure figure = new Figure();
      for(int j = 0;j < 4;j++) {
      for(int i =0;i < 13;i++) {
      Card c = new Card(figure.figureList.get(i),pattern.patternList.get(j));
      cardList.add(c);
      }
      }
      System.out.print("为:[");
      for (Card card : cardList) {
      System.out.print(card.pattern + card.figure + ",");
      }
      System.out.print("]");
      System.out.println();
      }
      /**

    • 洗牌操作
    • */
      public List<Card> shuffle() {
      List<Card> lc = new ArrayList<Card>();
      Random random = new Random();
      while(true) {
      Card c = cardList.get(random.nextInt(52));
      if(lc.contains(c)) continue;
      lc.add(c);
      if(lc.size() == 52) break;
      }
      return lc;
      }

    /**

    • 发牌
      */
      public Card deal(int num) {
      Random random = new Random();
      Card c = this.shuffle().get(random.nextInt(num));
      return c;
      }
      /**
    • 比较大小
    • @param args
      */
      public Card compare(Card c1, Card c2) {
      Figure figure = new Figure();
      Pattern pattern = new Pattern();
      Integer i1 = figure.figureList.indexOf(c1.figure);
      Integer i2 = figure.figureList.indexOf(c2.figure);
      int k = i1.compareTo(i2);
      if(k == 0) {
      //点数相同,则比较花色,按黑桃,红桃,梅花,方片的顺序依次递减
      Integer i3 = pattern.patternList.indexOf(c1.pattern);
      Integer i4 = pattern.patternList.indexOf(c2.pattern);
      int j = i3.compareTo(i4);
      if(j == 0) return c1;
      else if(j < 0) return c1;
      else return c2;
      } else if(k > 0) return c1;
      else return c2;
      }
      public static void main(String[] args) {
      CardList cl = new CardList();
      cl.creatCard();
      // for (Card string : cl.shuffle()) {
      // System.out.println(string.pattern + string.figure);
      // }
      Card c1=cl.deal(52);
      System.out.println(c1.pattern+c1.figure);
      Card c2=cl.deal(52);
      System.out.println(c2.pattern+c2.figure);
      Card c = cl.compare(c1, c2);
      System.out.println(c.pattern + c.figure);
      }

}
package cardgame;

import java.util.Scanner;

import sun.net.www.content.text.plain;

/**

  • 1.创建一副扑克牌,不考虑大小王
  • 2.创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
  • 3.洗牌,将之前创建的扑克牌顺序打乱
  • 4.发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
  • 5.开始游戏,比大小,大小相同比花色
  • @author tf
  • */
    public class CardGame {

    public static void main(String[] args) {
    System.out.println("------------------创建扑克牌---------------------------");
    System.out.println("------------------扑克牌创建成功!-----------------------");
    CardList cl = new CardList();
    cl.creatCard();
    System.out.println("-----------------------开始洗牌------------------------");
    //洗牌
    cl.shuffle();
    System.out.println("-----------------------洗牌结束------------------------");
    System.out.println("-----------------------创建玩家-------------------------");
    //提示创建玩家
    Player[] p = {new Player(),new Player()};
    for(int i = 0;i < 2;i++) {
    System.out.println("请输入第" +(i+1) +"位玩家的ID和姓名:");
    while(true) {
    System.out.println("请输入玩家ID:");
    try {
    Scanner console = new Scanner(System.in);
    p[i].id = console.nextInt();
    break;
    } catch (Exception e) {
    System.out.println("输入不合法,请重新输入整数类型的ID");
    continue;
    }
    }
    System.out.println("请输入玩家姓名:");
    Scanner console = new Scanner(System.in);
    p[i].name = console.next();
    }
    System.out.println("----------欢迎玩家:" + p[0].name);
    System.out.println("----------欢迎玩家:" + p[1].name);
    System.out.println("-----------------------开始发牌-----------------------");
    int k = 0;
    while(k < 2) {
    System.out.println("-------玩家:" + p[0].name+"-拿牌");
    System.out.println("-------玩家:" + p[1].name+"-拿牌");
    k++;
    }
    //发牌
    Card c11 = cl.deal(52);
    Card c12 = cl.deal(51);
    Card c21 = cl.deal(50);
    Card c22 = cl.deal(49);
    p[0].card1 = c11;
    p[0].card2 = c12;
    p[1].card1 = c21;
    p[1].card2 = c22;
    System.out.println("-----------------------发牌结束-----------------------");
    System.out.println("-----------------------开始游戏-----------------------");
    Card c1 = cl.compare(c11, c12);//玩家一的最大手牌
    Card c2 = cl.compare(c21, c22);//玩家二的最大手牌
    Card c = cl.compare(c1, c2);//比较两位玩家的最大手牌
    System.out.println("玩家:" + p[0].name + "最大的手牌为:" + c1.pattern + c1.figure);
    System.out.println("玩家:" + p[1].name + "最大的手牌为:" + c2.pattern + c2.figure);
    if(c.equals(c1) && !c.equals(c2))
    System.out.println("----------------玩家" + p[0].name + "获胜!----------------");
    if(c.equals(c2) && !c.equals(c1))
    System.out.println("----------------玩家" + p[1].name + "获胜!----------------");
    if(c.equals(c1) && c.equals(c2))
    System.out.println("---------------------平局,继续加油-----------------------");
    System.out.println("玩家各自的手牌为:");
    System.out.println(p[0].name + ":" +"[" + c11.pattern + c11.figure + "," + c12.pattern + c12.figure+ "]");
    System.out.println(p[1].name + ":" +"[" + c21.pattern + c21.figure + "," + c22.pattern + c22.figure + "]");

    }
    }