-1

I am a beginner Python programmer and my professor assigned this interestingly challenging assignment...

The assignment is to create a rock, paper, scissors game that is only played by the computer. The user has to input a number at the start to denotate how many bots will be in the game, and from there, the game will run in a bracket tournament style until there is one winner. I sat on this for quite some time and I am not really sure where to start. I don't just want the answer, I just need a little push in the right direction haha. I wrote the basic code of asking the player for the input and checking if it is a number or not, then assigning it to a variable. Really I just am not sure how to create a "player" for x amount, and put them in a bracket style tournament and then run them against each other (which is almost all of it haha)

Gage
  • 19
  • 4

3 Answers3

1

First of all, "amount" is a phrase used for uncountable things, like milk or fire. For countable things like players or lazy university students, you can use "number of" instead.

"create a "player" for x amount"

  1. Make a create_player(player_name) function
  2. Call that function in a for loop that runs x times

"put them in a bracket style tournament"

  1. Look into Array Representation Of Binary Heap
  2. You can then make the lowest leafs play against each other until the tree has only its root left

"and then run them against each other"

  1. make a which_player_wins(player_1, player_2) function
  2. keep calling that function until one of them wins
BarZamSr
  • 44
  • 3
1

Since it is a bracket tournament style, you should first check that the number the user inputs is a power of two. You can do this with the following code:

n = int(input("Please enter the number of players: "))
while (n & (n-1) != 0) or n == 0:
    print("The number of players is not valid to play a brackets style tornurament!\nValue must be power of 2.\n")
    n = int(input("Please enter the number of players: "))

You can learn more about how this works here. Note that it also checks that the number of players isn“t negative.

You can then use a random function to simulate the rock, scissors, paper game. The following function simulates a single game:

import random

def rock_paper_scissors_game():
    actions = ["paper", "rock", "scissors"]
    p1_choice = random.choice(actions)
    p2_choice = random.choice(actions)
    if p1_choice == p2_choice:
        print(f"Both players selected {p1_choice}. It's a tie!")
    elif p1_choice == "rock":
        if p2_choice == "scissors":
            print("Rock smashes scissors! Player 1 wins!")
        else:
            print("Paper covers rock! Player 2 wins!")
    elif p1_choice == "paper":
        if p2_choice == "rock":
            print("Paper covers rock! Player 1 wins!")
        else:
            print("Scissors cuts paper! Player 2 wins!")
    elif p1_choice == "scissors":
        if p2_choice == "paper":
            print("Scissors cuts paper! Player 1 wins!")
        else:
            print("Rock smashes scissors! Player 2 wins!")

You can then call this function each time a game is played and simulate the tournament.

LM2000
  • 59
  • 6
0

I would start by creating a player class that has all of the methods/logic an automated player would need. Next create a game method that takes two of these player classes and decides the winner. Finally instantiate an array/list of length N of player objects, and feed that into the game method.

Gordon
  • 170
  • 3
  • 18