Question: #pragma once #include #include #include #include Piece.h using namespace std; class Player { string name; string color; bool direction; // 1 - white | 0

#pragma once

#include

#include

#include

#include "Piece.h"

using namespace std;

class Player {

string name;

string color;

bool direction; // 1 - white | 0 - black

Piece *pieces;

bool isMachine;

int numOfPieces;

const int MAX_DEPTH = 2;

bool canMove(Piece* from, int endCol, int endRow, Player* opponent) {

if (opponent->getPiece(endCol, endRow) != NULL || getPiece(endCol, endRow) != NULL)return false;

int shift, kingRow;

if (direction) {

kingRow = 7;

shift = 1;

}

else {

kingRow = 0;

shift = -1;

}

if (!from->isKing()) {

if (abs(from->getCol() - endCol) == 1 && abs(from->getRow() - endRow) == 1 && endRow - from->getRow() == shift

) {

return true;

}

int intermediateCol = (from->getCol() + endCol) / 2;

int intermediateRow = (from->getRow() + endRow) / 2;

if (abs(from->getCol() - endCol) == 2 && abs(from->getRow() - endRow) == 2 && getPiece(intermediateCol, intermediateRow) == NULL

&& opponent->getPiece(intermediateCol, intermediateRow) != NULL) {

return true;

}

}

else {

bool inARow = false;

if (abs(from->getCol() - endCol) == abs(from->getRow() - endRow) && from->getRow() != endRow) {

for (int i = from->getCol(), j = from->getRow(); i != endCol, j != endRow;

i += (from->getCol() - endCol) / abs(from->getCol() - endCol), j += (from->getRow() - endRow) / abs(from->getRow() - endRow)) {

if (getPiece(i, j) != NULL) return false;

if (opponent->getPiece(i, j) != NULL&&inARow)return false;

if (opponent->getPiece(i, j) != NULL && !inARow)inARow = true;

if (opponent->getPiece(i, j) == NULL)inARow = false;

}

return true;

}

}

return false;

}

bool isMoved(Piece* from, int endCol, int endRow, Player* opponent){

if (opponent->getPiece(endCol, endRow) != NULL || getPiece(endCol, endRow) != NULL)return false;

int shift, kingRow;

if (direction){

kingRow = 7;

shift = 1;

}

else {

kingRow = 0;

shift = -1;

}

if (!from->isKing()) {

if (abs(from->getCol() - endCol) == 1 && abs(from->getRow() - endRow) == 1 && endRow - from->getRow() == shift

) {

from->setCol(endCol);

from->setRow(endRow);

if (endRow == kingRow)from->setKing();

return true;

}

int intermediateCol = (from->getCol() + endCol) / 2;

int intermediateRow = (from->getRow() + endRow) / 2;

if (abs(from->getCol() - endCol) == 2 && abs(from->getRow() - endRow) == 2 && getPiece(intermediateCol, intermediateRow) == NULL

&& opponent->getPiece(intermediateCol, intermediateRow) != NULL) {

opponent->deletePiece(intermediateCol, intermediateRow);

from->setCol(endCol);

from->setRow(endRow);

return true;

}

}

else {

bool inARow = false;

if (abs(from->getCol() - endCol) == abs(from->getRow() - endRow) && from->getRow() != endRow) {

for (int i = from->getCol(), j = from->getRow(); i != endCol, j != endRow;

i += (from->getCol() - endCol) / abs(from->getCol() - endCol), j += (from->getRow() - endRow) / abs(from->getRow() - endRow)) {

if (getPiece(i, j) != NULL) return false;

if (opponent->getPiece(i, j) != NULL&&inARow)return false;

if (opponent->getPiece(i, j) != NULL&&!inARow)inARow = true;

if (opponent->getPiece(i, j) == NULL)inARow = false;

}

for (int i = from->getCol(), j = from->getRow(); i != endCol, j != endRow;

i += (from->getCol() - endCol) / abs(from->getCol() - endCol), j += (from->getRow() - endRow) / abs(from->getRow() - endRow)) {

if (opponent->getPiece(i, j) != NULL) opponent->deletePiece(i, j);

}

return true;

}

}

return false;

}

int getNumber(string info){

int num = -1;

cout << info << endl;

while (num < 0 || num>7) {

cout << "Enter number from 0 to 7" << endl;

cin >> num;

cin.ignore();

if (num == -1)return num;

}

return num;

}

void make_moveAI(Player* opponent){

Piece *start = pieces;

do{

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (abs(start->getCol() - i) == 2 && abs(start->getRow() - j) == 2)

if (isMoved(start, i, j, opponent))return;

}

}

start = start->getNext();

} while (start != NULL);

start = pieces;

do{

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (isMoved(start, i, j, opponent))return;

}

}

start = start->getNext();

} while (start != NULL);

}

public:

Player(){

this->name = "";

this->color = "white";

this->direction = 1;

this->isMachine = 0;

this->numOfPieces = 0;

}

Player(string name, bool isMachine, bool direction){

this->name = name;

this->isMachine = isMachine;

this->direction = direction;

if (direction)color = "white";

else color = "black";

}

