Change Board::get() to return a pointer.
[tagua/yd.git] / src / variants / chess / pawn.cpp
blob78856cecd37f3d7a3a0fdaa9d95ef68fc6d6cc9f
1 /*
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.
9 */
11 #include "pawn.h"
12 #include "queen.h"
13 #include <core/behaviour.h>
14 #include <core/move.h>
15 #include <core/pathinfo.h>
16 #include <core/state.h>
17 #include <core/board.h>
18 #include <core/taguacast.h>
20 namespace Chess {
22 Pawn::Pawn() { }
24 QString Pawn::name() const { return "pawn"; }
26 bool Pawn::canMove(const Piece& piece, const Piece& target,
27 Move& move, const IState* state) const {
28 const IBehaviour* behaviour = state->behaviour();
29 if (!behaviour) return false;
30 bool enPassant = state->flags()->get("en_passant_square").value<Point>() == move.dst();
32 // moving
33 if (target == Piece() && !enPassant) {
34 if (move.delta() == behaviour->direction(piece.color())) {
35 if (move.dst().y == state->rank(state->board()->size().y - 1, piece.color())) {
36 setPromotion(move, Queen::self());
38 return true;
41 if (move.src().y == state->rank(1, piece.color()) &&
42 move.delta() == behaviour->direction(piece.color()) * 2 &&
43 (state->board()->get(move.src() + behaviour->direction(piece.color())) == NULL ||
44 *state->board()->get(move.src() + behaviour->direction(piece.color())) == Piece())) {
45 move.setType("en_passant_trigger");
46 return true;
48 else
49 return false;
52 // capturing
53 else if (enPassant || target.color() != piece.color()) {
54 if (move.delta().y == behaviour->direction(piece.color()).y &&
55 abs(move.delta().x) == 1) {
56 if (enPassant)
57 move.setType("en_passant_capture");
58 else if (move.dst().y == state->rank(state->board()->size().y - 1, piece.color())) {
59 setPromotion(move, Queen::self());
62 return true;
66 return false;
69 void Pawn::setPromotion(Move& move, const IType* type) const {
70 move.setType("promotion");
71 // FIXME use variant data to set promotion type, like e.g.:
72 // move.setPromotion(m_data.get("promotion-type").value<QObject*>())
73 if (!move.promotion()) move.setPromotion(type);
76 int Pawn::index() const { return 10; }
78 Pawn* Pawn::self() {
79 static Pawn s_instance;
80 return &s_instance;