Implement ActionList::Entry#clear.
[kaya.git] / lib / games / state_base.rb
blob5ddc8feeb0ba91400ca3d8a6a655443adfbdc0b7
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 StateBase
9   attr_accessor :turn
10   attr_accessor :board
11   attr_reader :move_factory, :piece_factory
13   def initialize(board, move_factory, piece_factory)
14     @board = board
15     @move_factory = move_factory
16     @piece_factory = piece_factory
17   end
18   
19   def initialize_copy(other)
20     @board = other.board.dup
21   end
22   
23   def try(move)
24     tmp = dup
25     tmp.perform! move
26     yield tmp
27   end
28   
29   def basic_move(move)
30     captured = @board[move.dst]
31     @board[move.dst] = @board[move.src]
32     @board[move.src] = nil
33     captured
34   end
35   
36   def promote_on!(p, type)
37     if @board[p]
38       @board[p] = piece_factory.new(@board[p].color, type)
39     end
40   end
41   
42   def to_s
43     [@board.to_s,
44      "turn #{@turn.to_s}"].join("\n")
45   end
47   def ==(other)
48     @board == other.board &&
49       @turn == other.turn
50   end
51 end