Logically determine game outcome with formula

后端 未结 4 1569
终归单人心
终归单人心 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:20

    List is a good idea. In your case validoptions = ["rock", "paper", "scissors"] you can see, everything beats the only one before it (the "paper" beats the "rock", the "rock" beats the "scissors" and the "scissors" beats the "paper". So if you sort it that way, it is solvable with the use of the indexes only. If you want to increase the choices, you can, but tke care, only the odd numbers will provide fair game.

    In general if you make a list of options, with a length of length, then:

    if u1 == u2:
        #it is a draw
    elif u2input in validoptions[u1 - int((length-1)/2):u1]:
        #player1 has won
    else:
        #player2 has won
    

提交回复
热议问题