Skip to content

Instantly share code, notes, and snippets.

@cdmcmahon
Created March 27, 2013 23:24
Show Gist options
  • Save cdmcmahon/5259064 to your computer and use it in GitHub Desktop.
Save cdmcmahon/5259064 to your computer and use it in GitHub Desktop.
This is a simple command line program written in C++ to solve and check solutions for sudoku puzzles. I programmed it for my Hacker School application. The sudoku is represented as an array of arrays.
//
// main.cpp
// sudoku_checker
//
// Created by Corey McMahon on 3/22/13.
// Copyright (c) 2013 Corey McMahon. All rights reserved.
//
#include <iostream>
using namespace std;
void printit(int s[][9]){
for(int i = 0; i<9; i++){
if(i==3 || i==6){
cout << "-----------------------------------------" << endl;
}
for(int j = 0; j<9; j++){
if(j!=8){
if(j==3 || j==6){
cout << "| " << s[i][j] << " ";
}else{
cout << s[i][j] << " ";
}
}
else{
cout << s[i][j] << endl;
}
}
}
}
void insertValue(int val, int row, int col, int s[][9]){
if(row > 9 || row<1){
cout << "\nERROR: INVALID ROW NUMBER\n\n";
}else if(col>9 || col<1){
cout << "\nERROR: INVALID COLUMN NUMBER\n\n";
}else if(val>9 || val<1){
cout << "\nERROR: INVALID VALUE\n\n";
}else{
s[(row-1)][(col-1)] = val;
}
}
// Looks for duplicates in an array of length 9
// returns true if there ARE duplicates
bool checkDuplicates(int arr[]){
bool b = false;
for(int i=0; i<9; i++){
if(arr[i]==0)
;
else{
for(int j=0; j<9; j++){
if(i==j)
;
else{
if(arr[i]==arr[j]) b = true;
}
}
}
}
return b;
}
// returns false if solution is NOT valid, true if it is.
bool checkValidity(int s[][9]){
// Check rows
for(int i=0; i<9; i++){
int rowarray[9];
for(int j=0; j<9; j++){
rowarray[j] = s[i][j];
}
if(checkDuplicates(rowarray)){
return false;
}
}
// Check columns
for(int i=0; i<9; i++){
int colarray[9];
for(int j=0; j<9; j++){
colarray[j] = s[j][i];
}
if(checkDuplicates(colarray)){
return false;
}
}
// Check squares
for(int x=0; x<3; x++){
for(int i=0; i<3; i++){
int sqrarray[9];
for(int j=0; j<3; j++){
for(int h=0; h<3; h++){
sqrarray[(3*j)+h] = s[j+(3*x)][(3*i)+h];
}
}
if(checkDuplicates(sqrarray)){
return false;
}
}
}
return true;
}
int main(int argc, const char * argv[])
{
// sudoku[row][column]
int sudoku[9][9];
// Initialize the sudoku matrix
for(int i = 0; i<9; i++){
for(int j = 0; j<9; j++){
sudoku[i][j] = 0;
}
}
cout << "YOUR SUDOKU:\n";
printit(sudoku);
cout << "What would you like to do?\n(1) Enter value\n(2) Check validity\n(3) Quit\n";
int inputval = 0;
cin >> inputval;
while(inputval != 3){
if(inputval ==1){
int value, row, column;
cout << "What value you would like to enter? ";
cin >> value;
cout << "In what row would you like to place this value? ";
cin >> row;
cout << "In what column would you like to place this value? ";
cin >> column;
insertValue(value, row, column, sudoku);
cout << "YOUR SUDOKU:\n";
printit(sudoku);
cout << "What would you like to do?\n(1) Enter value\n(2) Check validity\n(3) Quit\n";
cin >> inputval;
}else if(inputval==2){
if(checkValidity(sudoku)==false){
cout << "Your solution is invalid\n";
}else{
cout << "Your solution is valid\n";
}
cout << "What would you like to do?\n(1) Enter value\n(2) Check validity\n(3) Quit\n";
cin >> inputval;
}else{
cout << "Improper input value\n\n" << "What would you like to do?\n(1) Enter value\n(2) Check validity\n(3) Quit\n";
cin >> inputval;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment