Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction The aim of this take-home exam is to practice on loops (for/while statements). The use of loops is due to the nature of the

Introduction

The aim of this take-home exam is to practice on loops (for/while statements). The use of loops is due to the nature of the problem; that is, you cannot finish this take-home exam without using loop statements.

Description

In this take-home exam, your assignment is based on a straightforward chess problem. You'll be provided with a board layout containing just a bishop and a king. Your task is to determine whether the bishop can attack the king or not. Moreover, your task includes pinpointing and displaying all the cells the bishop can potentially move on the board with a designated character.

A bishop moves diagonally across the board. It can move any number of cells diagonally, but it cannot jump over other pieces. Therefore, while displaying the cells under attack by the bishop, you are going to stop displaying the cells succeeding the king's position as displayed below.

Inputs

The user will input the board layout as a string, where each row is separated by a semicolon (';'), and the cells within a row are separated by colons (','). Empty cells are represented by the at sign ('@'), the bishop is denoted by a 'B', and the king is represented by a 'K' (case-sensitively).

An exemplary input can be found below.

@,@,@,@,@,@,@,@;@,@,B,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,K,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

You can assume that the user will always enter a chess board with the appropriate dimensions, 8 rows and 8 columns. In other words, the input will consist of precisely 64 characters, excluding commas and semicolons. Additionally, each comma and semicolon will be accompanied by only one of the characters '@', 'K', or 'B' before, after, and between them. These are the assumptions that you can make about the input. However, right after the board layout is received from the user, you will check whether both the bishop and the king are positioned on the board. If not, your program will repeatedly ask for a board layout until both the bishop and the king are positioned on the board (see sample runs).

Output

Once you get the input (layout) from the user, you should first check its correctness as explained above. If one or both of the pieces (either the king or the bishop) are missing, then you will print "Missing pieces!"; and ask for another layout until both pieces are positioned on the board.

If both the bishop and the king are positioned on the board, then you are going to display the cells the bishop can attack on the board. And finally, print a message to the user regarding the safety of the king (see sample runs).

Your output board will be displayed in a structured grid format, with vertical lines ('|') defining vertical cell boundaries and dashes ('-') marking out individual rows for clarity. There must be exactly 17 dash characters before and after every single row. Non-occupied cells will be filled with whitespaces (' '), i.e., any "@" symbol in the input will be replaced by a whitespace in the final board output. The positions of the bishop and the king will be again highlighted with 'B' and 'K', respectively. The cells under attack by the bishop will be displayed with '+'. Keep in mind that the attacking cells succeeding the king's position will not be highlighted.

An exemplary output board is shown below.

If the king is not threatened by the bishop, then you will print "The king is safe". If, however, the bishop can attack the king, then you will print "The bishop can attack the king".

Hint #1: It is recommended to convert the provided string into a list of lists, where each sublist represents a row on the board so that the process of calculating the cells under the bishop's attack will be simplified. This is just a recommendation, you may follow another way.

Sample Runs

Below, we provide some sample runs of the program that you will develop. The italic and bold phrases are inputs taken from the user. You have to display the required information in the same order and with the same words and characters as below.

Sample Run 1

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,K,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

Missing pieces!

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

Missing pieces!

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,B

Missing pieces!

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,B,K

-----------------

| | | | | | | | |

-----------------

|+| | | | | | | |

-----------------

| |+| | | | | | |

-----------------

| | |+| | | | | |

-----------------

| | | |+| | | | |

-----------------

| | | | |+| | | |

-----------------

| | | | | |+| |+|

-----------------

| | | | | | |B|K|

-----------------

The king is safe

Sample Run 2

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

Missing pieces!

Enter the board layout: B,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,K,@

-----------------

|B| | | | | | | |

-----------------

| |+| | | | | | |

-----------------

| | |+| | | | | |

-----------------

| | | |+| | | | |

-----------------

| | | | |+| | | |

-----------------

| | | | | |+| | |

-----------------

| | | | | | |+| |

-----------------

| | | | | | |K|+|

-----------------

The king is safe

Sample Run 3

Enter the board layout: B,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,K

-----------------

|B| | | | | | | |

-----------------

| |+| | | | | | |

-----------------

| | |+| | | | | |

-----------------

| | | |+| | | | |

-----------------

| | | | |+| | | |

-----------------

| | | | | |+| | |

-----------------

| | | | | | |+| |

-----------------

| | | | | | | |K|

-----------------

The bishop can attack the king Sample Run 4

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,K,@,@,@,@;@,@,@,B,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

-----------------

| | | | | | | |+|

-----------------

|+| | | | | |+| |

-----------------

| |+| | | |+| | |

-----------------

| | |+|K|+| | | |

-----------------

| | | |B| | | | |

-----------------

| | |+| |+| | | |

-----------------

| |+| | | |+| | |

-----------------

|+| | | | | |+| |

-----------------

The king is safe

Sample Run 5

Enter the board layout: @,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,K,@,@,@,@,@;@,@,@,B,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@;@,@,@,@,@,@,@,@

-----------------

| | | | | | |+| |

-----------------

| | | | | |+| | |

-----------------

| | |K| |+| | | |

-----------------

| | | |B| | | | |

-----------------

| | |+| |+| | | |

-----------------

| |+| | | |+| | |

-----------------

|+| | | | | |+| |

-----------------

| | | | | | | |+|

-----------------

The bishop can attack the king

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

Students also viewed these Databases questions