带有哈希数组的数组会破坏ruby中的代码

时间:2013-06-10 14:18:26

标签: ruby arrays multidimensional-array hash

我是新的红宝石并致力于终端游戏。 我有一个deck_of_cards的数组

deck_of_cards =[ {:card=>"2C",:value=>2}, {:card=>"3C",:value=>3}, {:card=>"4C",:value=>4}, {:card=>"5C",:value=>5}, {:card=>"6C",:value=>6}, {:card=>"7C",:value=>7}, {:card=>"8C",:value=>8}, {:card=>"9C",:value=>9}, {:card=>"10C",:value=>10}, {:card=>"JC",:value=>11}, {:card=>"QC",:value=>12}, {:card=>"KC",:value=>13}, {:card=>"AC",:value=>14},
             {:card=>"2D",:value=>2}, {:card=>"3D",:value=>3}, {:card=>"4D",:value=>4}, {:card=>"5D",:value=>5}, {:card=>"6D",:value=>6}, {:card=>"7D",:value=>7}, {:card=>"8D",:value=>8}, {:card=>"9D",:value=>9}, {:card=>"10D",:value=>10}, {:card=>"JD",:value=>11}, {:card=>"QD",:value=>12}, {:card=>"KD",:value=>13}, {:card=>"AD",:value=>14},
             {:card=>"2H",:value=>2}, {:card=>"3H",:value=>3}, {:card=>"4H",:value=>4}, {:card=>"5H",:value=>5}, {:card=>"6H",:value=>6}, {:card=>"7H",:value=>7}, {:card=>"8H",:value=>8}, {:card=>"9H",:value=>9}, {:card=>"10H",:value=>10}, {:card=>"JH",:value=>11}, {:card=>"QH",:value=>12}, {:card=>"KH",:value=>13}, {:card=>"AH",:value=>14},
             {:card=>"2S",:value=>2}, {:card=>"3S",:value=>3}, {:card=>"4S",:value=>4}, {:card=>"5S",:value=>5}, {:card=>"6S",:value=>6}, {:card=>"7S",:value=>7}, {:card=>"8S",:value=>8}, {:card=>"9S",:value=>9}, {:card=>"10S",:value=>10}, {:card=>"JS",:value=>11}, {:card=>"QS",:value=>12}, {:card=>"KS",:value=>13}, {:card=>"AS",:value=>14}]

然后我将deck_of_cards洗牌并通过.slice交易给player_1 = []和player_2 = []!

我的问题是我运行逻辑测试后

until player1 == [] || player2 == []
    player1_battle = []
    player2_battle = []
    player1_battle_value = []
    player2_battle_value = []
    puts player1.slice(0).class
    puts player2.slice(0).class
    player1_battle << player1.slice!(0)
    player2_battle << player2.slice!(0)
    puts player1_battle.class
    puts player2_battle.class

    player1_battle_value << player1_battle.map{|h| h[:value].to_i}
    player2_battle_value << player2_battle.map{|h| h[:value].to_i}
    puts "Dealer draws #{player1_battle}" #shows each players card on the table
    puts "You draw #{player2_battle}" #takes top card out of player array
    puts player1_battle_value
    puts player2_battle_value
    puts player1_battle_value <=> player2_battle_value
    battle_value = player1_battle_value <=> player2_battle_value
    puts battle_value
     if battle_value == 1
      player1 << player1_battle
      player1 << player2_battle
      puts "Dealer Wins!"

我的player_1阵列然后在

处中断
player1_battle_value << player1_battle.map{|h| h[:value].to_i}

一旦player_1阵列变为

[[{:card=>"KD", :value=>13}], [{:card=>"AC", :value=>14}], [{:card=>"4S", :value=>4}]]

现在第一个索引和带有散列的数组.map方法分解为

:in `[]': can't convert Symbol into Integer (TypeError)
from terminal_war.rb:65:in `block in <main>'
from terminal_war.rb:65:in `map'
from terminal_war.rb:65:in `<main>'

我知道我的代码不是很好,但有没有更好的方法来测试:对着:card的值,并在循环后24-26次保持代码不被破坏?一旦数组中的所有哈希变为具有哈希值的数组,代码就会中断。

我希望我提供了足够的信息,谢谢。

1 个答案:

答案 0 :(得分:0)

player1_battle_value << player1_battle.map{|h| h[:value].to_i}
     

一旦player_1阵列变为

[{:card=>"KD", :value=>13}, [{:card=>"AC", :value=>14}], [{:card=>"4S", :value=>4}]]

你不应该使用concat吗?

player1_battle_value.concat(player1_battle.map{|h| h[:value].to_i})