lundi 3 septembre 2018

fruitBandit error when if statement checks random number

So I don't quite understand how the part which is checking for random number works. In the beginning it worked great until I redid the code and now it doesn't run.

First the game checks if there is any horizontal row (0,1,2) but also (3,4,5) and (6,7,8)

Then it's the vertical but I figure if I know how to solve the first one I can solve the other similar ones in the code.

I guess it's the part of the code when the if statements are checking:

EXAMPLE:

if(randNo[x] == randNo[x+0] and randNo[x] == randNo[x+1] and randNo[x] == randNo[x+2])

Basically if there is any vertical/horizontal bingo row then the game should take the bet and multiply it with the correct value, additionally there's a check for combinations where it's only two of the same instead of three of the same.

import random
from random import randint
filePath = 'json/scorelist.json'

def pull_arm():
    euros = 500
    bet = 1000
    print("One-armed Bandit")
    print("You have " + str(euros) + "£.")
    print("(Enter q to quit)")

    while euros > 0:

        bet = int(input("Select amount to bet: "))
        if euros == 'q':
            break
        if bet == 'q':
            break
        while bet > euros:
            print("Your bet is greater than the number of euros you have.",
            bet = input("Select amount to bet: "))
        fruits = ['Banana', 'Cherry', 'Mango','Watermelon','Clementine','Kiwi','Pear','Pineapple','Oranges']
        randNo = []
        for x in range(9):
            randNo.append(randint(0, 8))

        flag = False

        print(fruits[randNo[0]] + '  ' + fruits[randNo[1]] + ' ' + fruits[randNo[2]])
        print(fruits[randNo[3]] + '  ' + fruits[randNo[4]] + ' ' + fruits[randNo[5]])
        print(fruits[randNo[6]] + '  ' + fruits[randNo[7]] + ' ' + fruits[randNo[8]])

        #in this for loop we will be checking if any horizontal row is the same
        horList1 = [0, 1, 2]
        horList2 = [3, 4, 5]
        horList3 = [6, 7, 8]

        for x in horList1:
            if(randNo[x] == randNo[x+0] and randNo[x] == randNo[x+1] and randNo[x] == randNo[x+2]):
                total_euros = euros + bet * 10
                win = bet * 10
                print("You won " + str(win) + "£" + ". Total: " + str(total_euros) + "£.")
                print("Select amount to play (press q to quit)")
                flag = True
                break

        for x in horList2:
            if(randNo[x] == randNo[x+3] and randNo[x] == randNo[x+4] and randNo[x] == randNo[x+5]):
                total_euros = euros + bet * 10
                win = bet * 10
                print("You won " + str(win) + "£" + ". Total: " + str(total_euros) + "£.")
                print("Select amount to play (press q to quit)")
                flag = True
                break

        #in this for loop we will be checking if any vertical column is the same 
        verList = [0, 3, 6]
        verList2 = [1, 4, 7]
        verList3 = [2, 5, 8]

        if flag == False:
            for x in verList:
                if(randNo[x] == randNo[x+0] and randNo[x] == randNo[x+3] and randNo[x] == randNo[x+6]):
                    total_euros = euros + bet * 10
                    win = bet * 10
                    print("You won " + str(win) + "£" + ". Total: " + str(total_euros) + "£.")
                    print("Select amount to play (press q to quit)")
                    flag = True
                    break

        #in this for loop we will be checking if any diagonal is the same
        if flag == False and (randNo[0] == randNo[4] and randNo[0] == randNo[8]):
            total_euros = euros + bet * 30
            win = bet * 30
            print("You won " + str(win) + "£" + ". Total: " + str(total_euros) + "£.")
            print("Bingo diagonally")
            print("Select amount to play (press q to quit)")
            flag = True

        if flag == False and (randNo[2] == randNo[4] and randNo[2] == randNo[6]):
            total_euros = euros + bet * 30
            win = bet * 30
            print("You won " + str(win) + "£" + ". Total: " + str(total_euros) + "£.")
            print("Bingo diagonally")
            print("Select amount to play (press q to quit)")
            flag = True

        combCheck = [0,1,3,4,6,7]
        combVerticalCheck = [0,1,2,3,4,5]
        if flag == False:
            for x in combCheck:
                if(randNo[x] == randNo[x+1]):
                    total_euros = euros + bet #* 100
                    print("You won " + str(bet) + ". Total: " + str(total_euros) + "£.")
                    print ("Select amount to play (press q to quit)")
                    flag = True
                    break
        if flag == False:
            for x in combVerticalCheck:
                if(randNo[x] == randNo[x+3]):
                    total_euros = euros + bet #* 100
                    print("You won " + str(bet) + ". Total: " + str(total_euros) + "£.")
                    print ("Select amount to play (press q to quit)")
                    flag = True
                    break
        if flag == False:
            total_euros = euros - bet
            print("You lost " + str(bet) + ". Total: " + str(total_euros) + "£.")
            print("Select amount to play (press q to quit)")

def saveHighscore():
    high_score = int(round(self.stats.high_score, -1))
    high_score_str = "{:,}".format(high_score)

    userInput = raw_input(input("Enter your name: "))    
    scoreObj =  {userInput: high_score}
    try:
        with open(filePath, 'w') as f_obj:
            f_obj.write(json.dump(scoreObj))
    except:
        print('error/exception')

def loadHighscore():
    """load in existing highscore from highscore.json"""
    #take user input for his name -> 'Sam'
    userInput = raw_input(input("Enter your name: "))
    scoresDict = json.load(open(filePath)) #{'Sam':120}
    print(scoresDict[user_input])

def run_game():

    pull_arm()

run_game()

Aucun commentaire:

Enregistrer un commentaire