Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a 5x5 borads Knight escape game by c++. Create a board filled with chess knights, in which there is a single white knight in

Create a 5x5 borads Knight escape game by c++.

Create a board filled with chess knights, in which there is a single white knight in the bottom left space, the top right space is blank, and the rest of the spaces each contain a black knight. Allow the user to move the knights, where the objective of the game is to get the white knight to the empty square in the smallest amount of valid moves.

The knight can only move in the way that it does during a traditional chess game. This means that a knight can only move in a valid L-shape, following one of these two paths:

  • two squares in a horizontal direction, and one in a vertical direction OR

  • two squares in a vertical direction, and one in a horizontal direction.

  • Scoring Mechanism: The goal of the game is to try to help the knight escape in the smallest amount of moves possible. To motivate this, the game keeps track of a score, which decreases as the user makes more moves. Invalid moves result in a score penalty as well. See the samples of output shown on this page to determine how the score should be used and displayed throughout the game.

    1. The score starts at 500.

    2. The current score should be displayed with each move.

    3. If the user makes any type of invalid move, the score decreases 10 points.

    4. If the user makes a valid move, the score decreases 5 points.

    5. Once the score becomes zero or negative, the game automatically ends. See Note 7.10 below for the message that should be displayed when this occurs.

Scoring Mechanism: The goal of the game is to try to help the knight escape in the smallest amount of moves possible. To motivate this, the game keeps track of a score, which decreases as the user makes more moves. Invalid moves result in a score penalty as well. See the samples of output shown on this page to determine how the score should be used and displayed throughout the game.

1.The score starts at 500.

2.The current score should be displayed with each move.

3.If the user makes any type of invalid move, the score decreases 10 points.

4.If the user makes a valid move, the score decreases 5 points.

5.Once the score becomes zero or negative, the game automatically ends. See Note 7.10 below for the message that should be displayed when this occurs.

Output Messages: See the samples of output shown on this page to make sure your output exactly matches the spacing and blank lines needed to pass the test cases.

1.No matter the input or program behavior, at the very end your program should display: Thank you for playing!

2.When the user enters 'x' or 'X' to exit the program, your program should display: Exiting...

3.When the user enters 'r' or 'R' to restart the program, your program should display: *** Restarting

4.When the source position is not a valid position on the board, your program should display: The source position should be a valid position on the board (1-25). Try again.

5.When the destination position is not a valid position on the board, your program should display: The destination position should be a valid position on the board (1-25). Try again.

6.When the source position does not contain a knight, your program should display: The source position should have a knight. Try again.

7.When the destination position is not empty, your program should display: The destination position should be empty. Try again.

8.When the knight is not being moved in a valid L-shape, your program should display: Invalid move. Knights can only move in an L-shape. Try again.

9.When the user wins the game, your program should display an extra blank line and then the message: Congratulations, you did it!

10.When the user reaches a score less than or equal to zero, your program should display the score, an extra blank line and then the message: You have run out of moves. Try to do better next time!

Code given like this:

#include // for input and output #include // for toupper() using namespace std;

// Global variables for the pieces are allowed for this program, // but will generally not be allowed in the future. // You will likely want to declare global variables at least for the 25 board positions. // ...

// Characters of the pieces to be used on the board // Note that these are global constants, so their values cannot be changed. const string WhiteKnight = "\u2658"; // White knight character const string BlackKnight = "\u265E"; // Black knight character const string BlankPosition = " "; // Blank position character

//-------------------------------------------------------------------------------- // Display welcome message, introducing the user to the program and // describing the instructions for the game void displayWelcomeMessage() { cout << " Knight Escape " //makes use of global variables p1..p25, which represent // each of the positions on the board void displayBoard() { cout <<" " << " Board " << " Position " << " " << p1 << " " << p2 << " " << p3 << " " << p4 << " " << p5 << " 1 2 3 4 5 " << " " << p6 << " " << p7 << " " << p8 << " " << p9 << " " << p10 << " 6 7 8 9 10 " << " " << p11 << " " << p12 << " " << p13 << " " << p14 << " " << p15 << " 11 12 13 14 15 " << " " << p16 << " " << p17 << " " << p18 << " " << p19 << " " << p20 << " 16 17 18 19 20 " << " " << p21 << " " << p22 << " " << p23 << " " << p24 << " " << p25 << " 21 22 23 24 25 " << endl; } //end displayBoard()

// ---------------------------------------------------------------------- // Main() function of the program, containing the loop that controls // game play int main() { displayWelcomeMessage(); // Set board values to the default starting position // ... displayBoard(); // Loop that controls game play while(...) { cout << moveNumber << ". " << "Enter one of the following: " << " - M to move a knight from one position to another, " << " - R to reset the board back to the beginning, or " << " - X to exit the game. " << "Your choice -> "; cin >> menuOption; menuOption = toupper(menuOption); // convert user input to uppercase // If the user entered 'X' to exit, // break out of this loop that controls game play // ... // If the user entered 'R' to reset, // reset the board back to the beginning // ... // If the user entered 'M' to move a knight, // ask for the position of the knight to be moved // ... // Check that FROM position is valid, i.e. within 1-25 // Check that TO position is valid, i.e. within 1-25 // Check that the source position has a knight // Check that the destination position is empty // Check that the move is valid - knights can only move in an L-shape // Make the move and update the board } // end loop for game play // Check for a win // ...

cout << "Thank you for playing!" << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

=+ What is the role of government in bargaining?

Answered: 1 week ago

Question

=+ Who is the negotiation partner at company level?

Answered: 1 week ago

Question

=+Which associations exist?

Answered: 1 week ago