Question: ************************************************************************** ###################################################### @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #include #include #include Piece.h using namespace std; class Player { string name; string color; bool direction; // 1 - white | 0
**************************************************************************
######################################################
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#include
#include
#include "Piece.h"
using namespace std;
class Player {
string name;
string color;
bool direction; // 1 - white | 0 - black
Piece *pieces;
bool isMachine;
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;
}
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;
}
Player(string name, bool isMachine, bool direction){
this->name = name;
this->isMachine = isMachine;
this->direction = direction;
if (direction)color = "white";
else color = "black";
}
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;
}
void make_move(Player* opponent){
if (isMachine){
make_moveAI(opponent);
cout << endl << endl << endl;
return;
}
Piece* piece = NULL;
int startCol, startRow;
while (!piece) {
startCol = getNumber("Enter col from :");
startRow = getNumber("Enter row from :");
piece = getPiece(startCol, startRow);
if (piece == NULL){
cout << "Incorrect position!" << endl << endl;
}
}
int endCol, endRow;
endCol = getNumber("Enter col to ");
endRow = getNumber("Enter row to ");
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 ");
endRow = getNumber("Enter row to");
count++;
}
}
cout << endl << endl << endl;
}
Piece* getPieces(){
return pieces;
}
void addPiece(int col, int row){
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){
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);
}
};
please explain what is going on
from top to bott
explain get piece
getnumber
make move
add piece
player
etc
c++ codeblock
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
