-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleMovement.cpp
117 lines (94 loc) · 2.43 KB
/
SimpleMovement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "SimpleMovement.h"
#include "Board.h"
using namespace Chess::GameLogic::Movements;
SimpleMovement::SimpleMovement( Board* const board, const Position& from, const Position& to, MovementFlags flags )
: Movement( board, flags ),
m_from( from ),
m_to( to )
{
}
SimpleMovement::~SimpleMovement()
{
cout << "~SimpleMovement()\n";
// m_capturedPieces.clear();
}
bool SimpleMovement::doMove()
{
// if ( m_board->isPiece( m_to ) )
// {
// m_capturedPieces.push_back( m_board->pieceAt( m_to ) );
// }
if ( m_from == m_to ) // logic breaker
{
qDebug() << "Ka-kaw!";
if ( this->capturedPiece() != 0 ) // will cause unwanted beahaviour
{
m_board->addPiece( this->capturedPiece() );
}
}
else
{
const bool isPieceMoved = m_board->movePiece( m_from, m_to );
if ( isPieceMoved )
{
ChessPiece* piece = m_board->pieceAt( m_to );
int currentMoves = m_board->passedMoves( piece );
m_board->setPassedMoves( piece, ++ currentMoves );
}
return isPieceMoved;
}
return true;
}
bool SimpleMovement::undoMove()
{
ChessPiece* const piece = m_board->pieceAt( m_to );
if ( m_from == m_to ) // another logic breaker // aka if the thing is an en-passant
{
m_board->removePiece( piece );
}
bool tmp = m_board->movePiece( m_to, m_from );
if ( ! tmp )
{
return false;
}
m_board->m_undoMovesCountMap[ piece ] --;
return true;
}
void SimpleMovement::setFrom( const Position& from )
{
m_from = from;
}
Position SimpleMovement::from() const
{
return m_from;
}
void SimpleMovement::setTo( const Position& to )
{
m_to = to;
}
Position SimpleMovement::to() const
{
return m_to;
}
void SimpleMovement::setBoard( Board* board )
{
m_board = board;
}
Board* SimpleMovement::board() const
{
return m_board;
}
QString SimpleMovement::toChessNotation()
{
QString moveString = QString( QString( 'a' + m_from.x() + QString::number( m_board->rows() - m_from.y() ) )
+ " -> " + QString ( 'a' + m_to.x() + QString::number( m_board->rows() - m_to.y() ) ) );
switch ( m_flags ){
case NORMALMOVE_FLAG:
return moveString;
case CHECK_FLAG:
return QString( moveString + '#' );
// case CAPTURE_FLAG:
// return QString( moveString + 'x' );
}
return moveString;
}