mercredi 26 mai 2021

python - point system in a 2 player game

I'm working on a 2 player game where each player answers the same amount of question. I have the players inputting their answers respectively through the input method and contain their answers in 2 lists: player 1 list and player 2 list.

I am having trouble computing the point system as desired. The way it works is if both players get the same answer for a question, 2 point is added to an empty list for point collection and skip over the next question.

Now if both players' answers don't match for a question, 1 point is added to the empty list and goes to the next immediate question. In this case the next question is always a sequence of conditions in if satement and that's where my codes are not behaving:

I use zip method to pair the answers of each index from the 2 lists. A for loop to go through each index checking for matched and unmatched answers.

Problem is when the answers or pairs don't match. 1 point is added to the list when a pair doesn't match-- and on the next pair if it finds 'deal' and 'deal' add -2 to the empty list, and move on to next pair. Else if 'no deal' and 'no deal' is in the pair add 0 to the empty list, and move on to next. Otherwise if these instances don't match add -1 to the list and move on to the next.

The code seems to be skipping and not check the if and elif part of the condition and goes directly to the else part and just add -1 which I don't want. I want the code to check all the conditions before checking the else part and add -1. Please, help and apologies if this isn't clear.

# list of answers from the 2 players
player1 = ['yes', 'deal', 'yes', 'no deal', 'no', 'deal', 'yes', 'deal'] 
player2 = ['yes', 'no deal', 'no', 'deal', 'yes', 'deal', 'yes', 'deal']

pairs = zip(player1, player2)
points = [] #points are placed in here
for a, b in pairs:
    if a != b: #if pair doesn't match 1 point is added to the list
        points.append(1)
        next(pairs, True) #this makes it go to the next pair without skipping over
        if a == 'deal' and b == 'deal':
            points.append(-2)
            break
        elif a == 'no deal' and b == 'no deal':
            points.append(0)
            break     
        else:
            points.append(-1)
    else:
        a == b #if a pair is matched 2 point is added to the empty list
        points.append(2)
        next(pairs, None) #this skips over the next pair
       
print(count)

Aucun commentaire:

Enregistrer un commentaire