-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKnight.cpp
87 lines (79 loc) · 3.35 KB
/
Knight.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
#include "Knight.h"
#include "King.h"
Knight::Knight () {}
/*Knight::Knight ( std::pair < int, int > table, Color color, std::pair < int, int > texture) :
Pieces( table, color, texture) { type = Pieces::KNIGHT; }
*/
Knight::Knight ( int tableRow, int tableColumn, Color color, int textureRow, int textureColumn) :
Pieces(tableRow, tableColumn, color, textureRow, textureColumn) { type = Pieces::KNIGHT; }
bool Knight::valid ( std::pair < int, int > mPair, Pieces* const ChessPiece[8][8]) const {
std::pair <int, int> King_poz = Get_King_Pozition(getColor(), ChessPiece);
bool in_chess = false;
King* k;
if(ChessPiece[King_poz.first][King_poz.second] -> getType() == Pieces::KING) {
if( k = dynamic_cast<King*>(ChessPiece[King_poz.first][King_poz.second])) {
if(k -> are_in_chess(ChessPiece))
in_chess = true;
}
}
if( mPair.first >= 8 || mPair.first < 0 )
return false;
if( mPair.second >= 8 || mPair.second < 0 )
return false;
switch(in_chess)
{
case true:
{
Pieces* matrix_aux[8][8];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
matrix_aux[i][j] = ChessPiece[i][j];
matrix_aux[mPair.first][mPair.second] = matrix_aux[m_pozition.first][m_pozition.second];
matrix_aux[m_pozition.first][m_pozition.second] = NULL;
if(k -> are_in_chess(matrix_aux))
return false;
else
return true;
break;
}
case false:
{
if( ChessPiece[mPair.first][mPair.second] == NULL )
return true;
else
if( ChessPiece[mPair.first][mPair.second] -> getColor() == getColor() )
return false;
else
return true;
}
}
}
std::vector < std::pair < int, int > > Knight::getPossibleMoves(Pieces* const ChessPiece[8][8]) const {
std::vector < std::pair < int, int > > mList;
std::pair < int, int > aux;
aux = std::make_pair ( m_pozition.first - 2, m_pozition.second - 1 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first - 2, m_pozition.second + 1 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first - 1, m_pozition.second - 2 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first - 1, m_pozition.second + 2 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first + 2, m_pozition.second - 1 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first + 2, m_pozition.second + 1 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first + 1, m_pozition.second - 2 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
aux = std::make_pair ( m_pozition.first + 1, m_pozition.second + 2 );
if( valid ( aux, ChessPiece ) )
mList.push_back( aux );
return mList;
}