public static void AddSudoku(Sudoku sudoku, SudokuDifficulty difficulty) { if (difficulty == SudokuDifficulty.Easy) { Sudoku[] temp = new Sudoku[data_easy.sudokus.Length + 1]; for (int i = 0; i < data_easy.sudokus.Length; i++) { temp[i] = data_easy.sudokus[i]; } temp[data_easy.sudokus.Length] = sudoku; data_easy.sudokus = temp; EditorUtility.SetDirty(data_easy); } else { Sudoku[] temp = new Sudoku[data_normal.sudokus.Length + 1]; for (int i = 0; i < data_normal.sudokus.Length; i++) { temp[i] = data_normal.sudokus[i]; } temp[data_normal.sudokus.Length] = sudoku; data_normal.sudokus = temp; EditorUtility.SetDirty(data_normal); } }
private static int Generate(int size, SudokuDifficulty difficulty, String outfile) { SudokuPuzzle sudoku = SudokuGenerator.Generate(size, difficulty); if (outfile is null) { Console.WriteLine(sudoku); } else { if (Program.WriteToFile(sudoku.ToString(), outfile)) { Console.WriteLine($"Generated sudoku written to {outfile}"); } else { Console.WriteLine(Program.FileOutputErrorMessage); return(Program.FileOutputError); } } return(0); }
public void TestEquals() { SudokuDifficulty newDifficulty = SudokuPuzzleTest.Difficulty == SudokuDifficulty.None ? SudokuDifficulty.Easy : SudokuDifficulty.None; SudokuPuzzle puzzle = new SudokuPuzzle(this.Sudoku.Size, newDifficulty); SudokuGenerator.AddNumbers(puzzle); for (int i = 0; i < this.Sudoku.Size; i++) { for (int j = 0; j < this.Sudoku.Size; j++) { this.Sudoku[i, j] = puzzle[i, j]; } } Assert.AreEqual(this.Sudoku, puzzle); Assert.IsTrue(this.Sudoku.Equals(puzzle)); Assert.IsTrue(this.Sudoku == puzzle); Assert.IsFalse(this.Sudoku != puzzle); Assert.IsFalse(this.Sudoku == null); Assert.IsFalse(null == this.Sudoku); SudokuPuzzle nullPuzzle = null; Assert.IsTrue(null == nullPuzzle); this.Sudoku[0, 0] = 0; Assert.AreNotEqual(this.Sudoku, puzzle); }
public ISudokuPuzzle Generate(SudokuDifficulty config, int?seed = null) { if (seed == null) { seed = new Random().Next(); } SudokuPuzzle puzzle = new SudokuPuzzle((int)seed); m_AvailableNumbers.Clear(); int i = 0; while (i < 81) { if (!TryGetNextNumber(i, (int)seed, out int val)) { puzzle.Cells[i].Value = 0; i--; continue; } puzzle.Cells[i].Value = val; if (m_Validator.ValidateSudoku(puzzle)) { i++; } } AdjustPuzzleToConfig(puzzle, config, (int)seed); return(puzzle); }
public static List <Core.GrilleSudoku> GetSudokus(SudokuDifficulty difficulty) { string fileName = difficulty switch { SudokuDifficulty.Easy => "Sudoku_Easy50.txt", SudokuDifficulty.Medium => "Sudoku_hardest.txt", _ => "Sudoku_top95.txt" }; DirectoryInfo puzzlesDirectory = null; var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory); do { var subDirectories = currentDirectory.GetDirectories(); foreach (var subDirectory in subDirectories) { if (subDirectory.Name == PUZZLES_FOLDER_NAME) { puzzlesDirectory = subDirectory; break; } } currentDirectory = currentDirectory.Parent; if (currentDirectory == null) { throw new ApplicationException("couldn't find puzzles directory"); } } while (puzzlesDirectory == null); string filePath = $@"{puzzlesDirectory}\{fileName}"; var sudokus = Core.GrilleSudoku.ParseFile(filePath); return(sudokus); } }
public void SetDifficulty(int d) { difficulty = (SudokuDifficulty)d; DifficultyCanvas.SetActive(false); GameCanvas.SetActive(true); start = true; }
private static void SingleSolverTest() { var solvers = Core.GrilleSudoku.GetSolvers(); Console.WriteLine("Select difficulty: 1-Easy, 2-Medium, 3-Hard"); var strDiff = Console.ReadLine(); int.TryParse(strDiff, out var intDiff); SudokuDifficulty difficulty = intDiff switch { 1 => SudokuDifficulty.Easy, 2 => SudokuDifficulty.Medium, _ => SudokuDifficulty.Hard }; var sudokus = SudokuHelper.GetSudokus(difficulty); Console.WriteLine($"Choose a puzzle index between 1 and {sudokus.Count}"); var strIdx = Console.ReadLine(); int.TryParse(strIdx, out var intIdx); var targetSudoku = sudokus[intIdx - 1]; Console.WriteLine("Chosen Puzzle:"); Console.WriteLine(targetSudoku.ToString()); Console.WriteLine("Choose a solver:"); for (int i = 0; i < solvers.Count(); i++) { Console.WriteLine($"{(i + 1).ToString(CultureInfo.InvariantCulture)} - {solvers[i].GetType().FullName}"); } var strSolver = Console.ReadLine(); int.TryParse(strSolver, out var intSolver); var solver = solvers[intSolver - 1]; var cloneSudoku = targetSudoku.CloneSudoku(); var sw = Stopwatch.StartNew(); solver.Solve(cloneSudoku); var elapsed = sw.Elapsed; if (!cloneSudoku.IsValid(targetSudoku)) { Console.WriteLine($"Invalid Solution : Solution has {cloneSudoku.NbErrors(targetSudoku)} errors"); Console.WriteLine("Invalid solution:"); } else { Console.WriteLine("Valid solution:"); } Console.WriteLine(cloneSudoku.ToString()); Console.WriteLine($"Time to solution: {elapsed.TotalMilliseconds} ms"); } }
public static int GetNumber(SudokuDifficulty difficulty, int sudokuIndex, int rowNum, int colNum) { if (difficulty == SudokuDifficulty.Easy) { return(data_easy.sudokus[sudokuIndex].sudoku[rowNum].row[colNum].number); } else { return(data_normal.sudokus[sudokuIndex].sudoku[rowNum].row[colNum].number); } }
public static bool GetExistedInQues(SudokuDifficulty difficulty, int sudokuIndex, int rowNum, int colNum) { if (difficulty == SudokuDifficulty.Easy) { return(data_easy.sudokus[sudokuIndex].sudoku[rowNum].row[colNum].existedInQues); } else { return(data_normal.sudokus[sudokuIndex].sudoku[rowNum].row[colNum].existedInQues); } }
public static void SetIndexPlayed(int index, SudokuDifficulty difficulty) { if (difficulty == SudokuDifficulty.Easy) { data_easy.sudokus[index].played = true; notPlayedSudokuNum_easy--; } else { data_normal.sudokus[index].played = true; notPlayedSudokuNum_normal--; } }
public ActionResult <SudokuPuzzleDTO> Generate(SudokuDifficulty difficulty, int?seed) { ISudokuGenerator generator = new SudokuGenerator(); ISudokuPuzzle puzzle = generator.Generate(difficulty, seed); SudokuPuzzleDTO puzzleDTO = new SudokuPuzzleDTO() { Puzzle = puzzle.ToString(), Seed = puzzle.Id }; return(puzzleDTO); }
public static void FlushPlayedBoolean(SudokuDifficulty difficulty) { if (difficulty == SudokuDifficulty.Easy) { foreach (Sudoku sudoku in data_easy.sudokus) { sudoku.played = false; } } else { foreach (Sudoku sudoku in data_easy.sudokus) { sudoku.played = false; } } }
public static int GetQuestionIndex(SudokuDifficulty difficulty) { if (difficulty == SudokuDifficulty.Easy) { int randomNum = Random.Range(0, notPlayedSudokuNum_easy); int count = 0; for (int i = 0; i < data_easy.sudokus.Length; i++) { if (!data_easy.sudokus[i].played) { if (count == randomNum) { return(i); } else { count++; } } } Debug.Log("Something wrong in GetQuestionIndex of SudokuDataManagement"); return(0); } else { int randomNum = Random.Range(0, notPlayedSudokuNum_normal); int count = 0; for (int i = 0; i < data_normal.sudokus.Length; i++) { if (!data_normal.sudokus[i].played) { if (count == randomNum) { return(i); } else { count++; } } } Debug.Log("Something wrong in GetQuestionIndex of SudokuDataManagement"); return(0); } }
private int GetDifficulty(SudokuDifficulty config) { switch (config) { case SudokuDifficulty.easy: return(81 - 35); case SudokuDifficulty.medium: return(81 - 30); case SudokuDifficulty.hard: return(81 - 25); case SudokuDifficulty.solved: return(0); default: throw new NotImplementedException($"{config} is not implemented"); } }
public void TestGenerateArguments() { const int NEGATIVE_SIZE = -1; const int ZERO_SIZE = 0; const int BAD_POSITIVE_SIZE = 2; const int GOOD_POSITIVE_SIZE = 9; const SudokuDifficulty BAD_DIFF = SudokuDifficulty.None; const SudokuDifficulty GOOD_DIFF = SudokuDifficulty.Easy; const String BAD_DIFF_STRING = "NONE"; const String BAD_DIFF_FORMAT = "E"; const String GOOD_DIFF_STRING = "EaSy"; const String BAD_FILENAME = ""; const String GOOD_FILENAME = "Sudoku.txt"; const String GOOD_OUTNAME = "SudokuGenerate.txt"; Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "XXXXXX" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-XXXXXX" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", "-d" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", "-d", "-f" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString() })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", "-f" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", NEGATIVE_SIZE.ToString(), "-d", BAD_DIFF.ToString(), "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", ZERO_SIZE.ToString(), "-d", BAD_DIFF.ToString(), "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", BAD_POSITIVE_SIZE.ToString(), "-d", BAD_DIFF.ToString(), "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", BAD_DIFF.ToString(), "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", BAD_DIFF_STRING, "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", BAD_DIFF_FORMAT, "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF_STRING, "-o", BAD_FILENAME })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF_STRING, "-o", GOOD_FILENAME, "-XXX" })); Assert.AreEqual(Program.UsageMessageWarning, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF_STRING, "-o", GOOD_FILENAME, "XXX" })); Assert.AreEqual(0, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF_STRING, "-o", GOOD_OUTNAME })); Assert.AreEqual(0, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF_STRING })); Assert.AreEqual(0, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF.ToString(), "-o", GOOD_OUTNAME })); Assert.AreEqual(0, Program.Main(new String[] { "GENERATE", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF.ToString() })); Assert.AreEqual(0, Program.Main(new String[] { "G", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF.ToString() })); Assert.AreEqual(0, Program.Main(new String[] { "GEN", "-s", GOOD_POSITIVE_SIZE.ToString(), "-d", GOOD_DIFF.ToString() })); }
private void AdjustPuzzleToConfig(ISudokuPuzzle sudoku, SudokuDifficulty config, int seed) { int vals = GetDifficulty(config); List <int> locations = new List <int>(); Random rand = new Random(seed); for (int i = 0; i < vals; i++) { int location; do { location = rand.Next(81); } while (locations.Contains(location)); locations.Add(location); } foreach (int i in locations) { sudoku.Cells[i] = new UserCell(i); } }
public void TestGetHashCode() { SudokuDifficulty newDifficulty = SudokuPuzzleTest.Difficulty == SudokuDifficulty.None ? SudokuDifficulty.Easy : SudokuDifficulty.None; SudokuPuzzle puzzle = new SudokuPuzzle(this.Sudoku.Size, newDifficulty); SudokuGenerator.AddNumbers(puzzle); for (int i = 0; i < this.Sudoku.Size; i++) { for (int j = 0; j < this.Sudoku.Size; j++) { this.Sudoku[i, j] = puzzle[i, j]; } } int hashCode = puzzle.GetHashCode(); Assert.AreEqual(this.Sudoku, puzzle); Assert.AreEqual(this.Sudoku.GetHashCode(), hashCode); this.Sudoku[0, 0] = 0; Assert.AreNotEqual(this.Sudoku, puzzle); Assert.AreNotEqual(this.Sudoku.GetHashCode(), hashCode); }
public static Sudoku GenerateUnsolved(int seed, SudokuDifficulty difficulty) { throw new NotImplementedException(); }
public static ISudoku Generate(SudokuDifficulty difficulty) => Generate(Rand.Next(), difficulty);
public MediumSudokuTechniques(SudokuDifficulty sudokuDifficulty) { this.sudokuDifficulty = sudokuDifficulty; }
public static ISudoku Generate(int seed, SudokuDifficulty difficulty) { return(Generator.GenerateUnsolved(seed, difficulty)); }
public EasySudokuTechniques(SudokuDifficulty sudokuDifficulty) { this.sudokuDifficulty = sudokuDifficulty; }
/*public override void OnActivityCreated(Bundle savedInstanceState) * { * base.OnActivityCreated(savedInstanceState);*/ private void Start() { this.ViewModel = (SudokuViewModel) new ViewModelProvider(this).Get(Java.Lang.Class.FromType(typeof(SudokuViewModel))); ActionType action = this.ViewModel.Action = (ActionType)this.Activity.Intent.Extras.GetInt("action", 1); switch (action) { case ActionType.Continue: this.ViewModel.OpenAsync(this.Context.FilesDir, SudokuDifficulty.None); break; case ActionType.Generate: case ActionType.New: SudokuDifficulty difficulty = (SudokuDifficulty)this.Activity.Intent.Extras.GetInt("difficulty", 1); this.ViewModel.OpenAsync(this.Context.FilesDir, difficulty); break; case ActionType.Play: break; default: throw new NotSupportedException(); } SudokuView sudokuView = this.View.FindViewById <SudokuView>(Resource.Id.sudoku_view); ControlFragment controlFragment; SingleButtonFragment singleButtonFragment; this.SudokuViewChanged = new EventHandler <SudokuViewEventArgs>((sender, e) => this.ViewModel.SetRowAndColumn(e.Row, e.Column)); this.ViewModelCompleted = new EventHandler(this.OnCompletion); this.ViewModelStateChangedSudokuViewUpdate = new EventHandler <ControlEventArgs>((sender, e) => { sudokuView.SelectedNumber = e.Number; sudokuView.SetRowAndColumn(this.ViewModel.Row, this.ViewModel.Column); }); this.ViewModelSudokuChanged = new EventHandler((sender, e) => { sudokuView.Sudoku = this.ViewModel.Sudoku; sudokuView.Update(); }); sudokuView.Changed += this.SudokuViewChanged; this.ViewModel.StateChanged += this.ViewModelStateChangedControlFragmentUpdate; this.ViewModel.StateChanged += this.ViewModelStateChangedSudokuViewUpdate; this.ViewModel.Completed += this.ViewModelCompleted; this.ViewModel.SudokuChanged += this.ViewModelSudokuChanged; sudokuView.Sudoku = this.ViewModel.Sudoku; sudokuView.SetRowAndColumn(this.ViewModel.Row, this.ViewModel.Column); sudokuView.SelectedNumber = this.ViewModel.State.Number; FragmentTransaction ft = this.Activity.SupportFragmentManager.BeginTransaction();; switch (action) { case ActionType.Continue: case ActionType.New: case ActionType.Play: //controlFragment = this.ChildFragmentManager.FindFragmentById(Resource.Id.control_fragment) as ControlFragment; controlFragment = new ControlFragment(); this.ControlFragmentChanged = new EventHandler <ControlEventArgs>((sender, e) => this.ViewModel.State = e); this.ViewModelStateChangedControlFragmentUpdate = new EventHandler <ControlEventArgs>((sender, e) => controlFragment.State = e); controlFragment.Changed += this.ControlFragmentChanged; controlFragment.State = this.ViewModel.State; this.ViewModel.StateChanged += this.ViewModelStateChangedControlFragmentUpdate; ft.Add(Resource.Id.fragment_frame_layout, controlFragment, "control_fragment"); ft.Commit(); break; case ActionType.Generate: if (this.ViewModel.ButtonText is null) { this.ViewModel.ButtonText = this.Resources.GetString(Resource.String.add_button_text); } singleButtonFragment = new SingleButtonFragment(this.ViewModel.ButtonText); this.SingleButtonFragmentClicked = new EventHandler(this.OnSingleButtonFragmentClick); singleButtonFragment.Click += this.SingleButtonFragmentClicked; ft.Add(Resource.Id.fragment_frame_layout, singleButtonFragment, "button_fragment"); ft.Commit(); break; } }
private static bool ParseArguments(String[] args, out int command, out int size, out SudokuDifficulty difficulty, out String filename, out String outfile) { command = size = 0; filename = outfile = null; difficulty = SudokuDifficulty.None; try { switch (args[0].ToLower()) { case "c": case "comp": case "compare": command = Program.CompareCommand; break; case "e": case "enum": case "enumerate": command = Program.EnumerateCommand; break; case "g": case "gen": case "generate": command = Program.GenerateCommand; break; case "i": case "int": case "inter": case "interactive": command = Program.InteractiveCommand; break; case "p": case "print": command = Program.PrintCommand; break; case "s": case "solve": command = Program.SolveCommand; break; default: return(false); } for (int i = 1; i < args.Length; i++) { if (String.IsNullOrWhiteSpace(args[i])) { continue; } String arg = Regex.Replace(args[i].ToLower(), @"\s+", ""); if (arg[0] == '-') { if (arg.Length > 2) { return(false); } //String param = Regex.Replace(args[i + 1].ToLower(), @"\s+", ""); String param = args[i + 1]; switch (arg[1]) { case 'd': if (command != Program.GenerateCommand || difficulty != SudokuDifficulty.None) { return(false); } try { difficulty = (SudokuDifficulty)Enum.Parse(typeof(SudokuDifficulty), param, true); } catch { difficulty = (SudokuDifficulty)Int32.Parse(param); } if (difficulty == SudokuDifficulty.None) { return(false); } break; case 'f': if (command == Program.GenerateCommand || !(filename is null) || String.IsNullOrWhiteSpace(param) || command == Program.InteractiveCommand && size != 0) { return(false); } filename = param; break; case 'o': if (command == Program.PrintCommand || !(outfile is null) || String.IsNullOrWhiteSpace(param)) { return(false); } outfile = param; break; case 's': if (!(command == Program.GenerateCommand || command == Program.InteractiveCommand) || size != 0 || command == Program.InteractiveCommand && !(filename is null)) { return(false); } size = Int32.Parse(param); if (!SudokuPuzzle.VerifySize(size)) { return(false); } break; default: return(false); } i++; } else { return(false); } } } catch { return(false); } switch (command) { case Program.CompareCommand: if (String.IsNullOrWhiteSpace(filename) || String.IsNullOrWhiteSpace(outfile)) { return(false); } break; case Program.EnumerateCommand: if (filename is null) { return(false); } break; case Program.GenerateCommand: if (!SudokuPuzzle.VerifySize(size) || difficulty == SudokuDifficulty.None) { return(false); } break; case Program.InteractiveCommand: if (filename is null && !SudokuPuzzle.VerifySize(size) || !(filename is null || size == 0)) { return(false); } break; case Program.PrintCommand: if (String.IsNullOrWhiteSpace(filename)) { return(false); } break; case Program.SolveCommand: if (String.IsNullOrWhiteSpace(filename) || !(outfile is null) && String.IsNullOrWhiteSpace(outfile)) { return(false); } break; default: return(false); } return(true); }