2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 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.
11 #include "gameentity.h"
13 #include <core/policy.h>
14 #include <core/state.h>
15 #include <core/validator.h>
16 #include <core/variant.h>
19 #include "chessboard.h"
20 #include "pgnparser.h"
22 using namespace boost
;
24 GameEntity::GameEntity(Variant
* variant
, const boost::shared_ptr
<Game
>& game
,
25 ChessBoard
* chessboard
, AgentGroup
* group
)
28 , m_chessboard(chessboard
)
29 , m_dispatcher(group
, this) {
30 m_validator
= requestComponent
<IValidator
>(m_variant
, "validator");
31 m_policy
= requestComponent
<IPolicy
>(m_variant
, "policy");
34 QString
GameEntity::save() const {
38 void GameEntity::loadPGN(const PGN
& pgn
) {
42 StatePtr
GameEntity::doMove(const Move
& move
) const {
43 StatePtr
newState(position()->clone());
48 void GameEntity::executeMove(const Move
& move
) {
49 StatePtr ref
= position();
50 StatePtr pos
= doMove(move
);
51 m_game
->add(move
, pos
);
52 m_dispatcher
.move(m_game
->index());
55 void GameEntity::addPremove(const Move
& m
) {
59 void GameEntity::cancelPremove() {
60 m_premoveQueue
= Move();
61 m_chessboard
->cancelPremove();
64 void GameEntity::notifyMove(const Index
&) {
65 // the other player moved: execute premove
66 if (m_premoveQueue
!= Move() && testMove(m_premoveQueue
)) {
67 executeMove(m_premoveQueue
);
72 void GameEntity::notifyBack() { }
73 void GameEntity::notifyForward() { }
74 void GameEntity::notifyGotoFirst() { }
75 void GameEntity::notifyGotoLast() { }
77 bool GameEntity::testMove(Move
& move
) const {
78 return m_validator
->legal(position().get(), move
);
81 Piece
GameEntity::moveHint(const Move
&) const {
82 // return position()->moveHint(move);
83 return Piece(); // FIXME implement move hinting
86 bool GameEntity::testPremove(const Move
&) const {
87 return true; // FIXME implement premove checking
90 InteractionType
GameEntity::validTurn(const IColor
* pool
) const {
91 return m_policy
->droppable(position().get(), m_turn_test
, pool
);
94 InteractionType
GameEntity::validTurn(const Point
& point
) const {
95 return m_policy
->movable(position().get(), m_turn_test
, point
);
98 bool GameEntity::movable(const Point
& point
) const {
99 if (!m_enabled
) return false;
100 InteractionType action
= validTurn(point
);
101 return m_turn_test
.premove() ? action
!= NoAction
: action
== Moving
;
104 bool GameEntity::oneClickMoves() const {
105 return false; // FIXME support one-click moves
108 bool GameEntity::gotoFirst() {
113 bool GameEntity::gotoLast() {
118 bool GameEntity::goTo(const Index
& index
) {
123 bool GameEntity::forward() {
124 return m_game
->forward();
127 bool GameEntity::back() {
128 return m_game
->back();
131 bool GameEntity::undo() {
137 bool GameEntity::redo() {
143 bool GameEntity::truncate() {
148 bool GameEntity::promoteVariation() {
150 m_game
->promoteVariation();
154 bool GameEntity::canDetach() const {