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"
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 {
29 ((m_variant
->name() == "Chess") ? QString()
30 : ("[Variant \"" + m_variant
->name()) + "\"]")
34 void GameEntity::loadPGN(const PGN
& pgn
) {
38 AbstractPosition::Ptr
GameEntity::doMove(AbstractMove::Ptr move
) const {
39 AbstractPosition::Ptr newPosition
= position()->clone();
40 newPosition
->move(move
);
44 void GameEntity::executeMove(AbstractMove::Ptr move
) {
45 AbstractPosition::Ptr ref
= position();
46 AbstractPosition::Ptr pos
= doMove(move
);
47 m_game
->add(move
, pos
);
48 m_dispatcher
.move(m_game
->index());
51 void GameEntity::addPremove(const NormalUserMove
& m
) {
52 m_premoveQueue
= shared_ptr
<Premove
>(new Premove(m_variant
->createNormalMove(m
)));
55 void GameEntity::addPremove(const DropUserMove
& m
) {
56 m_premoveQueue
= shared_ptr
<Premove
>(new Premove(m_variant
->createDropMove(m
)));
59 void GameEntity::cancelPremove() {
60 m_premoveQueue
.reset();
61 m_chessboard
->cancelPremove();
64 void GameEntity::notifyMove(const Index
&) {
65 // the other player moved: execute premove
67 AbstractMove::Ptr move
= m_premoveQueue
->execute(m_game
->position());
74 void GameEntity::notifyBack() { }
75 void GameEntity::notifyForward() { }
76 void GameEntity::notifyGotoFirst() { }
77 void GameEntity::notifyGotoLast() { }
79 NormalUserMove
GameEntity::createMove(const Point
& from
, const Point
& to
) const {
80 NormalUserMove m
= UserEntity::createMove(from
, to
);
81 m_variant
->setupMove(m
);
85 AbstractMove::Ptr
GameEntity::testMove(const NormalUserMove
& move
) const {
86 AbstractMove::Ptr m
= m_variant
->createNormalMove(move
);
87 if (m
&& position()->testMove(m
))
90 return AbstractMove::Ptr();
93 AbstractMove::Ptr
GameEntity::testMove(const DropUserMove
& move
) const {
94 AbstractMove::Ptr m
= m_variant
->createDropMove(move
);
95 if (m
&& position()->testMove(m
))
98 return AbstractMove::Ptr ();
101 AbstractPiece::Ptr
GameEntity::moveHint(AbstractMove::Ptr move
) const {
102 return position()->moveHint(move
);
105 bool GameEntity::testPremove(const NormalUserMove
&) const {
109 bool GameEntity::testPremove(const DropUserMove
&) const {
113 InteractionType
GameEntity::validTurn(int pool
) const {
114 return position()->droppable(m_turn_test
, pool
);
117 InteractionType
GameEntity::validTurn(const Point
& point
) const {
118 // TODO: maybe replace this call with a new API position()->owner(point) ??
119 return position()->movable(m_turn_test
, point
);
122 bool GameEntity::movable(const Point
& point
) const {
123 if (!m_enabled
) return false;
124 InteractionType action
= validTurn(point
);
125 return m_turn_test
.premove() ? action
!= NoAction
: action
== Moving
;
128 bool GameEntity::oneClickMoves() const {
129 return m_variant
->simpleMoves();
132 bool GameEntity::gotoFirst() {
137 bool GameEntity::gotoLast() {
142 bool GameEntity::goTo(const Index
& index
) {
147 bool GameEntity::forward() {
148 return m_game
->forward();
151 bool GameEntity::back() {
152 return m_game
->back();
155 bool GameEntity::undo() {
161 bool GameEntity::redo() {
167 bool GameEntity::truncate() {
172 bool GameEntity::promoteVariation() {
174 m_game
->promoteVariation();
178 bool GameEntity::canDetach() const {