Android Sudoku game development, logical comb

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Utzw0p0985/article/details/93679295

Read the article about six minutes


Hi Hello everyone, welcome attention to my planet [Hi Android]

 

It benpian bring Android is a little game: Sudoku, although a small game, but also to count the old Liu dizzy, in fact, Android, there is a small part of the game is simple little can be done through a custom View , so today we have to implement it, give everyone a look at the preview: 

      640?wx_fmt=png      

 The total from the preview, we can see some of the following functions:

 

1. intro, after a delay of 1.5s into the Home

There are rules of the game above 2. Click the Home button to jump to the rules of the game

There is a set of above 3. Click the Home button to jump to game settings

4. The game has a timer top

The central game board is a custom, and the number is colorful

6. The game is below the 1-9 numeric buttons

7. There is provided a colorful text prompts and intelligent switch

 

Some general features, of course, you can also add some other features here because the project more complicated, so I will try to explain along the core ideas, other small UI what you own to deal with it, the end may be interested in articles Download Source.

 

Well, we started the text.


A. Drawing Style Box

In fact, drawing squares and small squares is relatively simple, I created a GameView, because it is squared, so if they had been 9, then we can View the aspect are obtained by dividing 9 aspect is that every box the aspect of the

       640?wx_fmt=pngAfter calculating the size of the box in onSizeChanged, draw squares becomes extremely simple, in GameView of onDraw, we can draw a line through the cross drawLine

640?wx_fmt=jpeg      

You can see, this MAX_VALUES = 9, we traverse the call canvasLine draw horizontal and vertical lines through the loop, so to achieve the squares


       640?wx_fmt=png  

    

for循环中的3是为了加深小九宫格的颜色,调用之后的效果如下:


       640?wx_fmt=png      


这样,我们的一个基础棋盘就完成了,那么我们来看下canvasLine里到底做了什么

       640?wx_fmt=jpeg      

正常的写法只要第一个drawLine和第三个即可,我这里在高宽上都+1是为了有一个视觉的加深效果,到这里九宫格就完成了,那么接下来,我们要做的事情是什么呢?那就是绘制绘制数字了


二.绘制数字

绘制数字和绘制九宫格的写法是基本类似的,只是坐标比较难计算,我们来看下绘制数字的代码

       640?wx_fmt=jpeg      

在这段代码中,我们需要关心mGame这个对象和canvasNumberText这个函数

 

Game对象就是我们的数独逻辑计算类,我们所有关于数独计算都会在里面实现,先来看下canvasNumberText函数


       640?wx_fmt=jpeg      

这个函数有必要仔细的说明一下,首先是这个画笔的文本大小,这里我设置了方块高度的0.75f,再结合文字绘制居中效果,那么视觉会舒服很多,各位也可以对此稍微进行修改,然后是FontMetrics,这是绘制文本测量类,关于FontMetrics我建议大家去查阅一下相关的资料,实际上只是对字符以及基准线的一个绘测,我们就是通过基准线的界定来实现绘制文本几种的,并且这里有一个sp的保存,是为了之后设置中是否使用多彩文字,多彩文字的实现很简单,主要是在canvasNumberColor这个函数中,你定义九个颜色就好了


       640?wx_fmt=jpeg      

我们可以先运行一下看看效果

 

       640?wx_fmt=png      

当然,我们还有Game类没有讲


三.Game逻辑类

Game类我要仔细的讲解,首先我定义了一个字符

 

//默认显示

private final String mDefData = "3600000000004230800000004200070460003820000014500013020001900000007048300000000045";

 

这个字符一共81个字节,对应的就是九宫格的81个小方块,其中0为未输入,一般的数独游戏都是按照规则随机的,但是我并不是很懂这个规则,所以只能写死了,接着我再定义一个二维数组来建立九宫格查询的索引列表

 

//装载数独

private int sudo[] = new int[9 * 9];

 

