Schachspiel Klasse
Hier ist die Schachklasse für mein eigenes Schachspiel. Alles ist in dieser Klasse. Züge generieren, einen Zug vom Computer bekommen, das Brett drucken usw. usw. Dies ist eine Folgefrage zur C ++ - Generatorfunktion für ein Schachspiel .
Dies sind die Werte für jedes Stück in der Tafel, die auch in der verwendet werden int board[8][8];
Bauer = 1
Bischof = 2
Ritter = 3
Turm = 5
Königin = 6
König = 10
Die gleichen Werte gelten auch für schwarze Teile, außer diesmal sind sie negativ. Zum Beispiel; Ein weißer Bauer hat den Wert 1
Ein schwarzer Bauer hat einen Wert von -1
Die Bewegungsgenerierung erfolgt für jedes Stück einfach durch Hinzufügen oder Subtrahieren von Werten zu Zeile und Spalte , wobei auf die beiden Dimensionen des Arrays innerhalb der Klasse Bezug genommen wird. Wenn sich zum Beispiel ein Bauer in befindet board[6][0]. Wenn Sie auf das Brett schauen, sehen Sie, dass das Aufsteigen bedeutet, in die nächste Reihe zu gehen. In diesem Fall würde das Aufsteigen die Werte in der Reihe verringern, dh 6,0 wird jetzt zu 5,0. Sobald ich eine Logik für die Bewegung der Figuren habe, muss ich überprüfen, ob es sich um eine gültige Bewegung handelt. Das bedeutet, dass ich mich nur auf ein leeres Feld bewege oder die Figur eines Gegners erobere. Die endgültige Validierung Schicht ist von der Regel im Schach, die eine ist Check in Schach.
In einfachen Worten, es ist eine Bedrohung für Ihren König. Wenn Ihr König einer Bedrohung ausgesetzt ist, dh wenn ein Teil ihn angreift, müssen Sie diesen Pfad blockieren oder den König bewegen, bevor Sie einen anderen Zug spielen können.
Nehmen wir an, ich habe diese Position.
Wenn der schwarze Spieler an der Reihe ist, kann er seinen Bischof nicht bewegen, da mein Turm seinem König einen Scheck gibt, wenn er dies tut. Da blockiert der Bischof den König von einem Scheck. Deshalb, bevor ich die letzten Züge generiere. Ich generiere Pseudolegalbewegungen, die im Grunde nur den einzelnen Regeln folgen. Dann gehe ich jede Bewegung im Container durch und führe sie aus. Wenn die Funktion nach der Ausführung bool check(int turntrue zurückgibt, bedeutet dies einfach, dass es sich um eine ungültige Verschiebung handelt. Ich lasse es fallen.
#include<iostream>
#include<vector>
#include<string>
typedef std::vector<std::string> buff;
typedef std::string str;
// Pawn - 1, Knight - 3, Bishop - 2, rook - 5,queen - 6,king - 10
str push(int row,int col,int desrow,int descol){
using std::to_string;
str mystr = to_string(row) + to_string(col) + to_string(desrow) + to_string(descol);
return mystr;
}
class Chess{
public:
short int board[8][8] = // This array represents the chess board
{
{-2,0,0,0,0,0,-10,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,-1,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,6,0,0,0,0,0},
{0,10,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
};
buff pseudomoves;
buff legal_moves;
private:
void undomove(int original,str Move){
board[Move[0]-48][Move[1]-48] = board[Move[2]-48][Move[3]-48];
board[Move[2]-48][Move[3]-48] = original;
}
public:
int perform(str Move){
int original;
original = board[Move[2]-48][Move[3]-48];
board[Move[2]-48][Move[3]-48] = board[Move[0]-48][Move[1]-48];
board[Move[0]-48][Move[1]-48] = 0;
return original;
}
private:
bool check(bool turn){
if (turn == true){
int row,col;
//Finding the king on the board
for (int i = 0;i < 8;i++){
for (int j = 0;j < 8;j++){
if (board[i][j] == 10){
row = i;
col = j;
}
}
}
//Finding the king on the board
if (row != 0 && col != 0 && board[row-1][col-1] == -1) return true;
else if (row != 0 && col != 7 && board[row-1][col+1] == -1) return true;
int a,b;
a = row;
b = col;
if (a != 0 && b != 0){
for(;;){
a-=1;
b-=1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 0 || b == 0) break;
}
}
a = row;
b = col;
if (a != 0 && b != 7){
for(;;){
a-=1;
b+=1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 0 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7 && b != 0){
for(;;){
a+=1;
b-=1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 7 || b == 0) break;
}
}
a = row;
b = col;
if (a != 7 && b != 7){
for(;;){
a+=1;
b+=1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 7 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7){
for(;;){
a+=1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || a == 7 ) break;
}
}
a = row;
b = col;
if (a != 0){
for(;;){
a-=1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || a == 0 ) break;
}
}
a = row;
b = col;
if (b != 7){
for(;;){
b+=1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || b == 7 ) break;
}
}
a = row;
b = col;
if (b != 0){
for(;;){
b-=1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || b == 0 ) break;
}
}
if (row > 0 && col < 6 && board[row-1][col+2] == -3)return true;
if (row > 1 && col < 7 && board[row-2][col+1] == -3)return true;
if (row < 7 && col < 6 && board[row+1][col+2] == -3)return true;
if (row < 6 && col < 7 && board[row+2][col+1] == -3)return true;
if (row < 6 && col > 0 && board[row+2][col-1] == -3)return true;
if (row < 7 && col > 1 && board[row+1][col-2] == -3)return true;
if (row > 1 && col > 0 && board[row-2][col-1] == -3)return true;
if (row > 0 && col > 1 && board[row-1][col-2] == -3)return true;
if (row != 7 && board[row+1][col] == -10)return true;
if (row != 0 && board[row-1][col] == -10)return true;
if (col != 7 && board[row][col+1] == -10) return true;
if (col != 0 && board[row][col-1] == -10) return true;
if (row != 7 && col != 7 && board[row+1][col+1] == -10)return true;
if (row != 7 && col != 0 && board[row+1][col-1] == -10) return true;
if (row != 0 && col != 7 && board[row-1][col+1] == -10) return true;
if (row != 0 && col != 0 && board[row-1][col-1] == -10) return true;
}
else if(turn == false){
int row,col;
//Finding the king on the board
for (int i = 0;i < 8;i++){
for (int j = 0;j < 8;j++){
if (board[i][j] == -10){
row = i;
col = j;
}
}
}
//Finding the king on the board
if (row != 7 && col != 0 && board[row+1][col-1] == 1) return true;
else if (row != 7 && col != 7 && board[row+1][col+1] == 1) return true;
int a,b;
a = row;
b = col;
if (a != 0 && b != 0){
for(;;){
a-=1;
b-=1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 0 || b == 0) break;
}
}
a = row;
b = col;
if (a != 0 && b != 7){
for(;;){
a-=1;
b+=1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 0 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7 && b != 0){
for(;;){
a+=1;
b-=1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 7 || b == 0) break;
}
}
a = row;
b = col;
if (a != 7 && b != 7){
for(;;){
a+=1;
b+=1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 7 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7){
for(;;){
a+=1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || a == 7 ) break;
}
}
a = row;
b = col;
if (a != 0){
for(;;){
a-=1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || a == 0 ) break;
}
}
a = row;
b = col;
if (b != 7){
for(;;){
b+=1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || b == 7 ) break;
}
}
a = row;
b = col;
if (b != 0){
for(;;){
b-=1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || b == 0 ) break;
}
}
if (row > 0 && col < 6 && board[row-1][col+2] == 3)return true;
if (row > 1 && col < 7 && board[row-2][col+1] == 3)return true;
if (row < 7 && col < 6 && board[row+1][col+2] == 3)return true;
if (row < 6 && col < 7 && board[row+2][col+1] == 3)return true;
if (row < 6 && col > 0 && board[row+2][col-1] == 3)return true;
if (row < 7 && col > 1 && board[row+1][col-2] == 3)return true;
if (row > 1 && col > 0 && board[row-2][col-1] == 3)return true;
if (row > 0 && col > 1 && board[row-1][col-2] == 3)return true;
if (row != 7 && board[row+1][col] == 10)return true;
if (row != 0 && board[row-1][col] == 10)return true;
if (col != 7 && board[row][col+1] == 10) return true;
if (col != 0 && board[row][col-1] == 10) return true;
if (row != 7 && col != 7 && board[row+1][col+1] == 10)return true;
if (row != 7 && col != 0 && board[row+1][col-1] == 10) return true;
if (row != 0 && col != 7 && board[row-1][col+1] == 10) return true;
if (row != 0 && col != 0 && board[row-1][col-1] == 10) return true;
}
return false;
}
void getdiagonalmoves(bool turn,int row,int col){
int a,b;
if(turn){
a = row;
b = col;
if (a != 0 && b != 0){
for (;;){
a-=1;
b-=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 0 && b != 7){
for (;;){
a-=1;
b+=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 7 && b != 7){
for (;;){
a+=1;
b+=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 7 && b != 0){
for (;;){
a+=1;
b-=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
}
else if(!turn){
a = row;
b = col;
if (a != 0 && b != 0){
for (;;){
a-=1;
b-=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 0 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 0 && b != 7){
for (;;){
a-=1;
b+=1;
if (board[a][b] < 0)
break;
if (board[a][b] > 0 || a == 0 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(board[a][b] == 0)
pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 7 && b != 7){
for (;;){
a+=1;
b+=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a != 7 && b != 0){
for (;;){
a+=1;
b-=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
}
}
}
}
void getstraigtmoves(bool turn ,int row,int col){
int a,b;
if (turn) {// white player
a = row;
b = col;
if (a != 0){
for (;;){
a-=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a!=7){
for(;;){
a+=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (b!= 0){
for(;;){
b-=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a =row;
b = col;
if (b != 7){
for(;;){
b+=1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
}
else if(!turn) // black player
{
a = row;
b = col;
if (a != 0){
for (;;){
a-=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (a!=7){
for(;;){
a+=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a = row;
b = col;
if (b!= 0){
for(;;){
b-=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || b == 0){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
a =row;
b = col;
if (b != 7){
for(;;){
b+=1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || b == 7){
pseudomoves.push_back(push(row,col,a,b));
break;
}
if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
}
}
}
//returnpseudomoves;
}
void getknightmoves(bool turn,int row,int col){
if (turn) {
if (row > 0 && col < 6 && board[row-1][col+2] <= 0) // one up two right
pseudomoves.push_back(push(row,col,row-1,col+2));
if (row > 1 && col < 7 && board[row-2][col+1] <= 0) // two up one right
pseudomoves.push_back(push(row,col,row-2,col+1));
if (row < 7 && col < 6 && board[row+1][col+2] <= 0) // one down two right
pseudomoves.push_back(push(row,col,row+1,col+2));
if (row < 6 && col < 7 && board[row+2][col+1] <= 0) // two down one right
pseudomoves.push_back(push(row,col,row+2,col+1));
if (row < 6 && col > 0 && board[row+2][col-1] <= 0) //two down one left
pseudomoves.push_back(push(row,col,row+2,col-1));
if (row < 7 && col > 1 && board[row+1][col-2] <= 0) // one down two left
pseudomoves.push_back(push(row,col,row+1,col-2));
if (row > 1 && col > 0 && board[row-2][col-1] <= 0) // two up one left
pseudomoves.push_back(push(row,col,row-2,col-1));
if (row > 0 && col > 1 && board[row-1][col-2] <= 0) // one up two left
pseudomoves.push_back(push(row,col,row-1,col-2));
}
else if (!turn){
if (row > 0 && col < 6 && board[row-1][col+2] >= 0)pseudomoves.push_back(push(row,col,row-1,col+2));
if (row > 1 && col < 7 && board[row-2][col+1] >= 0)pseudomoves.push_back(push(row,col,row-2,col+1));
if (row < 7 && col < 6 && board[row+1][col+2] >= 0)pseudomoves.push_back(push(row,col,row+1,col+2));
if (row < 6 && col < 7 && board[row+2][col+1] >= 0)pseudomoves.push_back(push(row,col,row+2,col+1));
if (row < 6 && col > 0 && board[row+2][col-1] >= 0)pseudomoves.push_back(push(row,col,row+2,col-1));
if (row < 7 && col > 1 && board[row+1][col-2] >= 0)pseudomoves.push_back(push(row,col,row+1,col-2));
if (row > 1 && col > 0 && board[row-2][col-1] >= 0)pseudomoves.push_back(push(row,col,row-2,col-1));
if (row > 0 && col > 1 && board[row-1][col-2] >= 0)pseudomoves.push_back(push(row,col,row-1,col-2));
}
//returnpseudomoves;
}
void getpawnmoves(bool turn,int row,int col){
if (turn) {
if (row == 6 && board[row-1][col] == 0 && board[row-2][col] == 0)
pseudomoves.push_back(push(row,col,row-2,col));
if (board[row-1][col] == 0)
pseudomoves.push_back(push(row,col,row-1,col));
if (col != 0 && board[row-1][col-1] < 0)
pseudomoves.push_back(push(row,col,row-1,col-1));
if (col != 7 && board[row-1][col+1] < 0)
pseudomoves.push_back(push(row,col,row-1,col+1));
}
else if(!turn){
if (row == 7) //returnpseudomoves;
if (row == 1 && board[row+1][col] == 0 && board[row+2][col] == 0)
pseudomoves.push_back(push(row,col,row+2,col));
if (board[row+1][col] == 0)
pseudomoves.push_back(push(row,col,row+1,col));
if (col != 0 && board[row+1][col-1] > 0)
pseudomoves.push_back(push(row,col,row+1,col-1));
if (col != 7 && board[row+1][col+1] > 0)
pseudomoves.push_back(push(row,col,row+1,col+1));
}
//returnpseudomoves;
}
void getkingmoves(bool turn,int row,int col){
if (!turn){
if (row != 7 && board[row+1][col] >=0) pseudomoves.push_back(push(row,col,row+1,col));
if (row != 0 && board[row-1][col] >=0) pseudomoves.push_back(push(row,col,row-1,col));
if (col != 7 && board[row][col+1] >=0) pseudomoves.push_back(push(row,col,row,col+1));
if (col != 0 && board[row][col-1] >=0) pseudomoves.push_back(push(row,col,row,col-1));
if (row != 7 && col != 7 && board[row+1][col+1] >=0) pseudomoves.push_back(push(row,col,row+1,col+1));
if (row != 7 && col != 0 && board[row+1][col-1] >=0) pseudomoves.push_back(push(row,col,row+1,col-1));
if (row != 0 && col != 7 && board[row-1][col+1] >=0) pseudomoves.push_back(push(row,col,row-1,col+1));
if (row != 0 && col != 0 && board[row-1][col-1] >=0) pseudomoves.push_back(push(row,col,row-1,col-1));
}
else if (turn){
if (row != 7 && board[row+1][col] <=0) pseudomoves.push_back(push(row,col,row+1,col));
if (row != 0 && board[row-1][col] <=0) pseudomoves.push_back(push(row,col,row-1,col));
if (col != 7 && board[row][col+1] <=0) pseudomoves.push_back(push(row,col,row,col+1));
if (col != 0 && board[row][col-1] <=0) pseudomoves.push_back(push(row,col,row,col-1));
if (row != 7 && col != 7 && board[row+1][col+1] <=0) pseudomoves.push_back(push(row,col,row+1,col+1));
if (row != 7 && col != 0 && board[row+1][col-1] <=0) pseudomoves.push_back(push(row,col,row+1,col-1));
if (row != 0 && col != 7 && board[row-1][col+1] <=0) pseudomoves.push_back(push(row,col,row-1,col+1));
if (row != 0 && col != 0 && board[row-1][col-1] <=0) pseudomoves.push_back(push(row,col,row-1,col-1));
}
//returnpseudomoves;
}
int evaluation(){
int score;
for (int i = 0;i < 8;i++){
for(int j =0;j < 8;j++){
if(!board[i][j]) continue;
if (board[i][j] == 1) score-=10;
else if (board[i][j] == 2)score-=30;
else if (board[i][j] == 3)score-=30;
else if (board[i][j] == 5)score-=50;
else if (board[i][j] == 6)score-=90;
else if (board[i][j] == 10)score-=900;
else if (board[i][j] == -1)score+=10;
else if (board[i][j] == -2)score+=30;
else if (board[i][j] == -3)score+=30;
else if (board[i][j] == -5)score+=50;
else if (board[i][j] == -6)score+=60;
else if (board[i][j] == -10)score+=900;
}
}
return score;
}
int miniMax(int depth,bool ismax,int alpha,int beta){
if (depth == 0){
return evaluation();
}
int maxeval = -999999;
int mineval = 999999;
buff possiblemoves;
int original;
int eval;
if (ismax == true){
possiblemoves = getallmoves(false);
for (long unsigned int i = 0;i < possiblemoves.size();i++){
original = perform(possiblemoves[i]);
eval = miniMax(depth-1,false,alpha,beta);
undomove(original,possiblemoves[i]);
if(eval > maxeval)
maxeval = eval;
if (alpha >= eval)
alpha = eval;
if (beta <= alpha)
break;
}
return maxeval;
}
else{
possiblemoves = getallmoves(true);
for (long unsigned int i = 0;i < possiblemoves.size();i++){
original = perform(possiblemoves[i]);
eval = miniMax(depth-1,true,alpha,beta);
undomove(original,possiblemoves[i]);
if (eval < mineval)
mineval = eval;
if (beta <= eval)
beta = eval;
if (beta <= alpha)
break;
}
return mineval;
}
}
str miniMaxroot(int depth,bool turn){
str bestmove;
int maxeval = -9999999;
buff allmoves = getallmoves(turn);
int original;
int eval;
for (long unsigned int i = 0;i < allmoves.size();i++){
original = perform(allmoves[i]);
eval = miniMax(depth-1,false,-99999999,99999999);
std::cout << "Move: " << allmoves[i] << " Points: " << eval << "\n";
undomove(original,allmoves[i]);
if (eval > maxeval){
maxeval = eval;
bestmove = allmoves[i];
}
}
return bestmove;
}
public:
void printboard(){
for(int i = 0; i< 8;i++){
for(int j = 0;j < 8;j++){
if (board[i][j] == 1)
std::cout << "P ";
else if (board[i][j] == 5)
std::cout << "R ";
else if (board[i][j] == 3)
std::cout << "K ";
else if (board[i][j] == 2)
std::cout << "B ";
else if (board[i][j] == 6)
std::cout << "Q ";
else if(board[i][j] == 10)
std::cout << "KI ";
else if (board[i][j] == 0)
std::cout << ". ";
else if (board[i][j] == -1)
std::cout << "p ";
else if (board[i][j] == -5)
std::cout << "r ";
else if (board[i][j] == -3)
std::cout << "k ";
else if (board[i][j] == -2)
std::cout << "b ";
else if (board[i][j] == -6)
std::cout << "q ";
else if(board[i][j] == -10)
std::cout << "ki ";
else if (board[i][j] == -109)
std::cout << "X";
}
std::cout << std::endl;
}
}
buff getallmoves(bool turn){
pseudomoves.clear();
legal_moves.clear();
int original;
if (turn){
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
if (!board[i][j]) continue;
else if(board[i][j] == 1) getpawnmoves(true,i,j);
else if(board[i][j] == 2) getdiagonalmoves(true,i,j);
else if(board[i][j] == 3) getknightmoves(true,i,j);
else if(board[i][j] == 5) getstraigtmoves(true,i,j);
else if(board[i][j] == 6){
getdiagonalmoves(true,i,j);
getstraigtmoves(true,i,j);
}
else if(board[i][j] == 10) getkingmoves(true,i,j);
}
}
return pseudomoves;
for(long unsigned int i = 0;i < pseudomoves.size();i++){
original = perform(pseudomoves[i]);
if(check(true) == false){
legal_moves.push_back(pseudomoves[i]);
}
undomove(original,pseudomoves[i]);
}
return legal_moves;
}
else if(!turn){
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
if (!board[i][j]) continue;
else if(board[i][j] == -1) getpawnmoves(false,i,j);
else if(board[i][j] == -2) getdiagonalmoves(false,i,j);
else if(board[i][j] == -3) getknightmoves(false,i,j);
else if(board[i][j] == -5) getstraigtmoves(false,i,j);
else if(board[i][j] == -6){
getdiagonalmoves(false,i,j);
getstraigtmoves(false,i,j);
}
else if(board[i][j] == -10) getkingmoves(false,i,j);
}
}
for(long unsigned int i = 0;i < pseudomoves.size();i++){
original = perform(pseudomoves[i]);
if(check(false) == false){
legal_moves.push_back(pseudomoves[i]);
}
undomove(original,pseudomoves[i]);
}
return legal_moves;
}
return legal_moves;
}
str computer_move(unsigned short int depth){
str bestmove;
bestmove = miniMaxroot(depth,false);
std::cout << "Bestmove: " << bestmove << "\n";
perform(bestmove);
return bestmove;
}
};
Antworten
Allgemeine Beobachtungen
Im Gegensatz zu einigen moderneren Sprachen wie C # und Java wird eine C ++ - Klasse im Allgemeinen als zwei Dateien implementiert: eine Header-Datei im Allgemeinen mit der Dateierweiterung .hund eine Quelldatei im Allgemeinen mit den Dateierweiterungen .cppoder .cc. Dies ist hauptsächlich auf die Tatsache zurückzuführen, dass C ++ die Programmiersprache C entwickelt hat, die auch Header- und Quelldateien enthält.
In C ++ befinden sich die Klassendeklarationen in der Header-Datei und die Implementierungen der Mitgliedsfunktionen in der Quelldatei. Diese Regelung hat oder hatte Vorteile. Solange die Deklarationen gleich bleiben, können die zugrunde liegenden Mitgliedsfunktionen nach Bedarf geändert, neu kompiliert und zur Installation an den Client oder Kunden gesendet werden, ohne dass eine Hauptversionsnummer geändert wird. Änderungen an Klassendeklarationen erforderten Änderungen der Versionsnummer.
Der zweite mögliche Vorteil der Trennung der Deklarationen von der Quelle besteht darin, dass die gesamte Implementierung überarbeitet werden kann, solange sich die Klassendeklarationen nicht ändern. Dies könnte größere Änderungen ermöglichen, um die Leistung oder die Speichernutzung zu verbessern, ohne dass der externe Code geändert werden muss.
Der Code in dieser Frage befindet sich in einer Datei, was die Wartung erschwert. Dies bedeutet auch, dass der gesamte Code mithilfe einer include-Anweisung in die Datei aufgenommen werden muss, die für die Verwendung erforderlich ist. Dies führt zu unnötig langen Kompilierungs- und Erstellungszeiten.
Am Ende der Antwort habe ich den möglichen Header und die Quelldatei für diesen Code eingefügt. Keine der Logik wurde geändert.
Befolgen Sie auf jeden Fall alle Vorschläge, die @Edward in der vorherigen Bewertung gemacht hat.
Stellen Sie öffentliche Erklärungen an die erste Stelle
Innerhalb der Header-Datei sollte zuerst die Liste aller öffentlichen Mitglieder beginnen, die mit den Konstruktoren beginnen, dann die geschützten Mitglieder und schließlich die privaten Mitglieder. Diese Organisation ist so eingerichtet, dass die Benutzer der Klasse schnell finden können, was sie benötigen. Dies wird nicht von der Sprache oder den Compilern erzwungen, sondern ist allgemein üblich. C ++ unterscheidet sich auch von mindestens C # (ich habe nicht in Java programmiert, daher weiß ich es nicht) darin, dass öffentliche Mitglieder gruppiert werden können.
class CLASSNAME {
public:
Public_Member_0;
Public_Member_1;
Public_Constructor_2(){}
Public_Member_3(){}
protected:
Private_Member_0;
Private_Member_1(){}
Private_Member_2(){}
private:
Private_Member_0;
Private_Member_1(){}
Private_Member_2(){}
}
Machen Sie Elemente, auf die nicht extern zugegriffen werden soll
Es gibt keinen Grund, warum die Datendarstellung der Karte öffentlich sein sollte, sie sollte höchstens geschützt sein, damit Klassen, die für die ChessKlasse erben, darauf zugreifen können. Es wäre sogar noch besser, wenn es privat wäre und geschützte Mitglieder Zugang gewähren würden. Es könnte auch als statische Variable in der Chess.cppDatei deklariert werden, was eine einfache Änderung der Datendarstellung ermöglichen würde.
Komplexität!
Die Klasse als Ganzes sowie die meisten Mitgliedsfunktionen sind zu komplex (zu viel tun). Funktionen sollten nur versuchen, ein Problem zu lösen, und sie sollten nicht größer sein als ein Bildschirm in einem Editor oder ein Blatt Papier, wenn sie gedruckt werden. Alles, was größer ist, ist sehr schwer zu verstehen, zu schreiben, zu lesen und somit zu warten.
Ich schlage vor, dass Sie sich über das Prinzip der Einzelverantwortung informieren, in dem es heißt:
dass jedes Modul, jede Klasse oder Funktion die Verantwortung für einen einzelnen Teil der von der Software bereitgestellten Funktionalität haben sollte und dass die Verantwortung vollständig von diesem Modul, dieser Klasse oder Funktion übernommen werden sollte.
Dies ist eines der Hauptprinzipien der objektorientierten Programmierung, tatsächlich ist es das Sin der SOLID-Programmierung .
In der objektorientierten Computerprogrammierung ist SOLID eine Abkürzung für fünf Designprinzipien, die Software-Designs verständlicher, flexibler und wartbarer machen sollen. Es hängt nicht mit den GRASP-Software-Designprinzipien zusammen. Die Prinzipien sind eine Teilmenge vieler Prinzipien, die vom amerikanischen Softwareentwickler und Ausbilder Robert C. Martin gefördert werden. Obwohl sie für jedes objektorientierte Design gelten, können die SOLID-Prinzipien auch eine Kernphilosophie für Methoden wie agile Entwicklung oder adaptive Softwareentwicklung bilden. Die Theorie der SOLID-Prinzipien wurde von Martin in seiner 2000 erschienenen Arbeit Design Principles and Design Patterns eingeführt, obwohl das Akronym SOLID später von Michael Feathers eingeführt wurde.
Es könnte besser sein, mehrere Klassen zu haben, eine Klasse zum Implementieren des Boards, eine abstrakte Basisklasse für alle Arten von Stücken mit einer abstrakten Funktion zum Abrufen des Zuges. Jedes Stück könnte dann von einer Unterklasse implementiert werden, die von der Basisklasse erbt . Die Board-Klasse würde die printboard()Funktion besitzen.
Alle lokalen Variablen initialisieren
Eine bewährte Methode besteht darin, jede Variable so zu initialisieren, wie sie deklariert ist. C ++ initialisiert lokale Variablen nicht auf einen Standardwert und das Fehlen einer Initialisierung kann zu undefiniertem Verhalten führen.
Die Variable scorein der Funktion evaluationwird nicht initialisiert.
In 2 Dateien reformiert
** chess2.h **
#ifndef CHESS2_H
#define CHESS2_H
#include<vector>
#include<string>
typedef std::vector<std::string> buff;
typedef std::string str;
class Chess2
{
public:
buff pseudomoves;
buff legal_moves;
short int board[8][8] = // This array represents the chess board
{
{-2,0,0,0,0,0,-10,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,-1,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,6,0,0,0,0,0},
{0,10,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
};
int perform(str Move);
void printboard();
str push(int row, int col, int desrow, int descol);
buff getallmoves(bool turn);
str computer_move(unsigned short int depth);
private:
bool check(bool turn);
void getdiagonalmoves(bool turn, int row, int col);
void getstraigtmoves(bool turn, int row, int col);
void getknightmoves(bool turn, int row, int col);
void getpawnmoves(bool turn, int row, int col);
void getkingmoves(bool turn, int row, int col);
int evaluation();
int miniMax(int depth, bool ismax, int alpha, int beta);
str miniMaxroot(int depth, bool turn);
void undomove(int original, str Move);
};
#endif // CHESS2_H
** chess2.cpp **
#include "Chess2.h"
#include<iostream>
int Chess2::perform(str Move) {
int original;
original = board[Move[2] - 48][Move[3] - 48];
board[Move[2] - 48][Move[3] - 48] = board[Move[0] - 48][Move[1] - 48];
board[Move[0] - 48][Move[1] - 48] = 0;
return original;
}
void Chess2::printboard()
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1)
std::cout << "P ";
else if (board[i][j] == 5)
std::cout << "R ";
else if (board[i][j] == 3)
std::cout << "K ";
else if (board[i][j] == 2)
std::cout << "B ";
else if (board[i][j] == 6)
std::cout << "Q ";
else if (board[i][j] == 10)
std::cout << "KI ";
else if (board[i][j] == 0)
std::cout << ". ";
else if (board[i][j] == -1)
std::cout << "p ";
else if (board[i][j] == -5)
std::cout << "r ";
else if (board[i][j] == -3)
std::cout << "k ";
else if (board[i][j] == -2)
std::cout << "b ";
else if (board[i][j] == -6)
std::cout << "q ";
else if (board[i][j] == -10)
std::cout << "ki ";
else if (board[i][j] == -109)
std::cout << "X";
}
std::cout << std::endl;
}
}
str Chess2::push(int row, int col, int desrow, int descol) {
using std::to_string;
str mystr = to_string(row) + to_string(col) + to_string(desrow) + to_string(descol);
return mystr;
}
str Chess2::computer_move(unsigned short int depth) {
str bestmove;
bestmove = miniMaxroot(depth, false);
std::cout << "Bestmove: " << bestmove << "\n";
perform(bestmove);
return bestmove;
}
buff Chess2::getallmoves(bool turn) {
pseudomoves.clear();
legal_moves.clear();
int original;
if (turn) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j]) continue;
else if (board[i][j] == 1) getpawnmoves(true, i, j);
else if (board[i][j] == 2) getdiagonalmoves(true, i, j);
else if (board[i][j] == 3) getknightmoves(true, i, j);
else if (board[i][j] == 5) getstraigtmoves(true, i, j);
else if (board[i][j] == 6) {
getdiagonalmoves(true, i, j);
getstraigtmoves(true, i, j);
}
else if (board[i][j] == 10) getkingmoves(true, i, j);
}
}
return pseudomoves;
for (long unsigned int i = 0; i < pseudomoves.size(); i++) {
original = perform(pseudomoves[i]);
if (check(true) == false) {
legal_moves.push_back(pseudomoves[i]);
}
undomove(original, pseudomoves[i]);
}
return legal_moves;
}
else if (!turn) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j]) continue;
else if (board[i][j] == -1) getpawnmoves(false, i, j);
else if (board[i][j] == -2) getdiagonalmoves(false, i, j);
else if (board[i][j] == -3) getknightmoves(false, i, j);
else if (board[i][j] == -5) getstraigtmoves(false, i, j);
else if (board[i][j] == -6) {
getdiagonalmoves(false, i, j);
getstraigtmoves(false, i, j);
}
else if (board[i][j] == -10) getkingmoves(false, i, j);
}
}
for (long unsigned int i = 0; i < pseudomoves.size(); i++) {
original = perform(pseudomoves[i]);
if (check(false) == false) {
legal_moves.push_back(pseudomoves[i]);
}
undomove(original, pseudomoves[i]);
}
return legal_moves;
}
return legal_moves;
}
bool Chess2::check(bool turn) {
if (turn == true) {
int row, col;
//Finding the king on the board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 10) {
row = i;
col = j;
}
}
}
//Finding the king on the board
if (row != 0 && col != 0 && board[row - 1][col - 1] == -1) return true;
else if (row != 0 && col != 7 && board[row - 1][col + 1] == -1) return true;
int a, b;
a = row;
b = col;
if (a != 0 && b != 0) {
for (;;) {
a -= 1;
b -= 1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 0 || b == 0) break;
}
}
a = row;
b = col;
if (a != 0 && b != 7) {
for (;;) {
a -= 1;
b += 1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 0 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7 && b != 0) {
for (;;) {
a += 1;
b -= 1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 7 || b == 0) break;
}
}
a = row;
b = col;
if (a != 7 && b != 7) {
for (;;) {
a += 1;
b += 1;
if (board[a][b] == -6 || board[a][b] == -2) return true;
if (board[a][b] != 0 || a == 7 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7) {
for (;;) {
a += 1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || a == 7) break;
}
}
a = row;
b = col;
if (a != 0) {
for (;;) {
a -= 1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || a == 0) break;
}
}
a = row;
b = col;
if (b != 7) {
for (;;) {
b += 1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || b == 7) break;
}
}
a = row;
b = col;
if (b != 0) {
for (;;) {
b -= 1;
if (board[a][b] == -6 || board[a][b] == -5) return true;
if (board[a][b] != 0 || b == 0) break;
}
}
if (row > 0 && col < 6 && board[row - 1][col + 2] == -3)return true;
if (row > 1 && col < 7 && board[row - 2][col + 1] == -3)return true;
if (row < 7 && col < 6 && board[row + 1][col + 2] == -3)return true;
if (row < 6 && col < 7 && board[row + 2][col + 1] == -3)return true;
if (row < 6 && col > 0 && board[row + 2][col - 1] == -3)return true;
if (row < 7 && col > 1 && board[row + 1][col - 2] == -3)return true;
if (row > 1 && col > 0 && board[row - 2][col - 1] == -3)return true;
if (row > 0 && col > 1 && board[row - 1][col - 2] == -3)return true;
if (row != 7 && board[row + 1][col] == -10)return true;
if (row != 0 && board[row - 1][col] == -10)return true;
if (col != 7 && board[row][col + 1] == -10) return true;
if (col != 0 && board[row][col - 1] == -10) return true;
if (row != 7 && col != 7 && board[row + 1][col + 1] == -10)return true;
if (row != 7 && col != 0 && board[row + 1][col - 1] == -10) return true;
if (row != 0 && col != 7 && board[row - 1][col + 1] == -10) return true;
if (row != 0 && col != 0 && board[row - 1][col - 1] == -10) return true;
}
else if (turn == false) {
int row, col;
//Finding the king on the board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == -10) {
row = i;
col = j;
}
}
}
//Finding the king on the board
if (row != 7 && col != 0 && board[row + 1][col - 1] == 1) return true;
else if (row != 7 && col != 7 && board[row + 1][col + 1] == 1) return true;
int a, b;
a = row;
b = col;
if (a != 0 && b != 0) {
for (;;) {
a -= 1;
b -= 1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 0 || b == 0) break;
}
}
a = row;
b = col;
if (a != 0 && b != 7) {
for (;;) {
a -= 1;
b += 1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 0 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7 && b != 0) {
for (;;) {
a += 1;
b -= 1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 7 || b == 0) break;
}
}
a = row;
b = col;
if (a != 7 && b != 7) {
for (;;) {
a += 1;
b += 1;
if (board[a][b] == 6 || board[a][b] == 2) return true;
if (board[a][b] != 0 || a == 7 || b == 7) break;
}
}
a = row;
b = col;
if (a != 7) {
for (;;) {
a += 1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || a == 7) break;
}
}
a = row;
b = col;
if (a != 0) {
for (;;) {
a -= 1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || a == 0) break;
}
}
a = row;
b = col;
if (b != 7) {
for (;;) {
b += 1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || b == 7) break;
}
}
a = row;
b = col;
if (b != 0) {
for (;;) {
b -= 1;
if (board[a][b] == 6 || board[a][b] == 5) return true;
if (board[a][b] != 0 || b == 0) break;
}
}
if (row > 0 && col < 6 && board[row - 1][col + 2] == 3)return true;
if (row > 1 && col < 7 && board[row - 2][col + 1] == 3)return true;
if (row < 7 && col < 6 && board[row + 1][col + 2] == 3)return true;
if (row < 6 && col < 7 && board[row + 2][col + 1] == 3)return true;
if (row < 6 && col > 0 && board[row + 2][col - 1] == 3)return true;
if (row < 7 && col > 1 && board[row + 1][col - 2] == 3)return true;
if (row > 1 && col > 0 && board[row - 2][col - 1] == 3)return true;
if (row > 0 && col > 1 && board[row - 1][col - 2] == 3)return true;
if (row != 7 && board[row + 1][col] == 10)return true;
if (row != 0 && board[row - 1][col] == 10)return true;
if (col != 7 && board[row][col + 1] == 10) return true;
if (col != 0 && board[row][col - 1] == 10) return true;
if (row != 7 && col != 7 && board[row + 1][col + 1] == 10)return true;
if (row != 7 && col != 0 && board[row + 1][col - 1] == 10) return true;
if (row != 0 && col != 7 && board[row - 1][col + 1] == 10) return true;
if (row != 0 && col != 0 && board[row - 1][col - 1] == 10) return true;
}
return false;
}
void Chess2::getdiagonalmoves(bool turn, int row, int col) {
int a, b;
if (turn) {
a = row;
b = col;
if (a != 0 && b != 0) {
for (;;) {
a -= 1;
b -= 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 0 && b != 7) {
for (;;) {
a -= 1;
b += 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7 && b != 7) {
for (;;) {
a += 1;
b += 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7 && b != 0) {
for (;;) {
a += 1;
b -= 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
}
else if (!turn) {
a = row;
b = col;
if (a != 0 && b != 0) {
for (;;) {
a -= 1;
b -= 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 0 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 0 && b != 7) {
for (;;) {
a -= 1;
b += 1;
if (board[a][b] < 0)
break;
if (board[a][b] > 0 || a == 0 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (board[a][b] == 0)
pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7 && b != 7) {
for (;;) {
a += 1;
b += 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7 && b != 0) {
for (;;) {
a += 1;
b -= 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b])pseudomoves.push_back(push(row, col, a, b));
}
}
}
}
void Chess2::getstraigtmoves(bool turn, int row, int col)
{
int a, b;
if (turn) {// white player
a = row;
b = col;
if (a != 0) {
for (;;) {
a -= 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7) {
for (;;) {
a += 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || a == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (b != 0) {
for (;;) {
b -= 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (b != 7) {
for (;;) {
b += 1;
if (board[a][b] > 0) break;
if (board[a][b] < 0 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
}
else if (!turn) // black player
{
a = row;
b = col;
if (a != 0) {
for (;;) {
a -= 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (a != 7) {
for (;;) {
a += 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || a == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (b != 0) {
for (;;) {
b -= 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || b == 0) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
a = row;
b = col;
if (b != 7) {
for (;;) {
b += 1;
if (board[a][b] < 0) break;
if (board[a][b] > 0 || b == 7) {
pseudomoves.push_back(push(row, col, a, b));
break;
}
if (!board[a][b]) pseudomoves.push_back(push(row, col, a, b));
}
}
}
//returnpseudomoves;
}
void Chess2::getknightmoves(bool turn, int row, int col) {
if (turn) {
if (row > 0 && col < 6 && board[row - 1][col + 2] <= 0) // one up two right
pseudomoves.push_back(push(row, col, row - 1, col + 2));
if (row > 1 && col < 7 && board[row - 2][col + 1] <= 0) // two up one right
pseudomoves.push_back(push(row, col, row - 2, col + 1));
if (row < 7 && col < 6 && board[row + 1][col + 2] <= 0) // one down two right
pseudomoves.push_back(push(row, col, row + 1, col + 2));
if (row < 6 && col < 7 && board[row + 2][col + 1] <= 0) // two down one right
pseudomoves.push_back(push(row, col, row + 2, col + 1));
if (row < 6 && col > 0 && board[row + 2][col - 1] <= 0) //two down one left
pseudomoves.push_back(push(row, col, row + 2, col - 1));
if (row < 7 && col > 1 && board[row + 1][col - 2] <= 0) // one down two left
pseudomoves.push_back(push(row, col, row + 1, col - 2));
if (row > 1 && col > 0 && board[row - 2][col - 1] <= 0) // two up one left
pseudomoves.push_back(push(row, col, row - 2, col - 1));
if (row > 0 && col > 1 && board[row - 1][col - 2] <= 0) // one up two left
pseudomoves.push_back(push(row, col, row - 1, col - 2));
}
else if (!turn) {
if (row > 0 && col < 6 && board[row - 1][col + 2] >= 0)pseudomoves.push_back(push(row, col, row - 1, col + 2));
if (row > 1 && col < 7 && board[row - 2][col + 1] >= 0)pseudomoves.push_back(push(row, col, row - 2, col + 1));
if (row < 7 && col < 6 && board[row + 1][col + 2] >= 0)pseudomoves.push_back(push(row, col, row + 1, col + 2));
if (row < 6 && col < 7 && board[row + 2][col + 1] >= 0)pseudomoves.push_back(push(row, col, row + 2, col + 1));
if (row < 6 && col > 0 && board[row + 2][col - 1] >= 0)pseudomoves.push_back(push(row, col, row + 2, col - 1));
if (row < 7 && col > 1 && board[row + 1][col - 2] >= 0)pseudomoves.push_back(push(row, col, row + 1, col - 2));
if (row > 1 && col > 0 && board[row - 2][col - 1] >= 0)pseudomoves.push_back(push(row, col, row - 2, col - 1));
if (row > 0 && col > 1 && board[row - 1][col - 2] >= 0)pseudomoves.push_back(push(row, col, row - 1, col - 2));
}
//returnpseudomoves;
}
void Chess2::getpawnmoves(bool turn, int row, int col) {
if (turn) {
if (row == 6 && board[row - 1][col] == 0 && board[row - 2][col] == 0)
pseudomoves.push_back(push(row, col, row - 2, col));
if (board[row - 1][col] == 0)
pseudomoves.push_back(push(row, col, row - 1, col));
if (col != 0 && board[row - 1][col - 1] < 0)
pseudomoves.push_back(push(row, col, row - 1, col - 1));
if (col != 7 && board[row - 1][col + 1] < 0)
pseudomoves.push_back(push(row, col, row - 1, col + 1));
}
else if (!turn) {
if (row == 7) //returnpseudomoves;
if (row == 1 && board[row + 1][col] == 0 && board[row + 2][col] == 0)
pseudomoves.push_back(push(row, col, row + 2, col));
if (board[row + 1][col] == 0)
pseudomoves.push_back(push(row, col, row + 1, col));
if (col != 0 && board[row + 1][col - 1] > 0)
pseudomoves.push_back(push(row, col, row + 1, col - 1));
if (col != 7 && board[row + 1][col + 1] > 0)
pseudomoves.push_back(push(row, col, row + 1, col + 1));
}
//returnpseudomoves;
}
void Chess2::getkingmoves(bool turn, int row, int col) {
if (!turn) {
if (row != 7 && board[row + 1][col] >= 0) pseudomoves.push_back(push(row, col, row + 1, col));
if (row != 0 && board[row - 1][col] >= 0) pseudomoves.push_back(push(row, col, row - 1, col));
if (col != 7 && board[row][col + 1] >= 0) pseudomoves.push_back(push(row, col, row, col + 1));
if (col != 0 && board[row][col - 1] >= 0) pseudomoves.push_back(push(row, col, row, col - 1));
if (row != 7 && col != 7 && board[row + 1][col + 1] >= 0) pseudomoves.push_back(push(row, col, row + 1, col + 1));
if (row != 7 && col != 0 && board[row + 1][col - 1] >= 0) pseudomoves.push_back(push(row, col, row + 1, col - 1));
if (row != 0 && col != 7 && board[row - 1][col + 1] >= 0) pseudomoves.push_back(push(row, col, row - 1, col + 1));
if (row != 0 && col != 0 && board[row - 1][col - 1] >= 0) pseudomoves.push_back(push(row, col, row - 1, col - 1));
}
else if (turn) {
if (row != 7 && board[row + 1][col] <= 0) pseudomoves.push_back(push(row, col, row + 1, col));
if (row != 0 && board[row - 1][col] <= 0) pseudomoves.push_back(push(row, col, row - 1, col));
if (col != 7 && board[row][col + 1] <= 0) pseudomoves.push_back(push(row, col, row, col + 1));
if (col != 0 && board[row][col - 1] <= 0) pseudomoves.push_back(push(row, col, row, col - 1));
if (row != 7 && col != 7 && board[row + 1][col + 1] <= 0) pseudomoves.push_back(push(row, col, row + 1, col + 1));
if (row != 7 && col != 0 && board[row + 1][col - 1] <= 0) pseudomoves.push_back(push(row, col, row + 1, col - 1));
if (row != 0 && col != 7 && board[row - 1][col + 1] <= 0) pseudomoves.push_back(push(row, col, row - 1, col + 1));
if (row != 0 && col != 0 && board[row - 1][col - 1] <= 0) pseudomoves.push_back(push(row, col, row - 1, col - 1));
}
//returnpseudomoves;
}
int Chess2::evaluation() {
int score;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j]) continue;
if (board[i][j] == 1) score -= 10;
else if (board[i][j] == 2)score -= 30;
else if (board[i][j] == 3)score -= 30;
else if (board[i][j] == 5)score -= 50;
else if (board[i][j] == 6)score -= 90;
else if (board[i][j] == 10)score -= 900;
else if (board[i][j] == -1)score += 10;
else if (board[i][j] == -2)score += 30;
else if (board[i][j] == -3)score += 30;
else if (board[i][j] == -5)score += 50;
else if (board[i][j] == -6)score += 60;
else if (board[i][j] == -10)score += 900;
}
}
return score;
}
int Chess2::miniMax(int depth, bool ismax, int alpha, int beta) {
if (depth == 0) {
return evaluation();
}
int maxeval = -999999;
int mineval = 999999;
buff possiblemoves;
int original;
int eval;
if (ismax == true) {
possiblemoves = getallmoves(false);
for (long unsigned int i = 0; i < possiblemoves.size(); i++) {
original = perform(possiblemoves[i]);
eval = miniMax(depth - 1, false, alpha, beta);
undomove(original, possiblemoves[i]);
if (eval > maxeval)
maxeval = eval;
if (alpha >= eval)
alpha = eval;
if (beta <= alpha)
break;
}
return maxeval;
}
else {
possiblemoves = getallmoves(true);
for (long unsigned int i = 0; i < possiblemoves.size(); i++) {
original = perform(possiblemoves[i]);
eval = miniMax(depth - 1, true, alpha, beta);
undomove(original, possiblemoves[i]);
if (eval < mineval)
mineval = eval;
if (beta <= eval)
beta = eval;
if (beta <= alpha)
break;
}
return mineval;
}
}
str Chess2::miniMaxroot(int depth, bool turn) {
str bestmove;
int maxeval = -9999999;
buff allmoves = getallmoves(turn);
int original;
int eval;
for (long unsigned int i = 0; i < allmoves.size(); i++) {
original = perform(allmoves[i]);
eval = miniMax(depth - 1, false, -99999999, 99999999);
std::cout << "Move: " << allmoves[i] << " Points: " << eval << "\n";
undomove(original, allmoves[i]);
if (eval > maxeval) {
maxeval = eval;
bestmove = allmoves[i];
}
}
return bestmove;
}
void Chess2::undomove(int original, str Move) {
board[Move[0] - 48][Move[1] - 48] = board[Move[2] - 48][Move[3] - 48];
board[Move[2] - 48][Move[3] - 48] = original;
}