Java: the path from basic to advanced learning-creation of Sudoku games (1), java advanced

Source: Internet
Author: User

Java: the path from basic to advanced learning-creation of Sudoku games (1), java advanced
Preface

Most people have played this game, so the specific game itself is not described.

See Baidu Encyclopedia: http://baike.baidu.com/subview/961/10842669.htm

Rules and objectives

The rule of DT independence is simple, that is, duplicate numbers cannot appear in each row, column, and cell (3X3.

First, paste the approximate game interface:

Requirement Function Analysis

From the perspective of learners

Learn about the MVC Framework

The main Java content involved in this project is:

1. Basic Java syntax, especially the Java set.

2. Java built-in Observer mode.

3. Java AWT and Swing Components

4. Java component layout and event model Programming

From the player's perspective

Currently, only the most basic functions are implemented.

1) you can create a new game map and click the new button.

2) error: indicates whether the number in the current game progress does not comply with the rules is entered.

As follows:


3) Game assistance

That is, to enable the help function, click the corresponding number, and a prompt is displayed on the map where the number can be entered.

As follows:


4) the game is over, prompting whether to continue the game

5) quit

Summary Design Analysis

Now that we have learned about wiki MVC, we use the MVC framework for this project.

Therefore, we need to divide the sudoku game into three parts: model, view, and controller.

First, briefly describe the functions of each part of MVC: (the content comes from WIKI. For details, refer to the wiki link)

  • Model)It is used to encapsulate data related to the application's business logic and to process the data. The "model" has the right to directly access data, such as accessing databases. "Model" does not depend on "View" and "controller", that is, the model does not care about how it is displayed or operated. However, data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model. (Comparison: Observer mode (software design mode ))
  • View)Data can be displayed purposefully (theoretically, this is not necessary ). Generally, there is no program logic in the view. To refresh a view, the view needs to access the data Model it monitors. Therefore, you should register the data that it monitors in advance.
  • Controller)It serves as an organizational unit at different levels and is used to control application processes. It processes events and responds. "Events" include user behaviors and changes in data models.

The frame chart is as follows:


Model Section

1) Game MAP Storage Data Structure

2) New map generation algorithm of the game

3) check, help, and other functional Algorithms

Controller

1) Option Button and number selection response action

2) the corresponding number is entered.

View

1) Option Button, Help button, and number group button

2) enter the number and display it

4) Listener


In this example, the Observer mode is used to assist in data independence update checks and other operations.


OK. That's all about this section.

NEXT

Detailed design, including class diagrams, methods, and algorithms.




Java Sudoku game code

Public class ShuDu {
/** Array for storing numbers */
Static int [] [] n = new int [9] [9];
/** Generate the source array of random numbers. Random numbers are generated from this array */
Static int [] num = {1, 2, 4, 5, 6, 7, 8, 9 };
Public static void main (String [] args ){
// Generate a number
For (int I = 0; I <9; I ++ ){
// Number of attempts to fill
Int time = 0;
// Fill in the number
For (int j = 0; j <9; j ++ ){
// Generate a number
N [I] [j] = generateNum (time );
// If the returned value is 0, it indicates that the request is stuck and the request is returned.
// The principle of return processing is: if it is not the first column, first go backwards to the previous column; otherwise, go backwards to the last column of the previous row.
If (n [I] [j] = 0 ){
// If the column is not the first, a backward column is returned.
If (j> 0 ){
J-= 2;
Continue;
} If else {// is the first column, it is regressed to the last column of the previous row.
I --;
J = 8;
Continue;
}
}
// Filled
If (isCorret (I, j )){
// Initialize time to prepare for the next fill
Time = 0;
} Else {// continue filling
// Increase by 1
Time ++;
// Continue filling the current grid
J --;
}
}
}
// Output result
For (int I = 0; I <9; I ++ ){
For (int j = 0; j <9; j ++ ){
System. out. print (n [I] [j] + "");
}
System. out. println ();
}
}

/**
* Whether the row, column, and 3x3 fields are not repeated
* @ Param row
* @ Param col column number
* @ Return true indicates that the request meets the requirements.
*/
Public static boolean isCorret (int row, int col ){
Return (checkRow (row) & checkLine (col) & checkNine (row, col ));
}

/**
* Check whether the row meets the requirements.
* @ Param row indicates the row number for the row check.
* @ Return true indicates that the request meets the requirements.
*/
Public static boolean checkRow (int row ){
For (int j = 0; j <8; j ++ ){
If (n [row] [j] = 0 ){
Continue;
}
For (int k = j + 1; k <9; k ++ ){
If (n [row] [j] = n [row] [k]) {
Return false;
}
}
}
Return true;
}

/**
* Check whether the column meets the requirements.
... The remaining full text>

Basic Methods to learn about Sudoku games

The following are the basic methods:
1. Exclude numbers that are not possible at each point based on the constraints of the horizontal, vertical, and square columns, and write all possible numbers one by one in a small font into each blank grid from 1 to 9. (This step takes about 15-20 minutes. This is the initial solution and must be ensured that no omission is made ).
2. Check the result of step 1. If a space has only one number, make sure that the space is the number. Based on this number, you can view the related horizontal, vertical, and square columns and draw the same number. (This situation may not happen much. In addition to relatively simple Sudoku, this is a necessary process and should be used repeatedly in the subsequent process .)
3. Check the possible numerical results listed in the horizontal, vertical, and square columns. If a number appears only once in each horizontal, vertical, or square, then you can confirm that the space is interpreted as a number. The second method is used to exclude the same numbers in columns or squares related to this space.
4. Review the possible results listed in the horizontal, vertical, and square columns, and find the space (or three or four combinations) of the two numbers with relative names ), make sure that the numbers of the two spaces (or three or four) can only be the two numbers, that is, the two numbers can be exchanged at the positions of the two spaces, however, it is not possible to reach this row, this column, or other positions in this square. Based on this result, You can exclude the possibility of listing related numbers in related columns or squares, and narrow down the scope. (The processing of this step is relatively complex and must be carried out based on accumulated experience, which is also the key to the Final Solution)
5. Use the steps mentioned in steps 2, 3, and 4 repeatedly to gradually obtain a space solution and eliminate all the possible results listed above one by one, so that the possible range is smaller and smaller, until the final result is obtained.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.