Wyom the Wyom
Wyom the Wyom

Reputation: 13

A random value isnt being random when inside an if

This is a little game im working on in lua and the damage is a math.random But when I put the function that makes the player attack in an if it no longer is random.

Here is what it does: Attacked enemy with 22 damage! current enemy health is 78 Enemy getting ready to attack! Enemy Attacked you with 19 damage! your current health is 81 Attack?

Attacked enemy with 22 damage! current enemy health is 78 Enemy getting ready to attack! Enemy Attacked you with 19 damage! your current health is 81 Attack?

Attacked enemy with 22 damage! current enemy health is 78 Enemy getting ready to attack! Enemy Attacked you with 19 damage! your current health is 81 Attack?

As you can see its not randomizing, any fixes to this?

math.randomseed(os.time())

player_attacked = 0

enemy_attack_time = 0

enemy_attack = 0

player = {}

player.health = 100

player_health = player.health

player.damage = math.random(0,25)

player_damage = player.damage


enemy = {}

enemy.health = 100

enemy_health = enemy.health

enemy.damage = math.random(5, 30)

enemy_damage = enemy.damage

function player.init_attack()
    print('Attack? ')
    wanna_attack = io.read()
    if wanna_attack == 'y' then
        print('Attacked enemy with '..player_damage..' damage! current enemy health is '..enemy_health - player_damage)
    else
        os.exit()
   end
   player_attacked = 1
end

function enemy.init_attack()
    print('Enemy Attacked you with '..enemy_damage..' damage! your current health is '..player_health - enemy_damage)
    if player_health <= 0 then
        os.exit()
    end
end

player.init_attack()

if player_attacked == 1 then
    print('Enemy getting ready to attack!')
    enemy.init_attack()
    player_attacked = 0 
end

while player_health ~= 0 or enemy_health ~= 0 do
    player.init_attack()

    if player_attacked == 1 then
        print('Enemy getting ready to attack!')
        enemy.init_attack()
        player_attacked = 0 
    end
end

Upvotes: 1

Views: 74

Answers (2)

ad absurdum
ad absurdum

Reputation: 21318

OP code is setting player.damage and enemy.damage values to a random value once, but this random value is never changed.

To fix the problem, code must find a way to call random each time a hit is made. One solution is to design a new function, player.hit that gets a random damage value and applies it to the enemy's health, returning the damage value from the function call. Here is what that might look like:

-- Player
player = {}

player.health = 100

player.hit = function ()
   local damage = math.random(0,25)
   enemy.health = enemy.health - damage
   return damage
end

It would be better to use a table constructor to do this, like so:

-- Enemy
enemy = {
   health = 100,
   hit = function ()
      local damage = math.random(5, 30)
      player.health = player.health - damage
      return damage
   end
}

Now the remaining code needs to be modified to use player.hit and enemy.hit, which are functions, and player.health and enemy.health, which are now modified by calls to the .hit functions:

function player.init_attack()
    print('Attack? ')
    wanna_attack = io.read()
    if wanna_attack == 'y' then
       print('Attacked enemy with '.. player.hit() ..' damage! current enemy health is '.. enemy.health)
    else
        os.exit()
   end
   player_attacked = 1
end

function enemy.init_attack()
   print('Enemy Attacked you with '.. enemy.hit() ..' damage! your current health is '.. player.health)
    if player.health <= 0 then
        os.exit()
    end
end

-- ...

while player.health ~= 0 or enemy.health ~= 0 do
    player.init_attack()

Here is a test run:

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
Attack? 
y
Attacked enemy with 2 damage! current enemy health is 98
Enemy getting ready to attack!
Enemy Attacked you with 25 damage! your current health is 75
Attack? 
y
Attacked enemy with 18 damage! current enemy health is 80
Enemy getting ready to attack!
Enemy Attacked you with 24 damage! your current health is 51
Attack? 
y
Attacked enemy with 22 damage! current enemy health is 58
Enemy getting ready to attack!
Enemy Attacked you with 28 damage! your current health is 23
Attack? 
y
Attacked enemy with 20 damage! current enemy health is 38
Enemy getting ready to attack!
Enemy Attacked you with 11 damage! your current health is 12
Attack? 
y
Attacked enemy with 6 damage! current enemy health is 32
Enemy getting ready to attack!
Enemy Attacked you with 7 damage! your current health is 5
Attack? 
y
Attacked enemy with 13 damage! current enemy health is 19
Enemy getting ready to attack!
Enemy Attacked you with 17 damage! your current health is -12

Upvotes: 2

Because you're only doing player.damage = math.random(0,25) and enemy.damage = math.random(5, 30) once during initialization, instead of repeating it for each attack.

Upvotes: 1

Related Questions