2012-09-06 45 views
3

我得到这个奇怪的错误...是什么 - 得到的是一个目录 - 错误消息:

/Users/gideon/Documents/ca_ruby/rubytactoe/lib/player.rb:13:in `gets': Is a directory - spec (Errno::EISDIR) 

player_spec.rb:

require_relative '../spec_helper' 

# the universe is vast and infinite...it contains a game.... but no players 
describe "tic tac toe game" do 
    context "the player class" do 
    it "must have a human player X"do 
     player = Player.new("X") 
     STDOUT.should_receive(:puts).with("human move") 
     # player.stub(:gets).and_return("") 
     player.move_human("X") 
    end 
    it "must have a computer player O" do 
     player = Player.new("O") 
     STDOUT.should_receive(:puts).with("computer move") #what I expect to get back 
     player.stub(:gets).and_return("") #what I am sending 
     player.move_computer("O") #the actual call 
    end 
    end 
end 

player.rb

# require_relative 'drawgrid' 

class Player 

    attr_reader :boardpiece # i exist so game can read me 

    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) 
    @human_winmoves = { 
     :wm01 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm02 => {:a1=>" ", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm03 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm05 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "}, 
     :wm07 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>"X", :c3=>" "}, 
     :wm08 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"X"}, 
     #check those corners 
     :wm09 => {:a1=>"X", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm10 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm11 => {:a1=>" ", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm12 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm13 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>"X"}, 
     :wm14 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>"X", :c3=>"X"}, 
     :wm15 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>"X", :c3=>" "}, 
     :wm16 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "}, 
     #check opposites 
     :wm17 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "}, 
     :wm18 => {:a1=>" ", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>"X", :c3=>" "}, 
     :wm19 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"X"}, 
     :wm20 => {:a1=>"X", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm21 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>" ", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}, 
     :wm22 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"X"}, 
     :wm23 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>"X"}, 
     :wm24 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "} 
    } 

    @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 O for thegrid and copy those into keys_with_o 
    # 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 } 
    # puts "matching_moves: " 
    # puts matching_moves 

    # select all values where value is X for thegrid and copy those into keys_with_x 
    keys_with_x = $thegrid.select{ |k, v| v == "X" }.keys 
    block_moves = @ai_winmoves.select{ |k, v| v == "X" }.keys 

    #DEBUG puts "keys_with_x: " 
    #DEBUG puts keys_with_x.to_s 

    # human_keys = v.select{ |k, v| v == "X"}.keys 
    @intersection = block_moves & keys_with_x 

    @thing = [] # initialize thing array 

    @human_winmoves.each do |k,v| # for test - go threw each win moves. 
     #get common elements between two arrays..recall from above that v contains a hash 
     human_keys = v.select{ |k, v| v == "X"}.keys 
     @intersection = human_keys & keys_with_x 
     if $thegrid[:b2] == " " #AND center spot is empty 
     ai_spot = "b2" 
     puts "take center "+ai_spot 
     @move = ai_spot.to_sym #must return this answer as a symbol 
     return @move 
     elsif @intersection.length >= 2 
     #DEBUG puts "where does a intersect b" 
     #DEBUG puts @intersection.to_s 
     #DEBUG puts "answer key:" 
     #DEBUG puts k 
     @thing << k # adds per iteration 
     #DEBUG puts "@thing array" 
     #DEBUG puts @thing.to_s # for test - find each intersection 
     #DEBUG puts "@thing array first key" 
     #DEBUG puts @thing.last.to_s 
     #DEBUG puts "value" 
     #DEBUG puts @anskey[@thing.last] 
     #DEBUG puts "symbol?" 
     answer = @anskey[@thing.last].to_sym 
     #DEBUG puts answer.is_a?(Symbol) 
     @move = answer # for test - at last intersection value found...return it as move value 
     end 
    end # END @human_winmoves.each do |k,v| 
    return @move # had this guy in the wrong place 
    end 
end 

任何人都可以将我指向正确的方向吗?

回答

8

当你拨打gets.chomp时,你实际上是指Kernel#gets,它需要一个文件读取。如果您传递命令行参数,则它们将被解释为这些输入文件Kernel#gets所期望的。如果其中一个参数是一个目录路径,那么你会得到你提到的错误。

您想改为使用IO#gets。这将按预期工作:

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

最近我们在使用RSpec 3和'gets'时遇到了这个问题。不确定为什么有些学生碰到问题而不是其他人,但是'STDIN.gets.chomp'修复了它。 – tibbon

0

您可能会给出一个目录名作为命令行参数。

内核#从作为命令行参数给出的文件中读取数据。 您可能在命令行中提供目录名称而不是文件名。

如果在命令行没有参数,它从STDIN读取。

相关问题