2
2
3
3
namespace chesspp
4
4
{
5
- Piece::Piece (const Position& bPos, const Position& tPos, Color c, Type t)
6
- :boardPos(bPos), color(c), type(t)
7
- {
8
- int tX = tPos.getX ();
9
- int tY = tPos.getY () + ( c == WHITE ? 0 : 80 );
10
- texturePos.set (tX, tY);
11
-
12
- Log::Debug::writeln (*this );
13
- }
14
-
15
-
16
- const Position& Piece::getBoardPos (void ) const
17
- {
18
- return boardPos;
19
- }
20
- const Position& Piece::getTexturePos (void ) const
21
- {
22
- return texturePos;
23
- }
24
- Color Piece::getColor (void ) const
25
- {
26
- return color;
27
- }
28
-
29
- const posList& Piece::getTrajectory (void )
30
- {
31
- return trajectory;
32
- }
33
- void Piece::shootPath (const Board* board, const Direction d)
34
- {
35
- Position pos = this ->boardPos ;
36
-
37
- pos.move (d);
38
-
39
- // Validity only has to be changed to false
40
- // Please see "OnValidity.txt" for more info
41
- while (pos.inBounds ())
42
- {
43
- if (board->hasPosition (pos))
44
- {
45
- if (board->at (pos)->getColor () == this ->color )
46
- {
47
- pos.setValid (false );
48
- this ->trajectory .push_back (pos);
49
- }
50
- else
51
- {
52
- this ->trajectory .push_back (pos);
53
- pos.setValid (false );
54
- }
55
- }
56
- else
57
- {
58
- this ->trajectory .push_back (pos);
59
- }
60
- pos.move (d);
61
- }
62
- }
63
- void Piece::updateTrajectory (const Board* board, const Position& oldPos, const Position& newPos)
64
- {
65
- Log::Debug::writeln (" Updating" , 1 );
66
-
67
- bool foundPos = false ;
68
- for (posList::iterator iter = trajectory.begin (); iter != trajectory.end (); iter++)
69
- {
70
- if (*iter == oldPos || *iter == newPos)
71
- {
72
- foundPos = true ;
73
- break ;
74
- }
75
- }
76
- if (foundPos) makeTrajectory (board);
77
- }
78
-
79
-
80
- bool Piece::move (const Position& moveTo)
81
- {
82
- if (!moveTo.inBounds ())
83
- {
84
- Log::Debug::write (" PE: moveTo not in bounds: " );
85
- Log::Debug::writeln (moveTo);
86
- return false ;
87
- }
88
-
89
-
90
- for (posList::iterator iter = trajectory.begin (); iter != trajectory.end (); iter++)
91
- {
92
- if (*iter == moveTo && iter->isValid ())
93
- {
94
- this ->boardPos = moveTo;
95
- Log::Debug::write (" PE: moveTo success: " );
96
- Log::Debug::write (moveTo);
97
- Log::Debug::writeln (boardPos);
98
- return true ;
99
- }
100
- }
101
- Log::Debug::write (" PE: moveTo fail: " );
102
- Log::Debug::write (moveTo);
103
- Log::Debug::writeln (boardPos);
104
- return false ;
105
- }
106
- bool Piece::isPawn (void )
107
- {
108
- return false ;
109
- }
110
- bool Piece::isKing (void )
111
- {
112
- return false ;
113
- }
114
-
115
-
116
-
117
-
118
- std::ostream& operator <<(std::ostream& out, const Piece& p)
119
- {
120
- return out << " PIECE: " << p.getBoardPos () << p.getTexturePos () << (p.getColor () == WHITE ? " WHITE" : " BLACK" );
121
- }
5
+ Piece::Piece (const Position& bPos, const Position& tPos, Color c, Type t)
6
+ :boardPos(bPos), color(c), type(t)
7
+ {
8
+ int tX = tPos.getX ();
9
+ int tY = tPos.getY () + ( c == WHITE ? 0 : 80 );
10
+ texturePos.set (tX, tY);
11
+
12
+ Log::Debug::writeln (*this );
13
+ }
14
+
15
+
16
+ const Position& Piece::getBoardPos (void ) const
17
+ {
18
+ return boardPos;
19
+ }
20
+ const Position& Piece::getTexturePos (void ) const
21
+ {
22
+ return texturePos;
23
+ }
24
+ Color Piece::getColor (void ) const
25
+ {
26
+ return color;
27
+ }
28
+
29
+ const posList& Piece::getTrajectory (void )
30
+ {
31
+ return trajectory;
32
+ }
33
+ void Piece::shootPath (const Board* board, const Direction d)
34
+ {
35
+ Position pos = this ->boardPos ;
36
+
37
+ pos.move (d);
38
+
39
+ // Validity only has to be changed to false
40
+ // Please see "OnValidity.txt" for more info
41
+ while (pos.inBounds ())
42
+ {
43
+ if (board->hasPosition (pos))
44
+ {
45
+ if (board->at (pos)->getColor () == this ->color )
46
+ {
47
+ pos.setValid (false );
48
+ this ->trajectory .push_back (pos);
49
+ }
50
+ else
51
+ {
52
+ this ->trajectory .push_back (pos);
53
+ pos.setValid (false );
54
+ }
55
+ }
56
+ else
57
+ {
58
+ this ->trajectory .push_back (pos);
59
+ }
60
+ pos.move (d);
61
+ }
62
+ }
63
+ void Piece::updateTrajectory (const Board* board, const Position& oldPos, const Position& newPos)
64
+ {
65
+ Log::Debug::writeln (" Updating" , 1 );
66
+
67
+ bool foundPos = false ;
68
+ for (posList::iterator iter = trajectory.begin (); iter != trajectory.end (); iter++)
69
+ {
70
+ if (*iter == oldPos || *iter == newPos)
71
+ {
72
+ foundPos = true ;
73
+ break ;
74
+ }
75
+ }
76
+ if (foundPos) makeTrajectory (board);
77
+ }
78
+
79
+
80
+ bool Piece::move (const Position& moveTo)
81
+ {
82
+ if (!moveTo.inBounds ())
83
+ {
84
+ Log::Debug::write (" PE: moveTo not in bounds: " );
85
+ Log::Debug::writeln (moveTo);
86
+ return false ;
87
+ }
88
+
89
+
90
+ for (posList::iterator iter = trajectory.begin (); iter != trajectory.end (); iter++)
91
+ {
92
+ if (*iter == moveTo && iter->isValid ())
93
+ {
94
+ this ->boardPos = moveTo;
95
+ Log::Debug::write (" PE: moveTo success: " );
96
+ Log::Debug::write (moveTo);
97
+ Log::Debug::writeln (boardPos);
98
+ return true ;
99
+ }
100
+ }
101
+ Log::Debug::write (" PE: moveTo fail: " );
102
+ Log::Debug::write (moveTo);
103
+ Log::Debug::writeln (boardPos);
104
+ return false ;
105
+ }
106
+ bool Piece::isPawn (void )
107
+ {
108
+ return false ;
109
+ }
110
+ bool Piece::isKing (void )
111
+ {
112
+ return false ;
113
+ }
114
+
115
+ Type Piece::getType ( void ) const
116
+ {
117
+ return type;
118
+ }
119
+
120
+
121
+
122
+
123
+ std::ostream& operator <<(std::ostream& out, const Piece& p)
124
+ {
125
+ return out << " PIECE: " << p.getBoardPos () << p.getTexturePos () << (p.getColor () == WHITE ? " WHITE" : " BLACK" );
126
+ }
122
127
}
0 commit comments