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/move.h>
19 State::State(const Point
& size
)
22 State::State(const State
& other
)
25 , m_board(other
.m_board
)
26 , m_flags(other
.m_flags
)
27 , m_turn(other
.m_turn
) { }
29 IState
* State::clone() const {
30 return new State(*this);
34 for (int c
= 0; c
< 2; c
++) {
35 IColor
* color
= COLORS
[c
];
36 int r0
= rank(0, color
);
37 int r1
= rank(1, color
);
39 for (int i
= 0; i
< m_board
.size().x
; i
++) {
40 m_board
.set(Point(i
, r1
), Piece(color
, Pawn::self()));
42 m_board
.set(Point(0, r0
), Piece(color
, Rook::self()));
43 m_board
.set(Point(1, r0
), Piece(color
, Knight::self()));
44 m_board
.set(Point(2, r0
), Piece(color
, Bishop::self()));
45 m_board
.set(Point(3, r0
), Piece(color
, Queen::self()));
46 m_board
.set(Point(4, r0
), Piece(color
, King::self()));
47 m_board
.set(Point(5, r0
), Piece(color
, Bishop::self()));
48 m_board
.set(Point(6, r0
), Piece(color
, Knight::self()));
49 m_board
.set(Point(7, r0
), Piece(color
, Rook::self()));
52 m_turn
= White::self();
55 const Board
* State::board() const {
59 Board
* State::board() {
63 const IColor
* State::turn() const {
67 void State::setTurn(const IColor
* turn
) {
71 bool State::equals(IState
* other
) const {
72 return m_board
.equals(other
->board()) &&
73 m_turn
== other
->turn() &&
74 m_flags
== other
->flags();
77 void State::assign(const IState
* other
) {
78 m_board
= *other
->board();
79 m_turn
= other
->turn();
80 m_flags
= other
->flags();
83 void State::move(const Move
& m
) {
84 Piece piece
= m_board
.get(m
.src());
85 if (piece
== Piece()) return;
87 captureOn(captureSquare(m
));
90 m_flags
.set("en_passant_square", QVariant(enPassantTrigger(m
)));
91 const IType
* promotion_type
= m
.promotion();
93 if (promotion_type
!= 0) {
94 m_board
.set(m
.dst(), Piece(piece
.color(), promotion_type
));
97 // handleCastling(piece, m); // FIXME
101 TaguaObject
State::flags() const {
105 Point
State::direction(const IColor
* turn
) const {
106 return Point(0, turn
== White::self() ? -1 : 1);
109 void State::basicMove(const Move
& move
) {
110 if (move
.dst() != move
.src()) {
111 m_board
.set(move
.dst(), m_board
.get(move
.src()));
112 m_board
.set(move
.src(), Piece());
116 void State::captureOn(const Point
& square
) {
117 m_board
.set(square
, Piece());
120 int State::rank(int n
, const IColor
* turn
) const {
121 if (turn
== White::self())
122 return m_board
.size().y
- n
- 1;
127 Point
State::captureSquare(const Move
& move
) const {
128 if (move
.type() == "en_passant_capture")
129 return Point(move
.dst().x
, move
.src().y
);
134 void State::advanceTurn() {
135 m_turn
= opponent(m_turn
);
139 const IColor
* State::opponent(const IColor
* color
) const {
140 return color
== White::self() ?
141 static_cast<IColor
*>(Black::self()) :
142 static_cast<IColor
*>(White::self());
145 Point
State::enPassantTrigger(const Move
& move
) const {
146 if (move
.type() == "en_passant_trigger")
147 return (move
.dst() + move
.src()) / 2;
149 return Point::invalid();