Phils quick-n-dirty intro to object-oriented thinking #5 - real code

In the last chapter, I discussed rationale and planning for assorted objects that could be made for a "Poker game" program. Your use of objects may differ quite a bit, depending on how much you are actually going to USE each potential object.

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 :-)

Object count comparison between two implementations

Fancy GUI
Player
Deck
Hand
Card
Table
Simple engine
Player
Deck
Hand(or individual cards)
 
 

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.

Simple poker engine, sample java code

Please note that this code just gives you something that compiles and runs. It does not actually allow "real" playing. That code would be too long for teaching purposes. However, the code below does give you a valid object structure, that could be extended into a full poker program.


/**** 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();
        }
}



What is missing from the above code

There are several corners that have been cut, in the above example:

BUT.. nevertheless, the above code does compile, run, and print out something that vaguely looks like two people who just sat down to play poker :-)


Previous section: A Game-Plan
Next section: Object inheritance


OOP Table of Contents --- phil@bolthole.com
Part of bolthole.com... Solaris tips ... ksh tutorial ... AWK Programming tutorial