Source Code for:
"Developing Collaborative Games Using Active Objects," Vol. 3, Issue 1, p. 52

 
Listings 1a-1b:  Active Object Framework
See Part 1 of this article in last month’s issue.
Listing 2a: Blackjack Main
import java.awt.*;

public class Blackjack extends Object
{
 public static final int MAX_PLAYERS = 5;
 public static BJServer server;
 public static BJClient[] client = new BJClient[MAX_PLAYERS+1];

 public Blackjack()
 {
  server = new BJServer();
  for (int i=1; i<=MAX_PLAYERS; i++) client[i] = new BJClient(i);
 }
 public static void main(String[] args)
 {
  new Blackjack();
  server.start();
  for (int i=1; i<=MAX_PLAYERS; i++) client[i].start();
 }

 public static final int hit = 1;
 public static final int stand = 2;
 public static final int game_result = 3;
 public static final int request_action = 4;
 public static final int bust = 5;
 public static final int you_bust = 6;
 public static final int deal_card = 7;
 public static final int show_card = 8;
}

Listing 2b: Event Definitions
class deal_card extends Object
{
 int clientid;
 Card card;
 public deal_card(int clientid, Card card) {
  this.clientid = clientid;
  this.card=card;
 }
 public int hashCode() { return Blackjack.deal_card; }
 public String toString() {return "Your card "+card.toString();}
}
class show_card extends deal_card
{
 public show_card(int clientid, Card card) {super(clientid, card);}
 public int hashCode() {return Blackjack.show_card;}
 public String toString() {return "Player "+clientid+" received "+card.toString();}
}

class game_result extends Object
{
 int winner;
 public game_result(int winner) {this.winner = winner;}
 public int hashCode() {return Blackjack.game_result;}
 public String toString() {return "Player "+winner;}
}
Listing 2c: Blackjack Server
class BJServer extends ActiveObject
{
 private ActiveSession session;
 private int[] cardTotals = new int[Blackjack.MAX_PLAYERS+1];
 private int[] aceCount = new int[Blackjack.MAX_PLAYERS+1];

 public BJServer()
 {
  this.session = new ActiveSession(this,"BJ",0);
 }

 public void handleEvent(Object evt) {}

 public void run() {
  while (true)
  {
   //deal cards
   for (int player=1; player<=Blackjack.MAX_PLAYERS; player++)
   {
    dealCardTo(player,Card.FACE_DOWN);
    dealCardTo(player,Card.FACE_UP);
   }

   //serve each player
   for (int player=1; player<=Blackjack.MAX_PLAYERS; player++)
   {
    Object evt = null;
    do
    {
     session.send(new request_action(),player);

     int[] evts = {Blackjack.hit, Blackjack.stand};
     evt = waitFor(evts);
     if (evt.hashCode()==Blackjack.hit)
     {
      dealCardTo(player,Card.FACE_UP);
      if (cardTotals[player] > 21)
      {
       cardTotals[player] = 0;
       session.mcast(new bust(player),0);
       session.send(new you_bust(),player);
       break;  //breaks out of do loop
      }
     }
    } while (evt.hashCode()==Blackjack.hit);
   }

   //send results
   session.mcast(new game_result(winner()),0);
  }
 }

 void dealCardTo(int player, int face)
 {
  Card card = Deck.dealCard(face);
  if (card.getValue()==1) aceCount[player]++;
  cardTotals[player]+= (card.getValue()==1 ? 11 : card.getValue());

  while (cardTotals[player] > 21 && aceCount[player] > 0)
   if (cardTotals[player] > 21) { cardTotals[player]-=10; aceCount[player]--;}

  session.mcast(new show_card(player,card),0);
  session.send(new deal_card(player,card),player);
 }

 int winner()
 {
  int max = 0;
  int theWinner = 0;
  for (int i=1; i<=Blackjack.MAX_PLAYERS; i++)
   if (cardTotals[i] > max) {max = cardTotals[i]; theWinner = i;}
  return theWinner;
 }
}
Listing 2d: Blackjack Client
class BJClient extends ActiveObject
{
 protected int clientID;

 private ActiveSession session;
 private TextArea textBox;

 public BJClient(int clientID)
 {
  this.clientID = clientID;
  this.session = ActiveSession.join(this,"BJ",0);
  Frame f = new Frame(this.getClass()+" "+clientID);
  f.add("Center",textBox = new TextArea(20,40));
  f.pack(); f.show();
  int[] evts = {Blackjack.show_card,Blackjack.bust};  registerCallback(evts);
 }

 public void handleEvent(Object evt)
 {
  switch (evt.hashCode())
  {
   case Blackjack.show_card:
    print("    "+evt);
    break;
   case Blackjack.bust:
    print("    "+evt);
  }
 }

 public void print(Object o) {textBox.appendText(o.toString()+"\n");}

 public void run()
 {
  while(true)
  {
   print(waitFor(Blackjack.deal_card));
   print(waitFor(Blackjack.deal_card));
ActionLoop:
   while (true)
   {
    int[] evts = {Blackjack.request_action,Blackjack.you_bust};
    Object evt = waitFor(evts);
    if (evt.hashCode() == Blackjack.you_bust)
    {
     print("You bust!");
     break;
    }

    print("[H]it or [S]tand?\n");
    char c = 's';
    do    {     try {c = (char) System.in.read();}
     catch (Exception e) {e.printStackTrace();}     switch (c)     {      case 'H': case 'h':         session.send(new hit(),0);        print(waitFor(Blackjack.deal_card));       break;      case 'S': case 's':
       session.send(new stand(),0);
       break ActionLoop;      default:      }    } while (c!='h' && c!='H');   }   print("The winner is "+waitFor(Blackjack.game_result)+"\n------------");  } }}
 

class bust extends Object
{
 int player;
 public bust(int player) {this.player = player;}
 public int hashCode() {return Blackjack.bust;}
 public String toString() {return "Player "+player+" busts!";}
}

class request_action extends Object
 {public int hashCode() {return Blackjack.request_action;}}
class hit extends Object
{public int hashCode() {return Blackjack.hit;}}
class stand extends Object
{public int hashCode() {return Blackjack.stand;}}
class you_bust extends Object
{public int hashCode() {return Blackjack.you_bust;}}