Package ipm.gerdc.baccarat.util

Source Code of ipm.gerdc.baccarat.util.ShuffleCards

/**
* @Author: Edmond Hoi
* @Description: IPM-ESAP-GERDC
* @Date: 2010.09.01
*/
package ipm.gerdc.baccarat.util;

import ipm.gerdc.baccarat.model.CardShoe;

import java.util.Random;

public class ShuffleCards {
  private static final String rank[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

  private static final String suit[] = { "Spade ", "Heart ", "Club ", "Diamond " };

  private static String[] newCards;

  private static CardShoe cardShoe;

  public ShuffleCards() {
    int deckSize = suit.length * rank.length;
    newCards = new String[SysConfig.getNumDeck() * deckSize];
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < suit.length; j++) {
        for (int k = 0; k < rank.length; k++) {
          newCards[i * deckSize + j * rank.length + k] = suit[j] + rank[k];
        }
      }
    }
    shuffle(SysConfig.getRngMethod(), SysConfig.getNumDeck() * deckSize);
  }

  private static String getACard(Random rng, int size) {
    String card = GameTools.BlankCard;
    try {
      long result = 0;
      while (true) {
        result = (long) rng.nextLong() % size;
        if (result < 0) {
          result = ~result;
        } else if (newCards[(int) result].equals(GameTools.BlankCard)) {
          card = GameTools.BlankCard;
        } else {
          card = newCards[(int) result];
          newCards[(int) result] = GameTools.BlankCard;
          break;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return card;
  }

  public static void shuffle(String rngMethod, int size) {
    try {
      Random rng = (Random) Class.forName("ipm.gerdc.baccarat.rng." + rngMethod).newInstance();
      cardShoe = new CardShoe();
      for (int i = 0; i < size; i++) {
        cardShoe.push(getACard(rng, size));
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public CardShoe getCardShoe() {
    return cardShoe;
  }
}
TOP

Related Classes of ipm.gerdc.baccarat.util.ShuffleCards

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.