Skip to content
Snippets Groups Projects
Commit 9ffd9e63 authored by Bolla Péter's avatar Bolla Péter
Browse files

Check functions in chess.cpp

* TestIfCheck(Team defense) corrected (given team is defense not attack ; corrected the method of checking if the king is attacked)
* CanStopCheck(Team defense) completed 
* Mate()
parent ea9818a7
Branches
No related tags found
No related merge requests found
...@@ -81,7 +81,9 @@ Chess::Chess(int w, int h) ...@@ -81,7 +81,9 @@ Chess::Chess(int w, int h)
} }
} }
void Chess::Move(int oi, int ti)
//MOVEMENT FUNCTIONS
Movement Chess::CanMove(int oi, int ti)
{ {
/*int _or = 8-_oy/HEIG *8; /*int _or = 8-_oy/HEIG *8;
int _oc = _ox/WID *8; int _oc = _ox/WID *8;
...@@ -116,29 +118,29 @@ void Chess::Move(int oi, int ti) ...@@ -116,29 +118,29 @@ void Chess::Move(int oi, int ti)
{ {
if(dr == -1 && (dc == 1 || dc == -1) && CanCapture(oi, ti)) //CAPTURE MOVE if(dr == -1 && (dc == 1 || dc == -1) && CanCapture(oi, ti)) //CAPTURE MOVE
{ {
//Move&Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
if(dr == -1 && dc == 0) //ONE MOVE FORWARD if(dr == -1 && dc == 0) //ONE MOVE FORWARD
{ {
//Move return Mov;
} }
else if(dr == -2 && dc == 0) //TWO MOVES FORWARD else if(dr == -2 && dc == 0) //TWO MOVES FORWARD
{ {
if(!Table[oi+8].occupied && Table[oi].row == 1) if(!Table[oi+8].occupied && Table[oi].row == 1)
{ {
//Move return Mov;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
} }
...@@ -148,34 +150,34 @@ void Chess::Move(int oi, int ti) ...@@ -148,34 +150,34 @@ void Chess::Move(int oi, int ti)
{ {
if(dr == 1 && (dc == 1 || dc == -1) && CanCapture(oi,ti)) //CAPTURE MOVE if(dr == 1 && (dc == 1 || dc == -1) && CanCapture(oi,ti)) //CAPTURE MOVE
{ {
//Move&Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
if(dr == 1 && dc == 0) //ONE MOVE FORWARD if(dr == 1 && dc == 0) //ONE MOVE FORWARD
{ {
//Move return Mov;
} }
else if(dr == 2 && dc == 0) //TWO MOVES FORWARD else if(dr == 2 && dc == 0) //TWO MOVES FORWARD
{ {
if(!Table[oi+8].occupied && Table[oi].row == 6) if(!Table[oi+8].occupied && Table[oi].row == 6)
{ {
//Move return Mov;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
} }
else{ else{
//Don't move return No;
} }
break; break;
} }
...@@ -192,18 +194,18 @@ void Chess::Move(int oi, int ti) ...@@ -192,18 +194,18 @@ void Chess::Move(int oi, int ti)
{ {
if(!Table[ti].occupied) if(!Table[ti].occupied)
{ {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dc >0) //Move to the left else if(dc >0) //Move to the left
...@@ -216,18 +218,18 @@ void Chess::Move(int oi, int ti) ...@@ -216,18 +218,18 @@ void Chess::Move(int oi, int ti)
{ {
if(!Table[ti].occupied) if(!Table[ti].occupied)
{ {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dc == 0) else if(dc == 0)
...@@ -242,18 +244,18 @@ void Chess::Move(int oi, int ti) ...@@ -242,18 +244,18 @@ void Chess::Move(int oi, int ti)
{ {
if(!Table[ti].occupied) if(!Table[ti].occupied)
{ {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dr >0) //Move down else if(dr >0) //Move down
...@@ -266,27 +268,47 @@ void Chess::Move(int oi, int ti) ...@@ -266,27 +268,47 @@ void Chess::Move(int oi, int ti)
{ {
if(!Table[ti].occupied) if(!Table[ti].occupied)
{ {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
break; break;
} }
case Knight: case Knight:{
if((abs(dc) == 2&& abs(dr) == 1) || (abs(dc) == 1 && abs(dr) == 22))
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi,ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
break;
}
case Bishop:{ case Bishop:{
if(abs(dr) == abs(dc) && dr !=0) if(abs(dr) == abs(dc) && dr !=0)
{ {
...@@ -304,18 +326,18 @@ void Chess::Move(int oi, int ti) ...@@ -304,18 +326,18 @@ void Chess::Move(int oi, int ti)
if(allempty) if(allempty)
{ {
if(!Table[ti].occupied) { if(!Table[ti].occupied) {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dr>0 && dc <0) //Move "down-right" else if(dr>0 && dc <0) //Move "down-right"
...@@ -331,18 +353,18 @@ void Chess::Move(int oi, int ti) ...@@ -331,18 +353,18 @@ void Chess::Move(int oi, int ti)
if(allempty) if(allempty)
{ {
if(!Table[ti].occupied) { if(!Table[ti].occupied) {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dr<0 && dc < 0)//Move "up-right" else if(dr<0 && dc < 0)//Move "up-right"
...@@ -358,18 +380,18 @@ void Chess::Move(int oi, int ti) ...@@ -358,18 +380,18 @@ void Chess::Move(int oi, int ti)
if(allempty) if(allempty)
{ {
if(!Table[ti].occupied) { if(!Table[ti].occupied) {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
else if(dr<0 && dc>0) //Move "up-left" else if(dr<0 && dc>0) //Move "up-left"
...@@ -385,48 +407,301 @@ void Chess::Move(int oi, int ti) ...@@ -385,48 +407,301 @@ void Chess::Move(int oi, int ti)
if(allempty) if(allempty)
{ {
if(!Table[ti].occupied) { if(!Table[ti].occupied) {
//Move return Mov;
} }
else if(CanCapture(oi, ti)) else if(CanCapture(oi, ti))
{ {
//Capture return Cap;
} }
else{ else{
//Don't move return No;
} }
} }
else{ else{
//Don't move return No;
} }
} }
break; break;
} }
else{ else{
//Don't move return No;
} }
break; break;
} }
case King: case King:
{
if((abs(dr) == 1 && dc == 0)
|| (abs(dr) == 1 && abs(dc) == 1)
|| (dr == 0 || abs(dc) == 1))
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
break; break;
}
case Queen: case Queen:
{
//Movement of bishop
if(abs(dr) == abs(dc) && dr !=0)
{
if(dr>0 && dc>0) //Move "down-left"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi-9].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dr>0 && dc <0) //Move "down-right"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi-7].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dr<0 && dc < 0)//Move "up-right"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi+9].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dr<0 && dc>0) //Move "up-left"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi+7].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
break;
}
//Movement of rook
else if(dr == 0)
{
if(dc < 0)//Move to the right
{
for(int d = 1; d<abs(dc)-1 && allempty; d++)
{
if(Table[oi+d].occupied) allempty = false;
}
if(allempty)
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dc >0) //Move to the left
{
for(int d = 1; d<abs(dc)-1 && allempty; d++)
{
if(Table[oi-d].occupied) allempty = false;
}
if(allempty)
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dc == 0)
{
if(dr < 0)//Move up
{
for(int d = 1; d<abs(dc)-1 && allempty; d++)
{
if(Table[oi+d*8].occupied) allempty = false;
}
if(allempty)
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else if(dr >0) //Move down
{
for(int d = 1; d<abs(dc)-1 && allempty; d++)
{
if(Table[oi-8*d].occupied) allempty = false;
}
if(allempty)
{
if(!Table[ti].occupied)
{
return Mov;
}
else if(CanCapture(oi, ti))
{
return Cap;
}
else{
return No;
}
}
else{
return No;
}
}
else{
return No;
}
}
break; break;
}
case Empty: case Empty:
break; break;
} }
} }
} }
}
void Chess::Capture(int _tx, int _ty) void Chess::Capture(int oi, int ti)
{ {
Piece p = Table[oi].piece;
Table[oi].occupied = false;
Table[oi].piece = {Empty, None};
Table[ti].occupied = true;
Table[ti].piece = p;
} }
bool Chess::Occupied(int x, int y) void Chess::Move(int oi, int ti)
{ {
for(Square t: Table) Piece p = Table[oi].piece;
{
if(t.row == x && t.column == y) return t.occupied; Table[oi].occupied = false;
} Table[oi].piece = {Empty, None};
Table[ti].occupied = true;
Table[ti].piece = p;
} }
bool Chess::Opponents(int i1, int i2) bool Chess::Opponents(int i1, int i2)
...@@ -445,3 +720,129 @@ bool Chess::CanCapture(int i1, int i2) ...@@ -445,3 +720,129 @@ bool Chess::CanCapture(int i1, int i2)
if(Opponents(i1, i2) && Table[i2].piece.type != King) return true; if(Opponents(i1, i2) && Table[i2].piece.type != King) return true;
else return false; else return false;
} }
//GETTER FUNCTIONS
bool Chess::Occupied(int pos)
{
if(pos>=0 && pos<Table.size())
{
return Table[pos].occupied;
}
else return false;
}
Piece Chess::GetPiece(int pos)
{
if(pos>=0 && pos<Table.size())
{
return Table[pos].piece;
}
else return {Empty,None};
}
Color Chess::GetColor(int pos)
{
if(pos>=0 && pos<Table.size())
{
return Table[pos].color;
}
else return White;
}
Square Chess::GetSquare(int pos)
{
if(pos >=0 && pos< Table.size())
{
return Table[pos];
}
else return{{Empty, None}, White, false, -1,-1};
}
bool Chess::IsMate()
{
return mate;
}
//CHECK
bool Chess::TestIfCheck(Team defense)
{
int kingp = -1;
for(int p = 0; p<Table.size(); p++)
{
if(Table[p].piece.type == King && Table[p].piece.team == defense)
{
kingp = p;
break;
}
}
Table[kingp].occupied = false;
Table[kingp].piece = {Empty, None};
for(int p = 0; p<Table.size(); p++)
{
if(Table[p].piece.team != defense && Table[p].piece.type != King) //Pieces that can put other king in check
{
if(CanMove(p, kingp) == Mov)
{
check = true;
return true;
}
}
}
Table[kingp].occupied = true;
Table[kingp].piece = {King, defense};
return false;
}
bool Chess::CanStopCheck(Team defense) //Checks if the defense team can defend check
{
bool can = false;
Square c;
std::vector<int> defensi;
for(int p = 0; p<Table.size(); p++)
{
if(Table[p].piece.type == Empty && Table[p].piece.team == defense)
{
defensi.push_back(p); //Defensive teams troops
}
}
for(int d = 0; d<defensi.size(); d++)
{
for(int t = 0; t<Table.size(); t++)
{
if(CanMove(defensi[d],t) == Mov)
{
Move(defensi[d],t);
}
else if(CanMove(defensi[d],t) == Cap)
{
c = Table[t];
Capture(defensi[d], t);
}
if(!TestIfCheck(defense))
{
can = true;
}
Move(t, defensi[d]);
Table[t] = c;
}
}
if(!can)
{
Mate();
}
return can;
}
void Chess::Mate()
{
mate = true;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment