Android third party

Yesterday we drew the chess pieces

Also completed a simple interaction

Today we will make a logical judgment

That is to judge whether they are in a row

 

image

    Since it is necessary to judge whether it is connected into five sons, there must be a boolean variable to indicate whether the game is over. And besides that, we have to judge whether white wins or black wins, so there are two Boolean variables in total:

image

Then to check whether the game is over, we write a function to determine whether black or white wins. We write a function. You may wonder why you can’t just use one function, that is to say, replace the check game with the function of black or white victory. Is the function ending? In fact, it is possible, but if you do this, the cleanliness and readability of the code will be reduced. We still hope that a function has its own specific function, not too complicated. OK, so how do we write the function to check the end of the game?

    We can naturally think that there must be an if to judge whether black wins or white wins, and then write some corresponding response codes, such as the words "white wins".

    Let's take a look at the code:

image

  • The first point: The checkFiveInLine() function is used later to judge whether the connection is a five-piece, because it also returns a boolean value, so we assign the result to the local variables whiteWin and blackWin as the later if judgment condition.

  • The second point: our if judgment is very simple, so I won’t repeat it;

  • The third point: If black chess wins or white chess wins, we assign the global variable isGameOver to true, indicating that the game is over. At the same time, we should also record whether black wins or white wins. Here, we use the connection of white to judge;

  • The first line of the fourth point clearly shows: if the white pieces are connected together, then mWhiteIsWinner is true, and we assign "white chess victory" to the string text, otherwise it is "black chess victory". This statement is a simple way of writing if.

    There is a knowledge point we need to know: Toast

    1) Toast is a View view that quickly displays a small amount of information to the user. Toast displays information floating on the application to the user. It will never get the focus and does not affect the user's input and other operations. It is mainly used for some help/tips.

    2) The most common way to create Toast is to use the static method Toast.makeText;

    Compared with the above code, the first parameter in parentheses is the current context, usually getContext() or this; the second parameter is the text or other information to be displayed; the third parameter is the length of the display time, there is The two default ones are LENGTH_LONG and LENGTH_SHORT, which can also be expressed in milliseconds.

  • 3) We can also set the position where toast appears:

    image

  • 4) Toast with pictures:

    image

We don't use this example now, but it's quite interesting. You can learn it and play with it later.

By the way, the point that should be noted above is that no matter what operation is done to toast, the show() method must be called to show the effect at the end.

Ok, now our checkGameOver function has been completed, then we should implement the following checkFiveInLine function:

image

Anyone who has played backgammon knows that there are four ways to connect five pieces, horizontally and vertically, left and right, so we used four functions above to make various judgments. I’ve circled the first method of writing that I’ve never seen before, and this is what we need to learn. Then there may be a little baby here who wants to ask, how do you judge that the five sons are in a line? Look at this code just passing in the horizontal and vertical coordinates of a point. Yes, we start from the abscissa and ordinate of this point, and check if there are consecutive pieces on the left side of it? there's a few? Can it be 5? If the left side is not completed into 5 pieces, check the right side. If the two add up and there is still no 5 pieces connected, then it is stable, there is no 5 pieces in the horizontal direction; down to see if there are 5 pieces in the vertical direction, the judgment method is the same as the horizontal one. . Left oblique and right oblique are the same:

image

Here we define a MAX_COUNT_IN_LINE to represent the maximum number of a line. The above code is still very easy to understand. What we need to learn is

imageOf course, the premise of this writing method is to know its meaning. Remember what we said before, each point is stored in a List, black chess has a list of black chess, and white chess has a list of white chess:

 

image

So now what we need to check is whether the point (xi, y) has a pawn, and it is the same type of pawn. So we can directly call the contains method in List.

    The above only gives the code for judging the five sons in the horizontal direction. In the vertical direction, the left and right diagonal are the same. Just pay attention to how the coordinate value should change, and make the corresponding change.

    Then we have written the logical judgments, but don’t forget to use them where appropriate:

For example, when we draw a chess piece, if the game has been judged to be over, the user should not place another move effectively. So there must be a judgment in onDraw().

Okay, let's show you the effect:

Video address: https://v.qq.com/x/page/a1322ov2jd3.html

007

 

https://v.qq.com/x/page/j1322b4xlwm.html

 

008


Originality is not easy, please support and pay attention~

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113987840