public static bool SolveSudokuLogical2(SudokuModel model) { for (int i = 0; i < model.Sudoku.Count; i++) { for (int j = 0; j < model.Sudoku[0].Count; j++) { if (model.Sudoku[i][j].value == 0) { for (int c = 1; c <= 9; c++) { if (SudokuSolvingSubroutines.CheckConstraints(model, i, j, c)) { model.Sudoku[i][j].value = c; if (SolveSudokuLogical2(model)) { return(true); } else { model.Sudoku[i][j].value = 0; } } } return(false); } } } return(true); }
public static int[][] GuessANumber(SudokuModel sudokuModel) { var rnd = new Random(); for (int i = 2; i < 10;) { sudokuModel.Coord = FindLocation(sudokuModel.Cells, sudokuModel.Coord); List <int> possibleNumbers = Numberchecker(sudokuModel.Cells, sudokuModel.Coord); if (possibleNumbers.Count == i) { sudokuModel.Cells[sudokuModel.Coord.Row][sudokuModel.Coord.Col] = possibleNumbers[rnd.Next(possibleNumbers.Count)]; return(sudokuModel.Cells); } else { sudokuModel.Coord = NextCoordinate(sudokuModel.Coord); if (sudokuModel.Coord.Row == 0 && sudokuModel.Coord.Col == 0) { i++; } } } return(sudokuModel.Cells); }
/// <summary> /// Creates a randomized sudoku using the empty sudoku. /// </summary> /// <returns></returns> public ActionResult CreateSudoku() { TempData["sudoku"] = new SudokuModel { Cells = solver.Create(SudokuList.ElementAt(2).Cells) }; return(RedirectToAction("Sudoku")); }
public static int CheckOptions(SudokuModel model, int row, int col, int c) { int amounofoptions = 0; for (int i = 0; i < 9; i++) { if (model.Sudoku[i][col].value != 0 && model.Sudoku[i][col].value == c) { continue; } if (model.Sudoku[row][i].value != 0 && model.Sudoku[row][i].value == c) { continue; } if (model.Sudoku[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3].value != 0 && model.Sudoku[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3].value == c) { continue; } else { amounofoptions++; } } return(amounofoptions); }
public static List <CellModel> GenerateSudokuCellsHierarchical(SudokuModel model) { SudokuModel returnmodel = new SudokuModel(); List <CellModel> emptycells = new List <CellModel>(); for (int i = 0; i < 9; i++) { for (int a = 0; a < 9; a++) { if (model.Sudoku[i][a].value == 0) { emptycells.Add(CountOptionsCellHierarchical(model.Sudoku[i][a], model)); //if (returnmodel.FaultyBoard == true) //{ // return returnmodel; //} } //if (i == 8 && a == 8) //{ // return returnmodel; //} } } return(emptycells); }
public SudokuViewModel(string difficultyLevel) { SudokuModel obj = new SudokuModel(9, difficultyLevel); Sudoku = obj.Sudoku; sudokuGame = obj.SudokuGame; }
public ActionResult Solve(string problemId) { SudokuModel model = new SudokuModel(); model = _problemService.GetProblemByProblemId(problemId); return(View(model)); }
public ActionResult ChangeSudoku(int sudokuNumber = 2) { TempData["sudoku"] = new SudokuModel { Cells = SudokuList.ElementAt(sudokuNumber).Cells, SudokuId = sudokuNumber }; return(RedirectToAction("Sudoku")); }
public ActionResult Solve(SudokuModel Model) { solver.Reset(); stopwatch.Reset(); stopwatch.Start(); int[][] cells = solver.Solve(Model.Cells, Model.X); stopwatch.Stop(); TempData["Iterations"] = solver.Iterations; TempData["NumbersTried"] = solver.NumbersTried; TempData["ElapsedMilliseconds"] = stopwatch.ElapsedMilliseconds; if (cells == null) { TempData["Message"] = "Invalid board"; } else { Model.Cells = cells; } TempData["sudoku"] = Model; return(RedirectToAction("Sudoku")); }
////[Route("sudoku/login")] //[HttpPost("/sudoku/login")] //public IActionResult Login(string login) //{ // loggedUser = login; // field = (Field)HttpContext.Session.GetObject("field"); // HttpContext.Session.SetObject("field", field); // updateModel(model); // return View("Index", model); //} //[Route("/sudoku/logout")] //public IActionResult logout() //{ // var field = (Field)HttpContext.Session.GetObject("field"); // HttpContext.Session.SetObject("field", field); // loggedUser = null; // updateModel(model); // return RedirectToAction("Index", model); //} //[HttpPost("/sudoku/addName")] //public IActionResult addName([FromForm] string name) //{ // var field = (Field)HttpContext.Session.GetObject("field"); // loggedUser = name; // _scoreService.AddScore(new Score // { // Name = loggedUser, // Points = field.Score, // TimeOfScore = DateTime.Now // }); // HttpContext.Session.SetObject("field", field); // updateModel(model); // return RedirectToAction("Index", model); //} //[HttpPost("/sudoku/addComment")] //public IActionResult addComment([FromForm] string comment) //{ // _commentService.AddComment(new Comment // { // Name = loggedUser, // Message = comment, // TimeOFComment = DateTime.Now // }); // updateModel(model); // return RedirectToAction("Index", model); //} //[HttpPost("/sudoku/addRating")] //public IActionResult addRating([FromForm] int rating) //{ // _ratingService.AddRating(new Rating // { // Name = loggedUser, // Mark = rating, // TimeOfRating = DateTime.Now // }); // updateModel(model); // return RedirectToAction("Index", model); //} private void updateModel(SudokuModel model) { model.LoggedUser = loggedUser; model.Field = field; model.Scores = _scoreService.GetTopScores(); model.Comments = _commentService.GetLastComments(); model.Rating = _ratingService.GetAverageRating(); }
/// <summary> /// Solve without guessing. /// </summary> /// <param name="Model"></param> /// <returns></returns> public ActionResult Solve(SudokuModel Model) { solver.AllowGuessing = false; Model.Alert = solver.LastState; Model.Cells = solver.Solve(Model.Cells); TempData["sudoku"] = Model; return(RedirectToAction("Sudoku")); }
public void SaveSudoku(SudokuModel sudokuModel) { using (var unitOfWork = new UnitOfWork()) { unitOfWork.SudokuRepository.SaveSudoku(sudokuModel); unitOfWork.Commit(); } }
public SudokuModel ConvertListToSudokuModel(List <List <int> > sudoku) { var convertedSudoku = new SudokuModel(); var sudokuArray = sudoku.SelectMany(s => s).Select(s => Convert.ToByte(s)).ToArray(); convertedSudoku.Date = System.DateTime.Now; convertedSudoku.Sudoku = sudokuArray; return(convertedSudoku); }
public void AddSudoku(SudokuModel sudoku) { SudokuSolverContext.Sudokus.Add(new Sudoku() { SudokuId = sudoku.SudokuId, SudokuArray = sudoku.Sudoku, SudokuDate = sudoku.Date, }); }
public ActionResult Generate() { var sudoku = new SudokuModel(); var generator = new SudokuGenerator(); sudoku.board = generator.board; return(View(sudoku)); }
// GET: Sudoku public ActionResult Index(string type) { // full list that will be broken into the 3 parts of the Model then passed to the view int[] fullList = new int[81]; switch (type) { case null: fullList = GetEasy(); break; case "blank": fullList = GetBlank(); break; case "easy": fullList = GetEasy(); break; case "medium": fullList = GetMedium(); break; case "hard": fullList = GetHard(); break; default: break; } //Get full array from above, Break into 3 model lists to pass into the view. rows1-3, rows4-6, rows7-9; List <int> rows1to3 = new List <int>(); List <int> rows4to6 = new List <int>(); List <int> rows7to9 = new List <int>(); for (int i = 0; i < 27; i++) { rows1to3.Add(fullList[i]); } for (int i = 27; i < 54; i++) { rows4to6.Add(fullList[i]); } for (int i = 54; i < 81; i++) { rows7to9.Add(fullList[i]); } SudokuModel nbrList = new SudokuModel(); nbrList.rows1to3 = rows1to3; nbrList.rows4to6 = rows4to6; nbrList.rows7to9 = rows7to9; return(View(nbrList)); }
/// <summary> /// Change the Sudoku to the given sudoku Id from the Sukokus model containing several Sudokus. /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult ChangeSudoku(int?id) { int sudokuNumber = id ?? 0; var model = new SudokuModel(); model.Cells = SudokuList.ElementAt(sudokuNumber).Cells; model.SudokuId = sudokuNumber; TempData["sudoku"] = model; return(RedirectToAction("Sudoku")); }
public int AddProblemStatement(SudokuModel model) { try { _problemRepository.AddProblemStatement(model); return(1); } catch (Exception ex) { return(-1); } }
/// <summary> /// Validates the specified sudoku. /// </summary> /// <param name="sudoku">The sudoku to validate.</param> /// <returns>Result of sudoku validation</returns> public static bool Validate(SudokuModel sudoku) { IEnumerable <IValidationRule> validationRules = new List <IValidationRule> { new FilledRule(), new RowRule(), new ColumnRule(), new BlockRule() }; return(validationRules.All(rule => rule.Check(sudoku))); }
public void SaveSudoku(SudokuModel sudoku) { var dbSudoku = SudokuSolverContext.Sudokus.Where(q => q.SudokuId == sudoku.SudokuId).FirstOrDefault(); if (dbSudoku == null) { AddSudoku(sudoku); } else { UpdateSudoku(dbSudoku, sudoku); } }
/// <summary> /// Checks the specified sudoku only by rows rule. /// </summary> /// <param name="sudoku">The sudoku to validate</param> /// <returns>Result of rows validation</returns> public bool Check(SudokuModel sudoku) { for (int i = 0; i < 9; i++) { HashSet <int> diffChecker = new HashSet <int>(); if (!sudoku.GetRow(i).All(x => diffChecker.Add(x))) { return(false); } } return(true); }
public ActionResult AddProblem(SudokuModel model) { int result = _problemService.AddProblemStatement(model); if (result > 0) { TempData[Constants.SUCCESS_MESSAGE] = "Problem statement creatred successfully"; } else { TempData[Constants.ERROR_MESSAGE] = "Error while adding problem statement"; } return(RedirectToAction("Index")); }
// GET: Sudoku public ActionResult Sudoku() { if (TempData["sudoku"] != null) { sudokuModel = TempData["sudoku"] as SudokuModel; } sudokuModel.Sudokus = SudokuList; if (sudokuModel.Cells == null) { sudokuModel.Cells = SudokuList.ElementAt(0).Cells; } return(View(sudokuModel)); }
public static SudokuModel SolveSudokuHierArchical(SudokuModel model) { var hierarchy = SudokuSolvingSubroutines.GenerateSudokuCellsHierarchical(model); for (int a = 1; a < 10; a++) { List <CellModel> ascendingoptionscellvalues = hierarchy.Where(x => x.amountofoptions == a).ToList(); if (ascendingoptionscellvalues.Any()) { for (int x = 0; x < ascendingoptionscellvalues.Count; x++) { SudokuSolvingSubroutines.SolveCell(ascendingoptionscellvalues[x], model); } } } return(model); }
/// <summary> /// gets all sudokus and their creation date and stores them in an IDictionary which is a list of integers and DateTimes /// and returns the IDictionary /// </summary> /// <returns></returns> private SudokuListView GetListViewModel() { string userID = User.Identity.Name; SudokuListView List = new SudokuListView(); SudoCRUD s = new SudoCRUD(); IDictionary <int, DateTime> DBList = s.SudokuList(); foreach (var item in DBList) { SudokuModel temp = new SudokuModel(); temp.ID = item.Key; temp.CreationDate = item.Value; List.AddSudoToList(temp); } return(List); }
public static SudokuModel SolveSudoku(int id, SudokuModel model) { switch (id) { case 5: SolveSudokuLogical2(model); break; case 6: SolveSudokuGuessing(model); break; default: break; } return(model); }
public ActionResult Solve() { if (TempData["sudoku"] != null) { SudokuModel sudoku = (SudokuModel)TempData["sudoku"]; TempData.Remove("sudoku"); if (solver.AttemptSolve(sudoku.board)) { return(View(sudoku)); } { return(RedirectToAction("Unsolvable")); } } return(View()); }
public void TestLogical() { SudokuModel sudoku = SudokuData.GetMockData().FirstOrDefault(sudku => sudku.Name == "Logical"); var solvedCells = solver.Solve(sudoku.Cells); var fullySolved = new int[][] { new int[9] { 5, 3, 4, 6, 7, 8, 9, 1, 2 }, new int[9] { 6, 7, 2, 1, 9, 5, 3, 4, 8 }, new int[9] { 1, 9, 8, 3, 4, 2, 5, 6, 7 }, new int[9] { 8, 5, 9, 7, 6, 1, 4, 2, 3 }, new int[9] { 4, 2, 6, 8, 5, 3, 7, 9, 1 }, new int[9] { 7, 1, 3, 9, 2, 4, 8, 5, 6 }, new int[9] { 9, 6, 1, 5, 3, 7, 2, 8, 4 }, new int[9] { 2, 8, 7, 4, 1, 9, 6, 3, 5 }, new int[9] { 3, 4, 5, 2, 8, 6, 1, 7, 9 } }; for (int y = 0; y < 9; y++) { for (int x = 0; x < 9; x++) { Assert.AreEqual(solvedCells[y][x], fullySolved[y][x], "Row: " + y + " | Column: " + x); } } }
public static bool CheckConstraints(SudokuModel model, int row, int col, int c) { for (int i = 0; i < 9; i++) { if (model.Sudoku[i][col].value != 0 && model.Sudoku[i][col].value == c) { return(false); } if (model.Sudoku[row][i].value != 0 && model.Sudoku[row][i].value == c) { return(false); } if (model.Sudoku[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3].value != 0 && model.Sudoku[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3].value == c) { return(false); } } return(true); }
public static bool CalculateListOfOptions(SudokuModel model) { List <CellModel> listofoptions = new List <CellModel>(); for (int i = 0; i < model.Sudoku.Count; i++) { for (int j = 0; j < model.Sudoku[0].Count; j++) { if (model.Sudoku[i][j].value == 0) { for (int c = 1; c <= 9; c++) { model.Sudoku[i][j].amountofoptions = CheckOptions(model, i, j, c); } return(false); } } } return(true); }