Class: Elus::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/elus/solver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin, stdout) ⇒ Solver

Returns a new instance of Solver.



5
6
7
8
# File 'lib/elus/solver.rb', line 5

def initialize(stdin, stdout)
  @stdin = stdin  
  @stdout = stdout
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



3
4
5
# File 'lib/elus/solver.rb', line 3

def game
  @game
end

Instance Method Details

#hintObject



81
# File 'lib/elus/solver.rb', line 81

def hint; @game.hint end

#input_piece(options = {}) ⇒ Object

Inputs single correct code from stdin, returns correct piece or nil if break requested. Rejects wrong codes.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/elus/solver.rb', line 39

def input_piece(options = {})
  loop do
    @stdout.puts options[:prompt] || "Enter code:"
    code = @stdin.gets
    return nil if code == "\n"
    if piece = Piece.create(code)
      return piece
    else
      @stdout.puts options[:failure] || "Invalid code: #{code}"
    end
  end
end

#input_pieces(label, num = 10) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/elus/solver.rb', line 29

def input_pieces(label, num=10)
  pieces = []
  while pieces.size < num and piece = input_piece(:prompt => "Enter #{label} Piece code (#{pieces.size+1}):") do
    @stdout.puts "You entered #{label} Piece (#{pieces.size+1}): #{piece.name}"
    pieces << piece 
  end
  pieces
end

#input_state(codes = nil) ⇒ Object

Inputs Game state either from given code string or interactively (from @stdin)



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/elus/solver.rb', line 17

def input_state(codes=nil)
  if codes
    pieces = codes.split("\n").map {|code| Piece.create(code)}.compact
    free=pieces[0..2]
    board=pieces[3..-1]
  else  
    free = input_pieces "Free", 3
    board = input_pieces "Board"
  end
  @game = Game.new(:free=>free, :board=>board, :generator=>@generator)
end

#make_moveObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elus/solver.rb', line 58

def make_move
  @stdout.puts @game.state
  @stdout.puts @game.hint
  piece = input_piece(:prompt => "Make your move:")
  if piece and @game.valid_move? piece
    @stdout.puts "You moved: #{piece.name}"
    @stdout.puts "Was the move right(Y/N)?:"
    if @stdin.gets =~ /[Yy]/
      @stdout.puts "Great, now enter new Free set:"
      free = input_pieces "Free", 3
      @game.move piece, free
    else
      @stdout.puts "Too bad"
      @game.move piece
    end
  elsif piece
    @stdout.puts "Wrong move (not in free set): #{piece.name}"
  else 
    @stdout.puts "Wrong move (no piece given)"
  end
end

#make_movesObject



52
53
54
55
56
# File 'lib/elus/solver.rb', line 52

def make_moves
  while not @game.finished?
    make_move
  end
end

#start(generator) ⇒ Object



10
11
12
13
14
# File 'lib/elus/solver.rb', line 10

def start (generator)
  @generator = generator
  @stdout.puts "Welcome to Elus Solver!"
  @stdout.puts "Enter Game state:"
end

#stateObject



80
# File 'lib/elus/solver.rb', line 80

def state; @game.state end