Search code examples
pythonlistsearch2d

how do i search a 2d list to be able to play rock, paper, scissors?


still kinda new to this site (sorry if i messed up) so my homework for the week is working with lists... my professor wants me to modify my rock, paper, Scissors game to use a 2d list. which i was able to make. the rest is the problem. i need to search the 2d list for the outcome, and post if the player won or the computer, and keep track of the number of wins each has, until the player doesn't want to play anymore. i've looked and tried two different ways to do a search but i either end up in an endless loop or it ends without doing anything... right now i'm stuck with an endless loop. the output is easy part i think.

This is my current code after doing multiple re-writes... i have everything else from the old version that doesn't use a list commented out so i didn't post it.

compchoice = " "
humwin = 0
compwin = 0
again = 'Y'
playing = True
outcomes = [[1, 2], [2, 3], [3, 1], [2, 1], [3, 2], [1, 3], [1, 1], [2, 2], [3, 3]]
#thelist = []
#thelist.append("Player 1 = {playerchoice}, Player 2 = {compchoice}")
while playing:
    playerpic = int(input("Pick 1 for Rock, 2 for Paper, or 3 for Scissors: "))
    comppic = random.randint(1, 3)
    pick = [playerpic, comppic]
    for x in range(len(outcomes)):
        for i in range(len(outcomes[x])):
            if pick == outcomes[x][i]:
                print("You chose Rock.")
                print("Computer choose Paper")
                print("You lost")
                compwin += 1
                print("Human: ", humwin, "  Computer: ", compwin)
                break
#will not break, seems to ignore if? missing something!

Solution

  • Basically, you have all the possible combinations that the player and computer can choose. It just appears that these choices need to be categorized into the ones where the computer is the winner, the ones where the player is the winner, and the ones that are ties. Also, it appeared that there needed to be an added bit to prompt the user as to when the game would conclude. With those additional bits, following is one possible refactored version of the code.

    import random
    
    compchoice = " "
    humwin = 0
    compwin = 0
    again = 'Y'
    playing = True
    choice = ['Rock', 'Paper', 'Scissors']
    outcomes = [[1, 2], [2, 3], [3, 1], [2, 1], [3, 2], [1, 3], [1, 1], [2, 2], [3, 3]]
    #thelist = []
    #thelist.append("Player 1 = {playerchoice}, Player 2 = {compchoice}")
    while playing:
        playerpic = int(input("Pick 1 for Rock, 2 for Paper, or 3 for Scissors: "))
        comppic = random.randint(1, 3)
        pick = [playerpic, comppic]
        for x in range(len(outcomes)):
            if pick == outcomes[x]:
                print("You chose", choice[playerpic - 1])
                print("Computer choose", choice[comppic - 1])
                if (x < 3):             # In those choice combos, the computer wins
                    print("You lost")
                    compwin += 1
                elif (x < 6):           # In choices 4 - 6, the player wins
                    print("You won")
                    humwin += 1
                else:                   # The last three combos are a tie
                    print("Tie")
                print("Human: ", humwin, "  Computer: ", compwin)   # List the current score
    
        cont = input("Play again Y/N ") # Need to ask a question to continue playing
    
        if (cont != 'Y'):
            break
    

    The things to note:

    • The various choice combinations are categorized based on who wins or if there is a tie.
    • Instead of outputting "1/2/3", the selections are given a descriptive output.
    • And, the user is prompted to see if they wish to continue playing.

    Testing out this refactored code, following is some sample terminal output.

    craig@Vera:~/Python_Programs/Games$ python3 RockPaperScissors.py 
    Pick 1 for Rock, 2 for Paper, or 3 for Scissors: 1
    You chose Rock
    Computer choose Scissors
    You won
    Human:  1   Computer:  0
    Play again Y/N Y
    Pick 1 for Rock, 2 for Paper, or 3 for Scissors: 1
    You chose Rock
    Computer choose Rock
    Tie
    Human:  1   Computer:  0
    Play again Y/N Y
    Pick 1 for Rock, 2 for Paper, or 3 for Scissors: 3
    You chose Scissors
    Computer choose Rock
    You lost
    Human:  1   Computer:  1
    Play again Y/N N
    

    Go ahead and dissect this and hopefully get a better understanding of lists and tests.