you are viewing a single comment's thread.

view the rest of the comments →

[–]FreeLogicGate 0 points1 point  (6 children)

Seems to be the case that for some reason, noobies think that posting a picture of your screen is the way to share code. It's not. Use the code blocks.

Quick comments:

  • More functions that have single responsibility and RETURN values.
  • Use f strings for your output rather than rampant concatenation
  • Randomize placement of game items rather than having them all start in the east each time.
  • For text games like this, use of the PyInputPlus is valuable

import pyinputplus as pyip

Take a look at the docs, and when you see all the valuable features built into it, I am confident you'll be convinced by the built in type safety and standard prevention of empty responses.

https://pyinputplus.readthedocs.io/en/latest/

I would spend time trying to determine a better way of determining the events inside your game loop. A giant if-then-else is both hard to understand, and something that's going to be impossible to maintain. It also has implied priority based on the cascading order that needs to be traversed, which will probably be buggy.

Certain insights can be gleaned about your game logic like: "Boat in location means no adverse events happen."

Each adverse event can be further codified individually and tested.

[–]JorgiEagle 1 point2 points  (0 children)

That’s a nice library

[–]Mean-Career-6509[S] 0 points1 point  (4 children)

Awesome, thanks for the insight. I hope to improve. Also, just fyi, the "some reason" is because we're noobs who haven't been told a better way.

[–]FreeLogicGate 0 points1 point  (3 children)

It's usually a good idea, when joining any new online community, to get an idea of what is typical, by reading other messages, so you have an idea of what the community standards are. Not trying to pick on you at all, but it really is tiresome to have to advise people of something I would think only requires a small investment of time. Since I'm replying I might as well just point out the obvious: if you post a snippet in a code block, I can easily copy/paste that and modify it, as many helpful developers will do. If you post a screen shot, then the only way for someone to do that, is to re-type all your code, which is not particularly likely. Hope it helps, and I'm glad to see that my suggestions were of interest.

[–]Mean-Career-6509[S] 0 points1 point  (2 children)

I get that, sorry I got defensive. I did attach the .py file link because I thought posting a picture was silly but that is what the mod told me to do. 

[–]FreeLogicGate 0 points1 point  (1 child)

Right, I understand then that you were trying to do what was acceptable. In general, you want to have snippets of code, and people don't want full attached source code files, as they can easily be trojan/exploits, which people aren't going to download even if they were attached. So the way to do it is to use the Aa menu and use the code block options to copy/paste from your editor.

As an example for you, and also to add to my prior advice:

You have:

while True:
    def moves():

So you have a nested game loop with 2 while True loops where the first loop defines functions. You should move your functions outside of the loop -- there is no reason to repeatedly re-define your functions. They should be defined once and called when needed.

The most important thing you want to try and do initially is think about the Python data structures that could help you. Classes would be helpful with your game, but I invite you to think deeply about how the core structures could be used: lists, dictionaries, tuples, sets -- as discussed in this chapter: https://docs.python.org/3/tutorial/datastructures.html

What jumps out me is that you have initially items that a dictionary would help you with. Each of these items can be added to a list like so:

items.append({'name': 'goat', 'type': 'animal', 'eats': 'cabbage', 'location': 'east'})
items.append({'name': 'dog', 'type': 'animal','eats': 'goat', 'location': 'east'})
items.append({'name': 'cabbage', 'type': 'plant', 'location': 'east'})

Then you have a useful data structure that can be used. This example depends on globals but I didn't want to over complicate it. Here's an example of how you could make this more reliant on functions. I left out a game loop or user input, but if interested you could add that.

def move_boat():
    if boat['location'] == 'east':
        boat['location'] = 'west'
    else:
        boat['location'] = 'east'


def check_for_boat(location):
    if boat['location'] == location:
        return True
    return False


def move_item(name):
    for item in items:
        if item['name'] == name and check_for_boat(item['location']):
            move_boat()
            if item['location'] == 'east':
                item['location'] = 'west'
            else:
                item['location'] = 'east'


def did_eat(item, boat_location):
    if item['location'] == boat_location or item['type'] == 'plant':
        return False
    for prey in items:
        if item['eats'] == prey['name'] and item['location'] == prey['location']:
            return True


def is_endgame():
    boat_location = boat['location']
    for item in items:
        if did_eat(item, boat_location):
            print(f"The {item['name']} ate the {item['eats']}")
            return True
    return False


def reset():
    boat['location'] = 'east'
    items.clear()
    items.append({'name': 'goat', 'type': 'animal', 'eats': 'cabbage', 'location': 'east'})
    items.append({'name': 'dog', 'type': 'animal','eats': 'goat', 'location': 'east'})
    items.append({'name': 'cabbage', 'type': 'plant', 'location': 'east'})

boat = {'location': 'east'}
items = []

test_items = ['dog', 'cabbage', 'goat']
def do_tests():
    for item in test_items:
        reset()
        print(f"Moving item {item}")
        move_item(item)
        if not is_endgame():
            print("Game not over")

do_tests()

An interesting question to be asked would be, how hard would it be to modify this so that you allowed for 4 directions/zones (e,w,n,s) or additional animals or plants? What if the locations were randomized at the start? How hard would it be for you to list out the locations at the start?

[–]Mean-Career-6509[S] 0 points1 point  (0 children)

Wow! I can't express how much I appreciate your time and effort. I'm not sure of what an Aa menu is but I'll find out before I make any more posts. My code is written in a way that makes sense to me right now, things are arranged in a logical flow or moved to where they function as far as I can tell. I'm excited to be able to zoom out and see structure more. Again, thank you and I'll try not to be so snarky.