Question: program crashes after it couts #6 please enter -1- please help debug- STOP IN CLASS GAME - USING C++ CODEBLOCK $$$$$$$$$$$$$$$$$$$$$$$$ ####################### ****************************** #include #include

program crashes after it couts #6 please enter -1- please help debug- STOP IN CLASS GAME - USING C++ CODEBLOCK

$$$$$$$$$$$$$$$$$$$$$$$$

#######################

******************************

#include

#include

#include "Game.h"

using namespace std;

struct node

{

int key;

struct node *left, *right;

};

struct node *newNode(int item)

{

struct node *temp = (struct node *)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

}

void inorder(struct node *root)

{

if (root != NULL)

{

inorder(root->left);

cout << ("%d ", root->key) << endl;

inorder(root->right);

}

}

struct node* insert(struct node* node, int key)

{

if (node == NULL) return newNode(key);

if (key < node->key)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;

}

struct node * minValueNode(struct node* node)

{

struct node* current = node;

while (current->left != NULL)

current = current->left;

return current;

}

struct node* deleteNode(struct node* root, int key)

{

if (root == NULL) return root;

if (key < root->key)

root->left = deleteNode(root->left, key);

else if (key > root->key)

root->right = deleteNode(root->right, key);

else

{

if (root->left == NULL)

{

struct node *temp = root->right;

free(root);

return temp;

}

else if (root->right == NULL)

{

struct node *temp = root->left;

free(root);

return temp;

}

struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);

}

return root;

}

struct node *changeKey(struct node *root, int oldVal, int newVal)

{

root = deleteNode(root, oldVal);

root = insert(root, newVal);

return root;

}

int main() {

Game game;

while (1) {

game.instruction();

if (game.play()) {

cout << "Do you want to play again? (y/n)";

string str;

cin >> satr;

cin.ignore();

if (str == "yes" || str == "y")continue;

else break;

}

else {

cout << "Restart!" << endl;

}

}

cout << "Good bye!" << endl;

return 0;

}

**************************

player

****************

#pragma once

#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;

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;

}

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;

}

bool make_move(Player* opponent){

if (isMachine){

make_moveAI(opponent);

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){

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);

}

};

***********************

piece

********************

#pragma once

using namespace std;

class Piece {

int col;

int row;

bool is_king;

Piece* next;

public:

Piece(int col, int row, bool is_king){

this->col = col;

this->row = row;

this->is_king = is_king;

this->next = NULL;

}

void setNext(Piece* next){

this->next = next;

}

int getCol(){

return col;

}

int getRow(){

return row;

}

bool isKing(){

return is_king;

}

Piece* getNext(){

return next;

}

void setRow(int row){

this->row = row;

}

void setCol(int col){

this->col = col;

}

void setKing(){

this->is_king = true;

}

};

****************************

GAME

*******************************

#pragma once

#include

#include

#include "Piece.h"

#include "Player.h"

using namespace std;

class Game {

char board[8][8];

Player* players[2];

public:

void generate_board(){

char pieces[2], kings[2];

pieces[0] = 'ht';

pieces[1] = 'q';

kings[0] = 'HT';

kings[1] = 'Q';

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

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

Piece* piece = players[0]->getPiece(i, j);

if (piece != NULL) {

if (piece->isKing())

board[i][j] = kings[0];

else board[i][j] = pieces[0];

}

else {

piece = players[1]->getPiece(i, j);

if (piece != NULL) {

if (piece->isKing())

board[i][j] = kings[1];

else board[i][j] = pieces[1];

}

else board[i][j] = ' ';

}

}

}

}

bool play(){

string name1 = "User";

players[0] = new Player(name1, 0, 1);

players[1] = new Player("AI", 1, 0);

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

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

if ((j % 2 == 0 && i % 2 == 0) || (j % 2 == 1 && i % 2 == 1)) players[0]->addPiece(i, j);

}

}

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

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

if ((j % 2 == 0 && i % 2 == 0) || (j % 2 == 1 && i % 2 == 1)) players[1]->addPiece(i, j);

}

}

bool turn = 0;

Player* winner = NULL;

generate_board();

display_board();

while (!winner) {

if (!players[turn]->make_move(players[!turn]))return false;

if (players[!turn]->lose())winner = players[turn];

turn = !turn;

generate_board();

display_board();

}

cout << "Player " << winner->getName() << " Won!!!" << endl;

return true;

}

void display_board(){

cout << players[0]->getName() << " " << endl;

cout << " ";

for (int i = 0; i < 8; i++)cout << i << " ";

cout << endl;

cout << (char)218;

for (int i = 0; i < 15; i++)cout << (char)196;

cout << (char)191;

cout << endl;

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

if (i != 0) {

cout << '|';

for (int j = 0; j < 15; j++)cout << (char)196;

cout << '|';

cout << endl;

}

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

cout << '|';

cout << board[j][i];

}

cout << '|';

cout << i;

cout << endl;

}

cout << (char)192;

for (int i = 0; i < 15; i++)cout << (char)196;

cout << (char)217;

cout << endl;

cout << players[1]->getName() << " " << endl;

}

void instruction() {

cout << "<<>>" << endl;

cout << " 1) The computer is represented by a 'q' " << endl;

cout << " 2) The human is represented by a 't' " << endl;

cout << " 3) When a piece is kinged it will be capitalized " << endl;

cout << " 4) When a piece is kinged it may move backwards " << endl;

cout << " 5) The user will move first each time " << endl;

cout << " 6) If you want to restart the game enter -1 instead of number column or row" << endl << endl; %%%%%STOPS WORKING HERE

}

};

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!