Java實現鬥地主的發牌功能

鬥地主是全國范圍內的一種桌面遊戲,盡管全國各種類型,但大同小異,今天我們先來實現一下鬥地主中的發牌功能,為瞭程序的可讀性以及可拓展性,此次我們使用面向對象的方法來實現此功能。

首先,鬥地主要求有撲克和玩傢才能開始遊戲,所以這裡我們首先要創建一個撲克牌實體類和一個玩傢實體類,具體代碼如下:

首先是Poker類,含點數,花色和排序屬性,大小王之後額外添加,這裡我們必須實現一個Comparable接口,方便之後進行排序:

public class Poker implements Comparable<Poker>{

 private String flower;
 private String pointer;
 private int sort;
 
 public Poker() {

 }

 public Poker(String flower, String pointer, int sort) {
  super();
  this.flower = flower;
  this.pointer = pointer;
  this.sort = sort;
 }

 public String getFlower() {
  return flower;
 }

 public void setFlower(String flower) {
  this.flower = flower;
 }

 public String getPointer() {
  return pointer;
 }

 public void setPointer(String pointer) {
  this.pointer = pointer;
 }

 public int getSort() {
  return sort;
 }

 public void setSort(int sort) {
  this.sort = sort;
 }

 @Override
 public String toString() {
  return flower +"-" + pointer;
 }

 @Override
 public int compareTo(Poker a) {
  return this.getSort() - a.getSort();
 }
 
}

然後就是Player類,這裡我們定義屬性有id,玩傢名字以及玩傢手牌和是否地主:

import java.util.ArrayList;

public class Player {

 private int id;
 private String name;
 private ArrayList<Poker> pokers = new ArrayList<>();
 private boolean boss;
 public Player() {

 }
 public Player(int id, String name) {
  super();
  this.id = id;
  this.name = name;
 }
 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;
 }
 public ArrayList<Poker> getPokers() {
  return pokers;
 }
 public void setPokers(ArrayList<Poker> pokers) {
  this.pokers = pokers;
 }
 public boolean isBoss() {
  return boss;
 }
 public void setBoss(boolean boss) {
  this.boss = boss;
 }

}

實體類建完之後,我們就要開始實現功能瞭,首先我們將要實現的功能簡單敘述一下,首先我們要求有一副54張的完整牌,然後洗牌(此處我們用Collections裡的靜態shuffle方法實現),然後開始按洗好的牌發牌51張,發完隨機地主,將剩餘的三張牌給地主,並將三名玩傢的牌均按大小顯示出來。

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


public class PokerGame {

 /**定義為靜態以簡化代碼量*/
 static List<Player> players;
 static ArrayList<Poker> list = new ArrayList<>();
 static String[] flowers = {"梅花","黑桃","方塊","紅桃"};
 static String[] pointers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
 int bossIndex;
 
 static{
  for(int i = 0;i < flowers.length;i++){
   for(int j = 0;j < pointers.length;j++){
    Poker p = new Poker(flowers[i],pointers[j],j);
    list.add(p);
   }
  }
  list.add(new Poker("小","王",13));
  list.add(new Poker("大","王",14));
  
  players = Arrays.asList(
    new Player(1,"胡燦"),
    new Player(2,"戴豪傑"),
    new Player(3,"馬濤")
    );
 }
 
 /**隨機地主*/ 
 public void pushBoss(){
  /**隨機一個索引,隨到的為地主*/
  Random r = new Random();
  bossIndex = r.nextInt(players.size());
  players.get(bossIndex).setBoss(true);
 }
 
 /**洗牌功能*/
 public void XiPai(){
  /**用Collections中的一個靜態方法實現隨機順序集合*/
  Collections.shuffle(list);
 }
 
 /**發牌功能*/
 public void FaPai(){
  /**每次按順序給每人發一張牌直到51張牌發完*/
  for(int i = 0;i < 51;i+=3){
   players.get(0).getPokers().add(list.get(i)); 
   players.get(1).getPokers().add(list.get(i+1)); 
   players.get(2).getPokers().add(list.get(i+2)); 
  }
  /**將剩餘的三張牌發給地主*/
  for(int j = 51;j < 54;j++){
   players.get(bossIndex).getPokers().add(list.get(j));
  }
 }
 
 /**排序功能*/
 public void PaiXu(){
  /**用Collections的sort方法實現排序功能*/
  Collections.sort(players.get(0).getPokers());
  Collections.sort(players.get(1).getPokers());
  Collections.sort(players.get(2).getPokers());
 }
 
 /**開始遊戲*/
 public void start(){
  pushBoss();
  XiPai();
  FaPai();
  PaiXu();
  for(Player p:players){
   System.out.println(p.getPokers());
  }
 }
}

下面我們來看看具體實現:

public class Test {

 public static void main(String[] args) {
  new PokerGame().start();
 }
 
}

結果如下:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: