Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #72 from introprogramming/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ViddeM authored Jul 1, 2019
2 parents 18d3911 + c15b0bb commit 8e52f3f
Show file tree
Hide file tree
Showing 62 changed files with 2,802 additions and 2,570 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

#Compiled python
*.pyc

#Pycharm idea folder
.idea/
40 changes: 20 additions & 20 deletions exercises/adventure/adventure.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sys
from locations import *
from location import *
from switch import *

current_location = 'haunted forest'
here = locations[current_location]


def move(direction):
global here, current_location
new_loc = here.get_neighbor(direction)
Expand All @@ -14,57 +13,58 @@ def move(direction):
current_location = new_loc
here = locations[current_location]
else:
print "No route leading " + direction + ".\n"
print("No route leading " + direction + ".\n")

look()


def look(direction=""):
if (direction):
target = here.get_neighbor(direction)
if (target):
view = locations[target].short_view()
else:
view = "There's nothing there."
print view
print(view)
else:
print here.name
print here.long_view()
print ""
print(here.name)
print(here.long_view())
print("")
here.print_exits()


def perform_action(input):
here.do_something(input)



def await_action():
input = raw_input("> ").split()
user_input = input("> ").split()

for case in switch(input[0]):
for case in switch(user_input[0]):
if case('quit'):
print "Bye!"
print("Bye!")
return

if case('go','move'):
if len(input) > 1:
move(input[1])
if case('go', 'move'):
if len(user_input) > 1:
move(user_input[1])
else:
print "Where to?"
print("Where to?")
break

if case('look'):
if len(input) > 1:
look(input[1])
if len(user_input) > 1:
look(user_input[1])
else:
look()
break

# We have a custom action
perform_action(input)
perform_action(user_input)

await_action()


"""
This starts the adventure game loop.
"""
Expand Down
31 changes: 14 additions & 17 deletions exercises/adventure/location.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
from switch import *

class LocationState:

class LocationState:
short_desc = ""
long_desc = ""
long_desc = ""

directions = {}

actions = {}

def __init__(self, short, long, dirs, acts):
self.short_desc = short
self.long_desc = long
self.long_desc = long
self.directions = dirs
self.actions = acts
self.actions = acts

class Location:

class Location:
name = "Alien Mothership"
states = []
current_state_ix = 0
current_state_ix = 0
current_state = ""

# Args should be a non-empty set of LocationStates
Expand All @@ -41,32 +41,29 @@ def get_neighbor(self, direction):
else:
return ""


def print_exits(self):
possible_dirs = self.current_state.directions.keys()

for case in switch(len(possible_dirs)):
if case(0):
print "All dressed up and nowhere to go!"
print("All dressed up and nowhere to go!")
break
if case(1):
print "There's an exit " + possible_dirs[0]
print("There's an exit " + possible_dirs[0])
break
else:
print "There are exits ",
print possible_dirs[0],
print("There are exits "),
print(possible_dirs[0]),
for ex in possible_dirs[1:]:
print ", " + ex,
print "."


print(", " + ex),
print(".")

def do_something(self, input):
possible_actions = self.current_state.actions
if (input[0] in possible_actions):
possible_actions[input[0]](input[1:])
else:
print "Interesting."
print("Interesting.")

def set_state(self, new_state):
self.current_state_ix = new_state
Expand Down
87 changes: 46 additions & 41 deletions exercises/adventure/locations.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
import sys

from location import *
from switch import *

"""
The haunted forest
"""


def __haunted_forest_pick(input):
if input:
for case in switch(input[0]):
if case('mushroom', 'mushrooms'):
if haunted_forest.current_state_ix == 0:
print "Mmm... Mushrooms!\n"
print("Mmm... Mushrooms!\n")
haunted_forest.set_state(1)
else:
print "Where have all the mushrooms gone? Young girls picked them everyone!"
print("Where have all the mushrooms gone? Young girls picked them everyone!")
break
if case('tree','trees'):
print "You're not that strong, silly!"
if case('tree', 'trees'):
print("You're not that strong, silly!")
break
if case('stuff'):
print "Some stuff stuffed away."
print("Some stuff stuffed away.")
break
else:
print "Ain't no " + input[0] + " around here"
print("Ain't no " + input[0] + " around here")
else:
print "Pick-pickety-pick... pickaxe?"
print("Pick-pickety-pick... pickaxe?")


