手记

Java入门第三季扑克牌小游戏

package com.zl.pukegame;

/**
 * 玩家类 id 
 * 姓名name 
 * 牌c1 
 * 牌c2
 */
public class Gamer {
    private String id;
    private String name;
    private Card c1;
    private Card c2;

    public Gamer(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public Gamer() {

    }

    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 Card getC1() {
        return c1;
    }

    public void setC1(Card c1) {
        this.c1 = c1;
    }

    public Card getC2() {
        return c2;
    }

    public void setC2(Card c2) {
        this.c2 = c2;
    }

}
package com.zl.pukegame;

/**
 * 扑克牌类 
 * 花色 
 * 大小
 */
public class Card {
    private String color;
    private String data;

    public Card(String color, String data) {
        this.color = color;
        this.data = data;
    }

    public Card() {

    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}
package com.zl.pukegame;

/**
 * 花色类
 */
import java.util.ArrayList;
import java.util.List;

public class ColorList {
    public List<String> colorList;

    public ColorList() {
        colorList = new ArrayList<String>();
        colorList.add("方块");
        colorList.add("梅花");
        colorList.add("红桃");
        colorList.add("黑桃");
    }

}
package com.zl.pukegame;

/**
 * 牌组类
 * 创建一副牌
 * 洗牌
 * 发牌
 * 比大小
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardList {
    public List<Card> cardList;// 有序牌组
    public List<Card> washedCardList;

    public CardList() {
        cardList = new ArrayList<Card>();
        washedCardList = new ArrayList<Card>();
    }

    /**
     * 创建一副牌
     */
    public void createCard() {
        ColorList cl = new ColorList();
        Data dt = new Data();
        System.out.println("-------创建扑克牌-------");
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                cardList.add(new Card(cl.colorList.get(j), dt.dataList.get(i)));
            }
        }
        // foreach打印牌组
        int i = 0;
        for (Card card : cardList) {
            if (i % 4 == 0) {
                System.out.println();
            }
            System.out.println("<" + card.getColor() + card.getData() + ">"
                    + " ");
        }
        System.out.println("------扑克牌创建成功------");
    }

    /**
     * 洗牌
     */
    public void washCardList() {
        Random random = new Random();
        for (int i = 0; i < 52; i++) {
            Card card;
            do {
                card = cardList.get(random.nextInt(52));
            } while (washedCardList.contains(card));
            washedCardList.add(card);
        }
        System.out.println("------洗牌成功------");
    }

    /**
     * 发牌
     */
    public Card deal() {
        Card card;
        card = washedCardList.get(washedCardList.size() - 1);
        washedCardList.remove(washedCardList.size() - 1);
        return card;
    }

    /**
     * 比较大小
     */
    public Card compare(Card c1, Card c2) {
        String data1 = c1.getData();
        String data2 = c2.getData();
        String color1 = c1.getColor();
        String color2 = c2.getColor();
        if (data1.compareTo(data2) > 0) {
            return c1;
        } else if (data1.compareTo(data2) < 0) {
            return c2;
        } else {// 大小相等比较花色:黑红梅方
            if (color1.compareTo(color2) >= 0) {
                return c1;
            } else {
                return c2;
            }
        }
    }
/**
 * 测试牌组类
 * @param args
 */
//  public static void main(String[] args) {
//      CardList cl = new CardList();
//      cl.createCard();
//      cl.washCardList();
//      int i=0;
//      System.out.println("------洗牌后牌组-----");
//      for (Card card : cl.washedCardList) {
//          if (i % 4 == 0) {
//              System.out.println();
//          }
//          System.out.println("<" + card.getColor() + card.getData() + ">"
//                  + " ");
//      }
//      System.out.println("-------洗牌成功------");
//      Card c1=cl.deal();
//      System.out.println("发牌:"+c1.getColor()+c1.getData());
//      Card c2=cl.deal();
//      System.out.println("发牌:"+c2.getColor()+c2.getData());
//      Card c3=cl.deal();
//      System.out.println("发牌:"+c3.getColor()+c3.getData());
//      Card c4=cl.deal();
//      System.out.println("发牌:"+c4.getColor()+c4.getData());
//      System.out.println("------发牌后牌组-----");
//      for (Card card : cl.washedCardList) {
//          if (i % 4 == 0) {
//              System.out.println();
//          }
//          System.out.println("<" + card.getColor() + card.getData() + ">"
//                  + " ");
//      }
//      System.out.println("-------发牌成功------");
//      Card c11=cl.compare(c1, c3);
//      Card c22=cl.compare(c2, c4);
//      System.out.println("发牌:"+c11.getColor()+c11.getData());
//      System.out.println("发牌:"+c22.getColor()+c22.getData());
//      System.out.println("--------比较成功--------");
//  }
}
package com.zl.pukegame;

/**
 * 牌大小
 */
import java.util.ArrayList;
import java.util.List;

public class Data {
    public List<String> dataList;

    public Data() {
        dataList = new ArrayList<String>();
        for (int i = 2; i <= 10; i++) {
            dataList.add(i + "");
        }
        dataList.add("J");
        dataList.add("Q");
        dataList.add("K");
        dataList.add("A");
    }
}

package com.zl.pukegame;

import java.util.Scanner;

public class PlayGame {
public void paly() {
Scanner console = new Scanner(System.in);
System.out.println("输入第一位玩家ID:");
String id1 = console.next();
System.out.println("输入第一位玩家姓名:");
String name1 = console.next();
System.out.println("输入第二位玩家ID:");
String id2 = console.next();
System.out.println("输入第二位玩家姓名:");
String name2 = console.next();
Gamer g1 = new Gamer(id1, name1);
Gamer g2 = new Gamer(id2, name2);
CardList cl = new CardList();
cl.createCard();
cl.washCardList();
System.out.println("-------开始发牌-------");
Card c1 = cl.deal();
g1.setC1(c1);
System.out.println("玩家" + g1.getName() + "发第一张牌" + c1.getColor()

  • c1.getData());
    Card c2 = cl.deal();
    g2.setC1(c2);
    System.out.println("玩家" + g2.getName() + "发第一张牌" + c2.getColor()
  • c2.getData());
    Card c3 = cl.deal();
    g1.setC2(c3);
    System.out.println("玩家" + g1.getName() + "发第二张牌" + c3.getColor()
  • c3.getData());
    Card c4 = cl.deal();
    g2.setC2(c4);
    System.out.println("玩家" + g2.getName() + "发第二张牌" + c4.getColor()
  • c4.getData());
    Card c11 = cl.compare(g1.getC1(), g1.getC2());
    Card c22 = cl.compare(g2.getC1(), g2.getC2());
    System.out.println("玩家" + g1.getName() + "的牌为: " + c11.getColor()
  • c11.getData());
    System.out.println("玩家" + g2.getName() + "的牌为: " + c22.getColor()
  • c22.getData());
    Card card = cl.compare(c11, c22);
    if (card.equals(c11) && (!card.equals(c22))) {
    System.out.println("玩家" + g1.getName() + "获胜");
    } else if (card.equals(c22) && (!card.equals(c11))) {
    System.out.println("玩家" + g2.getName() + "获胜");
    } else {
    System.out.println("平局!!!");
    }
    }
    }

package com.zl.pukegame;

import java.util.Scanner;

public class TestPlay {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
 String str="start";
 PlayGame pg=new PlayGame();
 Scanner console=new Scanner(System.in);
 System.out.println("--------开始游戏-------");
 while(!str.equals("end")){
     pg.paly();
     System.out.println("是否结束游戏?(输入end结束,任意键继续游戏)");
     str=console.next();
 }
}

}

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