Saturday, September 17, 2011

The Game of Video Poker

Prerequisites
This is a continuation of previous works.
You would need to read "Console Poker" first.

Quick Start
If you just want to play the game, go to the Source Code section below.


Game Flow
There would be a deal button in the screen. Once it is pressed, five cards would be dealt to you.

If the initial deal forms a winning pattern, you would hold all the cards and press the deal button again. Otherwise, you may hold any number of cards, hoping that the second deal would improve the chance of winning.

The cards are actually buttons and therefore are clickable objects. Clicking on them would change the hold status.

Image Buttons
We can apply a image to the JButton component. But first of all, we must have the card image on hand.
Drawing a playing card is not difficult, and this is already demonstrated in a previous article. For details, read :

We will reuse the file CardPanel.java in the above article. Actually, all we need is the createImage() method, which would be called during initialization in VideoPoker.java

Note that VideoPoker.java will be the only source file created in this article, all other source files come from previous works. This is one of the important software design principle :

Don't reinvent the wheel

Poker Patterns
The table of winning patterns and payouts are shown in the previous article "Console Poker" The game flow logic are inside the source file ConsolePoker.java in the above article. This file will be reused here.


Design
Ideally, the GUI should be separated from the game logic, this is not 100% true in this sample, but it is getting close.

  • ConsolePoker.java - game flow logic
  • VideoPoker.java - GUI components
Auxiliary files :
  • Poker.java - the pattern matcher
  • Pattern.java - define pattern constants
  • CardPanel.java - create card images

Source Code
You will need all the five java files in the same folder.

  • VideoPoker.java - GUI components
  • ConsolePoker.java - game flow logic
  • Poker.java - the pattern matcher
  • Pattern.java - define pattern constants
  • CardPanel.java - create card images

To compile :
javac *.java
To run :
javac VideoPoker
Only VideoPoker.java is new in this article, all other source files are from previous works. Anyway, all of them will be listed here for your convenience.



ConsolePoker.java
Originally defined in : Console Poker




Poker.java
Originally defined in : Console Poker




Pattern.java
Originally defined in : Console Poker




CardPanel.java
Originally defined in : How to display a deck of playing card in a panel ?


1 comment:

  1. Hi Ivan,
    Can u pls provide the answer to the following ques:

    In a university, students can enroll in different courses. A student may enroll for more than one course. Both students and courses can be identified by IDs given to them. Design a data structure to store students, courses, and the student-course relationships. You can use arrays, lists, stacks, trees, graphs, etc. or come up with your own data structures. Give the running times, in Big O notation, for the following operations for your data structure and justify the answers: a) Return all students in a list. b) Return all courses in a list. c) Return all courses in a list for a given student. d) Return all students in a list for a given course.

    Thanks in advance!

    ReplyDelete