Question:
can someone help me on how to write a C program for chess w the following specifications?
anonymous
2012-02-07 18:28:26 UTC
the function is int is_valid_move(char piece, char start_col, int start_row, char end_col,
int end_row); Its parameters represent a chess piece, its starting location on the chessboard, and the location of a potential
move. It will determine whether the piece, starting, and ending locations are valid, and whether the ending
location of the potential move represents a valid move for that type of piece from the starting location.
• The first parameter piece indicates the piece under consideration, using the following values:
1. An uppercase P indicates the piece is a pawn.
2. An uppercase R indicates the piece is a rook.
3. An uppercase B indicates the piece is a bishop.
4. An uppercase N indicates the piece is a knight.
15. An uppercase K indicates the piece is a king.
6. An uppercase Q indicates the piece is a queen.
• The second parameter start col is a character that represents the column of the piece’s starting position.
• The third parameter start row is an integer that represents the row of the piece’s starting position.
• The fourth parameter end col is a character that represents the column of the piece’s ending position.
• The fifth parameter end row is an integer that represents the row of the piece’s ending position.

The return values are this:
Your function will not produce any output, it will just return different values as described below after analyzing
the potential move:
1. Your function should return the value 0 if all of the parameters are valid, and the piece can legally move
from the starting position to the ending position, according to the rules in Section 4.
2. Your function should return the value −1 if the starting and ending position and piece are both valid, but
the piece in question cannot legally move from the starting position to the ending position, according to
the rules in the following subsection. This case includes when the starting and ending locations are the
same, since no piece can validly move to the same location it started from.
3. Your function should return the value −2 if the value of the parameter piece does not correctly indicate
one of the six piece types, as described above.
4. Your function should return the value −3 if the starting position is incorrect, in other words if the column
and row combination of the starting position does not represent a valid square on the chess board.
5. Your function should return the value −4 if the ending position is incorrect, in other words if the column
and row combination of the ending position does not represent a valid square on the chess board.
If the values of the parameters are such that the move would have more than one of the errors described,
your function can return the value for any of the applicable error conditions.
Three answers:
McFate
2012-02-07 18:46:03 UTC
By "help" you mean "write it for you"?



This isn't too difficult. For example, a knight must move +/-2 rows and +/-1 column or vice versa. You should be able to write it in a couple hours.



The outline for your code would be:



(1) check starting position and return -3 if invalid

(2) check ending position and return -4 if invalid

(3) switch statement by piece type (return -2 for default case not matching any)

(4) in each piece's case, validate the suggested move and return 0 or -1



@M
Link H
2012-02-07 18:56:47 UTC
First, check the validity of starting and ending positions (i.e. verify that they are different and both on the board). These are the same for all pieces, and can be done quickly. Return -1,-3, -4 for the respective errors. If the starting and ending positions are valid, then check the validity of the piece (return -2 if not).



The next step will be different because it is specific for each piece. In order to complete the step, you will need a move generating function for each type of piece. The move generating function must be able to generate all moves for a piece on a given starting position. The MGF can be written either to accept the ending position as an input, or to return the list of all legal moves. I would recommend the 1st method as being easier to write and a lot faster in running.



Compare the ending position with a legal move. If there is a match, return success (0). If all legal moves have been tested, return failure (-1). Repeat while there are any legal moves remaining to be tested.



Alternatively, you can perhaps devise a simple test to identify legal moves. For example, all legal rook moves will have either x (column) or y (row) changed, but not both. A legal bishop move will have equal magnitude change in x and y. A queen move can either be a legal bishop move or a legal rook move. A legal king move will change x and y by +/-1, or 0 (not considering castling). A legal pawn move will have x unchanged and y changed by +1 (not considering captures, or initial moves). Note that pieces other than pawns are not limited to moving only forward. A legal knight move will have either x change of +/-2 and y change of +/-1, or x change of +/-1 and y change of +/-2. That covers all the pieces.
anonymous
2012-02-07 18:46:38 UTC
Are you out of you ******* mind!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Nobody here wants to read what you wrote, and if you are smart enough to write a chess program, god bless you, and find somewhere else


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...