Logically determine game outcome with formula

后端 未结 4 1568
终归单人心
终归单人心 2021-01-21 14:41

I am trying to become a better coder, which includes getting rid of my \'hard-coding\' habits to keep my programs dynamic and easy to maintain.

Right now I am writing a

4条回答
  •  天涯浪人
    2021-01-21 15:29

    This is really cool! So, I think I would use a dictionary to control what loses to what:

    dict_loss = dict()
    dict_loss['paper']='scissors'
    dict_loss['scissors']='rock'
    dict_loss['rock']='paper'
    

    Then the players make a choice and you just check if their choices fall into the dictionaries:

    player_1='paper'
    player_2='rock'
    
    if player_2 in dict_loss[player_1]:
        print("Player 2 Wins")
    else:
        if player_1 in dict_loss[player_2]:
            print("Player 1 Wins")
        else:
            print("DRAW")
    

    You can extend the dictionary with the new objects you get, I'm not sure how Pans, swords and riffles work, but you can do:

    dict_loss['paper']=['scissors', 'riffle']
    

    if paper loses to riffles, and so on...

    Hope this helps, if you have any "data structure" restrictions let me know and I will try to think of something different.

提交回复
热议问题