def __haunted_forest_sing(input):
if input:
for case in switch(input[0]):
if case('halelujah'):
print "The world has become a brighter place!"
print("The world has become a brighter place!")
haunted_forest.set_state(2)
break
else:
print "*sings* " + input[1]
print("*sings* " + input[1])
else:
print "I'm siiiiiiingin' in the rain!"

print("I'm siiiiiiingin' in the rain!")

haunted_forest = Location('The Haunted Forest',
LocationState(
"A dark and dreary place.",
"A scary forest full of trees and mushrooms and stuff.",
{},
{'pick': __haunted_forest_pick}
),
LocationState(
"A dark and dreary place.",
"A scary forest full of trees but no mushrooms.",
{},
{'pick': __haunted_forest_pick,
'sing': __haunted_forest_sing}
),
LocationState(
"A dark and dreary place.",
"A scary forest full of trees but no mushrooms.",
{'west':'shining plains'},
{'pick': __haunted_forest_pick,
'sing': __haunted_forest_sing}
)
)

haunted_forest = Location('The Haunted Forest',
LocationState(
"A dark and dreary place.",
"A scary forest full of trees and mushrooms and stuff.",
{},
{'pick': __haunted_forest_pick}
),
LocationState(
"A dark and dreary place.",
"A scary forest full of trees but no mushrooms.",
{},
{'pick': __haunted_forest_pick,
'sing': __haunted_forest_sing}
),
LocationState(
"A dark and dreary place.",
"A scary forest full of trees but no mushrooms.",
{'west': 'shining plains'},
{'pick': __haunted_forest_pick,
'sing': __haunted_forest_sing}
)
)

"""
The Shining Plains.
"""


def __shining_plains_win(input):
print "Awesome! You have won the game!"
print("Awesome! You have won the game!")
sys.exit()


shining_plains = Location('The Shining Plains',
LocationState(
"A bright and beautiful place.",
"A really bright and beautiful plains with daffodils and dandelions.",
{'east':'haunted forest'},
{'win': __shining_plains_win}
))
LocationState(
"A bright and beautiful place.",
"A really bright and beautiful plains with daffodils and dandelions.",
{'east': 'haunted forest'},
{'win': __shining_plains_win}
))

#####################################################3

Expand All @@ -85,4 +91,3 @@ def __shining_plains_win(input):
'haunted forest': haunted_forest,
'shining plains': shining_plains
}

2 changes: 1 addition & 1 deletion exercises/adventure/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration

def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
Expand Down
8 changes: 4 additions & 4 deletions exercises/battleship/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ I denna uppgiften kommer ni att självständigt skapa en implementation av *Batt

Följande skall finnas i spelet:

1. Spelet skall börja med att båda spelarna plaserar sina skepp på sin sida av spelplanen utan att motståndaren kan se skeppen.
1. Spelet skall börja med att båda spelarna placerar sina skepp på sin sida av spelplanen utan att motståndaren kan se skeppen.

2. Ett skepp skall kunna roteras under plaseringssekvensen.
2. Ett skepp skall kunna roteras under placeringssekvensen.

3. När spelaren är klar kan inte dennes skepp flyttas.

4. När båda spelarna har plaserat sina skepp börjar stridsläget.

5. Varje spelare tar tur om att försöka skjuta på motståndarens skepp, vare sig man träffar en skeppsdel eller inte skall detta markeras, givetvis med olika markeringar. Man kan alltid se sina tidigare markeringar under sin egen tur, men inte under motståndarens.
5. Varje spelare turas om att försöka skjuta på motståndarens skepp, vare sig man träffar en skeppsdel eller inte skall detta markeras, givetvis med olika markeringar. Man kan alltid se sina tidigare markeringar under sin egen tur, men inte under motståndarens.

6. När alla en spelares skeppsdelar har sänkts vinner motståndaren och spelet avslutas, alternativt börjar om.
6. När alla av någon spelares skepsdelar har sänkts vinner motståndaren och spelet avslutas, alternativt börjar om.

7. På sin egen runda kan man se sina egna skepp, som då är markerade på de delar som motståndaren träffat, men
motståndarens missade skott syns inte.
Expand Down
Loading

0 comments on commit 8e52f3f

Please sign in to comment.