A TextBased UI
In designing PokerApp we have also developed a specification for a generic Pokerlnterface class. Our interface must support the methods for displaying information: setMoney, setDice, and showResult. It must also have methods that allow for input from the user: wantToPlay, and chooseDice. These methods can be implemented in many different ways, producing programs that look quite different even though the underlying model, PokerApp, remains the same.
Usually, graphical interfaces are much more complicated to design and build than text-based ones. If we are in a hurry to get our application running, we might first try building a simple text-based interface. We can use this for testing and debugging of the model without all the extra complication of a full-blown GUI.
First, let's tweak our PokerApp class a bit so that the user interface is supplied as a parameter to the constructor.
class PokerApp:
def _init_(self, interface):
self.dice = Dice() self.money = 100 self.interface = interface
Then we can easily create versions of the poker program using different interfaces.
Now let's consider a bare-bones interface to test out the poker program. Our text-based version will not present a finished application, but rather, give us a minimalist interface solely to get the program running. Each of the necessary methods can be given a trivial implementation. Here is a complete Textlnterface class using this approach:
# file: textpoker.py class Textlnterface:
print "Welcome to video poker."
def setMoney(self, amt):
def setDice(self, values): print "Dice:", values def wantToPlay(self):
ans = raw_input("Do you wish to try your luck? ") return ans[0] in "yY"
def close(self):
print "\nThanks for playing!"
def showResult(self, msg, score):
def chooseDice(self):
return input("Enter list of which to change ([] to stop) ")
Using this interface, we can test out our PokerApp program to see if we have implemented a correct model. Here is a complete program making use of the modules that we have developed:
# textpoker.py -- video dice poker using a text-based interface.
from pokerapp import PokerApp from textinter import TextInterface inter = TextInterface() app = PokerApp(inter) app.run()
Basically, all this program does is create a text-based interface and then build a PokerApp using this interface and start it running.
Running this program, we get a rough but useable interaction.
Welcome to video poker. Do you wish to try your luck? y You currently have $90. Dice: [6, 4, 4, 2, 4] Enter list of which to change ([] Dice: [1, 4, 4, 2, 2] Enter list of which to change ([] Dice: [2, 4, 4, 2, 2] Full House. You win $12. You currently have $102. Do you wish to try your luck? y You currently have $92. Dice: [5, 6, 4, 4, 5] Enter list of which to change ([] Dice: [5, 5, 4, 4, 5] Enter list of which to change ([] Full House. You win $12. You currently have $104. Do you wish to try your luck? y You currently have $94.
Enter list of which to change
Enter list of which to change
You currently have $109.
Do you wish to try your luck?
to stop) to stop)
Thanks for playing!
You can see how this interface provides just enough so that we can test out the model. In fact, we've got a game that's already quite a bit of fun to play!
Post a comment