Commit f9fa15d9 authored by AlaskaLinuxUser's avatar AlaskaLinuxUser
Browse files

Adding KingSafe.

parent dfe7ee34
......@@ -25,7 +25,7 @@ private:
// Public prototypes go here
public:
// Spawn a new jello
// Spawn a new board
void setup(string boardPositions);
string getBoard();
......
File added
#include <sstream>
#include "Moves.h"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string Moves::available(string boardPositions, bool whoseTurn) {
string list = "";
if (whoseTurn){
for (int i = 0; i < 64; i++) {
switch (boardPositions[i]) {
//case 'N': list+=nightMoves(i);break;
//case 'R': list+=rookMoves(i);break;
//case 'B': list+=bishopMoves(i);break;
//case 'Q': list+=queenMoves(i);break;
//case 'K': list+=kingMoves(i);break;
//case 'P': list+=pawnMoves(i);break;
}
}} else {
for (int i = 0; i < 64; i++) {
switch (boardPositions[i]) {
//case 'n': list+=nightMovesB(i);break;
//case 'r': list+=rookMovesB(i);break;
//case 'b': list+=bishopMovesB(i);break;
//case 'q': list+=queenMovesB(i);break;
//case 'k': list+=kingMovesB(i);break;
//case 'p': list+=pawnMovesB(i);break;
}
}}
//Debugging only // Log.i("WJH", list);
return list;
/*
* The list is in this format 1234,
* 1,2 = 2 digit from square
* 3,4 = 2 digit to square
* followed by a comma.
*/
}
bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
// For checking if the king is safe.
int z;
if (whoseTurn){
for (int i = 0; i < 64; i++) {
switch (boardPositions[i]) {
case 'K': z = boardPositions[i];break;
}
}
int g = z%8;
int h = z/8;
bool notI=true;
// Bishop or Queen
int k;
if (h < 7) {
// Up diagonal moves.
if (g < 7) {
k = z + 9;
while (boardPositions[k] == '*' && notI) {
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='b'||boardPositions[k]=='q') {
return false;} // When there is an enemy.
}
notI = true;
if (g > 0) {
k = z + 7;
while (boardPositions[k] == '*' && notI) {
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='b'||boardPositions[k]=='q') {
return false;} // When there is an enemy.
}}
if (h > 0) {
// down diagonal moves.
notI = true;
if (g > 0) {
k = z - 9;
while (boardPositions[k] == '*' && notI) {
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='b'||boardPositions[k]=='q') {
return false;} // When there is an enemy.
}
notI = true;
if (g < 7) {
k = z - 7;
while (boardPositions[k] == '*' && notI) {
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else { notI = false;}} // While it's empty.
if (boardPositions[k]=='b'||boardPositions[k]=='q') {
return false;} // When there is an enemy.
}}
// Rook or Queen
// Up moves
notI = true;
int j = 1;
int vert = 8;
k = z;
if (z < 56) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
vert += 8;
if (k < 56) {
k = z + (vert * j);
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='r'||boardPositions[k]=='q') {
return false;} // When there is an enemy..
// Down moves
notI = true;
j = -1;
vert = 8;
k = z;
if (z > 7) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
vert += 8;
if (k >7) {
k = z + (vert * j);
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='r'||boardPositions[k]=='q') {
return false;} // When there is an enemy..
// Right side....
notI = true;
int rj = 1;
int rk = z;
if (g < 7) {
rk = z + rj;
}
while (boardPositions[rk] == '*' && notI) {
rj++;
if (rk%8 < 7) {
rk = z + rj;
} else {notI = false;}} // While it's empty.
if (boardPositions[rk]=='r'||boardPositions[rk]=='q') {
return false;} // When there is an enemy..
// Left side....
notI=true;
rj = 1;
rk = z;
if (g > 0) {
rk = z - rj;
}
while (boardPositions[rk] == '*' && notI) {
rj++;
if (rk%8 > 0) {
rk = z - rj;
} else {notI = false;}} // While it's empty.
if (boardPositions[rk]=='r'||boardPositions[rk]=='q') {
return false;} // When there is an enemy..
// Knight
if (h < 7 ) {
if (g > 1 && boardPositions[z+6]=='n') {
return false;}
if (g < 6 && boardPositions[z+10]=='n') {
return false;}}
if (h < 6 ) {
if (g > 0 && boardPositions[z+15]=='n') {
return false;}
if (g < 7 && boardPositions[z+17]=='n') {
return false;}}
if (h > 0 ) {
if (g < 6 && boardPositions[z-6]=='n') {
return false;}
if (g > 1 && boardPositions[z-10]=='n') {
return false;}}
if (h > 1 ) {
if (g < 7 && boardPositions[z-15]=='n') {
return false;}
if (g > 0 && boardPositions[z-17]=='n') {
return false;}}
// King check // Don't move next to another king! // Also includes pawns.
if (h < 7 ) {
if (boardPositions[z+8]=='k') {
return false;}
if (g > 0) {
if (boardPositions[z+7]=='k' || boardPositions[z+7]=='p') {
return false;}}
if (g < 7) {
if (boardPositions[z+9]=='k' || boardPositions[z+9]=='p') {
return false;}}}
if (h > 0 ) {
if (boardPositions[z-8]=='k') {
return false;}
if (g > 0) {
if (boardPositions[z-9]=='k') {
return false;}}
if (g < 7) {
if (boardPositions[z-7]=='k') {
return false;}}}
if (g > 0) {
if (boardPositions[z-1]=='k') {
return false;}}
if (g < 7) {
if (boardPositions[z+1]=='k') {
return false;}}
// End white king is safe.
} else {
for (int i = 0; i < 64; i++) {
switch (boardPositions[i]) {
case 'k': z = boardPositions[i];break;
}
}
int g = z%8;
int h = z/8;
bool notI=true;
// Bishop or Queen
int k;
if (h < 7) {
// Up diagonal moves.
if (g < 7) {
k = z + 9;
while (boardPositions[k] == '*' && notI) {
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='B'||boardPositions[k]=='Q') {
return false;} // When there is an enemy.
}
notI = true;
if (g > 0) {
k = z + 7;
while (boardPositions[k] == '*' && notI) {
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='B'||boardPositions[k]=='Q') {
return false;} // When there is an enemy.
}}
if (h > 0) {
// down diagonal moves.
notI = true;
if (g > 0) {
k = z - 9;
while (boardPositions[k] == '*' && notI) {
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='B'||boardPositions[k]=='Q') {
return false;} // When there is an enemy.
}
notI = true;
if (g < 7) {
k = z - 7;
while (boardPositions[k] == '*' && notI) {
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else { notI = false;}} // While it's empty.
if (boardPositions[k]=='B'||boardPositions[k]=='Q') {
return false;} // When there is an enemy.
}}
// Rook or Queen
// Up moves
notI = true;
int j = 1;
int vert = 8;
k = z;
if (z < 56) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
vert += 8;
if (k < 56) {
k = z + (vert * j);
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='R'||boardPositions[k]=='Q') {
return false;} // When there is an enemy..
// Down moves
notI = true;
j = -1;
vert = 8;
k = z;
if (z > 7) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
vert += 8;
if (k >7) {
k = z + (vert * j);
} else {notI = false;}} // While it's empty.
if (boardPositions[k]=='R'||boardPositions[k]=='Q') {
return false;} // When there is an enemy..
// Right side....
notI = true;
int rj = 1;
int rk = z;
if (g < 7) {
rk = z + rj;
}
while (boardPositions[rk] == '*' && notI) {
rj++;
if (rk%8 < 7) {
rk = z + rj;
} else {notI = false;}} // While it's empty.
if (boardPositions[rk]=='R'||boardPositions[rk]=='Q') {
return false;} // When there is an enemy..
// Left side....
notI=true;
rj = 1;
rk = z;
if (g > 0) {
rk = z - rj;
}
while (boardPositions[rk] == '*' && notI) {
rj++;
if (rk%8 > 0) {
rk = z - rj;
} else {notI = false;}} // While it's empty.
if (boardPositions[rk]=='R'||boardPositions[rk]=='Q') {
return false;} // When there is an enemy..
// Knight
if (h < 7 ) {
if (g > 1 && boardPositions[z+6]=='N') {
return false;}
if (g < 6 && boardPositions[z+10]=='N') {
return false;}}
if (h < 6 ) {
if (g > 0 && boardPositions[z+15]=='N') {
return false;}
if (g < 7 && boardPositions[z+17]=='N') {
return false;}}
if (h > 0 ) {
if (g < 6 && boardPositions[z-6]=='N') {
return false;}
if (g > 1 && boardPositions[z-10]=='N') {
return false;}}
if (h > 1 ) {
if (g < 7 && boardPositions[z-15]=='N') {
return false;}
if (g > 0 && boardPositions[z-17]=='N') {
return false;}}
// King check // Don't move next to another king!
if (h < 7 ) {
if (boardPositions[z+8]=='K') {
return false;}
if (g > 0) {
if (boardPositions[z+7]=='K') {
return false;}}
if (g < 7) {
if (boardPositions[z+9]=='K') {
return false;}}}
if (h > 0 ) {
if (boardPositions[z-8]=='K') {
return false;}
if (g > 0) {
if (boardPositions[z-9]=='K' || boardPositions[z-9]=='P') {
return false;}}
if (g < 7) {
if (boardPositions[z-7]=='K' || boardPositions[z-7]=='P') {
return false;}}}
if (g > 0) {
if (boardPositions[z-1]=='K') {
return false;}}
if (g < 7) {
if (boardPositions[z+1]=='K') {
return false;}}
// End black king is safe.
}
// Nothing returned false, so we know the king is safe.
return true;
} // End is king safe?
#pragma once
#include <cmath>
#include <string>
using namespace std;
class Moves {
private:
char m_theBoard[64] = {'R','N','B','Q','K','B','N','R','P','P','P','P','P',
'P','P','P','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','p','p','p','p','p','p','p','p','r','n','b','q','k','b','n','r'};
bool m_whitesTurn = true;
bool m_Kcastle = true;
bool m_Qcastle = true;
bool m_kcastle = true;
bool m_qcastle = true;
bool m_enPassant = false;
string m_enPasPawn = "";
int m_moveSince = 0;
int m_turnCount = 0;
// Public prototypes go here
public:
// All available moves.
string available(string boardPositions, bool whoseTurn);
// Is the king safe?
bool isKingSafe(string boardPositions, bool whoseTurn);
};
File added
......@@ -12,9 +12,10 @@
# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ -I./ -Wall -ansi -Wextra -g -c -std=c++14 Board.cpp -o board.o
g++ -I./ -Wall -ansi -Wextra -g -c -std=c++14 Moves.cpp -o moves.o
g++ -I./ -Wall -ansi -Wextra -g -c -std=c++14 Engine.cpp -o engine.o
g++ board.o engine.o -o picoEngine
g++ board.o moves.o engine.o -o picoEngine
echo "type ./picoEngine to launch"
exit
File added
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment