-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.cpp
129 lines (124 loc) · 4.81 KB
/
King.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
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "King.h"
#include <iostream>
#include "Board.h"
using namespace std;
King::King(FigureColor color) :Figure("King ", color) {}
bool King::move(int row, int col, int newRow, int newCol, Board& chess)
{
if ((chess.get_field(row,col)->getColor().isWhite() == true && row==0 && col ==4 && newRow==0 && (newCol==1 || newCol==6))
|| (chess.get_field(row,col)->getColor().isWhite() == false && row==7 && col ==4 && newRow==7 && (newCol==1 || newCol==6)))
return castle(newRow,newCol,chess);
if (row > newRow+1 || row < newRow-1 || col > newCol+1 || col < newCol-1)
{
cout << "Illegal move. No action taken." << endl;
return false;
}
if (!chess.isFree(newRow,newCol))
{
if ((chess.get_field(newRow,newCol)->getColor().isWhite() == true && chess.get_field(row,col)->getColor().isWhite() == true)
|| (chess.get_field(newRow,newCol)->getColor().isWhite() == false && chess.get_field(row,col)->getColor().isWhite() == false))
{
cout << "Illegal move. No action taken." << endl;
return false;
}
chess.take(newRow,newCol);
}
if (chess.get_field(row,col)->getColor().isWhite()) cout << "W ";
else cout << "B ";
chess.set_field(newRow,newCol,*this);
chess.get_field(row,col)->setName("Free");
chess.get_field(row,col)->unmoved=false;
cout << "King from ";
chess.announce(row,col,newRow,newCol);
return true;
}
bool King::castle(int newRow, int newCol, Board& chess)
{
bool white = true;
if (newCol==1)
{
if (chess.get_field(newRow,4)->unmoved && chess.get_field(newRow,0)->unmoved && chess.isFree(newRow,1) && chess.isFree(newRow,2) && chess.isFree(newRow,3))
{
if (newRow == 0)
{
if (chess.isAttacked(newRow,1,0) || chess.isAttacked(newRow,2,0) || chess.isAttacked(newRow,3,0))
{
cout << "Illegal move. No action taken. " << endl;
return false;
}
cout << "W ";
}
else
{
if (chess.isAttacked(newRow,1,1) || chess.isAttacked(newRow,2,1) || chess.isAttacked(newRow,3,1))
{
cout << "Illegal move. No action taken. " << endl;
return false;
}
cout << "B ";
white=false;
}
Bishop bishop(white);
chess.set_field(newRow,1,*this);
chess.set_field(newRow,2,bishop);
chess.get_field(newRow,0)->setName("Free");
chess.get_field(newRow,4)->setName("Free");
chess.get_field(newRow,0)->unmoved=false;
chess.get_field(newRow,4)->unmoved=false;
cout << "King castle to B1. " << endl;
return true;
}
}
if (newCol==6)
{
if (chess.get_field(newRow,4)->unmoved && chess.get_field(newRow,7)->unmoved && chess.isFree(newRow,5) && chess.isFree(newRow,6))
{
if (newRow == 0)
{
if (chess.isAttacked(newRow,5,0) || chess.isAttacked(newRow,6,0))
{
cout << "Illegal move. No action taken. " << endl;
return false;
}
cout << "W ";
}
else
{
if (chess.isAttacked(newRow,5,1) || chess.isAttacked(newRow,6,1))
{
cout << "Illegal move. No action taken. " << endl;
return false;
}
cout << "B ";
white=false;
}
Bishop bishop(white);
chess.set_field(newRow,6,*this);
chess.set_field(newRow,5,bishop);
chess.get_field(newRow,4)->setName("Free");
chess.get_field(newRow,7)->setName("Free");
chess.get_field(newRow,4)->unmoved=false;
chess.get_field(newRow,7)->unmoved=false;
cout << "King castle to B1. " << endl;
return true;
}
}
cout << "Illegal move. No action taken. " << endl;
return false;
}
bool King::canAttack(int row, int col, int newRow, int newCol, Board& chess)
{
if (row > newRow+1 || row < newRow-1 || col > newCol+1 || col < newCol-1)
{
return false;
}
if (!chess.isFree(newRow,newCol))
{
if ((chess.get_field(newRow,newCol)->getColor().isWhite() == true && chess.get_field(row,col)->getColor().isWhite() == true)
|| (chess.get_field(newRow,newCol)->getColor().isWhite() == false && chess.get_field(row,col)->getColor().isWhite() == false))
{
return false;
}
}
return true;
}