Solving Draught Puzzle - Step 1

◀ Exercise #6: Solving Draught Puzzle▶ Solving Draught Puzzle - Step 2
Amazon Let’s apply the 1st step of the Four-Step Programming Model to solve the Draught puzzle!
Four Step Programming Model: Step 1
I need a class to represent the pieces and a class to represent the orientations of the pieces. I also need nonmember functions to do relevant tasks such as outputting the board, initializing an array, and copying an array to another.

In this program, we will be relying on two-dimensional char arrays because they can be used to represent the board as well as all the pieces.

In my design, both transpose() and rotate90() are void and the modification to the piece is done in their arguments (arrays are passed by reference); allUnprintable() is needed to determine whether a given line in the input file contains part of a piece; impasse() is the optimization function; solve() is the recursive function that attempts to solve the puzzle.

I probably will need many more functions while coding. Anyway, here is my skeleton:
void outputBoard();
void init(char[numRows][numCols]);
bool allUnprintable(string s);
void setEqual(char[numRows][numCols], char[numRows][numCols]); bool impasse(char[boardHeight][boardWidth]); void solve(int); class Trans { public: char pattern[numRows][numCols]; char id; Trans(); Trans(char[numRows][numCols]); bool place(int, int); void clear(); }; Trans::Trans() { - default constructor, so do nothing } Trans::Trans(char cc[numRows][numCols]) { - copy the contents of cc to pattern
} bool Trans::place(int r, int c) { - place pattern on the board in the given location } void Trans::clear() { - clear out the invoking pattern on the board } class Piece { public: Trans *patterns[numTrans]; int count; Piece(); Piece(char[numRows][numCols]); void transpose(char[numRows][numCols]); void rotate90(char[numRows][numCols]); }; Piece::Piece() { - default constructor, so do nothing
} Piece::Piece(char cc[numRows][numCols]) { - acquire all orientations and store them in patterns } void Piece::transpose(char cc[numRows][numCols]) { - transpose cc, and cc contains the new pattern } void Piece::rotate90(char cc[numRows][numCols]) { - rotate cc by 90 degrees counterclockwise, and cc contains the new pattern } int main(int argc, char **argv) { - quit if no input file is given in the command line - quit if the input file is invalid
- acquire the pieces from the input file and store them in appropriate places - declare and initialize a two-dimensional array that symbolizes the board - solve the puzzle and outputs solutions } void outputBoard() { - display the board } void init(char cc[numRows][numCols]) { - initialize cc by making every element in the array a space } bool allUnprintable(string s) { - return true if s doesn't contain '#'; return false otherwise } void setEqual(char cc[numRows][numCols], char cc2[numRows][numCols]) {
- set the contents of cc equal to those of cc2 } bool impasse(char b[boardHeight][boardWidth]) { - detect various board layouts that definitely lead nowhere to speed up the process of finding the solution } void solve(int n) { - try all arrangements of the pieces recursively }
Let's look at the next step!
◀ Exercise #6: Solving Draught Puzzle▶ Solving Draught Puzzle - Step 2

fShare
Questions? Let me know!