Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SudokuChecker.h"
- #include <iostream>
- #include <sstream>
- #include <fstream>
- SudokuChecker::SudokuChecker(int _board[][9]) {
- for (int i = 0; i < 9; i++) {
- std::vector<int> temp;
- for (int j = 0; j < 9; j++) {
- temp.push_back(_board[i][j]);
- }
- board.push_back(temp);
- }
- }
- SudokuChecker::SudokuChecker(const char *filename) {
- std::ifstream infile(filename);
- if (infile) {
- std::string line;
- while (std::getline(infile, line)) {
- std::vector<int> temp;
- std::istringstream ss(line);
- int val;
- while (ss >> val) {
- temp.push_back(val);
- }
- board.push_back(temp);
- }
- }
- infile.close();
- }
- void SudokuChecker::printBoard() const {
- for (size_t i = 0; i < board.size(); i++) {
- for (size_t j = 0; j < board[i].size(); j++) {
- std::cout << board[i][j] << " ";
- }
- std::cout << std::endl;
- }
- }
- bool SudokuChecker::checkRow(int row) {
- std::set<int> found;
- for (int i = 0; i < 9; i++) {
- int val = board[row][i];
- if (found.count(val) == 1)
- return false;
- else
- found.insert(val);
- }
- return found == required_vals;
- }
- bool SudokuChecker::checkCol(int col) {
- std::set<int> found;
- for (int i = 0; i < 9; i++) {
- int val = board[i][col];
- if (found.count(val) == 1)
- return false;
- else
- found.insert(val);
- }
- return found == required_vals;
- }
- bool SudokuChecker::checkSquare(int startY, int startX) {
- std::set<int> found;
- for (int i = startY; i < startY + 3; i++) {
- for (int j = startX; j < startX + 3; j++) {
- int val = board[i][j];
- if (found.count(val) == 1)
- return false;
- else
- found.insert(val);
- }
- }
- return found == required_vals;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement