Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 口袋妖怪战斗计划中的偶然无限循环_Ruby_Infinite Loop - Fatal编程技术网

Ruby 口袋妖怪战斗计划中的偶然无限循环

Ruby 口袋妖怪战斗计划中的偶然无限循环,ruby,infinite-loop,Ruby,Infinite Loop,这只在程序运行时发生约15%。我通过“counter”变量将“轮数”限制为10,从而停止了无止境的终端循环。如果计数器不停止循环,它会将攻击伤害降低为负指数值(例如1.80e-80),并停止从生命值中减去 pokemon = ['Bulbasaur', 'Charizard', 'Squirtle'] player_one_selection = rand(3) player_two_selection = rand(3) puts player_one_selection counter

这只在程序运行时发生约15%。我通过“counter”变量将“轮数”限制为10,从而停止了无止境的终端循环。如果计数器不停止循环,它会将攻击伤害降低为负指数值(例如1.80e-80),并停止从生命值中减去

pokemon = ['Bulbasaur', 'Charizard', 'Squirtle']


player_one_selection = rand(3)
player_two_selection = rand(3)

puts player_one_selection
counter = 0
until player_one_selection != player_two_selection
    counter += 1
    player_two_selection = rand(3)
    print "#{counter} "
end

puts player_two_selection

player_one_pkmn = pokemon[player_one_selection]
player_two_pkmn = pokemon[player_two_selection]


puts player_one_pkmn
puts player_two_pkmn

p1_dm = 1
p2_dm = 1

if player_one_pkmn == 'Bulbasaur' && player_two_pkmn == 'Charizard'
    p1_dm = 0.9
    p2_dm = 1.1
end
if player_one_pkmn == 'Squirtle' && player_two_pkmn == 'Charizard'
    p1_dm = 1.1
    p2_dm = 0.9
end
if player_one_pkmn == 'Bulbasaur' && player_two_pkmn == 'Squirtle'
    p1_dm = 1.1
    p2_dm = 0.9
end
if player_one_pkmn == 'Squirtle' && player_two_pkmn == 'Bulbasaur'
    p1_dm = 0.9
    p2_dm = 1.1
end
if player_one_pkmn == 'Charizard' && player_two_pkmn == 'Squirtle'
    p1_dm = 0.9
    p2_dm = 1.1
end
if player_one_pkmn == 'Charizard' && player_two_pkmn == 'Bulbasaur'
    p1_dm = 1.1
    p2_dm = 0.9
end

jugador_uno = {
    "pokemon"           => player_one_pkmn,
    "pokemon-type"      => "",
    "pokemon-health"    => 100,
    "damage-modifier"   => p1_dm,
    "pokemon-lvl"       => rand(5)+5,
    "current-attack" => ["",0]
}
jugador_dos = {
    "pokemon"           => player_two_pkmn,
    "pokemon-type"      => "",
    "pokemon-health"    => 100,
    "damage-modifier"   => p2_dm,
    "pokemon-lvl"       => rand(5)+5,
    "current-attack" => ["",0]
}

puts jugador_uno
puts jugador_dos

puts "\nReady to Battle! \n"

attacks = [
    ['tackle', 20], ['leer', 5], ['bite', 35], ['special-attack', 50]
]
counter = 0
# until jugador_uno['pokemon-health'] <= 0 || jugador_dos['pokemon-health'] <= 0
loop do
    jugador_uno['current-attack'] = attacks[rand(4)]
    jugador_dos['current-attack'] = attacks[rand(4)]

    jugador_uno['current-attack'][1] = jugador_uno['current-attack'][1] * jugador_uno['pokemon-lvl'] / 10 * jugador_uno['damage-modifier']
    jugador_dos['current-attack'][1] = jugador_dos['current-attack'][1] * jugador_dos['pokemon-lvl'] / 10 * jugador_dos['damage-modifier']

    puts "Lvl. #{jugador_uno['pokemon-lvl']} #{jugador_uno['pokemon']} attack: #{jugador_uno['current-attack'][0].capitalize} did #{jugador_uno['current-attack'][1]} damage."
    puts "Lvl. #{jugador_dos['pokemon-lvl']} #{jugador_dos['pokemon']} attack: #{jugador_dos['current-attack'][0].capitalize} did #{jugador_dos['current-attack'][1]} damage.\n"

    jugador_dos['pokemon-health'] -= jugador_uno['current-attack'][1]
    jugador_uno['pokemon-health'] -= jugador_dos['current-attack'][1]

    puts "\n#{jugador_uno['pokemon']} health: #{jugador_uno['pokemon-health']}"
    puts "#{jugador_dos['pokemon']} health: #{jugador_dos['pokemon-health']}\n"

    counter += 1

    break if jugador_uno['pokemon-health'] <= 0 || jugador_dos['pokemon-health'] <= 0 || counter > 10
end
假设您的问题是“为什么这个程序无限循环?”,那么答案是在某些情况下,攻击数组中的值:

attacks = [
    ['tackle', 20], ['leer', 5], ['bite', 35], ['special-attack', 50]
]
逐渐减少到0

通过修改上述代码以打印出攻击中的值,您可以自己查看:

loop do
  puts "attack choices: #{attacks}"

  jugador_uno['current-attack'] = attacks[rand(4)]
  jugador_dos['current-attack'] = attacks[rand(4)]

  jugador_uno['current-attack'][1] = jugador_uno['current-attack'][1] * jugador_uno['pokemon-lvl'] / 10 * jugador_uno['damage-modifier']
  jugador_dos['current-attack'][1] = jugador_dos['current-attack'][1] * jugador_dos['pokemon-lvl'] / 10 * jugador_dos['damage-modifier']

  # ...
end
您会发现,在第一次迭代中,攻击包含初始值
['trick',20],'leer',5],'bite',35],'special-attack',50]
,但后续迭代将用更新的值替换它们

比如说,

attack choices: [["tackle", 20], ["leer", 5], ["bite", 35], ["special-attack", 50]]
attack choices: [["tackle", 20], ["leer", 5], ["bite", 10.395000000000001], ["special-attack", 50]]
attack choices: [["tackle", 20], ["leer", 2.7], ["bite", 1.6980232500000005], ["special-attack", 50]]

# bunch of iterations later

attack choices: [["tackle", 5.219269710359129e-11], ["leer", 2.46107539497387e-11], ["bite", 2.5860511581722106e-16], ["special-attack", 9.938279578366043e-12]]
将在以下两行代码中替换这些值:

jugador_uno['current-attack'][1] = jugador_uno['current-attack'][1] * jugador_uno['pokemon-lvl'] / 10 * jugador_uno['damage-modifier']
jugador_dos['current-attack'][1] = jugador_dos['current-attack'][1] * jugador_dos['pokemon-lvl'] / 10 * jugador_dos['damage-modifier']

jugador_uno['damage-modifier']
jugador_dos['damage-modifier']
小于1(例如,0.9)时,您将遇到问题。

虽然这并不能回答您的问题,但为两名玩家获得不同随机选择的更简单方法是使用:
[0,1,2]。示例(2)
。请将代码减少到演示问题所需的最低限度。谢谢,我没有考虑重置值以使它们不会减少,但我觉得这将在以后处理,一旦我不再制作简单的口袋妖怪游戏,哈哈。@hjing,我不能投票支持你,因为我没有足够的声誉,我不知道如何将此标记为已回答,但您肯定给出了一个很好的答案,非常感谢!
jugador_uno['current-attack'][1] = jugador_uno['current-attack'][1] * jugador_uno['pokemon-lvl'] / 10 * jugador_uno['damage-modifier']
jugador_dos['current-attack'][1] = jugador_dos['current-attack'][1] * jugador_dos['pokemon-lvl'] / 10 * jugador_dos['damage-modifier']