The two situations touched on were:
a) A simplified "poker engine", with almost no interface, just the basic mechanics of dealing, etc. The idea being that we let something else (like the main routine) deal with presenting the data.b) A unified, razzle-dazzle GUI program with dancing cards, and players that blow smoke in your face :-)
|
|
Since some folks have requested some "real code", I shall put together a quick version of the "simple engine" purely for something to poke at.
/**** Save this code as "Poker.java" and compile with javac. Then run
**** with "java Poker"
****/
class Card {
int Number;
String Suit;
public Card(int number, String suit){ Number=number; Suit=suit;}
public int getNumber(){ return Number;}
public String getSuit(){ return Suit;}
public void printName(){
System.out.println(Number + " of " + Suit);
}
}
class Deck {
String Suits="CHSD"; /* notused, but should be */
Card cards[];
public Deck(){
int number,cardpos=0;
cards=new Card[52];
for(number=1;number<=13; number++){
cards[cardpos++]=new Card(number,"Clubs");
}
for(number=1;number<=13; number++){
cards[cardpos++]=new Card(number,"Hearts");
}
for(number=1;number<=13; number++){
cards[cardpos++]=new Card(number,"Spades");
}
for(number=1;number<=13; number++){
cards[cardpos++]=new Card(number,"Diamonds");
}
}
public Card getCard(){
int cardnum=(int)(Math.random() * 52); // Pick a card!
Card newcard;
while(cards[cardnum]==null){ // If taken, use next one
cardnum++;
if(cardnum>51) cardnum=0;
}
newcard=cards[cardnum];
cards[cardnum]=null;
return newcard;
}
}
class Player {
Card hand[];
public void getHand(Deck deck){
hand=new Card[5];
hand[0]=deck.getCard();
hand[1]=deck.getCard();
hand[2]=deck.getCard();
hand[3]=deck.getCard();
hand[4]=deck.getCard();
}
public void printHand(){
int count=0;
for(count=0;count < hand.length;count++){
hand[count].printName();
}
}
}
/*** This is the main, top-level object that java requires ***/
public class Poker {
public static void main(String args[]){
Player player1=new Player(), player2=new Player();
Deck deck = new Deck();
player1.getHand(deck);
player2.getHand(deck);
System.out.println("Player 1's hand is");
player1.printHand();
System.out.println("Player 2's hand is");
player2.printHand();
}
}
There are several corners that have been cut, in the above example:
OOP Table of Contents ---
Part of bolthole.com...
Solaris tips ...
ksh tutorial ...
AWK Programming tutorial