所以我们就可以实现如下的三个方法

 

      640?wx_fmt=jpeg      

 这三个方法,首先是parsingStringChat,就是解析默认字符串到我定义的sudo索引数组中,这样我们就可以通过getCardNumber来索引到对应的字符了,这里的算法是 纵列 * 9 + 横列,打个比方,第三行的第六个,那么就是3 * 9 + 6 得出来的下标再去sudo中索引,就可以拿到对应的数字了,而getCardString这个方法只是为了方便文字的绘制,而去将0转换成空的一个方法,到了这里,基本的UI就完成了,我们接下来就要实现它的点击逻辑了


四.点击逻辑

我们的点击逻辑思路很简答, 你点击后这个方块红色包裹,然后输入数字,即可,这里掺杂了一个只能提示的功能,所以变得复杂起来了,我们一步步来,首先,来看下这个触摸事件的代码


       640?wx_fmt=jpeg这段代码比较长,我们一个个来解释一下,首先是过滤事件,如果不是ACTION_DOWN,那么就按照系统事件下发就好了,如果是ACTION_DOWN,那么通过getXY再除以方块的高宽,就可以得到选择的方块的横纵位置了,然后是获取当前方块上的数字,这里我没有用到,继续往下看,绘制一个红色边框,因为有了他的X,Y,所以绘制起来比较简单

 

       640?wx_fmt=png      

 

这里是否智能提示,如果打开了智能提示,那么我会将这个方块的已显示的数字数组通过接口传递出去,在主页面做逻辑处理,那么怎么计算已显示的数字数组呢?,这里还需要用到我们的Game对象

 

我定义了一个三维数组

 

//存储每个单元格已经不可用数据 x y data

private int userNo[][][] = new int[9][9][];

 

通过他,我可以传递x,y后获取到他所显示过的数组集合,也就是Game中的parsingAllUserNoNumber函数

       640?wx_fmt=jpeg      

 parsingAllUserNoNumber就是遍历一遍所有的方块,最终的实现还是在parsingUserNoNumber中,我们来看下逻辑,首先定义一个长度为9的数组z,然后通过遍历横纵获取已显示的数字存储在z中,并且我们还需要计算小九宫格也不能有重复的,最终将0去除,得到的就是我们所传递方块的x,y的已显示数组,那么我们智能提示,就需要将这些给去除

 

我们在主页定义了九个按钮,这里我为了方便管理,直接定义了一个数组

 

private Button mView[] = new Button[9];

 

然后进行初始化

      640?wx_fmt=jpeg      

 这里初始化后设置点击事件,我只要点击某一个按钮,就将对应的数字通过setNumberText传递给GameView绘制,并且设置了一遍全部显示,因为有可能因为智能提示导致有些按钮不显示

 

我们回到GameView中关于setNumberText函数

       640?wx_fmt=jpeg      

 

In fact, here is the final call Game object refreshSudo, we do not need to go directly to draw text, after only need to sudo array corresponding to the coordinates I just defined characters can modify the refresh View, there's a check is the end game content every time we enter the text will be checked again whether the corresponding logic game on, then the game has ended, but the logic is more complex, yet I did not go to realize, we understand the idea of ​​buttons, continue to look refreshSudo

       640?wx_fmt=jpeg      

Here is a look at sudo update and recalculate display an array of numbers

 

Our basic logic here is complete, in fact, the final core content is that GameView and Game, which GameView still relatively simple, but the logic of transit, we look at all of the Game Code;


Five .Game

       640?wx_fmt=jpeg      

 Mainly logic and thinking, for circulation too much, Naogua Zi buzzing call it, we look at the final preview it Gif

 

       640?wx_fmt=gif      

 

Well, we are interested can join my knowledge Planet Hi Android [click to read] or adding text

 

Android Developer exchange group: 417 046 685 now join unlimited, pay close attention oh

 

Download the source code can read the original text in the planet or join the QQ group

 

Our next goodbye!

 



Guess you like

Origin blog.csdn.net/Utzw0p0985/article/details/93679295