Untitled
unknown
plain_text
2 years ago
6.6 kB
14
Indexable
int ChessGame::Score() {
white = whitePieces;
black = blackPieces;
save = selectedPiece;
int maxx = -9999;
int che = -1;
int moveAt = -1;
int scr = getScore(whitePieces, blackPieces);
if (!playerTurn) {
for (int i = 0; i < 16; i++) {
bool check = false;
if (blackPieces[i].getPosition() != -1) {
int count = 0;
selectedPiece = &blackPieces[i];
for (int j = 0; j < selectedPiece->getPossibleMoves().size(); j++) {
killChess(selectedPiece->getPossibleMoves().at(j), check);
cout << "possible: " << selectedPiece->getPossibleMoves().at(j) << "i = " << i << endl;
blackPieces[i].setPosition(selectedPiece->getPossibleMoves().at(j));
int x = minimax(whitePieces, blackPieces, 3, -9999, 9999, playerTurn);
blackPieces = black;
if (x > maxx) {
maxx = x;
moveAt = selectedPiece->getPossibleMoves().at(j);
che = i;
}
}
}
}
}
selectedPiece = save;
whitePieces = white;
blackPieces = black;
cout << "score: " << maxx << ", move " << che << "at" << moveAt << endl;
return (maxx);
}
int ChessGame::minimax(const std::array<Piece, 16>& white , const std::array<Piece, 16>& black, int depth, int alpha, int beta, bool maximizingPlayer) {
if (depth == 0 /* hoặc trạng thái kết thúc */) {
return getScore(white, black);
}
std::array<Piece, 16> childBlack;
std::array<Piece, 16> childWhite;
Piece* selectP;
childBlack = black;
childWhite = white;
if (maximizingPlayer) {
int maxEval = INT_MIN;
// Tạo danh sách các trạng thái con
for (int i = 0; i < 16; i++) {
if (childBlack[i].getPosition() != -1) {
selectP = &childBlack[i];
for (int j = 0; j < selectP->getPossibleMoves().size(); j++) {
killChess(selectP->getPossibleMoves().at(j), false);
childBlack[i].setPosition(selectP->getPossibleMoves().at(j));
//xxx++;
int eval = minimax(childWhite, childBlack, depth - 1, alpha, beta, false);
maxEval = max(maxEval, eval);
alpha = max(alpha, eval);
// Cắt tỉa Alpha-Beta
if (beta <= alpha) {
break;
}
}
}
}
return maxEval;
}
else {
int minEval = INT_MAX;
// Tạo danh sách các trạng thái con
for (int i = 0; i < 16; i++) {
if (childWhite[i].getPosition() != -1) {
selectP = &childWhite[i];
for (int j = 0; j < selectP->getPossibleMoves().size(); j++) {
killChess(selectP->getPossibleMoves().at(j), true);
childWhite[i].setPosition(selectP->getPossibleMoves().at(j));
int eval = minimax(childWhite, childBlack, depth - 1, alpha, beta, true);
minEval = std::min(minEval, eval);
beta = std::min(beta, eval);
//xxx++;
// Cắt tỉa Alpha-Beta
if (beta <= alpha) {
break;
}
}
}
}
return minEval;
}
}
void ChessGame::undo(bool check) {
if (st > 1) {
int x = front(saveMove);
if (x > 0) {
if (check) {
whitePieces[x / 100].setPosition(x % 100 - 1);
}
else {
blackPieces[x / 100].setPosition(x % 100 - 1);
}
}
int y = front(saveDead);
if (y > 0) {
if (check) {
whitePieces[y / 100].setPosition(y % 100 - 1);
}
else {
blackPieces[y / 100].setPosition(y % 100 - 1);
}
}
}
}
void ChessGame::killChess(int go, bool check) {
for (int i = 0; i < 16; i++) {
if (check) { // if white
if (blackPieces[i].getPosition() == go) {
blackPieces[i].setPosition(-1);
push(saveDead, i * 100 + go + 1); // neu quan trang chet /100 == 1, khi su dung -1
break;
}
}
else { // if black
if (whitePieces[i].getPosition() == go) {
whitePieces[i].setPosition(-1);
push(saveDead, i * 100 + go + 1); // neu quan den chet /100 == 0, khi su dung -1
break;
}
}
}
}
int ChessGame::getScore(const std::array<Piece, 16>& whitePiece, const std::array<Piece, 16>& blackPiece) {
int count = 0;
for (int i = 0; i < 16; i++) {
if (blackPieces[i].getType() == 'P' && blackPieces[i].getPosition() != -1) {
count = count + 1;
}
if (blackPieces[i].getType() == 'R' && blackPieces[i].getPosition() != -1) {
count = count + 7;
}
if (blackPieces[i].getType() == 'B' && blackPieces[i].getPosition() != -1) {
count = count + 4;
}
if (blackPieces[i].getType() == 'Q' && blackPieces[i].getPosition() != -1) {
count = count + 10;
}
if (blackPieces[i].getType() == 'K' && blackPieces[i].getPosition() != -1) {
count = count + 100;
}
if (blackPieces[i].getType() == 'N' && blackPieces[i].getPosition() != -1) {
count = count + 4;
}
if (whitePieces[i].getType() == 'P' && whitePieces[i].getPosition() != -1) {
count = count - 1;
}
if (whitePieces[i].getType() == 'R' && whitePieces[i].getPosition() != -1) {
count = count - 7;
}
if (whitePieces[i].getType() == 'B' && whitePieces[i].getPosition() != -1) {
count = count - 4;
}
if (whitePieces[i].getType() == 'Q' && whitePieces[i].getPosition() != -1) {
count = count - 10;
}
if (whitePieces[i].getType() == 'N' && whitePieces[i].getPosition() != -1) {
count = count - 4;
}
if (whitePieces[i].getType() == 'K' && whitePieces[i].getPosition() != -1) {
count = count - 100;
}
}
return count;
}
Editor is loading...