Results 1 to 4 of 4

Threaded View

  1. #4
    Vox's Avatar
    Join Date
    Nov 2014
    Gender
    male
    Location
    USA
    Posts
    60
    Reputation
    10
    Thanks
    52
    My Mood
    Innocent
    I took a stab at this using python 3.6.4 (My favourite). At first, i didn't want to try, as i thought i would just end up complicating things a lot more, but i actually think i did a pretty good job, and i had a lot of fun doing it as well.
    Code:
    import random
    import sys
    
    #this is a script i added a bit extra, bare minimum code is in spoiler below
    
    def main():
        cheats = False
        if len(sys.argv) == 2:
            cheats = True if sys.argv[1] == "-c"
    
    
        choices = ["rock", "paper", "scissors"]
        computer_action = random.choice(choices)
    
        if cheats:
            print(f"Computer chose: {computer_action}")
    
        player_action = None
        while player_action == None:
            player_action = input("Choose your weapon: ").lower()
    
            if player_action not in choices:
                player_action = None
    
        computer_result = choices.index(computer_action)
        player_result = choices.index(player_action)
    
        if player_result != computer_result:
            if player_result != 2 and computer_result != 2:
                winner = "Computer" if computer_result > player_result else "You"
    
            elif computer_result > player_result:
                winner = "Computer" if player_result == 1 else "You"
    
            elif player_result > computer_result:
                winner = "You" if computer_result == 1 else "Computer"
        else:
            print("It's a Tie!")
            main()
    
        print(f"{winner} won!")
        sys.exit()
    
    if __name__ == "__main__":
        main()
     

    Code:
    import random
    #this is the bare minimum for (my) rock paper scissors.
    #It removes error checking, and re-rolling on a tie, as well as letting python close itself.
    
    choices = ["rock", "paper", "scissors"]
    
    computer_action = random.choice(choices)
    player_action = input("Choose your weapon: ").lower()
    
    computer_result = choices.index(computer_action)
    player_result = choices.index(player_action)
    
    if player_result != computer_result:
    	if player_result != 2 and computer_result != 2:
    		winner = "Computer" if computer_result > player_result else "You"
    
    	elif computer_result > player_result:
    		winner = "Computer" if player_result == 1 else "You"
    
    	elif player_result > computer_result:
    		winner = "You" if computer_result == 1 else "Computer"
    else:
    	print("It's a Tie!")
    
    print(f"{winner} won!")
    Last edited by Vox; 08-27-2018 at 06:30 AM.
    --- Hack to learn, don't learn to hack.



  2. The Following User Says Thank You to Vox For This Useful Post:

    Hell_Demon (08-27-2018)

Similar Threads

  1. Rock Paper Scissors Source Code
    By Poloh in forum Coders Lounge
    Replies: 4
    Last Post: 01-11-2017, 07:46 PM
  2. [Help Request] Rock, Paper, Scissor game. Random number generator not working.
    By samlarenskoog in forum C++/C Programming
    Replies: 6
    Last Post: 04-19-2016, 04:22 AM
  3. Rock paper scissor lizard spock
    By Woods in forum General
    Replies: 2
    Last Post: 10-27-2011, 07:05 PM
  4. [Release]Scissors, rock, paper
    By /b/oss in forum Visual Basic Programming
    Replies: 21
    Last Post: 05-19-2010, 06:19 PM
  5. rock, paper, scissors
    By ace76543 in forum General
    Replies: 5
    Last Post: 12-01-2007, 04:31 PM