Java 扑克发牌算法实现

A. 扑克发牌算法是棋牌游戏中常用的基础算法,也是游戏开发人员需要熟悉的基础算法之一。下面介绍一下该算法的一种实现方式。
  
首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下:
  u    红桃按照从小到大依次为:1-13
  u    方块按照从小到大依次为:14-26
  u    黑桃按照从小到大依次为:27-39
  u    梅花按照从小到大依次为:40-52
  u    小王为53,大王为54
  算法实现如下:
  u    首先按照以上编号规则初始化一个包含108个数字的数组
  u    每次随机从该数组中抽取一个数字,分配给保存玩家数据的数组
  实现该功能的代码如下所示:
  import Java.util.*;
  /**
  * 发牌算法的实现
  * 要求:把2副牌,也就是108张,发给4个人,留6张底牌
*/
public class Exec{
    public static void main(String[] args){
       //存储108张牌的数组
       int[] total = new int[108];
       //存储四个玩家的牌
       int[][] player = new int[4][25];
       //存储当前剩余牌的数量
       int leftNum = 108;
       //随机数字
       int ranNumber;
       //随机对象
       Random random = new Random();
       //初始化数组
       for(int i = 0;i < total.length;i++){
           total[i] = (i + 1) % 54;
           //处理大小王编号
           if(total[i] == 0){
              total[i] = 54; 
           }
       }
       //循环发牌
       for(int i = 0;i < 25;i++){
           //为每个人发牌
           for(int j = 0;j < player.length;j++){
              //生成随机下标
              ranNumber = random.nextInt(leftNum);
              //发牌
              player[j][i] = total[ranNumber];
              //移动已经发过的牌
              total[ranNumber] = total[leftNum - 1];
              //可发牌的数量减少1
              leftNum--;   
           }  
       }
       //循环输出玩家手中的牌
       for(int i = 0;i < player.length;i++){
           for(int j = 0;j < player[i].length;j++){
              System.out.print(" " + player[i][j]);  
           }
           System.out.println();
       }
       //底牌
       for(int i = 0;i < 8;i++){
           System.out.print(" " + total[i]); 
       }
       System.out.println();
    }
}


B.利用Random类的对象的链表中一随机的顺序存储一副52张的纸牌。用含有连个字符的字符串代表纸牌,例如“1C”表示梅花A,”JD”表示方片J等。从栈中输出4手牌,每手牌有13张纸牌。

package com.tengfei.lesson06;   
import java.util.Vector;  
import java.util.LinkedList;  
import java.util.Random;  
import java.util.ListIterator;  
  
public class DealCards {  
  public static void main(String[] args) {  
    String[] suits = {"C", "D", "H", "S"};  
    String[] cardValues = { "1","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};  
  
    int cardsInDeck = 52;  
    Vector<String> deck = new Vector<String>(cardsInDeck);  
    LinkedList<String> shuffledDeck = new LinkedList<String>();  
    Random chooser = new Random();             // Card chooser  
  
    // Load the deck  
    for(String suit : suits) {  
      for(String cardValue : cardValues) {  
        deck.add(cardValue+suit);  
      }  
  }  
  
    // Select cards at random from the deck to transfer to shuffled deck  
    int selection = 0;                        // Selected card index  
    for(int i = 0 ; i<cardsInDeck ; i++) {  
      selection = chooser.nextInt(deck.size());  
      shuffledDeck.add(deck.remove(selection));  
    }  
      
    // Deal the cards from the shuffled deck into four hands  
    StringBuffer[] hands = { new StringBuffer("Hand 1:"), new StringBuffer("Hand 2:"),  
                             new StringBuffer("Hand 3:"), new StringBuffer("Hand 4:")};  
    ListIterator cards = shuffledDeck.listIterator();  
     
    while(cards.hasNext()) {   
      for(StringBuffer hand : hands) {  
        hand.append(' ').append((String)(cards.next()));  
      }  
    }  
  
    // Display the hands  
    for(StringBuffer hand : hands) {  
      System.out.println(hand.toString());  
    }  
  }  
}  


C.斗地主简单发牌。
import java.util.*;

public class Dou {

 /*
  * 生成扑克牌
  */
 public String[] puke() {
  String[] s1 = { "黑桃", "红桃", "梅花", "方片" };
  String[] s2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
    "Q", "K" };
  String[] s3 = { "小王", "大王" };
  String[] s4 = new String[54];
  s4[52] = s3[0];
  s4[53] = s3[1];

  for (int i = 0; i < 52; i++) {
   s4[i] = s1[i % 4] + s2[i / 4];
  }
  return s4;
 }

 /*
  * 洗牌
  */
 public  String[] wash(String[] s2) {
//  Dou dou = new Dou();
//  String s2[] = dou.puke();
  for (int i = 0; i < 100; i++) {
   String k;
   Random ran = new Random();
   int ran1 = ran.nextInt(54);
   int ran2 = ran.nextInt(54);
   // 随机两张牌交换实现洗牌
   k = s2[ran1];
   s2[ran1] = s2[ran2];
   s2[ran2] = k;
  }
  return s2;
 }
 /*
  * 斗地主发牌
  */
 public  String[] check(String[] s1) {
//  Dou dou = new Dou();
//  String s1[] = dou.wash();
  for (int i = 0; i < 54; i++) {
   if (i % 17 == 0) {
    System.out.println("");
    int k = i / 17 + 1;
    if (k == 4)
     System.out.print("底牌:" + "/t");
    else
     System.out.print("玩家" + k + ":" + "/t");
   }
   System.out.print(s1[i] + "/t");
  }
  return s1;
 }

 /*
  * 
  * @main
  */
 public static void main(String[] args) {
  Dou dou = new Dou();
  //String[] s1= dou.check();
  dou.check(dou.wash(dou.puke()));//相当于下面三句
//  String[] s1=dou.puke();
//  String[] s2= dou.wash(s1);
//  dou.check(s2);
 } 
}

你可能感兴趣的:(java,算法,util)