I would like to demonstrate what kind of approach we have established when dealing with problems/programs such as what you need to do for Homework Two. We discussed this before, for example: http://www.cs.indiana.edu/classes/a348/fall2009/lec0922.txt ------------------------------------------(this was the basic addition quiz)---------------- Task: http://www.cs.indiana.edu/classes/a202-dger/fall2009/lab0925.txt ------------------------------------------(this was just the definition and prototype of the war game)------- So we will implement a game of cards I guess let's play with its prototype. Notes, clarifying what we wrote last week: -- initially the deck is 52 cards, shuffled -- deckOne is mine, deckTwo is Jake's, empty in the beginning -- we draw cards in cardOne, cardTwo: these are cards, not decks I start with a sketch of the output: ----------------------------------(sample output)--------- Welcome to the game. The deck is shuffled, take a look: [deck] Dilbert: [deckOne] Ratbert: [deckTwo] Press to start the game. ---------------------------------------------------------- That's a good start, let's work on it some more: ----------------------------------(sample output)--------- [feedback] The deck is shuffled, take a look: [deck] [scoreOne] Dilbert: [handOne] [deckOne] [scoreTwo] Ratbert: [handTwo] [deckTwo] Press to start the game. ---------------------------------------------------------- The [feedback] is "Welcome to the game" originally, later is grading info. My overall template is like this: 1. program starts 2. picks up the phone and presses the right buttons 3. reads the input 4. provides the answer 5. hangs up This is how Apache, the web, http forces you to behave. It's an event driven, connectionless web. But we need to keep the state, so the exact same program should act like this too: 1. retrieve state (and input) 2. if state is empty: initialize state else: update state based on input 3. save state 4. report state 5. get ready for more input You can put these two requirements together as follows: #!/usr/bin/python print "Content-type: text/html\n\n" import cgi data = cgi.FieldStorage() if data.has_key("balance"): balance = data["balance"].value else: balance = "0" balance = str(int(balance) + 1) print """
Your balance is: %s

Press to add one to it.

""" % (balance, balance) Like here: http://www.cs.indiana.edu/classes/a348/fall2009/lec0922.html (See step 1 in the notes above). Or here: http://silo.cs.indiana.edu:8346/ Under: http://silo.cs.indiana.edu:8346/hwTwo.html There's a program: http://silo.cs.indiana.edu:8346/cgi-bin/hwTwo And the source code is posted too: http://silo.cs.indiana.edu:8346/hwTwo.txt This is just like in the quiz of last week. Try the program above at this link: http://silo.cs.indiana.edu:8346/cgi-bin/jlmarsh Now, let's move on. So we start developing ~/apache/cgi-bin/war001 #!/usr/bin/python print "Content-type: text/html\n\n" import cgi data = cgi.FieldStorage() (deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo, feedback) = ("", "", "", "", "", "", "", "") if data.has_key("deck"): deck = data["deck"].value if data.has_key("deckOne"): deckOne = data["deckOne"].value if data.has_key("deckTwo"): deckTwo = data["deckTwo"].value if data.has_key("handOne"): handOne = data["handOne"].value if data.has_key("handTwo"): handTwo = data["handTwo"].value if data.has_key("scoreOne"): scoreOne = data["scoreOne"].value if data.has_key("scoreTwo"): scoreTwo = data["scoreTwo"].value if deck: pass else: feedback = "Welcome to the game." print """
%s

Deck: %s

Dilbert: %s %s %s
Ratbert: %s %s %s

""" % (feedback, deck, scoreOne, handOne, deckOne, scoreTwo, handTwo, deckTwo, deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo) From here on we have to specifically initialize the state, and this is where we would look at the prototype: #!/usr/bin/python print "Content-type: text/html\n\n" import cgi, random data = cgi.FieldStorage() (deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo, feedback) = ("", "", "", "", "", "", "", "") if data.has_key("deck"): deck = data["deck"].value if data.has_key("deckOne"): deckOne = data["deckOne"].value if data.has_key("deckTwo"): deckTwo = data["deckTwo"].value if data.has_key("handOne"): handOne = data["handOne"].value if data.has_key("handTwo"): handTwo = data["handTwo"].value if data.has_key("scoreOne"): scoreOne = data["scoreOne"].value if data.has_key("scoreTwo"): scoreTwo = data["scoreTwo"].value if deck: pass else: feedback = "Welcome to the game." deck = [] for i in range(13): for j in range(4): deck.append(str(i)) random.shuffle(deck) deck = ",".join(deck) print """
%s

