0

だから私はハッシュの問題を抱えています...私は思います:

比較しているハッシュが2つあります。コードのある時点で、次のような応答が返されます。

@moves = {:wm01 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}

これは、回答キーのハッシュに対応します。

@anskey = {
    :wm01=>":c3",:wm02=>":c2",:wm03=>":c1",:wm04=>":b3",:wm05=>":b1",:wm06=>":a3",:wm07=>":a2",:wm08=>":a1",
    :wm09=>":a3",:wm10=>":c1",:wm11=>":a1",:wm12=>":c3",:wm13=>":c3",:wm14=>":c1",:wm15=>":c3",:wm16=>":a1",
    :wm17=>":b1",:wm18=>":b2",:wm19=>":b3",:wm20=>":a2",:wm21=>":b2",:wm22=>":b2",:wm23=>":c2",:wm24=>":b2"
  }

この場合、@ anskeyを繰り返す必要があります。ここで、@ moves.keys == @ anskey.keys

(十分に単純なようですが、機能していないようです)

そして設定...

my_answer_is = @anskey.value

だからこのようなもの?

@anskey.each do |key. value|
  if @moves.keys == key
     my_answer_is = @value
  end
end

以下の問題のあるコード

class Player

  attr_reader :boardpiece

  def initialize(letter)
    @boardpiece = letter
  end

  def move_human(game)
    puts "human move..."
    @move = gets.chomp
  end

  def move_computer(game)
    puts "computer move..."

    # all possible third moves as 'O' (computer)
    @ai_winmoves = {
        :wm01 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm02 => {:a1=>" ", :a2=>"O", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm03 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm05 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "},
        :wm07 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>"O", :c3=>" "},
        :wm08 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"O"},
        #check those corners
        :wm09 => {:a1=>"O", :a2=>"O", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm10 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm11 => {:a1=>" ", :a2=>"O", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm12 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm13 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>"O"},
        :wm14 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>"O", :c3=>"O"},
        :wm15 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>"O", :c3=>" "},
        :wm16 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "},
        #check opposites
        :wm17 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "},
        :wm18 => {:a1=>" ", :a2=>"O", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>"O", :c3=>" "},
        :wm19 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"O"},
        :wm20 => {:a1=>"O", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm21 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>" ", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>" "},
        :wm22 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"O"},
        :wm23 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>"O"},
        :wm24 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "}
      }
    # match current answers located in @thegrid with possible @anskey array, iterate for each item
    @anskey={
        :wm01=>":c3",:wm02=>":c2",:wm03=>":c1",:wm04=>":b3",:wm05=>":b1",:wm06=>":a3",:wm07=>":a2",:wm08=>":a1",
        :wm09=>":a3",:wm10=>":c1",:wm11=>":a1",:wm12=>":c3",:wm13=>":c3",:wm14=>":c1",:wm15=>":c3",:wm16=>":a1",
        :wm17=>":b1",:wm18=>":b2",:wm19=>":b3",:wm20=>":a2",:wm21=>":b2",:wm22=>":b2",:wm23=>":c2",:wm24=>":b2"
      }
    #
    # scan board for available move locations

    # select all values where value is X for thegrid and copy those into keys_with_x

    # compare result to ai_winmoves
    keys_with_o = $thegrid.select{ |k, v| v == "O" }.keys
    matching_moves = @ai_winmoves.select{ |k, v| v.select{ |k, v| v == "O" }.keys == keys_with_o }
    # if matching_moves.keys contains anything with wm print results
    test_str = matching_moves.keys.to_s
    win_key  = matching_moves.keys
    # str = [:wm21]
    puts "keys with o:"
    puts keys_with_o
    puts "matching:"
    puts win_key

THIS IS THE AREA THAT BOTHERS ME

    if test_str =~ /wm/ #match found then...
      puts "WIN DETECTED" #TODO - which player won?
      puts win_key
      puts test_str
      # which answer is it?
      @anskey.each do |key, value|
        if matching_moves.keys == key
          puts "answer:"
          @move = @anskey[value]
          puts @move
          exit
        else
          puts "error in if else loop"
        end
      end
    else

END AREA THAT BOTHERS ME

      # scan board for available moves... 
      available_moves = $thegrid.select{ |k, v| v == " " }.keys
      # then make a random move
      puts "availble:"
      puts available_moves
      @move = available_moves[rand(available_moves.length)]
      # @move = "b2"
    end
  end
end
4

2 に答える 2

5

文字列は記号ではありません。

ハッシュの1つに、のような文字列がありますが、これは*":c3"と等しくなりません。:c3

.to_symを使用して文字列を記号に変換したり、を使用して記号を文字列に変換したりできます.to_s

Maxが言うように、記号の代わりに文字列を格納する(良い?)理由はありません。コードを変更できる場合は、キーだけでなく値として記号を使用し、変換を完全にスキップします。これが望ましい、IMO。

* Railsを使用しているか、。と同等のものを使用している場合を除きますHashWithIndifferentAccess

于 2012-08-17T23:26:44.650 に答える
0

あなたが言うとき、あなた@moves.keysはのすべてのキーの配列を取得します@moves

この行if @moves.keys == keyでは、1要素配列[:wm01]をシンボルと比較しようとしていますが:wm01、タイプが異なるため、同じになることはありません。

@movesその時点でコードにキーが1つしかないことがわかっている場合は、を実行するだけでそれを取得できます@moves.keys.first@anskey次に、そのキーをループする代わりに、そのキーを使用してインデックスを作成します。

@anskey[@moves.keys.first]

すべてのキーが等しいかどうかをチェックするハッシュをループするべきではありません。ハッシュのポイントは、hash[key]構文がkeyループを必要とせずにの値を提供することです。

于 2012-08-18T00:55:39.133 に答える