Instead of using a string (such as "correct-guess", "wrong-guess", "to-guess") or an int (1 for correct, 2 for wrong, 3 for to-guess) to represent the status of a cell, JDK 5 introduces a feature known as enumeration ( enum) to efficiently maintain a set of constants in a type-safe manner. The CellStatus Enumeration ( enum) of Constants If you are using JDK/TextEditor, create a sub-directory called sudoku and place the classes under the sub-directory.You can then create the classes under the sudoku package. In Eclipse/NetNeans, first create a "Java Project" called "sudoku" then create a new package (new ⇒ package) also called sudoku. The method newPuzzle() can be used to generate a new puzzle for a new game.Īll the classes are kept in a package called sudoku. Puzzle: A class called Puzzle is designed to model the number puzzle, which holds the numbers and clues in 9x9 int array numbers and boolean array isGiven.SudokuMain: We further customize the JFrame, by creating a subclass called SudokuMain, to hold the GameBoardPanel ( JPanel) in its ContentPane.Similar to Cell, the GameBoardPanel has its own methods to paint() itself. GameBoardPanel: We also customize the JPanel, by creating a subclass called GameBoardPanel, to hold the grid of 9x9 Cells ( JTextFields).CellStatus: An enumeration ( enum) called CellStatus is designed to hold the status constants, including GIVEN, CORRECT_GUESS, WRONG_GUESS and TO_GUESS.The Cell has its own methods to paint() itself. Cell: We customize the JTextField, by creating a subclass called Cell, with additional variables row, col, number and status, to model each cell of the grid.GRID_SIZE constant: Instead of hardcoding the grid size (to 9x9), we define a static constant GRID_SIZE in the GameBoardPanel class, which can be referred to as GameBoardPanel.GRID_SIZE. However, it is hard to identify the row and column of the JTextField triggering an event.įor better OO and modular design, we design the following classes (in a package called sudoku) as shown in the above class diagram: We could simply use 9x9 JTextFields (for entering guesses) arranged in 9x9 GridLayout on a JPanel/ ContentPane. There is no fast algorithm for solving the puzzle. Mathematically, Sudoku is a NP-complete problem. The One Rule can be compactly stated as: "Each digit appears once in each group." Rows, columns and regions can be collectively referred to as groups, of which the grid has 27. The initially defined values are clues or givens. A grid has 9 rows, 9 columns and 9 boxes (or blocks or regions), each having 9 cells (or squares), for a total of 81 cells. the puzzle) is a partially completed grid. You could wiki "Sudoku" to understand the rules of the game.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |