Add program entry point and a MainWindow class.
[kaya.git] / lib / history.rb
blobcdd42dd0c32f13614b616c80f04972cc60128762
1 require 'observer_utils.rb'
3 class History
4   include Enumerable
5   include Observer
6   
7   attr_reader :current
8   
9   Item = Struct.new(:state, :move)
10   OutOfBound = Class.new(Exception)
12   def initialize(state)
13     @history = [Item.new(state.dup, nil)]
14     @current = 0
15   end
16   
17   def each
18     @history.each {|item| yield item.state, item.move }
19   end
20   
21   def add_move(state, move)
22     item = Item.new(state.dup, move)
23     @history = @history[0..@current]
24     @history << item
25     @current = @history.size - 1
26   end
27   
28   def forward
29     raise OutOfBound if @current >= @history.size - 1
30     @current += 1
31     item = @history[@current]
32     [item.state, item.move]
33   end
34   
35   def back
36     raise OutOfBound if @current <= 0
37     move = @history[@current].move
38     @current -= 1
39     [@history[@current].state, move]
40   end
41   
42   def state
43     @history[current].state
44   end
45   
46   def move
47     @history[current].move
48   end
49   
50   def size
51     @history.size
52   end
53 end