patternpythonMinor
LPTHW extra credit game
Viewed 0 times
creditgamelpthwextra
Problem
I quite like this extra credit assignment I did for Zed Shaw's Learn Python The Hard Way. Its a neat little text adventure game that uses stats. I would like to eventually expand this, but let me know how the code looks so far!
```
# Legend Of Doobieus
import random
from sys import exit
#rolls 3d6
def roll_3d6():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
list = [a, b, c,]
list.sort()
add = sum(list[0:3])
return add
#stats
def display_stats():
global pow, cun, per
pow = roll_3d6()
print "Power: ", pow
cun = roll_3d6()
print "Cunning: ", cun
per = roll_3d6()
print "Personality: ", per
# Check Random Encounter
def chk_rn():
x = random.randint(1, 6)
if x == 1:
rn_ec1()
# First Encounter (main program really)
def fi_en():
global pow, cun, per
print"""
It smells of damp vegetation, and the air is particularly thick. You can
hear some small animals in the distance. This was a nice place to sleep.
"""
answer = int(raw_input(prompt))
if answer == 1:
cun_one = roll_3d6()
if cun_one "
print "\n\t\tWelcome to LeGeNeD oF DoObIeUs..."
print "\n\t\tProgrammed using Python 2.7 by Ray Weiss"
name = raw_input("\nWhat is your name brave rogue? > ")
print "\nOk %s, lets roll up some character stats." % name
print "\nPress enter to roll 3D6 for each stat."
raw_input()
display_stats()
print "\nThese are your stats. They can be changed by things in game."
print "\nThese stats will have an effect on the outcomes that you choose."
print "\nYou will be presented with many choices, type in a number."
print "\nOr risk typing something different."
print "\nPress enter to start the game"
raw_input()
print"""You are %s, you are but a w
```
# Legend Of Doobieus
import random
from sys import exit
#rolls 3d6
def roll_3d6():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
list = [a, b, c,]
list.sort()
add = sum(list[0:3])
return add
#stats
def display_stats():
global pow, cun, per
pow = roll_3d6()
print "Power: ", pow
cun = roll_3d6()
print "Cunning: ", cun
per = roll_3d6()
print "Personality: ", per
# Check Random Encounter
def chk_rn():
x = random.randint(1, 6)
if x == 1:
rn_ec1()
# First Encounter (main program really)
def fi_en():
global pow, cun, per
print"""
It smells of damp vegetation, and the air is particularly thick. You can
hear some small animals in the distance. This was a nice place to sleep.
- Stay close, find some cover, and wait for food to show up.
- Explore the nearby marsh & find the nearest river, following it downstream.
- Walk towards the large mysterious mountain in the distance.
"""
answer = int(raw_input(prompt))
if answer == 1:
cun_one = roll_3d6()
if cun_one "
print "\n\t\tWelcome to LeGeNeD oF DoObIeUs..."
print "\n\t\tProgrammed using Python 2.7 by Ray Weiss"
name = raw_input("\nWhat is your name brave rogue? > ")
print "\nOk %s, lets roll up some character stats." % name
print "\nPress enter to roll 3D6 for each stat."
raw_input()
display_stats()
print "\nThese are your stats. They can be changed by things in game."
print "\nThese stats will have an effect on the outcomes that you choose."
print "\nYou will be presented with many choices, type in a number."
print "\nOr risk typing something different."
print "\nPress enter to start the game"
raw_input()
print"""You are %s, you are but a w
Solution
Glenn Rogers' answer is well-aimed at your level of expertise, so you should pay attention to his advice rather than mine, but I think you might be interested to peep ahead to see a more sophisticated approach to this kind of application.
In your program (a multiple-choice adventure game), the set of operations is highly stereotyped. There are a small number of common operations:
and so on. Coding up each of these operations every time makes your code repetitious, hard to follow, and hard to change (because the rules for, say, testing a statistic are distributed all over the code).
In this kind of situation, one way to make the code easier to read and follow is to use a domain-specific language to separate the implementation of the operations from their expression. (You'll sometimes see the phrase "language-oriented programming" used for this approach.)
Here's an example implementation of a domain-specific language for this kind of application:
`GWYDIONS_CASTLE = '''
PAGE 1
It is only the autumn night that makes you shiver, you tell yourself, but you
know better.
The castle stands on the hill in the gathering dusk. Wrapped in your grey cloak
against the cold, you crouch on the edge of the counterscarp. Eyes peer down
from the battlements, but you need not fear discovery: they are only the empty
sockets in the skulls of the adventurers who have come before you to harrow the
fortress of the wizard Gwydion.
Soon it will be dark enough to make your move.
You have boots.
You have a cloak.
You have a dagger.
To scale the wall, turn to page 27.
To swim the moat, turn to page 24.
PAGE 2
The dagger makes no impression on the chain. "Ah, you wish to free me," says
the monk. "You have a good heart, but no blade forged of iron can cut a chain
forged with magic."
Suddenly he stands up. "Only Gwidion's death can undo his magic. But if you
wish to defeat him, take this." He selects a tattered scroll from the shelf
above the desk and hands it to you.
Defeat Gwydion? You only meant to rob him. But you nod and take the scroll.
You have a tattered scroll.
To leave the monk and follow the stone passageway, turn to page 13.
PAGE 3
You are in a long stone passageway.
To go through the doorway on the right, turn to page 33.
To follow the passageway, turn to page 13.
PAGE 4
You push the door. It opens noiselessly on well-oiled hinges and you peer
through the gap. What you see inside takes your breath away. Golden coins in
heaps! Tapestries in silk and damask! Kingly crowns studded with polished
stones. This is what you came for.
To fill your pockets and make a run for it, turn to page 34.
To try the other door instead, turn to page 10.
PAGE 5
To tag onto the back of the troop, turn to page 6.
To stay hidden until they pass, and then try your key in the door of the keep,
turn to page 40.
PAGE 6
You follow the troop, imitating their shambolic walk as best you can. No one
seems to notice your presence. The door to the keep swings open, squealing on
its rusty hinges like a stuck pig, and you pass through one by one. The massive
door swings shut behind you.
If you have a cloak, turn to page 36.
Test your luck. If you succeed, turn to page 23.
Otherwise, turn to page 21.
PAGE 7
The courtyard is quiet in the moonlight, but you remain on your guard. Who
knows what traps Gwydion has set for unwary intruders?
To approach the keep, turn to page 41.
PAGE 8
Your boots echo loudly on the tiles. Too loud. You freeze, but the sound of
footsteps does not stop. You turn to see an armoured knight step emerging from
an alcove. Its visor is down, but somehow you doubt that there is a face behind
it.
suit of armour (the, its) SKILL 10 STAMINA 10
If you win, turn to page 30.
PAGE 9
It looks as if the goblin dropped a key in the struggle. You pick it up.
You gained a key.
To leave the scullery by the door, turn to page 7.
PAGE 10
You push the door. It opens noiselessly on well-oiled hinges and you peer
through the gap. Inside is a study lined with tapestries, and at a desk sits a
tall white-haired man, bent in concentration over a grimoire. It is the wizard
Gwydion. His staff rests at his side.
If you have a tattered scroll, turn to page 47.
To attack Gwydion, turn to page 45.
To try the other door instead, turn to page 4.
PAGE 11
You leap through the window.
Test your luck. If you succeed, turn to page 12.
Otherwise, turn to page 32.
PAGE 12
It is a long fall, but the moat is deep. You pull yourself out and slink away
empty-handed into the night. Is that distant laughter you can hear? No
matter. You will be back.
PAGE 13
The dark passageway enters a large hall lit by a candelabra. Suits of armour
stand silently in alcoves. The floor is tiled in a black and white checkerboard
pattern, and at the far side a wide stairc
In your program (a multiple-choice adventure game), the set of operations is highly stereotyped. There are a small number of common operations:
- printing a paragraph of text;
- presenting a numbered sequence of choices and getting the player to choose one;
- rolling dice and comparing the total to a statistic;
and so on. Coding up each of these operations every time makes your code repetitious, hard to follow, and hard to change (because the rules for, say, testing a statistic are distributed all over the code).
In this kind of situation, one way to make the code easier to read and follow is to use a domain-specific language to separate the implementation of the operations from their expression. (You'll sometimes see the phrase "language-oriented programming" used for this approach.)
Here's an example implementation of a domain-specific language for this kind of application:
`GWYDIONS_CASTLE = '''
PAGE 1
It is only the autumn night that makes you shiver, you tell yourself, but you
know better.
The castle stands on the hill in the gathering dusk. Wrapped in your grey cloak
against the cold, you crouch on the edge of the counterscarp. Eyes peer down
from the battlements, but you need not fear discovery: they are only the empty
sockets in the skulls of the adventurers who have come before you to harrow the
fortress of the wizard Gwydion.
Soon it will be dark enough to make your move.
You have boots.
You have a cloak.
You have a dagger.
To scale the wall, turn to page 27.
To swim the moat, turn to page 24.
PAGE 2
The dagger makes no impression on the chain. "Ah, you wish to free me," says
the monk. "You have a good heart, but no blade forged of iron can cut a chain
forged with magic."
Suddenly he stands up. "Only Gwidion's death can undo his magic. But if you
wish to defeat him, take this." He selects a tattered scroll from the shelf
above the desk and hands it to you.
Defeat Gwydion? You only meant to rob him. But you nod and take the scroll.
You have a tattered scroll.
To leave the monk and follow the stone passageway, turn to page 13.
PAGE 3
You are in a long stone passageway.
To go through the doorway on the right, turn to page 33.
To follow the passageway, turn to page 13.
PAGE 4
You push the door. It opens noiselessly on well-oiled hinges and you peer
through the gap. What you see inside takes your breath away. Golden coins in
heaps! Tapestries in silk and damask! Kingly crowns studded with polished
stones. This is what you came for.
To fill your pockets and make a run for it, turn to page 34.
To try the other door instead, turn to page 10.
PAGE 5
To tag onto the back of the troop, turn to page 6.
To stay hidden until they pass, and then try your key in the door of the keep,
turn to page 40.
PAGE 6
You follow the troop, imitating their shambolic walk as best you can. No one
seems to notice your presence. The door to the keep swings open, squealing on
its rusty hinges like a stuck pig, and you pass through one by one. The massive
door swings shut behind you.
If you have a cloak, turn to page 36.
Test your luck. If you succeed, turn to page 23.
Otherwise, turn to page 21.
PAGE 7
The courtyard is quiet in the moonlight, but you remain on your guard. Who
knows what traps Gwydion has set for unwary intruders?
To approach the keep, turn to page 41.
PAGE 8
Your boots echo loudly on the tiles. Too loud. You freeze, but the sound of
footsteps does not stop. You turn to see an armoured knight step emerging from
an alcove. Its visor is down, but somehow you doubt that there is a face behind
it.
suit of armour (the, its) SKILL 10 STAMINA 10
If you win, turn to page 30.
PAGE 9
It looks as if the goblin dropped a key in the struggle. You pick it up.
You gained a key.
To leave the scullery by the door, turn to page 7.
PAGE 10
You push the door. It opens noiselessly on well-oiled hinges and you peer
through the gap. Inside is a study lined with tapestries, and at a desk sits a
tall white-haired man, bent in concentration over a grimoire. It is the wizard
Gwydion. His staff rests at his side.
If you have a tattered scroll, turn to page 47.
To attack Gwydion, turn to page 45.
To try the other door instead, turn to page 4.
PAGE 11
You leap through the window.
Test your luck. If you succeed, turn to page 12.
Otherwise, turn to page 32.
PAGE 12
It is a long fall, but the moat is deep. You pull yourself out and slink away
empty-handed into the night. Is that distant laughter you can hear? No
matter. You will be back.
PAGE 13
The dark passageway enters a large hall lit by a candelabra. Suits of armour
stand silently in alcoves. The floor is tiled in a black and white checkerboard
pattern, and at the far side a wide stairc
Context
StackExchange Code Review Q#16126, answer score: 6
Revisions (0)
No revisions yet.