Assignment 2

In this assignment, you will gain experience with struct, enum, and switch.

At the top of your program, you will be have a chessboard definition as a two-dimensional array of characters, like this one, which shows the initial position of a game:

const char charboard[][9] = {"rnbqkbnr",
                             "pppppppp",
            		     "        ",
            		     "        ",
            		     "        ",
            		     "        ",
            		     "PPPPPPPP",
            		     "RNBQKBNR"};
   

(Notice that only the last dimension must be given here, since the first can be determined from the initializer, and notice that the last dimension must be 9, not 8, because of the implicit null character at the end of each string.)

Rows (called ranks) are traditionally numbered from 1 to 8 starting at the bottom (so carefully consider how you want to represent the board). Columns (called files) are traditionally labeled a to h from left to right. Capital letters are used for white pieces, lowercase letters are used for black pieces, and spaces are used for empty squares. The letters stand for pieces as follows:

LetterPiece
Ppawn
Nknight
Bbishop
Rrook
Qqueen
Kking

First, you must create an appropriate data structure to store a chessboard (using enums---manipulating arrays of characters is not convenient), and write a function to convert a chessboard in the above format into your internal format.

Then write a function that, given a piece color, prints out all the legal moves for rooks of that color. A legal move for a rook is any horizontal or vertical move that doesn't pass through another piece and lands either on an empty square or a square containing a piece of the opposite color. Moves should be printed in the form b4-f4 for a move that ends on an empty square, or b4xf4 for a move that lands on a square occupied by an enemy piece.

Your main() should consist of a call to your function that creates your internal data structure, followed by two calls to your move generator (once to generate moves for black rooks, and once to generate moves for white rooks).

Before you start coding, you need to consider how you're going to set up your data structures and what functions you'll need to manipulate them. For example, having a function to output a move is probably a good idea. Try to make your code readable (so use descriptive identifier names and good indentation style), and use assert() where appropriate to avoid troubles with array bounds.

This assignment is due in printed form with sample output (pick good test cases!) on Monday, February 8. Start early, and come to class or office hours with questions. I don't want people starting on the weekend again!