Back in 2009 at the University we had to implement a game for mobile devices in J2ME. We chose Black Jack and I did the business logic.
The logic consists of 3 packages. The bl package implements interfaces from the interfaces package and the executable is in the game package.
So here is the code …
Package: game
/**
*
*/
package bj.game.game;
import bj.game.bl.BjDeck;
import bj.game.bl.BjGame;
import bj.game.bl.BjPlayer;
import bj.game.bl.BjPlayerTyp;
import bj.game.interfaces.Deck;
import bj.game.interfaces.Game;
import bj.game.interfaces.Player;
/**
* @author Dzenan Hamzic
*
*/
public class RunGame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Deck deck = new BjDeck();
//List<Card>; cards= deck.getCards();
// list all cards in deck
/*
for (Card card : cards) {
System.out.println(card.getTyp() + " " + card.getValue());
}
*/
Player dealer = new BjPlayer(BjPlayerTyp.DEALER);
Player looser = new BjPlayer(BjPlayerTyp.PLAYER);
/*
dealer.getCards(deck.deal());
looser.getCards(deck.deal());
System.out.println("dealers cards");
dealer.showCards();
System.out.println("loosers cards");
looser.showCards();
System.out.println("dealers takes card...");
dealer.takeCard(deck.getRandomCard());
System.out.println("looser takes card...");
looser.takeCard(deck.getRandomCard());
System.out.println("dealers cards");
dealer.showCards();
System.out.println("loosers cards");
looser.showCards();
*/
//------------------------
Game newGame = new BjGame(dealer, looser);
newGame.start();
}
}
Package: interfaces
package bj.game.interfaces;
import bj.game.bl.BjCardTyp;
/**
* @author Dzenan Hamzic
*
*/
public interface Card {
public int getValue();
public void setUsed(boolean used);
public boolean isUsed();
public BjCardTyp getTyp();
public void setTyp(BjCardTyp typ);
public void setPhoto(String path);
public String getPhoto();
}
/**
*
*/
package bj.game.interfaces;
/**
* @author Dzenan Hamzic
*
*/
public interface Constants {
static int START_PLAYER_MONEY_AMOUNT = 1000;
static int START_DEALER_MONEY_AMOUNT = 100000;
static final int CARD_SUM = 21;
}
/**
*
*/
package bj.game.interfaces;
import java.util.List;
/**
* @author Dzenan Hamzic
*
*/
public interface Deck {
public List getCards();
public int getSize();
public void shuffle();
public List deal();
public Card getRandomCard();
}
package bj.game.interfaces;
/**
* @author Dzenan Hamzic
*
*/
public interface Game {
public void initialize();
public void start();
public void showResults();
public void quit();
public void showPlayerMoney();
public void addMoneyInGame(int money);
public int showMoneyInGame();
public void dealerWon();
public void playerWon();
public void tie();
boolean checkPlayersCards(int choice);
/**
* check if dealer has less than 17 and give one more card
*/
public void checkDealersCardsSum();
}
package bj.game.interfaces;
import java.util.List;
import bj.game.bl.BjPlayerTyp;
public interface Player {
public void showCards(boolean b);
public void setCards(List cards);
public void takeCard(Card card);
public BjPlayerTyp getTyp();
public int getMoney();
public void setMoney(int money);
public void winMoney(int amount);
public void selectChip(int amount);
public void fold();
public int checkCardsSum();
}
Package: bl (business logic)
/**
*
*/
package bj.game.bl;
import bj.game.interfaces.Card;
/**
* @author Dzenan Hamzic
*
*/
public class BjCard implements Card {
private boolean used;
private BjCardTyp typ;
private int value;
private String photoPath;
/* (non-Javadoc)
* @see bj.game.interfaces.Card#getUsed()
*/
public BjCard(BjCardTyp typ, int value) {
super();
this.used = false;
this.typ = typ;
this.value = value;
}
@Override
public boolean isUsed() {
return this.used;
}
/* (non-Javadoc)
* @see bj.game.interfaces.Card#getValue()
*/
@Override
public int getValue() {
return this.value;
}
/* (non-Javadoc)
* @see bj.game.interfaces.Card#setUsed()
*/
@Override
public void setUsed(boolean used) {
this.used = used;
}
@Override
public BjCardTyp getTyp() {
return this.typ;
}
@Override
public void setTyp(BjCardTyp typ) {
this.typ = typ;
}
@Override
public String getPhoto() {
return this.photoPath;
}
@Override
public void setPhoto(String path) {
this.photoPath = path;
}
@Override
public String toString(){
return typ.toString() + " " + value;
}
}
/**
*
*/
package bj.game.bl;
/**
* @author Dzenan Hamzic
*
*/
public enum BjCardTyp {
NORMAL,
AS,
JOKER,
QUEEN,
KING;
}
/**
*
*/
package bj.game.bl;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import bj.game.interfaces.Card;
import bj.game.interfaces.Deck;
/**
* @author Dzenan Hamzic
*
*/
public class BjDeck implements Deck {
private static final int CARD_NUMBER = 51;
private List cards;
private Random R;
public BjDeck() {
super();
cards = new ArrayList<Card>();
R = new Random();
initialize(cards);
}
private void initialize(List cards){
Card card;
int i, k;
// four aces AS
for(i=0;i&lt;4;i++){
card = new BjCard(BjCardTyp.AS, 1);
cards.add(card);
}
// 2,3,4,5,6,7,8,9,10 x 4
for(i = 2; i &lt; 11; i++){
for(k=0;k &lt; 4;k++){
card = new BjCard(BjCardTyp.NORMAL, i);
cards.add(card);
}
}
// four Jokers
for(i=0;i &lt; 4;i++){
card = new BjCard(BjCardTyp.JOKER, 10);
cards.add(card);
}
// four Queens
for(i=0;i &lt; 4;i++){
card = new BjCard(BjCardTyp.QUEEN, 10);
cards.add(card);
}
// four Kings
for(i=0;i &lt; 4;i++){
card = new BjCard(BjCardTyp.KING, 10);
cards.add(card);
}
}
/* (non-Javadoc)
* @see bj.game.interfaces.Deck#deal()
*/
@Override
public List<Card>; deal() {
List<Card>; output = new ArrayList<Card>;(2);
output.add(getRandomCard());
output.add(getRandomCard());
return output;
}
/* (non-Javadoc)
* @see bj.game.interfaces.Deck#getSize()
*/
@Override
public int getSize() {
return this.cards.size();
}
/* (non-Javadoc)
* @see bj.game.interfaces.Deck#shuffle()
*/
@Override
public void shuffle() {
for (Card card : this.cards) {
card.setUsed(false);
}
}
@Override
public List<Card>; getCards() {
return this.cards;
}
@Override
public Card getRandomCard() {
int randomNr = 0;
Card randomCard = null;
try {
randomNr = R.nextInt(CARD_NUMBER);
randomCard = cards.get(randomNr);
if (randomCard.isUsed()) {
getRandomCard();
}
randomCard.setUsed(true);
} catch (StackOverflowError e) {
System.out.println("error with random: " + randomNr);
}
return randomCard;
}
}
package bj.game.bl;
import java.util.Scanner;
import bj.game.interfaces.Constants;
import bj.game.interfaces.Deck;
import bj.game.interfaces.Game;
import bj.game.interfaces.Player;
/**
* @author Dzenan Hamzic
*
*/
// this should be threaded!
public class BjGame implements Game {
private Deck deck;
private Player dealer;
private Player looser;
private int moneyInGame;
private boolean started;
private BjPlayerTyp winner;
private Scanner scan;
public BjGame(Player dealer, Player looser) {
super();
this.deck = new BjDeck();
this.dealer = dealer;
this.looser = looser;
this.scan = new Scanner(System.in);
initialize();
}
@Override
public void quit() {
}
@Override
public void showResults() {
if(winner == BjPlayerTyp.DEALER)
System.out.println("YOU LOST!");
else if(winner == BjPlayerTyp.PLAYER)
System.out.println("YOU WON!");
else
System.out.println("TIE");
showPlayerMoney();
}
@Override
public void start() {
started = true;
while(started){
this.showPlayerMoney();
// wait for user to insert money
// inserting chips
System.out.println("insert some money");
addMoneyInGame(scan.nextInt());
this.showMoneyInGame();
this.showPlayerMoney();
// dealing cards
dealer.setCards(deck.deal());
looser.setCards(deck.deal());
gameLoop();
}
showResults();
playSomeMore();
}
private void playSomeMore() {
if(looser.getMoney() > 0){
System.out.println("continue playing? y/n");
String input = scan.next();
if(input.equals("y")){
moneyInGame = 0;
deck.shuffle();
start();
}else
quit();
}
}
private void gameLoop(){
// showing cards
dealer.showCards(false);
looser.showCards(false);
System.out.println("waiting for user to take action...");
System.out.println("1 hit me, 2 stand");
// hit me or stand
int input = scan.nextInt();
if(checkPlayersCards(input)){ // should I continue?
gameLoop();
}else{
started = false;
}
}
@Override
public void initialize() {
moneyInGame = 0;
this.dealer.setMoney(Constants.START_DEALER_MONEY_AMOUNT);
this.looser.setMoney(Constants.START_PLAYER_MONEY_AMOUNT);
}
@Override
public void showPlayerMoney() {
System.out.println("Currently You have : " + this.looser.getMoney() + " chips left!");
}
@Override
public void addMoneyInGame(int money) {
this.moneyInGame += money;
this.looser.setMoney(this.looser.getMoney()-money);
}
@Override
public int showMoneyInGame() {
return this.moneyInGame;
}
@Override
public boolean checkPlayersCards(int choice) {
boolean weiter = true;
if(choice == 1){// hit me
this.looser.takeCard(this.deck.getRandomCard());// take another card
looser.showCards(false);
if(looser.checkCardsSum() > Constants.CARD_SUM && dealer.checkCardsSum() <= 21){
dealerWon();
weiter = false;
}
if(looser.checkCardsSum() == Constants.CARD_SUM && (dealer.checkCardsSum() < 21 || dealer.checkCardsSum() > 21) && looser.checkCardsSum() != dealer.checkCardsSum() ){
// player won
playerWon();
weiter = false;
}
if(looser.checkCardsSum() == dealer.checkCardsSum()){
// tie
tie();
weiter = false;
}
}else{// stand
checkDealersCardsSum();
dealer.showCards(true);
if(looser.checkCardsSum() > dealer.checkCardsSum() || (dealer.checkCardsSum()>21 && dealer.checkCardsSum() != 21))
playerWon();
else if(looser.checkCardsSum() == dealer.checkCardsSum())
tie();
else
dealerWon();
weiter = false;
}
return weiter;
}
@Override
public void dealerWon() {
started = false;
winner = BjPlayerTyp.DEALER;
}
@Override
public void playerWon() {
started = false;
looser.setMoney(looser.getMoney()+2*moneyInGame);
winner = BjPlayerTyp.PLAYER;
}
@Override
public void tie() {
started = false;
looser.setMoney(looser.getMoney()+moneyInGame);
winner = null;
}
/*
* (non-Javadoc)
* @see bj.game.interfaces.Game#checkDealerCards()
*/
@Override
public void checkDealersCardsSum() {
if(dealer.checkCardsSum() < 17)
dealer.takeCard(deck.getRandomCard());
if(dealer.checkCardsSum() < 17)
checkDealersCardsSum();
}
}
/**
*
*/
package bj.game.bl;
import java.util.ArrayList;
import java.util.List;
import bj.game.interfaces.Card;
import bj.game.interfaces.Player;
/**
* @author Dzenan Hamzic
*
*/
public class BjPlayer implements Player{
private List<Card> cards;
private BjPlayerTyp typ;
private int money;
public BjPlayer(BjPlayerTyp typ) {
this.cards = new ArrayList<Card>();
this.typ = typ;
}
@Override
public void showCards(boolean b) {
System.out.println(this.typ + " has: ");
for (Card card : this.cards) {
System.out.println(card);
// show only first card from dealer
if(this.typ == BjPlayerTyp.DEALER && b == false)
return;
}
System.out.println("-------");
System.out.println(checkCardsSum());
}
@Override
public void setCards(List<Card> cards) {
this.cards = cards;
}
@Override
public void takeCard(Card card) {
this.cards.add(card);
}
@Override
public BjPlayerTyp getTyp() {
return typ;
}
@Override
public void fold() {
// TODO Auto-generated method stub
}
@Override
public int getMoney() {
return this.money;
}
@Override
public void selectChip(int amount) {
this.money -= amount;
}
@Override
public void winMoney(int amount) {
this.money += amount;
}
@Override
public void setMoney(int money) {
this.money = money;
}
@Override
public int checkCardsSum() {
int sum = 0;
for(Card card: this.cards)
sum += card.getValue();
return sum;
}
}
/**
*
*/
package bj.game.bl;
/**
* @author Dzenan Hamzic
*
*/
public enum BjPlayerTyp {
DEALER, PLAYER;
}
Feel free to use the code as you wish.
Cheers!

