2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
13 #include <core/behaviour.h>
14 #include <core/move.h>
15 #include <core/defaultpoolcollection.h>
16 #include <core/defaultpool.h>
23 State::State(const IBehaviour
* behaviour
,
26 , m_behaviour(behaviour
)
28 DefaultPoolCollection
* c
= new DefaultPoolCollection
;
29 for (int i
= 0; i
< 2; i
++)
30 c
->addPool(COLORS
[i
], new DefaultPool(COLORS
[i
]));
34 State::State(const State
& other
)
37 , m_board(other
.m_board
)
38 , m_flags(other
.m_flags
)
39 , m_turn(other
.m_turn
)
40 , m_behaviour(other
.m_behaviour
)
42 , m_pools(other
.m_pools
) { }
44 State::~State() { delete m_pools
; }
46 IState
* State::clone() const {
47 State
* s
= new State(*this);
48 s
->m_pools
= m_pools
->clone();
52 #define COL(i, c) c == Black::self() ? (i) : (m_board.size().x - i - 1)
54 for (int c
= 0; c
< 2; c
++) {
55 IColor
* color
= COLORS
[c
];
56 int r0
= rank(0, color
);
57 int r1
= rank(1, color
);
58 int r2
= rank(2, color
);
60 for (int i
= 0; i
< m_board
.size().x
; i
++) {
61 m_board
.set(Point(i
, r2
), Piece(color
, Pawn::self()));
63 m_board
.set(Point(0, r0
), Piece(color
, Lance::self()));
64 m_board
.set(Point(1, r0
), Piece(color
, Knight::self()));
65 m_board
.set(Point(2, r0
), Piece(color
, Silver::self()));
66 m_board
.set(Point(3, r0
), Piece(color
, Gold::self()));
67 m_board
.set(Point(4, r0
), Piece(color
, King::self()));
68 m_board
.set(Point(5, r0
), Piece(color
, Gold::self()));
69 m_board
.set(Point(6, r0
), Piece(color
, Silver::self()));
70 m_board
.set(Point(7, r0
), Piece(color
, Knight::self()));
71 m_board
.set(Point(8, r0
), Piece(color
, Lance::self()));
73 m_board
.set(Point(COL(1, color
), r1
), Piece(color
, Bishop::self()));
74 m_board
.set(Point(COL(7, color
), r1
), Piece(color
, Rook::self()));
77 m_turn
= White::self();
81 const Board
* State::board() const {
85 Board
* State::board() {
89 const IColor
* State::turn() const {
93 void State::setTurn(const IColor
* turn
) {
97 bool State::equals(IState
* other
) const {
98 return m_board
.equals(other
->board()) &&
99 m_turn
== other
->turn() &&
100 m_flags
== *other
->flags();
103 void State::assign(const IState
* other
) {
104 m_board
= *other
->board();
105 m_turn
= other
->turn();
106 m_flags
= *other
->flags();
108 const IPoolCollection
* pools
= other
->pools();
111 m_pools
= pools
->clone();
115 void State::move(const Move
& m
) {
116 if (m
.drop() != Piece()) {
117 const Piece
* captured
= board()->get(m
.dst());
118 board()->set(m
.dst(), m
.drop());
119 pools()->pool(m
.drop().color())->take(m
.drop());
122 // handle capturing by drop: some variants could use it
123 if (captured
!= NULL
&& *captured
!= Piece()) {
124 if (captured
->get("promoted").toBool()) {
125 captured
->setType(pawn
);
127 pools()->pool(behaviour()->opponent(captured
->color()))
128 ->insert(-1, *captured
);
132 const Piece
* piece
= m_board
.get(m
.src());
133 if (piece
== NULL
|| *piece
== Piece()) return;
135 Point captureSquare
= behaviour()->captureSquare(this, m
);
136 behaviour()->captureOn(m_delegator
, captureSquare
);
137 behaviour()->move(m_delegator
, m
);
139 const IType
* promotion_type
= m
.promotion();
141 if (promotion_type
!= 0) {
142 m_board
.set(m
.dst(), Piece(piece
->color(), promotion_type
));
146 behaviour()->advanceTurn(m_delegator
);
149 const TaguaObject
* State::flags() const { return &m_flags
; }
150 TaguaObject
* State::flags() { return &m_flags
; }
152 int State::rank(int n
, const IColor
* turn
) const {
153 if (turn
== White::self())
154 return m_board
.size().y
- n
- 1;
159 IPoolCollection
* State::pools() { return m_pools
; }
160 const IPoolCollection
* State::pools() const { return m_pools
; }
162 const IBehaviour
* State::behaviour() const { return m_behaviour
; }
164 void State::setDelegator(IState
* delegator
) { m_delegator
= delegator
; }
166 Component
* State::clone(const IBehaviour
* behaviour
,
167 const Point
& size
) const {
168 return new State(behaviour
, size
);