diff --git a/examples/history.txt b/examples/history.txt index aa178b017c..f2109fcd47 100644 --- a/examples/history.txt +++ b/examples/history.txt @@ -1 +1 @@ -3,3,0 \ No newline at end of file +7,5,3 \ No newline at end of file diff --git a/examples/rock_paper_scissors.py b/examples/rock_paper_scissors.py index 4a1899f900..e05b41caf9 100644 --- a/examples/rock_paper_scissors.py +++ b/examples/rock_paper_scissors.py @@ -1,35 +1,35 @@ -#import module we need +# import module we need import random -#file i/o functions for historical results -def load_results(): - text_file = open("history.txt", "r") - history = text_file.read().split(",") - text_file.close() - return history +# file i/o functions for historical results +# def load_results(): +# text_file = open("history.txt", "r") +# history = text_file.read().split(",") +# text_file.close() +# return history -def save_results( w, t, l): - text_file = open("history.txt", "w") - text_file.write( str(w) + "," + str(t) + "," + str(l)) - text_file.close() +# def save_results( w, t, l): +# text_file = open("history.txt", "w") +# text_file.write( str(w) + "," + str(t) + "," + str(l)) +# text_file.close() -#welcome message +# welcome message results = load_results() wins = int(results[0]) -ties = int( results[1]) +ties = int(results[1]) losses = int(results[2]) print("Welcome to Rock, Paper, Scissors!") print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses)) print("Please choose to continue...") -#initialize user, computer choices -computer = random.randint(1,3) +# initialize user, computer choices +computer = random.randint(1, 3) user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n")) -#gamplay loop +# gamplay loop while not user == 9: - #user chooses ROCK + # user chooses ROCK if user == 1: if computer == 1: print("Computer chose rock...tie!") @@ -41,7 +41,7 @@ def save_results( w, t, l): print("Computer chose scissors...you wins :)") wins += 1 - #user chooses PAPER + # user chooses PAPER elif user == 2: if computer == 1: print("Computer chose rock...you win :)") @@ -52,8 +52,8 @@ def save_results( w, t, l): else: print("Computer chose scissors...computer wins :(") losses += 1 - - #user chooses SCISSORS + + # user chooses SCISSORS elif user == 3: if computer == 1: print("Computer chose rock...computer wins :(") @@ -66,14 +66,14 @@ def save_results( w, t, l): ties += 1 else: print("Invalid selection. Please try again.") - #print updated stats + # print updated stats print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses)) - #prompt user to make another selection + # prompt user to make another selection print("Please choose to continue...") - #initialize user, computer choices - computer = random.randint(1,3) + # initialize user, computer choices + computer = random.randint(1, 3) user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n")) # #game over, save results -save_results(wins, ties, losses) \ No newline at end of file +save_results(wins, ties, losses) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..d417975b42 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,51 +1,31 @@ from room import Room +from player import Player +from room_info import room_info -# Declare all the rooms +room_map = room_info() -room = { - 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), +player = Player("Harry Potter", room_map["outside"]) - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), - 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling -into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +while True: + print(player.room.name) - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), + user_input = input( + "Please choose from the following to play: 1) move in directions n,s,e,w 2) p for pickup 3) q for quit ") - 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure -chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), -} + if user_input.lower() in ['n', 's', 'e', 'w']: + if player.room.connections is not None: + player.move(user_input) + print("You are now in ", player.room.name) + print("Items in this room are: ", player.room.items) + else: + print("You have reached a dead end") + elif user_input.lower() == 'p': + item_check = input("what item will you choose? ") + player.pickup(item_check) + elif user_input.lower() == 'q': + print("Thank you for playing!") + break - -# Link rooms together - -room['outside'].n_to = room['foyer'] -room['foyer'].s_to = room['outside'] -room['foyer'].n_to = room['overlook'] -room['foyer'].e_to = room['narrow'] -room['overlook'].s_to = room['foyer'] -room['narrow'].w_to = room['foyer'] -room['narrow'].n_to = room['treasure'] -room['treasure'].s_to = room['narrow'] - -# -# Main -# - -# Make a new player object that is currently in the 'outside' room. - -# Write a loop that: -# -# * Prints the current room name -# * Prints the current description (the textwrap module might be useful here). -# * Waits for user input and decides what to do. -# -# If the user enters a cardinal direction, attempt to move to the room there. -# Print an error message if the movement isn't allowed. -# -# If the user enters "q", quit the game. + else: + print("Do not understand your input. Please enter a valid command or press q to quit. ") diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..cbd098809f --- /dev/null +++ b/src/item.py @@ -0,0 +1,6 @@ + + +class Item: + def __init__(self, name, description): + self.name = name + self.description = description diff --git a/src/logo.py b/src/logo.py new file mode 100644 index 0000000000..0d1829b30f --- /dev/null +++ b/src/logo.py @@ -0,0 +1,51 @@ +import turtle +import random +myPen = turtle.Turtle() +myPen.shape("turtle") +myPen.speed(10) + +window = turtle.Screen() +window.bgcolor("#00B2C0") + + +def corner(turtle): + for x in range(0, 6): + turtle.left(15) + turtle.forward(2) + + +def cornered_box(turtle, x1, y1, x2, y2, letter): + turtle.penup() + turtle.goto(x1, y1) + turtle.pendown() + turtle.color("black") + turtle.fillcolor("black") + turtle.begin_fill() + for x in range(0, 4): + turtle.forward(40) + corner(turtle) + turtle.end_fill() + turtle.penup() + turtle.goto(x2, y2) + turtle.color("white") + turtle.write(letter, None, None, "28pt bold") + + +cornered_box(myPen, -40, 20, -35, 35, "C") +cornered_box(myPen, 25, 20, 30, 35, "O") +cornered_box(myPen, -40, -45, -35, -30, "D") +cornered_box(myPen, 25, -45, 30, -30, "E") + +myPen.color("white") +myPen.goto(-115, -92) +myPen.write("Anyone Can Learn", None, None, "24pt bold") +myPen.goto(-115, -127) +myPen.write("Anyone Can Teach", None, None, "24pt bold") +myPen.left(90) + + +myPen.goto(10, 105) + +for x in range(0, 100): + myPen.color("#%06x" % random.randint(0, 2**24 - 1)) +myPen.color("white") diff --git a/src/player.py b/src/player.py index d79a175029..470093a116 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,30 @@ # Write a class to hold player information, e.g. what room they are in # currently. +from room import Room + + +class Player(): + def __init__(self, name, room, inventory=[]): + self.name = name + self.room = room + self.inventory = inventory + + def __str__(self): + return "You are currently in: " + self.room + + def move(self, direction): + if self.room.connections[direction] is not None: + self.room = self.room.connections[direction] + else: + print("You can not go this way. Choose another direction.") + + def pickup(self, item): + if item in self.room.items: + self.inventory.append(item) + print(f"You have obtained {item.name}") + else: + print("The item is not in the room") + + def print_inventory(self): + for item in self.inventory: + print(item.name + item.description) diff --git a/src/room.py b/src/room.py index 24c07ad4c8..75276b9eaf 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,12 @@ -# Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +class Room(): + def __init__(self, name, description, n_to=None, e_to=None, s_to=None, w_to=None, items=[]): + self.name = name + self.description = description + self.connections = { + 'n': n_to, + 's': s_to, + 'e': e_to, + 'w': w_to + } + + self.items = items diff --git a/src/room_info.py b/src/room_info.py new file mode 100644 index 0000000000..9b637c807e --- /dev/null +++ b/src/room_info.py @@ -0,0 +1,36 @@ +from room import Room + + +def room_info(): + room_map = { + 'outside': Room("Outside Cave Entrance", + "North of you, the cave mount beckons"), + + 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty +passages run north and east."""), + + 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling +into the darkness. Ahead to the north, a light flickers in +the distance, but there is no way across the chasm."""), + + 'narrow': Room("Narrow Passage", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure +chamber! Sadly, it has already been completely emptied by +earlier adventurers. The only exit is to the south."""), + } + + +# Link rooms together + + room_map['outside'].connections["n"] = room_map['foyer'] + room_map['foyer'].connections["s"] = room_map['outside'] + room_map['foyer'].connections["n"] = room_map['overlook'] + room_map['foyer'].connections["e"] = room_map['narrow'] + room_map['overlook'].connections["s"] = room_map['foyer'] + room_map['narrow'].connections["w"] = room_map['foyer'] + room_map['narrow'].connections["n"] = room_map['treasure'] + room_map['treasure'].connections["s"] = room_map['narrow'] + + return room_map