/// <summary> /// 스도쿠 보드 (Grid)에 마지막으로 생성된 스도쿠를 채웁니다. /// </summary> public void RestartSudoku() { playerActions.Push(new RestartAction(currentSudokuGrid.ToArray())); var restartedSudokuGrid = new SudokuRow[9]; SudokuUtils.CopySudokuGrid(initialSudokuGrid, restartedSudokuGrid); UpdateSudokuGridItems(restartedSudokuGrid); IsUnvalidCellValueAdded = false; InitiallyFilledSudokuCellsCount = SudokuUtils.GetFilledSudokuCellsCount(initialSudokuGrid, false); }
/// <summary> /// 스도쿠 보드 (Grid)에 새로운 스도쿠를 채웁니다. /// </summary> public void GenerateAndPopulateSudoku() { var newSudokuGrid = sudokuGenerator.GenerateSudoku(SudokuDifficulty); initialSudokuGrid = new SudokuRow[9]; SudokuUtils.CopySudokuGrid(newSudokuGrid, initialSudokuGrid); InitiallyFilledSudokuCellsCount = SudokuUtils.GetFilledSudokuCellsCount(initialSudokuGrid, false); UpdateSudokuGridItems(newSudokuGrid); IsUnvalidCellValueAdded = false; playerActions = new Stack <IPlayerAction>(); undonePlayerActions = new Stack <IPlayerAction>(); }
/// <summary> /// 플레이어가 채운 셀을 기반으로 스도쿠의 현재 진행 상황을 반환합니다. /// </summary> public double GetProgress() { if (InitiallyFilledSudokuCellsCount == MaxFilledSudokuCellsCount) { return(100); } int playerFilledCells = SudokuUtils.GetFilledSudokuCellsCount(currentSudokuGrid.ToArray(), true); double progress = playerFilledCells / (double)(MaxFilledSudokuCellsCount - InitiallyFilledSudokuCellsCount) * 100; if (progress == 100) { SudokuSolved?.Invoke(this, new EventArgs()); } return(progress); }
/// <summary> /// 플레이어가 마지막으로 수행한 동작을 취소합니다. /// 새로운 스도쿠가 생성되면 플레이어 액션이 다시 시작됩니다. /// </summary> /// <returns>취소할 플레이어 액션이 있는지를 반환합니다.</returns> public bool UndoPlayerAction() { if (IsUnvalidCellValueAdded) { return(false); } if (playerActions.Count > 0) { var playerAction = playerActions.Pop(); if (playerAction.PlayerActionType == PlayerActionType.FillCell) { undonePlayerActions.Push(playerAction); var fillCellDec = playerAction as FillCellAction; currentSudokuGrid[fillCellDec.Row][fillCellDec.Column].Value = null; RefreshSudokuGridItems(); } else if (playerAction.PlayerActionType == PlayerActionType.Restart) { undonePlayerActions.Push(new RestartAction(currentSudokuGrid.ToArray())); var restartDec = playerAction as RestartAction; UpdateSudokuGridItems(restartDec.SudokuGridBeforeAction); } else if (playerAction.PlayerActionType == PlayerActionType.Solve) { undonePlayerActions.Push(new SolveAction(currentSudokuGrid.ToArray())); var solveDec = playerAction as SolveAction; UpdateSudokuGridItems(solveDec.SudokuGridBeforeAction); InitiallyFilledSudokuCellsCount = SudokuUtils.GetFilledSudokuCellsCount(initialSudokuGrid, false); } return(true); } return(false); }