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

R-Studio (R Programming Language) 4. Let the data x be given by `x <- c(1, 8,...
R-Studio (R Programming Language) 4. Let the data x be given by `x <- c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5)` Use R to compute the following functions. Note, we use X1 to denote the first element of x (which is 1) etc.    1. `(X1 + X2 + . . .+ X10)/10` (use sum)    2. Find log10(Xi) for each i. (Use the log function which by default is base e) 3. Find `(Xi - 4.4)/2.875`...
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...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
If the total cost function for a product is C(x) = 8(x + 5)^3 dollars, where...
If the total cost function for a product is C(x) = 8(x + 5)^3 dollars, where x represents the number of hundreds of units produced, producing how many units will minimize average cost? x = hundred units Find the minimum average cost per hundred units. $
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT