Skip to content

Commit

Permalink
Code Gardening. Resolved all code analysis warnings, deleted unused n…
Browse files Browse the repository at this point in the history
…amespaces, and split apart a few tests to be more logical
  • Loading branch information
samsmithnz committed Feb 28, 2021
1 parent 9318900 commit 4572eae
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 90 deletions.
16 changes: 6 additions & 10 deletions SudokuSolver/SudokuSolver.Core/GameState.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Core
{
Expand Down Expand Up @@ -73,14 +70,13 @@ public void LoadGame(string game)
throw new Exception("Game not loaded");
}

Rules rules = new Rules();
RuleResult ruleResult = null;
RuleResult ruleResult;
int squaresSolved = 0;

//Process the board and update with any changes
if (useRowRule == true)
{
ruleResult = rules.RowEliminationRule(GameBoard, GameBoardPossibilities);
ruleResult = Rules.RowEliminationRule(GameBoard, GameBoardPossibilities);
if (ruleResult != null)
{
GameBoard = ruleResult.GameBoard;
Expand All @@ -91,7 +87,7 @@ public void LoadGame(string game)

if (useColumnRule == true)
{
ruleResult = rules.ColumnEliminationRule(GameBoard, GameBoardPossibilities);
ruleResult = Rules.ColumnEliminationRule(GameBoard, GameBoardPossibilities);
if (ruleResult != null)
{
GameBoard = ruleResult.GameBoard;
Expand All @@ -102,7 +98,7 @@ public void LoadGame(string game)

if (useSquareGroupRule == true)
{
ruleResult = rules.SquareGroupEliminationRule(GameBoard, GameBoardPossibilities);
ruleResult = Rules.SquareGroupEliminationRule(GameBoard, GameBoardPossibilities);
if (ruleResult != null)
{
GameBoard = ruleResult.GameBoard;
Expand Down Expand Up @@ -132,7 +128,7 @@ public int SolveGame()
return squaresSolved;
}

private string UpdateProcessedGameBoardString(int[,] gameBoard)
private static string UpdateProcessedGameBoardString(int[,] gameBoard)
{
StringBuilder sb = new StringBuilder();
int i = 0;
Expand All @@ -141,7 +137,7 @@ private string UpdateProcessedGameBoardString(int[,] gameBoard)
{
for (int x = 0; x < 9; x++)
{
sb.Append(gameBoard[x, y].ToString());
sb.Append(gameBoard[x, y]);
i++;
}
sb.Append(Environment.NewLine);
Expand Down
8 changes: 8 additions & 0 deletions SudokuSolver/SudokuSolver.Core/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Style", "IDE0090:Use 'new(...)'", Justification = "Not used to this new rule, ignoring for now", Scope = "namespaceanddescendants", Target = "~N:SudokuSolver.Core")]
18 changes: 18 additions & 0 deletions SudokuSolver/SudokuSolver.Core/RuleResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace SudokuSolver.Core
{
public class RuleResult
{
public int SquaresSolved;
public int[,] GameBoard;
public HashSet<int>[,] GameBoardPossibilities;

public RuleResult(int squaresSolved, int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
{
SquaresSolved = squaresSolved;
GameBoard = gameBoard;
GameBoardPossibilities = gameBoardPossibilities;
}
}
}
66 changes: 6 additions & 60 deletions SudokuSolver/SudokuSolver.Core/Rules.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;

namespace SudokuSolver.Core
{
//TODO: Add a rule that adds up the numbers and calculates what is missing (1 to 9 is 45)
public class Rules
{
//public RuleResult SquareGroupEliminationRuleMethod1(SquareGroup group)
//{
// int squaresSolved = 0;
// int i = 0;

// //First mark all of the possible numbers in available squares, within the square group
// foreach (int item in group.SolvedSquares)
// {
// i = 0;
// for (int y = 0; y < 3; y++)
// {
// for (int x = 0; x < 3; x++)
// {
// if (group.Squares[i].CurrentSquare == 0)
// {
// group.Squares[i].EliminatePossibleSquare(item);
// }
// i++;
// }
// }
// }

// //Then run a simple elimination, to see if the item can be solved
// i = 0;
// for (int y = 0; y < 3; y++)
// {
// for (int x = 0; x < 3; x++)
// {
// if (group.Squares[i].CurrentSquare == 0 && group.Squares[i].PossibleSquareCount == 1)
// {
// squaresSolved++;
// group.Squares[i].CurrentSquare = group.Squares[i].PossibleSquaresFiltered[0];
// }
// i++;
// }
// }

// return new RuleResult(squaresSolved, group, null);
//}

public RuleResult SquareGroupEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)

public static RuleResult SquareGroupEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
{
int squaresSolved = 0;

Expand Down Expand Up @@ -103,7 +64,7 @@ public RuleResult SquareGroupEliminationRule(int[,] gameBoard, HashSet<int>[,] g
return new RuleResult(squaresSolved, gameBoard, gameBoardPossibilities);
}

public RuleResult RowEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
public static RuleResult RowEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
{
int squaresSolved = 0;

Expand Down Expand Up @@ -146,7 +107,7 @@ public RuleResult RowEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoard
return new RuleResult(squaresSolved, gameBoard, gameBoardPossibilities);
}

public RuleResult ColumnEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
public static RuleResult ColumnEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
{
int squaresSolved = 0;

Expand Down Expand Up @@ -190,21 +151,6 @@ public RuleResult ColumnEliminationRule(int[,] gameBoard, HashSet<int>[,] gameBo
return new RuleResult(squaresSolved, gameBoard, gameBoardPossibilities);
}

//TODO: Add a rule that adds up the numbers and calculates what is missing (1 to 9 is 45)
//TODO: Add a rule to look at the columns and rows
}

public class RuleResult
{
public int SquaresSolved;
public int[,] GameBoard;
public HashSet<int>[,] GameBoardPossibilities;

public RuleResult(int squaresSolved, int[,] gameBoard, HashSet<int>[,] gameBoardPossibilities)
{
SquaresSolved = squaresSolved;
GameBoard = gameBoard;
GameBoardPossibilities = gameBoardPossibilities;
}
}
}
10 changes: 2 additions & 8 deletions SudokuSolver/SudokuSolver.Core/Utility.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Core
namespace SudokuSolver.Core
{
public class Utility
{
public static int[] SquareSet = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public static readonly int[] SquareSet = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

public static string TrimNewLines(string input)
{
Expand Down
1 change: 0 additions & 1 deletion SudokuSolver/SudokuSolver.Tests/GameStateTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SudokuSolver.Core;
using System;
using System.Collections.Generic;

namespace SudokuSolver.Tests
{
Expand Down
8 changes: 8 additions & 0 deletions SudokuSolver/SudokuSolver.Tests/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Style", "IDE0090:Use 'new(...)'", Justification = "Not used to this new rule, ignoring for now", Scope = "namespaceanddescendants", Target = "~N:SudokuSolver.Tests")]
2 changes: 0 additions & 2 deletions SudokuSolver/SudokuSolver.Tests/RulesTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SudokuSolver.Core;
using System;
using System.Collections.Generic;

namespace SudokuSolver.Tests
{
Expand Down
2 changes: 0 additions & 2 deletions SudokuSolver/SudokuSolver.Tests/SolveGameTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SudokuSolver.Core;
using System;
using System.Collections.Generic;

namespace SudokuSolver.Tests
{
Expand Down
8 changes: 1 addition & 7 deletions SudokuSolver/SudokuSolver.Tests/Utility.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Tests
namespace SudokuSolver.Tests
{
public class Utility
{
Expand Down

0 comments on commit 4572eae

Please sign in to comment.