Python Forum
Rock paper scissors in python with "algorithm"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock paper scissors in python with "algorithm"
#1
In the end I was able to do it thanks to what I was looking for through the dictionary of victory made
Reply
#2
You are thinking about this backward. Why do you have a game history? The computer's next action is based on the results of the current game. The computer AI is one line per result. This program should be about 30 lines long. In Pythonny pseudo code:

computer_play = random play
while True:
    get_human_play
    if human wins:
        next_play = human_play + 1
    else if computer wins:
        next_play = human_play - 1
    else:
        next_play = random play
kabeer_m likes this post
Reply
#3
In the end I was able to do it thanks to what I was looking for through the dictionary of victory made
kabeer_m likes this post
Reply
#4
This is a working version:
import random

plays = ("Rock", "Paper", "Scissors")
results = ("Rock smashes scissors", "Paper covers rock", "Scissors cuts paper")

def get_player_choice():
    while True:
        try:
            return "RPS".index(input("Select (R)ock, (P)aper, (S)cissors: ").upper()[0])
        except (ValueError, IndexError):
            print("That is not a valid selection.")

computer_choice = random.randint(0,2)
while True:
    player_choice = get_player_choice()
    print(f"Computer picks {plays[computer_choice]}")

    if (computer_choice + 1) % 3 == player_choice:
        print(f"{results[player_choice]}.  Player wins!")
        computer_choice = (player_choice + 1) % 3
    elif (player_choice + 1) % 3 == computer_choice:
        print(f"{results[computer_choice]}.  Computer wins!")
        computer_choice = (player_choice - 1) % 3
    else:
        print("Game is a draw.")
        computer_choice = random.randint(0,2)
Reply
#5
In the end I was able to do it thanks to what I was looking for through the dictionary of victory made
Reply
#6
I am confused. You didn't write any of this code and all you need to provide is a function that tells you who the winner is and another that randomly selects Rock, Paper or Scissors?
Agat0 likes this post
Reply
#7
I have written the first code following a documentation, but now they have given me this new one and this code that I have to implement, from this last code I have to create get_winner_action and a get_random_computer_action() because it is the only new thing.
Reply
#8
I'm still a little confused because you already have this.
def get_computer_action():
    computer_selection = random.randint(0, len(GameAction) - 1)
    computer_action = GameAction(computer_selection)
    print(f"Computer picked {computer_action.name}.")
How would "get_random_computer_action()" differ? At the very least this should be a very good start.

And get_winner_acction() is going to look a lot like the code the computer uses to pick its next move when the computer wins.
computer_action = GameAction((user_actions_history[-1].value + 1) %
        len(GameAction))
Agat0 likes this post
Reply
#9
In the end I was able to do it thanks to what I was looking for through the dictionary of victory made
Reply
#10
That makes more sense than the previous AI. Your history list now makes sense as you will have to count recent player choices. I would use a Counter dictionary from the Collections library.
import random
from collections import Counter
 
plays = ("Rock", "Paper", "Scissors")
results = ("Rock smashes scissors", "Paper covers rock", "Scissors cuts paper")
history = []

def get_player_choice():
    while True:
        try:
            return "RPS".index(input("Select (R)ock, (P)aper, (S)cissors: ").upper()[0])
        except (ValueError, IndexError):
            print("That is not a valid selection.")
 
while True:
    player_choice = get_player_choice()

    if history:
        # Select a move that beats the most frequent player choice from the history list
        choice_counts = Counter(history).most_common()  # Get counts for each choice
        most_common = choice_counts[0][0]  # Get choice with the highest count
        computer_choice = (most_common+1) % len(plays)
    else:
        computer_choice = random.randint(0,2)
    print(f"Computer picks {plays[computer_choice]}")
 
    if (computer_choice + 1) % 3 == player_choice:
        print(f"{results[player_choice]}.  Player wins!")
    elif (player_choice + 1) % 3 == computer_choice:
        print(f"{results[computer_choice]}.  Computer wins!")
    else:
        print("Game is a draw.")
    
    # Keep record of the last 5 player choices
    history.append(player_choice)
    history = history[-5:]
I think the AI is going to be boring to play against. I think basing the computer choice on the player history makes sense, but adding in some randomness will make it less "robotic". It is very easy to do a weighted random choice. The code is actually simpler.
import random
 
plays = ("Rock", "Paper", "Scissors")
results = ("Rock smashes scissors", "Paper covers rock", "Scissors cuts paper")
history = []

def get_player_choice():
    while True:
        try:
            return "RPS".index(input("Select (R)ock, (P)aper, (S)cissors: ").upper()[0])
        except (ValueError, IndexError):
            print("That is not a valid selection.")
 
while True:
    player_choice = get_player_choice()

    # Use history to predict what the player might do.
    # User choices add "weight" to which play is randomly selected by the computer
    weighted_choice = random.choice(history + [0, 1, 2])  # What the player is likely to do
    computer_choice = (weighted_choice+1) % len(plays)  # How I can beat that
    print(f"Computer picks {plays[computer_choice]}")
 
    if (computer_choice + 1) % 3 == player_choice:
        print(f"{results[player_choice]}.  Player wins!")
    elif (player_choice + 1) % 3 == computer_choice:
        print(f"{results[computer_choice]}.  Computer wins!")
    else:
        print("Game is a draw.")
    
    # Keep record of the last 5 player choices
    history.append(player_choice)
    history = history[-5:]
Randomly choosing a play from the player history gives you a weighted random choice. The more often a choice appears in the history list the more likely it is picked, but there is still the possibility of randomly choosing something else. To make anything possible I add [0, 1, 2] to the history values so the computer can select anything. This has a nice side-benefit of not needing special code for the case where history is empty.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 545 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Rock Paper Scissors Project in Python ankitdixit 8 4,869 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Trying to create a visual rock paper scissors game urmom33 1 1,041 Dec-03-2022, 09:12 PM
Last Post: deanhystad
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,638 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Odd behavior with Rock Paper Scissor game DustinKlent 2 1,953 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 3,376 May-11-2020, 04:31 PM
Last Post: ShAhCh
  Rock Paper Scissors with dictionaries ewgreht 2 3,904 May-01-2020, 03:19 PM
Last Post: deanhystad
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 2,680 Mar-19-2020, 02:46 AM
Last Post: jefsummers
  POS receipt print cannot make paper cut using python AP_Development 1 2,743 Dec-12-2019, 08:48 AM
Last Post: buran
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,461 Sep-13-2019, 03:28 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020