Java入門第三季撲克牌小遊戲

piny發表於2021-09-09
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 colorList;

    public ColorList() {
        colorList = new ArrayList();
        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 cardList;// 有序牌組
    public List washedCardList;

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

    /**
     * 建立一副牌
     */
    public void createCard() {
        ColorList cl = new ColorList();
        Data dt = new Data();
        System.out.println("-------建立撲克牌-------");
        for (int i = 0; i "
                    + " ");
        }
        System.out.println("------撲克牌建立成功------");
    }

    /**
     * 洗牌
     */
    public void washCardList() {
        Random random = new Random();
        for (int i = 0; i  0) {
            return c1;
        } else if (data1.compareTo(data2) = 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(""
//                  + " ");
//      }
//      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(""
//                  + " ");
//      }
//      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 dataList;

    public Data() {
        dataList = new ArrayList();
        for (int i = 2; i 

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();
 }
}

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3034/viewspace-2798634/,如需轉載,請註明出處,否則將追究法律責任。

相關文章