Deck: %s

Dilbert: %s %s %s
Ratbert: %s %s %s

""" % (feedback, deck, scoreOne, handOne, deckOne, scoreTwo, handTwo, deckTwo, deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo) Let's finish the initialization and set up the basic processing: #!/usr/bin/python print "Content-type: text/html\n\n" import cgi, random data = cgi.FieldStorage() (deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo, feedback) = ("", "", "", "", "", "", "", "") if data.has_key("deck"): deck = data["deck"].value if data.has_key("deckOne"): deckOne = data["deckOne"].value if data.has_key("deckTwo"): deckTwo = data["deckTwo"].value if data.has_key("handOne"): handOne = data["handOne"].value if data.has_key("handTwo"): handTwo = data["handTwo"].value if data.has_key("scoreOne"): scoreOne = data["scoreOne"].value if data.has_key("scoreTwo"): scoreTwo = data["scoreTwo"].value if deck: deck = deck.split(",") deckOne = deckOne.split(",") deckTwo = deckTwo.split(",") handOne = deck[0] handTwo = deck[1] deckOne[0:0] = [handOne] deckTwo[0:0] = [handTwo] deck = deck[2:] deck = ",".join(deck) deckOne = ",".join(deckOne) deckTwo = ",".join(deckTwo) else: feedback = "Welcome to the game." deck = [] for i in range(13): for j in range(4): deck.append(str(i)) random.shuffle(deck) deck = ",".join(deck) scoreOne = "0" scoreTwo = "0" print """
%s

Deck: %s

Dilbert: %s %s %s
Ratbert: %s %s %s

Press to move on.

""" % (feedback, deck, scoreOne, handOne, deckOne, scoreTwo, handTwo, deckTwo, deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo) Finally, we add the rules of the game: #!/usr/bin/python def sum(los): if len(los) == 0: return 0 else: try: value = int(los[0]) + sum(los[1:]) except: value = sum(los[1:]) return value print "Content-type: text/html\n\n" import cgi, random data = cgi.FieldStorage() (deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo, feedback) = ("", "", "", "", "", "", "", "") if data.has_key("deck"): deck = data["deck"].value if data.has_key("deckOne"): deckOne = data["deckOne"].value if data.has_key("deckTwo"): deckTwo = data["deckTwo"].value if data.has_key("handOne"): handOne = data["handOne"].value if data.has_key("handTwo"): handTwo = data["handTwo"].value if data.has_key("scoreOne"): scoreOne = data["scoreOne"].value if data.has_key("scoreTwo"): scoreTwo = data["scoreTwo"].value if deck: deck = deck.split(",") deckOne = deckOne.split(",") deckTwo = deckTwo.split(",") handOne = deck[0] handTwo = deck[1] if int(handOne) > int(handTwo): deckOne[0:0] = [handOne] deckOne[0:0] = [handTwo] feedback = "(%s, %s) Dilbert's hand" % (handOne, handTwo) elif int(handTwo) > int(handOne): deckTwo[0:0] = [handOne] deckTwo[0:0] = [handTwo] feedback = "(%s, %s) Ratbert's hand" % (handOne, handTwo) else: deckOne[0:0] = [handOne] deckTwo[0:0] = [handTwo] feedback = "(%s, %s) Tie" % (handOne, handTwo) scoreOne = sum(deckOne) scoreTwo = sum(deckTwo) deck = deck[2:] deck = ",".join(deck) deckOne = ",".join(deckOne) deckTwo = ",".join(deckTwo) else: feedback = "Welcome to the game." deck = [] for i in range(13): for j in range(4): deck.append(str(i)) random.shuffle(deck) deck = ",".join(deck) scoreOne = "0" scoreTwo = "0" print """
%s

Deck: %s

Player Current score Hand Player Deck
Dilbert: %s %s %s
Ratbert: %s %s %s

Press to move on.

""" % (feedback, deck, scoreOne, handOne, deckOne, scoreTwo, handTwo, deckTwo, deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo) Find this working at http://silo.cs.indiana.edu:8346/cgi-bin/war004 It's easy to add images now. We also need to detect the end of the game and start a new game.