0

我是新红宝石,正在开发终端游戏。我有一组我的 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 的 :value 并防止代码在循环 24-26 次后中断?一旦数组中的所有散列成为带有散列的数组,代码就会中断。

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

4

1 回答 1

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})
于 2013-06-10T16:30:08.263 回答