Player(Player *copy) {

this->color = copy->color;

this->direction = copy->direction;

this->name = copy->name;

this->isMachine = copy->isMachine;

this->numOfPieces = copy->numOfPieces;

Piece* start = copy->pieces;

Piece *piece = NULL;

Piece *startThis = piece;

int i = 0;

while (start) {

Piece *nextPiece = new Piece(start->getCol(), start->getRow(), start->isKing());

if (i != 0) {

piece->setNext(nextPiece);

}

else {

startThis = nextPiece;

}

piece = nextPiece;

start = start->getNext();

i++;

}

this->pieces = startThis;

}

int minimax(Player *player,Player *opponent, int depth, bool isMaxPlayer) {

int bestMoveFromCol = -1, bestMoveFromRow = -1, bestMoveToCol = -1,bestMoveToRow = -1;

if (depth >= MAX_DEPTH ||player->numOfPieces == 0 || opponent->numOfPieces==0) {

return player->getNumberOfPieces() - opponent->getNumberOfPieces();

}

if (isMaxPlayer) {

int bestValue = -100000000;

Player* tempPlayer = new Player(player);

Player* tempOpponent = new Player(opponent);

Piece *start = tempPlayer->getPieces();

while (start) {

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (tempPlayer->isMoved(new Piece(start->getCol(),start->getRow(),start->isKing()), j, i, tempOpponent)) {

int val = minimax(tempPlayer, tempOpponent, depth + 1, !isMaxPlayer);

if (bestValue < val) {

bestValue = val;

bestMoveFromCol = start->getCol();

bestMoveFromRow = start->getRow();

bestMoveToCol = j;

bestMoveToRow = i;

}

tempPlayer = new Player(player);

tempOpponent = new Player(opponent);

}

}

}

start = start->getNext();

}

}

else {

int bestValue = 100000000;

Player* tempPlayer = new Player(player);

Player* tempOpponent = new Player(opponent);

Piece *start = tempPlayer->getPieces();

while (start) {

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (tempPlayer->isMoved(new Piece(start->getCol(), start->getRow(), start->isKing()), j, i, tempOpponent)) {

int val = minimax(tempPlayer, tempOpponent, depth + 1, !isMaxPlayer);

if (bestValue > val) {

bestValue = val;

bestMoveFromCol = start->getCol();

bestMoveFromRow = start->getRow();

bestMoveToCol = j;

bestMoveToRow = i;

}

tempPlayer = new Player(player);

tempOpponent = new Player(opponent);

}

}

}

start = start->getNext();

}

}

if (bestMoveToCol == -1) {

return player->getNumberOfPieces() - opponent->getNumberOfPieces();

}

if (depth == 0 && bestMoveToCol != -1) {

Piece *piece = player->getPiece(bestMoveFromCol, bestMoveFromRow);

player->isMoved(piece, bestMoveToCol, bestMoveToRow, opponent);

}

}

int getNumberOfPieces() {

return this->numOfPieces;

}

Piece* getPiece(int col, int row){

Piece *start = pieces;

if (start == NULL) return NULL;

do{

if (start->getCol() == col && start->getRow() == row)return start;

start = start->getNext();

} while (start != NULL);

return NULL;

}

string getName(){

return name;

}

void setDirection(bool direction){

this->direction = direction;

}

bool make_move(Player* opponent){

if (isMachine){

//make_moveAI(opponent);

minimax(this, opponent, 0, true);

cout << endl << endl << endl;

return true ;

}

Piece* piece = NULL;

int startCol, startRow;

while (!piece) {

startCol = getNumber("Enter col from :");

if (startCol == -1)return false;

startRow = getNumber("Enter row from :");

if (startRow == -1)return false;

piece = getPiece(startCol, startRow);

if (piece == NULL){

cout << "Incorrect position!" << endl << endl;

}

}

int endCol, endRow;

endCol = getNumber("Enter col to ");

if (endCol == -1)return false;

endRow = getNumber("Enter row to ");

if (endRow == -1)return false;

int count = 0;

while (!isMoved(piece, endCol, endRow, opponent)) {

cout << "Incorrect position!" << endl << endl;

if (count > 4){

cout << "Do you want to pick another from position?(y,n)" << endl;

string choice;

cin >> choice;

if (choice == "y")return make_move(opponent);

}

else {

endCol = getNumber("Enter col to ");

if (endCol == -1)return false;

endRow = getNumber("Enter row to");

if (endRow == -1)return false;

count++;

}

}

cout << endl << endl << endl;

return true;

}

Piece* getPieces(){

return pieces;

}

void addPiece(int col, int row){

numOfPieces++;

Piece* newPiece = new Piece(col, row, 0);

newPiece->setNext(NULL);

Piece *start = pieces;

if (start != NULL) {

while (start->getNext() != NULL) {

start = start->getNext();

}

start->setNext(newPiece);

}

else {

pieces = newPiece;

}

}

void deletePiece(int col, int row){

numOfPieces--;

Piece *start = pieces;

if (start == NULL)

return;

if (start->getNext() == NULL) {

delete start;

pieces = NULL;

}

else {

Piece *previous = NULL;

do {

if (start->getCol() == col && start->getRow() == row) break;

previous = start;

start = start->getNext();

} while (previous != NULL);

previous->setNext(start->getNext());

delete start;

}

}

bool lose(){

return (pieces == NULL);

}

};

&&&&&&&&&&&explain / explain minimax function thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!