Question

In: Computer Science

C Programming Language. A chessboard consists of 8 squares x 8 squares for a total of...

C Programming Language.

A chessboard consists of 8 squares x 8 squares for a total of 64 squares. The squares of the chessboard are identified, from the perspective of the player with the white pieces, by the letters a – h for the 8 columns or files (starting from that player’s left), and 1 – 8 for the 8 rows or ranks (starting closest to that player). One of the chess pieces, the knight, can move in any direction by moving 2 rows up or down and 1 column left or right, or by moving 1 row up or down and 2 columns left or right. The knight can jump over pieces with the only restrictions being that the knight can’t move off the board and the knight can’t move to a square already occupied by a piece of the same color. Assume no other pieces on the board will be captured by, or block the movement of, the knight. Write a C language function that reads in a two character file and rank location (e.g., a5, g6, etc.). The function will then compute and display all the different squares by two character file and rank location to identify where the knight could move. You may use multiple functions, rather than only one, if you prefer.

          Include a commented listing with your report.

Solutions

Expert Solution

Hi,

The code below will help you find all possible knight moves from a given postion on the chess board.

#include <stdio.h>

//Function to see if move is valid or not
void moves(char ch, int rank, int m,int n)
{
    // Position of knight after move 
    int x = ch + m; 
    int y = rank + n;
        
    //Condition check    
    if (x>=97 && x <=103 && y >=1 && y <=8)
        printf("%c%d\n", x,y);
}

int main()
{
    char string[] = "e4";
    char ch = string[0];
    int rank = string[1] - 48;
    //All possibilities
    int X[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; 
    int Y[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
    
    // Check if each possible move is valid or not 
    for (int i = 0; i < 8; i++) 
    { 
        moves(ch,rank,X[i],Y[i]);
    }
}

Here, we taking the input string and breaking it into the character and the integer part. The -48 is to convert ASCII value to integer value. X[] and Y[] indicate all possible move sets from any position for a knight piece. As there are 8 possible sets we run a loop which runs 8 times and calls the function moves() with a set of values each time.

The function moves(), takes in the char, rank and also possible moves in x and y direction as arguments. We then calculate the next position of the knight by adding the x and y values. If the x value is in [a,g] and y value is in [1,8] the new position is valid and we print it.

Output for pos = "e4":

Reference:

Hope the code helps you. The question was really fun to solve :)

Thanks and All the Best :D


Related Solutions

A special chessboard is 2 squares wide and n squares long. Using n dominoes that are...
A special chessboard is 2 squares wide and n squares long. Using n dominoes that are 1 square by 2 squares, there are many ways to completely cover this chessboard with no overlap. How many are there? Prove your answer.
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
One rook is placed in some unit squares of a 100 × 100 chessboard. Castles in...
One rook is placed in some unit squares of a 100 × 100 chessboard. Castles in the same row or in the same column, with no other castles threaten each other. Regardless of the number and location of the castles, if any two castles that threaten each other can be in different colors, what is the smallest value of the number n that can always be painted in one of n colors?
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT