1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'observer_utils.rb'
16 Item = Struct.new(:state, :move, :text)
17 OutOfBound = Class.new(Exception)
20 @history = [Item.new(state.dup, nil, "Mainline")]
25 @history.each {|item| yield item.state, item.move }
28 def add_move(state, move)
29 item = Item.new(state.dup, move, nil)
30 old_size = @history.size
32 @history = @history[0..@current]
35 @current = @history.size - 1
41 raise OutOfBound if @current >= @history.size - 1
43 item = @history[@current]
46 [item.state, item.move]
50 raise OutOfBound if @current <= 0
51 move = @history[@current].move
55 [@history[@current].state, move]
63 [item.state, item.move]
76 @history[current].state
80 @history[current].move
88 if index >= @history.size || index < 0