Statement:
This article describes the development of the sudoku game. The sudoku game is a digital-filling game. Within a 9x9 Square, the 9x9 big lattice can be divided into 9 3x3 small jiugongge, fill in numbers ranging from 1 to 9 in these grids, so that the numbers in each row and column are not repeated. The gameplay is simple and the combination of numbers is ever changing, so it is very interesting to play.
It seems that Chinese Sudoku games are not so popular, but in Japan this game is very popular. On the commuter tram, we often see some people holding a book on the sudoku game in one hand, the other hand is holding a pencil, and that's how it counts. Now I use the lufylegend. js engine to move the game to a browser, as shown in the game interface.
Figure 1
The game is divided into two stages. The first stage is relatively simple gameplay. It only needs to be horizontal, vertical, and there are no repeated numbers. The other is an advanced stage, we also need to ensure that the numbers in each small nine cells are not repeated. If you want to challenge yourself, you can click the following game link to try out how many levels you can pass.
Http://lufylegend.com/demo/sudoku
Like the previous box-pushing games, there are 6 levels in total, and the game also has a ranking system. Each pass can upload your own scores to compete with everyone.
First, you need to download the lufylegend. js engine.
Below is my post on Blog lufylegend-1.6
Http://blog.csdn.net/lufy_legend/article/details/8593968
Next we will step into the development topic.
2. Game Algorithms
In this game, we must first solve the problem of how numbers are disrupted, because not only do we need to disrupt numbers, but we also need to ensure that these numbers are still in compliance with the rule of sudoku, then you can hide a part of the disrupted number to start the game.
Let's first look at a group of numbers.
Figure 2
We can see that in this group of numbers, the numbers in the horizontal and vertical columns are not repeated. How can we disrupt its order? It is not hard to see that if we only disrupt every line of it, its integrity will not be affected. Similarly, we only disrupt each column of it, and it will not be affected. Therefore, to disrupt it, you only need to disrupt it by row and column. The algorithm is as follows.
function randomNum01(lv){var i,j,list = new Array(),result = new Array();for(i=0;i<9;i++){list.push([1,2,3,4,5,6,7,8,9]);for(j=0;j.5?-1:1;});var rand = new Array(0,1,2,3,4,5,6,7,8).sort(function(a,b){return Math.random()>.5?-1:1;});for(i=0;i<9;i++){for(j=0;j<9;j++){result[i].push(list[i][rand[j]]);}}for(i=0;i<9;i++){for(j=0;j>> 0;result[i][ran1] = 0;ran1 = Math.random()*9 >>> 0;result[ran1][i] = 0;}}return result;}
In the above function, I first generate a set of regular numbers, then disrupt them according to the rows and slashes, and finally randomly remove some numbers.
Next, let's look at another set of numbers.
Figure 3
In this case, what should we do to ensure the integrity of the numbers in each small cell? Here I have a lazy algorithm. See figure 4 below.
Figure 4
If we disrupt every three rows and columns as a unit, it is easy to achieve the goal. Of course, this is just a lazy algorithm. If you have a better algorithm, welcome to discuss it together. My algorithm is as follows.
function randomNum02(lv){var i,j,k,list = [],result = [],rand;for(i=0;i<9;i++){list.push([1,2,3,4,5,6,7,8,9]);for(j=0;j.5?-1:1;}).concat(new Array(3,4,5).sort(function(a,b){return Math.random()>.5?-1:1;}),new Array(6,7,8).sort(function(a,b){return Math.random()>.5?-1:1;}));for(i=0;i<9 i="" result="" push="" list="" rand="" i="" list="result;" rand="new" array="" 0="" 1="" 2="" sort="" function="" a="" b="" return="" math="" random="">.5?-1:1;}).concat(new Array(3,4,5).sort(function(a,b){return Math.random()>.5?-1:1;}),new Array(6,7,8).sort(function(a,b){return Math.random()>.5?-1:1;}));result = [];for(i=0;i<9;i++){result.push([]);for(j=0;j<9;j++){result[i].push(list[i][rand[j]]);}}for(i=0;i<9;i++){for(j=0;j>> 0;result[i][ran1] = 0;ran1 = Math.random()*9 >>> 0;result[ran1][i] = 0;}}return result;}
3. Determine the correctness of numbers
When a player restores all the removed numbers, it is necessary to determine whether the number they entered is correct and whether it meets the rule of the sudoku game. The method is simple, that is, to verify each row, if there are no duplicates in each column and number in each nine cells in the advanced stage, below is the code
function checkWin(){var check01,check02;for(var i=0;i<9;i++){check01 = [];check02 = [];for(var j=0;j<9 j="" if="" stagenumlist="" i="" j="" value=""> 0)check01.push(stageNumList[i][j].value);if(stageNumList[j][i].value > 0)check02.push(stageNumList[j][i].value);}check01 = deleteEleReg(check01);check02 = deleteEleReg(check02);if(check01.length < 9)return false;if(check02.length < 9)return false;}var stage = stageMenu[stageIndex];if(stage.flag){return checkWin02();}return true;}function checkWin02(){for(var i=0;i<3;i++){for(var j=0;j<3;j++){if(!check_mini(i,j))return false;}}return true;}function check_mini(i2,j2){var check_arr = [];for(var i=i2*3;i<i2*3+3;i++){for(var j=j2*3;j<j2*3+3;j++){if(check_arr[stageNumList[i][j].value])return false;check_arr[stageNumList[i][j].value] = 1;}}return true;}
This game is very simple. The core algorithms of the entire game have been solved.
4. Create a Starting Screen
As follows.
Figure 4
As I said last time, using the lufylegend. js engine to create an interface is no difficulty. The Code is as follows.
Function gamelogo () {base (this, lsprite, []); var self = This; var logolist =, 2, 1], [,]; var bitmap, logolayer; logolayer = new lsprite (); bitmap = new lbitmap (New lbitmapdata (imglist ["logo"]); bitmap. scalex = bitmap. scaley = 2; logolayer. addchild (Bitmap); self. addchild (logolayer); var social = new social (); Social. X = 60; Social. y= 500; self. addchild (social); labeltext = new ltextfield (); labeltext. font = "Hg row entity"; labeltext. size = 14; labeltext. X = 50; labeltext. y = 650; labeltext. TEXT = "-HTML5 game engine lufylegend. JS "; self. addchild (labeltext); labeltext = new ltextfield (); labeltext. color = "#006400"; labeltext. font = "Hg row entity"; labeltext. size = 14; labeltext. X = 50; labeltext. y = 700; labeltext. TEXT = "http://www.lufylegend.com/lufylegend"; self. addchild (labeltext); self. addeventlistener (lmouseevent. mouse_up, menushow );};
This time I used an image as the interface, and the code was simpler. The text display is still an ltextfield object. For how to use it, see the official API documentation.
5. Create a selection screen
As follows.
Figure 5
The Code is as follows.
Function gamemenu () {base (this, lsprite, []); var self = This; var menulayer; menulayer = new lsprite (); bitmap = new lbitmap (New lbitmapdata (imglist ["menu_back"]); bitmap. scalex = bitmap. scaley = 2; menulayer. addchild (Bitmap); self. addchild (menulayer); labeltext = new ltextfield (); labeltext. color = "# b22222"; labeltext. font = "Hg row entity"; labeltext. size = 40; labeltext. X = 30; labeltext. y = 700; labeltext. stroke = T Rue; labeltext. linewidth = 4; labeltext. Text = "Please Select !! "; Menulayer. addchild (labeltext); For (VAR I = 0; I <stagemenu. length; I ++) {self. stagevsmenu (stagemenu [I]) ;}}; gamemenu. prototype. stagevsmenu = function (OBJ) {var self = This; var menubutton = new lsprite (); var bitmap = new lbitmap (New lbitmapdata (imglist ["menu_stage"]); menubutton. addchild (Bitmap); menubutton. X = obj. x * 220 + 30; menubutton. y = obj. y * 200 + 50; self. addchild (menubutton); If (obj. open) {labeltex T = new ltextfield (); labeltext. color = "# ffffff"; labeltext. font = "Hg row entity"; labeltext. size = 20; labeltext. X = 50; labeltext. y = 90; menubutton. addchild (labeltext) labeltext. TEXT = "nth" + (obj. index + 1) + "off"; labeltext = new ltextfield (); labeltext. color = "# ffffff"; labeltext. font = "Hg row entity"; labeltext. size = 12; labeltext. X = 30; labeltext. y = 30; menubutton. addchild (labeltext) labeltext. TEXT = "Times:" + obj. times; m Enubutton. OBJ = OBJ; menubutton. addeventlistener (lmouseevent. mouse_up, function (event, self) {gamestart (self. OBJ. index) ;}) ;}else {labeltext = new ltextfield (); labeltext. color = "# ffffff"; labeltext. font = "Hg row entity"; labeltext. size = 20; labeltext. X = 60; labeltext. y = 40; menubutton. addchild (labeltext) labeltext. TEXT = "??? ";};}
All right, the basic game code has been posted.
Source code
The complete game source code is provided below. If you want to study it, you can click the link below to download it.
Http://lufylegend.com/lufylegend_download/sudoku.rar
Note: This attachment only contains the source code of this Article. For the lufylegend. js engine, go to http://lufylegend.com/lufylegendto download it.
Reprinted Please note: transferred from lufy_legend's blog
Continue to follow my blog
Http://blog.csdn.net/lufy_legend