ECS 40

Program #2 (50 points)

Winter 2010

 

New concepts: None, this is just a review of C.             

File names (case sensitive): board.cpp, board.h, checker.h, main.cpp, move.cpp, move.h, and Makefile.

Executable name: checkers.out

Due: Wednesday, January 27th at 11:59 PM in p2 of cs40 using handin.

 

For this program you will implement in C (or C++ if you prefer), the first part of a more extensive checkers project.  You will use three structs, Board, Checker, and Move to store the data.  The structs are given below.  The style of your program must obey the programming style specified in the style handout.  Remember the body of any function (excluding variable declarations) cannot be more than 30 lines long.

Here are some other specifications:

1. Because you will be adapting this program to C++ in program #3, all of your functions, other than main(), should have a pointer to exactly one of the structs.  makeMove(), will have a pointer to a Board, and a const reference to a Move.  List the prototypes in the header file that matches the struct pointer type, and the implementation code in the corresponding .cpp file.  You will note that there is no checker.cpp, so there are no functions that have a Checker* as a parameter.

2. main() should create the Board using malloc(), initialize the Board struct, and then have a loop that: 1) shows the WHITE board; 2) gets the WHITE move, 3) makes the WHITE move; 4) shows the BLACK board; 5) gets the BLACK move; and 6) makes the BLACK move.  Your program will break out of the loop whenever the user enters 0 for the checker number.

2.1. initBoard() is the trickiest function to write.  The concept of “7 – x” is quite useful in allowing the initialization of WHITE and BLACK within the same loop.

2.2. showBoard() will take WHITE or BLACK as one of its parameters.  It will call separate functions based on the color specified.

2.3. Have getMove() return a bool.

3. The output of your program must be identical to that of mine.  You will find my executable in ~davis/40/p2.  A small sample is on the back.  If you come up with a better design for the board, please post it to the newsgroup, but do not use it for this assignment.

4. WHITE and BLACK should be defined as 0 and 1 in checker.h.

5. For this assignment, you may assume that all of the user’s inputs will be correct, so there will be no need for range checking of any kind.  Checker number, direction, and the location to which the checker is to move will all be valid.

6. Compile using g++ -Wall –ansi –g.  Your Makefile must create .o files for the three .cpp files.  We will deduct one point for each warning during compilation.

7. Because we are using g++ instead of gcc, “//” comments can, and should, be used throughout your code.  Do not use the “/*  */” type of comment anywhere in your code.  Remember that EVERY right curly bracket, “}”, must have a comment.

8. Remember to put the name(s) of the author(s) on the first line of each file.

 


struct Checker

{

  short row;

  short col;

  unsigned short number;

  short color;

} ;


struct Move

{

  short color;

  unsigned short checkerNum;

  char direction;

};


 

struct Board

{

  Checker checkers[2][13];  // Zeroth entry of 13 is not used.

  Checker *grid[8][8];  // each entry holds NULL, or an address of an element in

                        // the checkers array.

};



[davis@lect15 private]$ checkers.out

 

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

| |B| |B| |B| |B|

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

|B| |B| |B| |B| |

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

| |B| |B| |B| |B|

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

| | | | | | | | |

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

| | | | | | | | |

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

|9| |a| |b| |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

White's move (num direction): 9r

 

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

| |W| |W| |W| |W|

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

|W| |W| |W| |W| |

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

| |W| |W| |W| | |

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

| | | | | | |W| |

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

| | | | | | | | |

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

|9| |a| |b| |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

Black's move (num direction): bl

 

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

| |B| |B| |B| |B|

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

|B| |B| |B| |B| |

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

| |B| | | |B| |B|

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

| | | | |B| | | |

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

| |9| | | | | | |

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

| | |a| |b| |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

White's move (num direction): 9r

 

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

| |W| |W| |W| |W|

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

|W| |W| |W| |W| |

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

| |W| |W| |W| | |

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

| | | | | | | | |

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

| | | |b| |W| | |

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

|9| |a| | | |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

Black's move (num direction): al

 

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

| |B| |B| |B| |B|

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

|B| |B| |B| |B| |

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

| |B| | | | | |B|

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

| | |9| |B| |B| |

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

| | | | | | | | |

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

| | |a| |b| |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

White's move (num direction): 5l

 

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

| |W| |W| |W| |W|

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

|W| |W| |W| | | |

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

| |W| |W| |W| |W|

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

| | | | | | | | |

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

| |a| |b| |W| | |

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

|9| | | | | |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

Black's move (num direction): al

 

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

| |B| |B| |B| |B|

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

|B| |B| |B| |B| |

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

| |B| | | | | |B|

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

| | |9| |B| | | |

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

| | | | | | | |B|

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

|5| |a| |b| |c| |

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

| | | |6| |7| |8|

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

|1| |2| |3| |4| |

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

White's move (num direction): 9r

 

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

| |W| |W| |W| |W|

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

|W| |W| |W| | | |

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

| |W| |W| |W| |W|

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

|a| | | | | | | |

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

| | | |b| | | | |

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

|9| | | |W| |c| |

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

| |5| |6| |7| |8|

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

|1| |2| |3| |4| |

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

Black's move (num direction): 6l

 

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

| |B| |B| |B| |B|

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

|B| |B| | | |B| |

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

| |B| |9| |B| |B|

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

| | | | |B| | | |

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

| | | | | | | |B|

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

|5| |a| |b| |c| |

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

| | | |6| |7| |8|

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

|1| |2| |3| |4| |

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

White's move (num direction): 9r

 

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

| |W| |W| |W| |W|

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

|W| |W| |W| | | |

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

| |W| |W| |W| |W|

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

|a| | | | | | | |

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

| | | |b| | | | |

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

|9| |6| | | |c| |

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

| |5| |W| |7| |8|

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

|1| |2| |3| |4| |

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

Black's move (num direction): 0r

[davis@lect15 private]$