vendredi 4 juin 2021

creating a python 2 player game with functions & classes

This is a game where player 1 gets asked questions and store all player 1 answers in a list. After player 1 finishes his/her turn, then player 2 gets to play and ask the same exact questions as player 1 and store player 2 answers in a different list.

I have player 1 in a function, problem is once player1 function is done running the program exits.

1- how do I make it so player2 gets asked the same questions as player1. Also, that once player1 function is done running that the program goes to player2 function.

2- would it be better to store my list of dict questions in a class, because I intend to increase the number of questions in the qa list, and how do I do so? Please see code below.

import random

qa = [{"question": 'mi', 'yes': 'si', 'no': 'bay'}, {"question": 'see', 'yes': 'boom', 'no': 'dos'}, {"question": 'do', 'yes': 'fool', 'no': 'sil'}, {"question": 're', 'yes': 'but', 'no': 'fool'}]

user1 = []
user2 = []

# this function is for bad input
def valid_input(prompt, opt1, opt2):
    while True:
        response = input(prompt).lower()
        if opt1 in response:
            break
        elif opt2 in response:
            break
        else:
            print('error, enter again')
    return response

def player1():
    cap = len(qa)
    i = 0
    print("Hello player 1")
    while i < cap:
    rand_q = random.choice(qa)
    print(rand_q["question"])
    answer1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
    if 'yes' in answer1:
        user1.append('yes')
        print(rand_q.get(answer1, ''))
        
        answer1_1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
        if 'yes' in answer1_1:
            user1.append('correct')
            qa.remove(rand_q)
            i += 1

        elif 'no' in answer1_1:
            user1.append('wrong')
            qa.remove(rand_q)
            i += 1


    elif 'no' in answer1:
        user1.append('no')
        print(rand_q.get(answer1, ''))
       

        answer1_1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
        if 'yes' in answer1_1:
            user1.append('correct')
            qa.remove(rand_q)
            i += 1

        elif 'no' in answer1_1:
            user1.append('wrong')
            qa.remove(rand_q)
            i += 1

def player2():
    cap = len(qa)
    i = 0
    print("Hello player 2")
    while i < cap:
    rand_q = random.choice(qa)
    print(rand_q["question"])
    answer1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
    if 'yes' in answer1:
        user1.append('yes')
        print(rand_q.get(answer1, ''))
        
        answer1_1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
        if 'yes' in answer1_1:
            user1.append('correct')
            qa.remove(rand_q)
            i += 1

        elif 'no' in answer1_1:
            user1.append('wrong')
            qa.remove(rand_q)
            i += 1


    elif 'no' in answer1:
        user1.append('no')
        print(rand_q.get(answer1, ''))
       

        answer1_1 = valid_input("enter 'yes' or 'no'\n", "yes", "no")
        if 'yes' in answer1_1:
            user1.append('correct')
            qa.remove(rand_q)
            i += 1

        elif 'no' in answer1_1:
            user1.append('wrong')
            qa.remove(rand_q)
            i += 1

def play():
    player1()
    player2()
    
play()

since I'm new at coding I'm wondering if this the best approach for a 2 player game since I plan on growing the size of the game. Any suggestions, ideas are welcome. Thank you.




Aucun commentaire:

Enregistrer un commentaire