Squidsheep
Squidsheep

Reputation: 15

Randomiser not working

So I am using python to create a text based pokemon game. However a bug keeps popping up, where the enemy does two attacks in one turn, which are the same every time, even if though they should be random. Apologies for the layout

CODE

import math, random
print("-------------POKEMON BROWN-------------")
print("welcome to pokemon brown")
print("choose a pokemon")
print("Charmander, Squirtle, Bulbasaur")
pokemon_1 = input()
pokemon_1 = (pokemon_1.lower())

print("ahh " + pokemon_1 + " a good choice")

enemy = 1

print("Ok lets battle!")

battle = 1
hp = 20
mp = 10
enemyhp = 20
enemymp = 10
print("----Battle Start----")
print("")
battle = 1
while True:
    turn = 1
    player_turn = 1
    if(pokemon_1 == "squirtle"):
        player_turn = 1
        print("----Your turn----")
        print("")
        print("Pokemon:" + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
        print("Tackle(1 DMG), Tail Whip(2 DMG, 2MP), Water Gun(5 DMG, 10 MP), Back")
        move = input()
        move = (move.lower())
        if(move == "tackle"):
            print("Your Squirtle uses tackle and deals 1 DMG")
            enemyhp -= 1
            turn = 1
        if(move == "tail whip"):
            if(mp >= 2):
                print("Your Squirtle uses tail whip and deals 2 DMG")
                enemyhp -= 2
                mp -= 2
                turn = 1
            else:
                print("not enough MP")

        if (move == "water gun"):
            if(mp >= 10):
                print("Your Squirtle uses water gun and deals 5 DMG")
                enemyhp -= 5
                mp -= 10
                turn = 1
            else:
                print("not enough MP")

    if(pokemon_1 == "charmander"):
        player_turn = 1
        print("")
        print("----Your turn----")    
        print("Pokemon: " + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
        print("Scratch(2 DMG), Ember(4 DMG, 5MP), Back")
        move = input()
        move = (move.lower())
        if(move == "scratch"):
            print("Your Charmander uses scratch and deals 2 DMG")
            enemyhp -= 2
            turn = 1
        if(move == "ember"):
            if(mp >= 2):
                print("Your Charmander uses Ember and deals 4 DMG")
                enemyhp -= 4
                mp -= 5
                turn = 1
            else:
                print("not enough MP")

    if(pokemon_1 == "bulbasaur"):
        player_turn = 1
        print("")
        print("----Your turn----")

        print("Pokemon: " + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
        print("Tackle(1 DMG), Vine whip(5 DMG, 5MP)")
        move = input()
        move = (move.lower())
        if(move == "scratch"):
            print("Your Bulbasuar uses Tackle and deals 2 DMG")
            enemyhp -= 2
            turn = 1
        if(move == "tail whip"):
            if(mp >= 2):
                print("Your Bulbasaur uses Vine Whip and deals 5 DMG")
                enemyhp -= 4
                mp -= 5
                turn = 1
            else:
                print("not enough MP")

    if(turn == 1):
        if(enemy == 1):
            print("")
            print("----Enemy's turn----")
            print("Enemy Pokemon: Squirtle / HP = " + str(enemyhp) + " / MP = " + str(enemymp))


            if(enemymp == 10):
                enemy_move = random.randint(1,6)

            if(enemymp < 10 and enemymp >= 2):
                enemy_move = random.randint(1,5)

            if(enemymp < 2):
                enemy_move = 0

            if(enemy_move == 1 or 2 or 3):
                print("Enemy Squirtle uses tackle and deals 1 DMG")
                hp -= 1
                turn = 0
            if(enemy_move == 4 or 5):
                if(mp >= 2):
                    print("Enemy Squirtle uses tail whip and deals 2 DMG")
                    hp -= 2
                    enemymp -= 2
                    turn = 0
                else:
                    print("not enough MP")

            if (enemy_move == 6):
                if(mp >= 10):
                    print("Enemy Squirtle uses water gun and deals 5 DMG")
                    hp -= 5
                    enemymp -= 10
                    turn = 0
                else:
                    print("not enough MP")

SHELL

-------------POKEMON BROWN-------------
welcome to pokemon brown
choose a pokemon
Charmander, Squirtle, Bulbasaur
charmander
ahh charmander a good choice
Ok lets battle!
----Battle Start----


----Your turn----
Pokemon: charmander/HP = 20/MP = 10
Scratch(2 DMG), Ember(4 DMG, 5MP), Back
scratch
Your Charmander uses scratch and deals 2 DMG

----Enemy's turn----
Enemy Pokemon: Squirtle / HP = 18 / MP = 10
Enemy Squirtle uses tackle and deals 1 DMG
Enemy Squirtle uses tail whip and deals 2 DMG

----Your turn----
Pokemon: charmander/HP = 17/MP = 10
Scratch(2 DMG), Ember(4 DMG, 5MP), Back        

Upvotes: 1

Views: 90

Answers (5)

JonnyNich
JonnyNich

Reputation: 1281

The problem I think is with the if statements Do NOT do this with if statements:

if (enemy_move == 2 or 3 or 4):

What you want to do is the following:

if (enemy_move == 2 or enemy_move == 3 or enemy_move =-- 4)

Upvotes: 0

zstewart
zstewart

Reputation: 2177

if(enemy_move == 1 or 2 or 3):

This checks if enemy_move is equal to 1, and if that evaluates to false, it checks if 2 is a true value, which it always is. To check if the value is any of the three you need three comparisons, or you can use in:

if enemy_move in (1, 2, 3):

Or you could use a double sided range check:

if 1 <= enemy_move < 4:

This is python, so you don't need to put parentheses around the if condition.

Upvotes: 1

Steven Summers
Steven Summers

Reputation: 5384

The main problem is that you are not using or correctly.

if(enemy_move == 1 or 2 or 3):
if(enemy_move == 4 or 5):

Will evaluate to True every time. This is because logically 0 has a boolean value of False and any other number has a value of True

>>> bool(0)
False
>>> bool(1)
True

So your condition ends up as.

if (enemy_move == 1 or True or True):

To work correctly you need to compare each value.

if(enemy_move == 1 or enemy_move == 2 or enemy_move == 3)

This can be cleaned up various ways such as.

if enemy_move in (1, 2, 3):
if 1 <= enemy_move <= 3:

Now, your other issues include having multiple if statements, now unless you want to check each one every time you should use if/elif

if enemy_move in (1, 2, 3):
    ...
elif enemy_move in (4, 5):
    ...
elif enemy_move == 6:
   ...
else:
   ...

This will then check each one in order from first to last, if one evaluates to False then it will move on to the next, once the condition is met, True. Then it won't do the rest.

Upvotes: 2

товіаѕ
товіаѕ

Reputation: 3244

when you use random, you should always use a unique seed, otherwise the generated numbers will always be the same

set your seed once when you start your program like so

random.seed( ... )

what seed you use is not really important, as long as it changes. Best example most people use is the current timestamp.

time.time()

hope this helps

Upvotes: 0

encryptoferia
encryptoferia

Reputation: 247

during an if statement like :

...
            if(enemy_move == 1 or 2 or 3):
...
            if(enemy_move == 4 or 5):
...
            if (enemy_move == 6):
...

Notice that only the first two will always execute together, because when the if evaluates, it checks [if enemy_move equals to 1] or if [2] or [3], the 2 and 3 should be enemy_move == 2 and enemy_move == 3 , if you don't put 'enemy_move ==' it would always evaluate as TRUE with if operator, which is why only the last ... if (enemy_move == 6): ... Don't get executed together.

Upvotes: 0

Related Questions