@ericqweinstein/

CaveCommander

Python

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

fork
loading
Files
  • main.py
  • cave.py
  • constants.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6659349040167213","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"JycnCiAgICBBIGxpdHRsZSBjbG9uZSBvZiBDb2xvc3NhbCBDYXZlIEFkdmVudHVyZSwgd3JpdHRlbiBpbiBQeXRob24uCiAgICBAc2VlIGh0dHBzOi8vZW4ud2lraXBlZGlhLm9yZy93aWtpL0NvbG9zc2FsX0NhdmVfQWR2ZW50dXJlCicnJwoKX19hdXRob3JfXyA9ICdFcmljIFdlaW5zdGVpbicKCgppbXBvcnQgcmUKCmZyb20gY29uc3RhbnRzIGltcG9ydCBEaXJlY3Rpb25zLCBDb21tYW5kcwpmcm9tIGNhdmUgaW1wb3J0IE1hcAoKY2xhc3MgR2FtZShvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYubWFwID0gTWFwCiAgICAgICAgc2VsZi5sb2NhdGlvbiA9ICdzdGFydCcKICAgICAgICBzZWxmLmludmVudG9yeSA9IFtdCgogICAgZGVmIHRha2VfaXRlbShzZWxmLCBpdGVtKToKICAgICAgICBpZiBpdGVtID09ICdiaXJkJzoKICAgICAgICAgICAgaWYgJ2NhZ2UnIG5vdCBpbiBzZWxmLmludmVudG9yeToKICAgICAgICAgICAgICAgIHByaW50KCdXaXRoIHdoYXQ/IFlvdXIgYmFyZSBoYW5kcz8gWW91IHNob3VsZCBwcm9iYWJseSB1c2UgYSBjYWdlIG9yIHNvbWV0aGluZy4uLicpCiAgICAgICAgICAgICAgICByZXR1cm4KICAgICAgICAgICAgaWYgJ3JvZCcgaW4gc2VsZi5pbnZlbnRvcnk6CiAgICAgICAgICAgICAgICBwcmludCgnVGhlIGJpcmQgaXMgZnJpZ2h0ZW5lZCBhbmQgaW1wb3NzaWJsZSB0byBjYXRjaC4nKQogICAgICAgICAgICAgICAgcmV0dXJuCiAgICAgICAgdHJ5OgogICAgICAgICAgICBzZWxmLm1hcFtzZWxmLmxvY2F0aW9uXVsnaXRlbXMnXS5yZW1vdmUoaXRlbSkKICAgICAgICAgICAgc2VsZi5pbnZlbnRvcnkuYXBwZW5kKGl0ZW0pCiAgICAgICAgICAgIHByaW50KCdZb3UgcGlja2VkIHVwIGEgJXMuJyAlIGl0ZW0pCiAgICAgICAgZXhjZXB0IFZhbHVlRXJyb3I6CiAgICAgICAgICAgIHByaW50KCdUaGVyZVwncyBubyAlcyBmb3IgeW91IHRvIHRha2UuJyAlIGl0ZW0pCgogICAgZGVmIGZvbGxvdyhzZWxmLCBpbnN0cnVjdGlvbik6CiAgICAgICAgaWYgcmUuc2VhcmNoKERpcmVjdGlvbnMuTk9SVEgsIGluc3RydWN0aW9uKToKICAgICAgICAgICAgdHJ5OgogICAgICAgICAgICAgICAgc2VsZi5sb2NhdGlvbiA9IHNlbGYubWFwW3NlbGYubG9jYXRpb25dWydleGl0cyddWydOb3J0aCddCiAgICAgICAgICAgICAgICBwcmludChzZWxmLm1hcFtzZWxmLmxvY2F0aW9uXVsnZGVzY3JpcHRpb24nXSkKICAgICAgICAgICAgZXhjZXB0IEtleUVycm9yOgogICAgICAgICAgICAgICAgcHJpbnQoJ1lvdSBjYW5cJ3QgZ28gdGhhdCB3YXkuJykKICAgICAgICBlbGlmIHJlLnNlYXJjaChEaXJlY3Rpb25zLlNPVVRILCBpbnN0cnVjdGlvbik6CiAgICAgICAgICAgIHRyeToKICAgICAgICAgICAgICAgIHNlbGYubG9jYXRpb24gPSBzZWxmLm1hcFtzZWxmLmxvY2F0aW9uXVsnZXhpdHMnXVsnU291dGgnXQogICAgICAgICAgICAgICAgcHJpbnQoc2VsZi5tYXBbc2VsZi5sb2NhdGlvbl1bJ2Rlc2NyaXB0aW9uJ10pCiAgICAgICAgICAgIGV4Y2VwdCBLZXlFcnJvcjoKICAgICAgICAgICAgICAgIHByaW50KCdZb3UgY2FuXCd0IGdvIHRoYXQgd2F5LicpCiAgICAgICAgZWxpZiByZS5zZWFyY2goRGlyZWN0aW9ucy5FQVNULCBpbnN0cnVjdGlvbik6CiAgICAgICAgICAgIHRyeToKICAgICAgICAgICAgICAgIHNlbGYubG9jYXRpb24gPSBzZWxmLm1hcFtzZWxmLmxvY2F0aW9uXVsnZXhpdHMnXVsnRWFzdCddCiAgICAgICAgICAgICAgICBwcmludChzZWxmLm1hcFtzZWxmLmxvY2F0aW9uXVsnZGVzY3JpcHRpb24nXSkKICAgICAgICAgICAgZXhjZXB0IEtleUVycm9yOgogICAgICAgICAgICAgICAgcHJpbnQoJ1lvdSBjYW5cJ3QgZ28gdGhhdCB3YXkuJykKICAgICAgICBlbGlmIHJlLnNlYXJjaChEaXJlY3Rpb25zLldFU1QsIGluc3RydWN0aW9uKToKICAgICAgICAgICAgdHJ5OgogICAgICAgICAgICAgICAgc2VsZi5sb2NhdGlvbiA9IHNlbGYubWFwW3NlbGYubG9jYXRpb25dWydleGl0cyddWydXZXN0J10KICAgICAgICAgICAgICAgIHByaW50KHNlbGYubWFwW3NlbGYubG9jYXRpb25dWydkZXNjcmlwdGlvbiddKQogICAgICAgICAgICBleGNlcHQgS2V5RXJyb3I6CiAgICAgICAgICAgICAgICBwcmludCgnWW91IGNhblwndCBnbyB0aGF0IHdheS4nKQogICAgICAgIGVsaWYgcmUuc2VhcmNoKENvbW1hbmRzLlRBS0UsIGluc3RydWN0aW9uKToKICAgICAgICAgICAgbWF0Y2ggPSByZS5zZWFyY2goQ29tbWFuZHMuVEFLRSwgaW5zdHJ1Y3Rpb24pLmdyb3VwKDEpLnN0cmlwKCkKICAgICAgICAgICAgc2VsZi50YWtlX2l0ZW0obWF0Y2gpCiAgICAgICAgZWxpZiByZS5zZWFyY2goQ29tbWFuZHMuRFJPUCwgaW5zdHJ1Y3Rpb24pOgogICAgICAgICAgICBtYXRjaCA9IHJlLnNlYXJjaChDb21tYW5kcy5EUk9QL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dGVtczoKICAgICAgICAgICAgW3ByaW50KCdUaGVyZVwncyBhICVzIGhlcmUuJyAlIGl0ZW0pIGZvciBpdGVtIGluIGl0ZW1zXQoKICAgICAgICBpZiBub3Qgc2VsZi5tYXBbc2VsZi5sb2NhdGlvbl1bJ2V4aXRzJ106CiAgICAgICAgICAgIHByaW50KCdZb3Ugd2luIScpCgogICAgZGVmIHN0YXJ0KHNlbGYpOgogICAgICAgIHByaW50KHNlbGYubWFwW3NlbGYubG9jYXRpb25dWydkZXNjcmlwdGlvbiddKQoKICAgICAgICB3aGlsZSBUcnVlOgogICAgICAgICAgICBpbnN0cnVjdGlvbiA9IGlucHV0KCc+ICcpCgogICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICBzZWxmLmZvbGxvdyhpbnN0cnVjdGlvbikKICAgICAgICAgICAgZXhjZXB0IFJ1bnRpbWVFcnJvciBhcyBlcnI6CiAgICAgICAgICAgICAgICBwcmludChlcnIpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIGdhbWUgPSBHYW1lKCkKICAgIGdhbWUuc3RhcnQoKQ=="},"asBuffer":null},"loaded":true}}
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()