/// <summary> /// Turns the jagged byte array array into a SudokuRow array. /// </summary> public static SudokuRow[] GenerateSudokuGridFromBoard(byte[][] sudokuBoard) { if (!IsSudokuBoardValid(sudokuBoard)) { throw new ArgumentException(InvalidSudokuBoardMessage); } var sudokuGrid = new SudokuRow[9]; for (int i = 0; i < 9; i++) { sudokuGrid[i] = new SudokuRow(); for (int j = 0; j < 9; j++) { if (sudokuBoard[i][j] == 0) { sudokuGrid[i][j] = new SudokuCell(null, false);; } else { sudokuGrid[i][j] = new SudokuCell(sudokuBoard[i][j], true); } } } return(sudokuGrid); }
// CONSTRUCTORS internal SudokuPuzzle() { this.Grid = new SudokuCell[9, 9]; this.Rows = new SudokuRow[9]; this.Columns = new SudokuColumn[9]; this.Blocks = new SudokuBlock[3, 3]; this.Errors = 0; this.Values = 81; for (int i = 0; i < 9; i++) { Rows[i] = new SudokuRow(); } for (int i = 0; i < 9; i++) { Columns[i] = new SudokuColumn(); } for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { Blocks[x, y] = new SudokuBlock(); } } }
public static void CopySudokuGrid(SudokuRow[] sudokuGridFrom, SudokuRow[] sudokuGridTo) { if (sudokuGridFrom == null || sudokuGridFrom.Length != 9 || sudokuGridTo == null || sudokuGridTo.Length != 9) { throw new ArgumentException(InvalidSudokuGridMessage); } for (int i = 0; i < 9; i++) { sudokuGridTo[i] = new SudokuRow(sudokuGridFrom[i]); } }
/// <summary> /// Populates the SudokuGrid with the last generated sudoku. /// </summary> public void RestartSudoku() { this.playerDecisions.Push(new RestartDecision(this.currentSudokuGrid.ToArray())); var restartedSudokuGrid = new SudokuRow[9]; GameUtils.CopySudokuGrid(this.initialSudokuGrid, restartedSudokuGrid); this.UpdateSudokuGridItems(restartedSudokuGrid); this.IsUnvalidCellValueAdded = false; this.InitiallyFilledSudokuCellsCount = GameUtils.GetFilledSudokuCellsCount(this.initialSudokuGrid, false); }
/// <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); }
// Start is called before the first frame update void Start() { string text; if (匯入難度 == SudokuDifficulty.Easy) { text = File.ReadAllText(Application.dataPath + "\\素材\\SudokuData\\easy.txt"); } else { text = File.ReadAllText(Application.dataPath + "\\素材\\SudokuData\\normal.txt"); } bool putAnswerTime = true; int rowNum = 0; Sudoku sudoku = new Sudoku { sudoku = new SudokuRow[9], played = false }; SudokuRow row = new SudokuRow { row = new OneNumber[9] }; int columnIndex = 0; for (int i = 0; i < text.Length; i++) { if (text[i] == '\r') { i++; if (putAnswerTime) { sudoku.sudoku[rowNum] = row; row = new SudokuRow { row = new OneNumber[9] }; } else { if (columnIndex != 9) { for (int j = columnIndex; j < 9; j++) { sudoku.sudoku[rowNum].row[j].existedInQues = false; } } columnIndex = 0; } rowNum++; if (rowNum == 9) { rowNum = 0; if (!putAnswerTime) { //put final sudoku into data SudokuDataManagement.AddSudoku(sudoku, 匯入難度); sudoku = new Sudoku { sudoku = new SudokuRow[9], played = false }; } putAnswerTime = !putAnswerTime; } } else { if (putAnswerTime) //put answer { row.row[columnIndex] = new OneNumber { number = text[i] - 48, existedInQues = false }; columnIndex = (columnIndex == 8) ? 0 : columnIndex + 1; } else //indicate the question { int checkIndex = columnIndex; while (/*text[i] != '\n' && */ sudoku.sudoku[rowNum].row[checkIndex].number != text[i] - 48) { sudoku.sudoku[rowNum].row[checkIndex].existedInQues = false; checkIndex++; } sudoku.sudoku[rowNum].row[checkIndex].existedInQues = true; columnIndex = checkIndex + 1; } } } }