repl.it
@ericqweinstein/

CaveCommander

Python

A little text-based adventure game based on Colossal Cave Adventure.

fork
loading
Files
  • main.py
  • cave.py
  • constants.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
    A little clone of Colossal Cave Adventure, written in Python.
    @see https://en.wikipedia.org/wiki/Colossal_Cave_Adventure
'''

__author__ = 'Eric Weinstein'


import re

from constants import Directions, Commands
from cave import Map

class Game(object):
    def __init__(self):
        self.map = Map
        self.location = 'start'
        self.inventory = []

    def take_item(self, item):
        if item == 'bird':
            if 'cage' not in self.inventory:
                print('With what? Your bare hands? You should probably use a cage or something...')
                return
            if 'rod' in self.inventory:
                print('The bird is frightened and impossible to catch.')
                return
        try:
            self.map[self.location]['items'].remove(item)
            self.inventory.append(item)
            print('You picked up a %s.' % item)
        except ValueError:
            print('There\'s no %s for you to take.' % item)

    def follow(self, instruction):
        if re.search(Directions.NORTH, instruction):
            try:
                self.location = self.map[self.location]['exits']['North']
                print(self.map[self.location]['description'])
            except KeyError:
                print('You can\'t go that way.')
        elif re.search(Directions.SOUTH, instruction):
            try:
                self.location = self.map[self.location]['exits']['South']
                print(self.map[self.location]['description'])
            except KeyError:
                print('You can\'t go that way.')
        elif re.search(Directions.EAST, instruction):
            try:
                self.location = self.map[self.location]['exits']['East']
                print(self.map[self.location]['description'])
            except KeyError:
                print('You can\'t go that way.')
        elif re.search(Directions.WEST, instruction):
            try:
                self.location = self.map[self.location]['exits']['West']
                print(self.map[self.location]['description'])
            except KeyError:
                print('You can\'t go that way.')
        elif re.search(Commands.TAKE, instruction):
            match = re.search(Commands.TAKE, instruction).group(1).strip()
            self.take_item(match)
        elif re.search(Commands.DROP, instruction):
            match = re.search(Commands.DROP, instruction).group(1).strip()

            try:
                self.inventory.remove(match)
                self.map[self.location]['items'].append(match)
            except ValueError:
                print('You don\'t have a %s to drop.' % match)
        elif re.search(Commands.INVENTORY, instruction):
            [print('You\'ve got a %s.' % item) for item in self.inventory] if self.inventory else print('You\'re not carrying anything.')
        elif re.search(Commands.HELP, instruction):
            print('Available directions are:')
            [print('  %s' % direction) for direction in self.map[self.location]['exits']]
        else:
            print('I don\'t understand which way you want to go. ' \
                  'Please try NORTH, SOUTH, EAST, or WEST.')

        items = self.map[self.location]['items']

        if items:
            [print('There\'s a %s here.' % item) for item in items]

        if not self.map[self.location]['exits']:
            print('You win!')

    def start(self):
        print(self.map[self.location]['description'])

        while True:
            instruction = input('> ')

            try:
                self.follow(instruction)
            except RuntimeError as err:
                print(err)


if __name__ == '__main__':
    game = Game()
    game.start()