#include #include "Square.h" using namespace std; /* * Creates an nxn square */ Square::Square(int width) : n(width), numFilled(0) { max = n*n; target = (n*(max+1))/2; entry = new int*[n]; // array of what will be arrays for (int i=0; i0) { numFilled--; Cell last = whichCell(numFilled,n); entry[last.r][last.c] = 0; } } /* * Return the width of the square */ int Square::width() const { return n; } /* * This accessor returns the (row,column) entry to value, where both * rows and columns are zero-indexed. * * Returns '-1' if the command fails (e.g., the indicies are invalid) */ int Square::get(int row, int column) const { if (row<0 || row>=n || column<0 || column>=n) return -1; else return entry[row][column]; } /* * Checks validity of the current settings, ensuring that all rows, * columns and diagonals add up to the desired value. Furthermore, * it verifies that each number from [1, n^2] has been used once, * and only once. */ bool Square::valid() { bool success = true; // first, lets check and see if every number was used once and only once. for (int i=max; i>0; i--) used[i] = false; for (int i=0; i0; i--) if (!used[i]) success = false; // did not use all values // look for canonical form if (success) success = canonical(); // next lets check the sums of the rows for (int row=0; success && row