2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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"
15 #include "pgnparser.h"
17 using namespace boost
;
19 GameEntity::GameEntity(const VariantPtr
& variant
, const boost::shared_ptr
<Game
>& game
,
20 Board
* chessboard
, AgentGroup
* group
)
23 , m_chessboard(chessboard
)
24 , m_dispatcher(group
, this) {
27 QString
GameEntity::save() const {
31 void GameEntity::loadPGN(const PGN
& pgn
) {
35 AbstractPosition::Ptr
GameEntity::doMove(AbstractMove::Ptr move
) const {
36 AbstractPosition::Ptr newPosition
= position()->clone();
37 newPosition
->move(move
);
41 void GameEntity::executeMove(AbstractMove::Ptr move
) {
42 AbstractPosition::Ptr ref
= position();
43 AbstractPosition::Ptr pos
= doMove(move
);
44 m_game
->add(move
, pos
);
45 m_dispatcher
.move(m_game
->index());
48 void GameEntity::addPremove(const NormalUserMove
& m
) {
49 m_premoveQueue
= shared_ptr
<Premove
>(new Premove(m_variant
->createNormalMove(m
)));
52 void GameEntity::addPremove(const DropUserMove
& m
) {
53 m_premoveQueue
= shared_ptr
<Premove
>(new Premove(m_variant
->createDropMove(m
)));
56 void GameEntity::cancelPremove() {
57 m_premoveQueue
.reset();
58 m_chessboard
->cancelPremove();
61 void GameEntity::notifyMove(const Index
&) {
62 // the other player moved: execute premove
64 AbstractMove::Ptr move
= m_premoveQueue
->execute(m_game
->position());
71 void GameEntity::notifyBack() { }
72 void GameEntity::notifyForward() { }
73 void GameEntity::notifyGotoFirst() { }
74 void GameEntity::notifyGotoLast() { }
76 NormalUserMove
GameEntity::createMove(const Point
& from
, const Point
& to
) const {
77 NormalUserMove m
= UserEntity::createMove(from
, to
);
81 AbstractMove::Ptr
GameEntity::testMove(const NormalUserMove
& move
) const {
82 AbstractMove::Ptr m
= m_variant
->createNormalMove(move
);
83 if (m
&& position()->testMove(m
))
86 return AbstractMove::Ptr();
89 AbstractMove::Ptr
GameEntity::testMove(const DropUserMove
& move
) const {
90 AbstractMove::Ptr m
= m_variant
->createDropMove(move
);
91 if (m
&& position()->testMove(m
))
94 return AbstractMove::Ptr ();
97 AbstractPiece::Ptr
GameEntity::moveHint(AbstractMove::Ptr move
) const {
98 return position()->moveHint(move
);
101 bool GameEntity::testPremove(const NormalUserMove
&) const {
105 bool GameEntity::testPremove(const DropUserMove
&) const {
109 InteractionType
GameEntity::validTurn(int pool
) const {
110 return position()->droppable(m_turn_test
, pool
);
113 InteractionType
GameEntity::validTurn(const Point
& point
) const {
114 // TODO: maybe replace this call with a new API position()->owner(point) ??
115 return position()->movable(m_turn_test
, point
);
118 bool GameEntity::movable(const Point
& point
) const {
119 if (!m_enabled
) return false;
120 InteractionType action
= validTurn(point
);
121 return m_turn_test
.premove() ? action
!= NoAction
: action
== Moving
;
124 bool GameEntity::oneClickMoves() const {
125 return m_variant
->simpleMoves();
128 bool GameEntity::gotoFirst() {
133 bool GameEntity::gotoLast() {
138 bool GameEntity::goTo(const Index
& index
) {
143 bool GameEntity::forward() {
144 return m_game
->forward();
147 bool GameEntity::back() {
148 return m_game
->back();
151 bool GameEntity::undo() {
157 bool GameEntity::redo() {
163 bool GameEntity::truncate() {
168 bool GameEntity::promoteVariation() {
170 m_game
->promoteVariation();
174 bool GameEntity::canDetach() const {