NOTE: skip to bottom for real question
Hey guys, right now I am in a rut where I know the basics how to solve my sudoku program, but if I did it the way i know how it would be ugly, long and a lot of unnecessary code.
Right I have sudoku.cc, sudokuboard.cc, sudokuboard.h, stack.cc, and stack.h,
So as of right now it doesn’t matter what my stack.cc and my stack.h are because they are rather generic and anyone can find the programs to them and frankly I don’t underdstand why I even need to include the stack.cc/h due.
My sudoku.cc as of right now is:
#include <iostream>
#include <cassert>
#include <fstream>
#include "sudokuboard.h"
#include "stack.h"
using namespace std;
int main (void)
{
FILE *fp = fopen("test.txt","r");
char sudoku_grid[9][9];
char ch;
if(fp != NULL)
{
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
ch = fgetc(fp);
sudoku_grid[i][j] = ch;
}
ch = fgetc(fp);
}
for(int i = 0; i< 9;i++)
{
for(int j= 0; j<9;j++)
cout<<sudoku_grid[i][j];
cout<<endl;
}
}
return 0;
}
Which so far does nothing but print out the file I gave it and have it stored in a 2D array.
My sudokuboard.h is
#include <iostream>
#define SDIM 9
class SudokuBoard {
public:
//------------------------------------------------------------------------
SudokuBoard();
// Construct a blank sudoku board
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void print(std::ostream & ostr) const;
// display it. duh.
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void place(size_t r, size_t c, char digit);
// PRE: safe(r,c,digit)
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void remove(size_t r, size_t c, char digit);
// PRE: get(r,c) == digit
//------------------------------------------------------------------------
//------------------------------------------------------------------------
char get(size_t r, size_t c) const;
// Return the digit at (r,c) on the board. or ' ' if blank.
//------------------------------------------------------------------------
//------------------------------------------------------------------------
bool safe(size_t r, size_t c, char digit) const;
//
//------------------------------------------------------------------------
//------------------------------------------------------------------------
bool done() const;
// Return true iff every cell has a number
//------------------------------------------------------------------------
private:
std::string rows[SDIM];
};
and my sudokuboard.cc:
#include <iostream>
#include <cassert>
#include "sudokuboard.h"
#define ASSERTBOUNDS assert(0 <= r and r < SDIM and 0 <= c and c < SDIM)
SudokuBoard::SudokuBoard()
{
for (size_t i = 0;i<SDIM;i++) {
rows[i] = "";
for (size_t j=0;j<SDIM;j++)
rows[i] += ' ';
}
}
void SudokuBoard::place(size_t r, size_t c, char digit)
{
ASSERTBOUNDS;
assert(safe(r,c,digit));
}
void SudokuBoard::remove(size_t r, size_t c, char digit)
{
ASSERTBOUNDS;
assert(get(r,c)==digit);
rows[r][c] = ' ';
}
char SudokuBoard::get(size_t r, size_t c) const
{
ASSERTBOUNDS;
return rows[r][c];
}
void SudokuBoard::print(std::ostream & ostr) const
{
for (size_t i=0;i<SDIM;i++)
ostr << rows[i] << std::endl;
}
bool SudokuBoard::safe(size_t r, size_t c, char digit) const
{
for(size_t r=0; r<SDIM; r++)
for(size_t c=0; c<SDIM; c++)
if (get(r,c) == digit)
return false;
for(size_t c=0; c<SDIM; c++)
for(size_t r=0; r<SDIM; r++)
if (get(r,c) == digit)
return false;
return true;
}
bool SudokuBoard::done() const
{
for (size_t r=0;r<SDIM;r++)
for (size_t c=0;c<SDIM;c++)
if (rows[r][c]==' ')
return false;
return true;
}
So as of right now I want to use sudokuboard.cc methods and use the SudokuBoard() in my sudoku.cc because I know if I do that I will be able to use the useful methods provided by Sudokuboard.cc thus making my code seem a lot cleaner because it isn’t all cluttered up on one program.
So as of right now I think the gateway to linking these two programs together in order to maximize my space I would have to set my current 2D array which is sudoku_grid to be part of Sudokuboard() some how.
The problem is that I literally have no blinkin idea how to hook this puppies up. I know I cannot do sudoku_grid = Sudokuboard() because even if it did work, it would probably set all the arrays to being just spaces.
Anyone have a solution of where to start off?
At the top of the main file put
#include "sudokuboard.h"so that the compiler knows it’s there. Next, at the top of yourint main()you need to create an instance of theSudokuBoardclass, withSudokuBoard mainBoard;, then you can access it’s methods viamainBoard.doSomething();somewhere else in the main. I’d recommend creating a new method in the SudokuBoard class that reads data from a file and places it directly into SB’s privatestd::string rows[SDIM];array from within (basically move most of the code from main into the class). With the code as-is, this should work assuming I didn’t mangle any of the names: