C# Sudoku Puzzle I need to change this code from 9x9 to 16x16. I have altered so
ID: 3808095 • Letter: C
Question
C# Sudoku Puzzle
I need to change this code from 9x9 to 16x16. I have altered some of the code, but my rows 10-16 are off a bit. Also have to figure out what number combinations to add to the bottom of the code to make this a functional puzzle. I dont play Sudoku so Im having trouble with the correct numbers to solve the puzzle.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter8Program1
{
class SudokuPuzzle
{
private int[,] board;
private int[,] start;
private const int SIZE = 16;
private const int GRID_SIZE = 3;
public SudokuPuzzle()
{
start = new int[SIZE, SIZE];
board = new int[SIZE, SIZE];
}
public override string ToString()
{
String puzzleString = "Row/Col 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ";
puzzleString = puzzleString + " ----------------------------------------------- ";
for (int i = 0; i < SIZE; i++)
{
puzzleString = puzzleString + (i + 1) + " |";
for (int j = 0; j < SIZE; j++)
{
if (board[i, j] == 0)
puzzleString = puzzleString + " " + ".|";
else
puzzleString = puzzleString + " " + board[i, j] + "|";
}
puzzleString = puzzleString + " ";
puzzleString = puzzleString + " |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__| ";
}
return puzzleString;
}
public void AddInitial(int row, int col, int value)
{
if (row >= 0 && row <= SIZE && col >= 0 && col <= SIZE && value >= 1 && value <= SIZE)
{
start[row, col] = value;
board[row, col] = value;
}
}
public void AddGuess(int row, int col, int value)
{
//only set the value if the start is 0
if (row >= 0 && row <= SIZE && col >= 0 && col <= SIZE && value >= 1 && value <= SIZE && start[row, col] == 0)
{
board[row, col] = value;
}
}
public int GetValueIn(int row, int col)
{
return board[row, col];
}
public void Reset()
{
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
board[i, j] = start[i, j];
}
public bool IsFull()
{
bool allFilled = true;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
allFilled = allFilled && board[i, j] > 0;
return allFilled;
}
public bool[] GetAllowedValues(int row, int col)
{
//Save the value at the location, then try all 16 values
int savedValue = board[row, col];
bool[] result = new bool[SIZE];
for (int value = 1; value <= SIZE; value++)
{
board[row, col] = value;
result[value - 1] = CheckPuzzle();
}
board[row, col] = savedValue;
return result;
}
public bool CheckPuzzle()
{
bool looksGood = true;
for (int i = 0; i < SIZE; i++)
{
looksGood = looksGood && CheckRow(i);
looksGood = looksGood && CheckCol(i);
looksGood = looksGood && CheckSub(i);
}
return looksGood;
}
public bool CheckRow(int row)
{
int[] count = new int[SIZE + 1];
for (int col = 0; col < SIZE; col++)
{
count[board[row, col]]++;
}
bool countIsOk = true;
for (int i = 1; i <= SIZE; i++)
countIsOk = countIsOk && (count[i] <= 1);
return countIsOk;
}
public bool CheckCol(int col)
{
int[] count = new int[SIZE + 1];
for (int row = 0; row < SIZE; row++)
{
count[board[row, col]]++;
}
bool countIsOk = true;
for (int i = 1; i <= SIZE; i++)
countIsOk = countIsOk && (count[i] <= 1);
return countIsOk;
}
public bool CheckSub(int sub)
{
int[] count = new int[SIZE + 1];
int rowBase = (sub / GRID_SIZE) * GRID_SIZE;
//The above will give 0,3, or 6 because of interger division
int colBase = (sub % GRID_SIZE) * GRID_SIZE;
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
count[board[rowBase + 1, colBase + j]]++;
}
}
bool countIsOkay = true;
for (int i = 1; i <= SIZE; i++)
countIsOkay = countIsOkay && (count[i] <= 1);
return countIsOkay;
}
public void InitializePuzzle()
{
AddInitial(0, 0, 9);
AddInitial(0, 3, 3);
AddInitial(0, 6, 1);
AddInitial(0, 8, 8);
AddInitial(1, 0, 4);
AddInitial(1, 2, 3);
AddInitial(1, 3, 6);
AddInitial(1, 5, 8);
AddInitial(2, 0, 8);
AddInitial(2, 4, 9);
AddInitial(3, 1, 6);
AddInitial(3, 2, 1);
AddInitial(3, 4, 3);
AddInitial(3, 5, 7);
AddInitial(4, 1, 8);
AddInitial(4, 2, 2);
AddInitial(4, 6, 6);
AddInitial(4, 7, 1);
AddInitial(5, 3, 2);
AddInitial(5, 4, 6);
AddInitial(5, 6, 3);
AddInitial(5, 7, 8);
AddInitial(6, 4, 4);
AddInitial(6, 8, 6);
AddInitial(7, 3, 9);
AddInitial(7, 5, 3);
AddInitial(7, 6, 5);
AddInitial(7, 8, 1);
AddInitial(8, 0, 2);
AddInitial(8, 2, 7);
AddInitial(8, 5, 6);
AddInitial(8, 8, 9);
//I NEED TO ADD MORE NUMBERS
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter8Program1
{
class SudokuPuzzle
{
private int[,] board;
private int[,] start;
private const int SIZE = 16;
private const int GRID_SIZE = 3;
public SudokuPuzzle()
{
start = new int[SIZE, SIZE];
board = new int[SIZE, SIZE];
}
public override string ToString()
{
String puzzleString = "Row/Col 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ";
puzzleString = puzzleString + " ----------------------------------------------- ";
for (int i = 0; i < SIZE; i++)
{
puzzleString = puzzleString + (i + 1) + " |";
for (int j = 0; j < SIZE; j++)
{
if (board[i, j] == 0)
puzzleString = puzzleString + " " + ".|";
else
puzzleString = puzzleString + " " + board[i, j] + "|";
}
puzzleString = puzzleString + " ";
puzzleString = puzzleString + " |__|__|__|__|__|__|__|__|__|n";
}
return puzzleString;
}
public void AddInitial(int row, int col, int value)
{
if (row >= 0 && row <= SIZE && col >= 0 && col <= SIZE && value >= 1 && value <= SIZE)
{
start[row, col] = value;
board[row, col] = value;
}
}
public void AddGuess(int row, int col, int value)
{
//only set the value if the start is 0
if (row >= 0 && row <= SIZE && col >= 0 && col <= SIZE && value >= 1 && value <= SIZE && start[row, col] == 0)
{
board[row, col] = value;
}
}
public int GetValueIn(int row, int col)
{
return board[row, col];
}
public void Reset()
{
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
board[i, j] = start[i, j];
}
public bool IsFull()
{
bool allFilled = true;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
allFilled = allFilled && board[i, j] > 0;
return allFilled;
}
public bool[] GetAllowedValues(int row, int col)
{
int savedValue = board[row, col];
bool[] result = new bool[SIZE];
for (int value = 1; value <= SIZE; value++)
{
board[row, col] = value;
result[value - 1] = CheckPuzzle();
}
board[row, col] = savedValue;
return result;
}
public bool CheckPuzzle()
{
bool looksGood = true;
for (int i = 0; i < SIZE; i++)
{
looksGood = looksGood && CheckRow(i);
looksGood = looksGood && CheckCol(i);
looksGood = looksGood && CheckSub(i);
}
return looksGood;
}
public bool CheckRow(int row)
{
int[] count = new int[SIZE + 1];
for (int col = 0; col < SIZE; col++)
{
count[board[row, col]]++;
}
bool countIsOk = true;
for (int i = 1; i <= SIZE; i++)
countIsOk = countIsOk && (count[i] <= 1);
return countIsOk;
}
public bool CheckCol(int col)
{
int[] count = new int[SIZE + 1];
for (int row = 0; row < SIZE; row++)
{
count[board[row, col]]++;
}
bool countIsOk = true;
for (int i = 1; i <= SIZE; i++)
countIsOk = countIsOk && (count[i] <= 1);
return countIsOk;
}
public bool CheckSub(int sub)
{
int[] count = new int[SIZE + 1];
int rowBase = (sub / GRID_SIZE) * GRID_SIZE;
//The above will give 0,3, or 6 because of interger division
int colBase = (sub % GRID_SIZE) * GRID_SIZE;
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
count[board[rowBase + 1, colBase + j]]++;
}
}
bool countIsOkay = true;
for (int i = 1; i <= SIZE; i++)
countIsOkay = countIsOkay && (count[i] <= 1);
return countIsOkay;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.