Restore game actions.
[kaya.git] / lib / premover.rb
blobc373849e6a9203ff399293d99d962e0b169490cf
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
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 class Premover
9   attr_reader :index
10   
11   # Create a premover object
12   # executor: an object with execute_move and execute_direct_drop methods
13   # board: a graphical board
14   # pools: a hash of graphical pools
15   # 
16   def initialize(executor, board, pools)
17     @executor = executor
18     @board = board
19     @pools = pools
20   end
21   
22   def execute
23     if @board.premove_dst
24       if @board.premove_src
25         @executor.execute_move(@board.premove_src, @board.premove_dst)
26       else
27         @pools.each do |color, pool|
28           if pool.premove_src
29             @executor.execute_direct_drop(color, pool.premove_src, @board.premove_dst)
30             break
31           end
32         end
33       end
34     end
35     cancel
36   end
38   def move(move_index, src, dst)
39     cancel
40     @index = move_index
41     if src != dst
42       @board.premove(src, dst)
43     end
44   end
45   
46   def drop(move_index, color, index, dst)
47     cancel
48     @index = move_index
49     @board.premove_dst = dst
50     @pools[color].premove_src = index
51   end
52   
53   def cancel
54     @board.cancel_premove
55     @pools.each do |color, pool|
56       pool.premove_src = nil
57     end
58     @index = nil
59   end
60 end