Add promotion actions for chess.
[kaya.git] / lib / plugins / games / chess.rb
blob2a527de08b0f158ff2a34677f31f2882fbafecab
1 require 'games/games'
2 require 'games/chess/state'
3 require 'games/chess/move'
4 require 'games/chess/board'
5 require 'games/chess/policy'
6 require 'games/chess/animator'
7 require 'games/chess/validator'
8 require 'games/chess/serializer'
9 require 'games/chess/pgn'
10 require 'plugins/plugin'
11 require 'games/game_actions'
13 module Chess
15 class Game
16   include Plugin
17   include GameActions
18   
19   plugin :name => 'Chess',
20          :id => :chess,
21          :interface => :game,
22          :keywords => %w(chess)
23          
24   attr_reader :size, :policy, :state, :board, :move,
25               :animator, :validator, :piece, :players,
26               :types, :serializer, :game_writer,
27               :game_extensions
28               
29   def initialize
30     @size = Point.new(8, 8)
31     @state_component = State
32     @state = Factory.new(State) { State.new(board.new, move, piece) }
33     @board = Factory.new(Board) { Board.new(size) }
34     @move = Move
35     @animator = Animator
36     @validator = Validator
37     @piece = Piece
38     @policy = Factory.new { Policy.new(Move) }
39     @players = [:white, :black]
40     @types = [:pawn, :knight,:bishop, :rook, :queen, :king]
41     @serializer = Factory.new(Serializer) {|rep| 
42       Serializer.new(rep, validator, move, piece) }
43     @keywords = %w(chess)
45     @game_writer = PGN.new(serializer.new(:compact), state)
46     @game_extensions = %w(pgn)
47     
48     action :promote_to_queen,
49            :text => 'Promote to Queen' do |policy| 
50       policy.promotion = :queen
51     end
52     action :promote_to_rook, 
53            :text => 'Promote to Rook' do |policy| 
54       policy.promotion = :rook
55     end
56     action :promote_to_bishop, 
57            :text => 'Promote to Bishop' do |policy| 
58       policy.promotion = :bishop
59     end
60     action :promote_to_knight, 
61            :text => 'Promote to Knight' do |policy| 
62       policy.promotion = :knight
63     end
64   end
65   
66   def game_reader
67     @game_writer
68   end
69   
70   def actions(parent, collection, policy)
71     acts = super
72     group = Qt::ActionGroup.new(parent)
73     group.exclusive = true
74     acts.each do |act| 
75       act.checkable = true
76       act.action_group = group
77     end
78     acts.first.checked = true
79     acts
80   end
81 end
83 end
85 module Chess5x5
87 class Game < Chess::Game
88   plugin :name => 'Chess 5x5',
89          :id => :chess5x5,
90          :interface => :game,
91          :keywords => %w(chess)
92   
93   def initialize
94     super
95     @size = Point.new(5, 5)
96     @game_extensions = []
97   end
98   
99 end