Add shogi serializer.
[kaya.git] / lib / plugins / games / chess.rb
blobcb2544003ed40e1a4150d8ddb8efc2a85d053519
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, :notation
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     @notation = SAN.new(piece, size)
42     @serializer = Factory.new(Serializer) {|rep| 
43       Serializer.new(rep, validator, move, piece, notation) }
44     @keywords = %w(chess)
46     @game_writer = PGN.new(serializer.new(:compact), state)
47     @game_extensions = %w(pgn)
48     
49     action :promote_to_queen,
50            :text => 'Promote to &Queen' do |policy| 
51       policy.promotion = :queen
52     end
53     action :promote_to_rook, 
54            :text => 'Promote to &Rook' do |policy| 
55       policy.promotion = :rook
56     end
57     action :promote_to_bishop, 
58            :text => 'Promote to &Bishop' do |policy| 
59       policy.promotion = :bishop
60     end
61     action :promote_to_knight, 
62            :text => 'Promote to &Knight' do |policy| 
63       policy.promotion = :knight
64     end
65   end
66   
67   def game_reader
68     @game_writer
69   end
70   
71   def actions(parent, collection, policy)
72     acts = super
73     group = Qt::ActionGroup.new(parent)
74     group.exclusive = true
75     acts.each do |act| 
76       act.checkable = true
77       act.action_group = group
78     end
79     acts.first.checked = true
80     acts
81   end
82 end
84 end
86 module Chess5x5
88 class Game < Chess::Game
89   plugin :name => 'Chess 5x5',
90          :id => :chess5x5,
91          :interface => :game,
92          :keywords => %w(chess)
93   
94   def initialize
95     super
96     @size = Point.new(5, 5)
97     @game_extensions = []
98   end
99