Android game development: My little game 1-Game 5-win

Source: Internet
Author: User
Tags drawtext

Here, to determine whether to win or lose is to judge whether there are five pieces of the same color in four directions: horizontal, vertical, left-oblique, and right-oblique. The idea is to scan the two-dimensional array, check whether there are consecutive chess pieces of the same color in this direction. If it is determined that one party wins, it should be prohibited to continue playing chess. At this time, the screen should be locked, make the program do not respond to touch events.

// Determine whether a pawn wins or loses private Boolean checkwin (INT chessflag) {for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {// determine the vertical if (I + 4 <grid_num & chess [I] [J] = chessflag & chess [I + 1] [J] = chessflag && chess [I + 2] [J] = chessflag & chess [I + 3] [J] = chessflag & chess [I + 4] [J] = chessflag) return true;
// Determine the horizontal if (J + 4 <grid_num & chess [I] [J] = chessflag & chess [I] [J + 1] = chessflag & chess [I] [J + 2] = chessflag & chess [I] [J + 3] = chessflag & chess [I] [J + 4] = chessflag) return true;
// Determine the left oblique if (I + 4 <grid_num & J + 4 <grid_num & chess [I] [J] = chessflag & chess [I + 1] [J + 1] = chessflag & chess [I + 2] [J + 2] = chessflag & chess [I + 3] [J + 3] = chessflag & & chess [I + 4] [J + 4] = chessflag) return true;
// Determine the right oblique if (I-4> 0 & J + 4 <grid_num & chess [I] [J] = chessflag & chess [I-1] [J + 1] = chessflag & chess [I-2] [J + 2] = chessflag & chess [I-3] [J + 3] = chessflag & chess [I-4] [J + 4] = chessflag) return true ;}} return false ;}// determine if the account is full, that is, when a draw occurs, private Boolean checkfull () {for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {If (chess [I] [J] = 0) return false ;}} return true ;}

 

Then you can set a flag to determine whether the player is playing chess or has won. If the player wins, you do not need to re-paint the screen each time you touch the screen, saving resources, try not to make judgment in the drawing, just block the invalidate () function.

The complete code is pasted as follows:

Public class gameview extends view {// screen width and height private int screenwidth = 0; private int screenheight = 0; // draw the start position of the Board private int startx = 0; private int starty = 0; // the height and width of each grid in the chessboard private int grid_width = 40; private int grid_num = 12; // number of lines in the board to be painted private paint = NULL; // represents the two-dimensional array of the pawns, each element in the array represents a private int [] [] chess = new int [grid_num] [grid_num]; private int chess_black = 1; // indicates the color of the chess piece. 1 indicates black, 2 indicates white, and 0 indicates not reaching the standard. Chess Piece private int chess_white = 2; private int chess_flag = 0; // used to record the color of the last chess piece. The value 1 indicates black, the value 2 indicates white, and the value 0 indicates playing chess at the beginning, last time no pawn private Boolean winflag = false; // judge the winning or losing Sign public gameview (context) {super (context); this. setfocusable (true); // gets the focus to perceive the touch screen event paint = new paint (); // instantiate a paint brush. setantialias (true); // sets the paint brush to be deprecated. Without this statement, the line drawn or the image is not smooth.} @ overrideprotected void ondraw (canvas) {// override this method in the view. This method is mainly used for drawing. Every time it is refreshed, the su method is called once. Per. ondraw (canvas); screenwidth = This. getwidth (); screenheight = This. getheight (); startx = (screenwidth-grid_width * (GRID_NUM-1)/2; starty = (screenheight-grid_width * (GRID_NUM-1)/2; system. out. println (". screenwidth .. "+ screenwidth + "... startx .. "+ startx); canvas. drawcolor (0xffffd700); // draws the background color of the screen in yellow. It is not only used to paint the screen into this color, but also to paint the screen. setcolor (0xff458b00); // here, the paint brush turns green, and the canvas turns green for (INT I = 0; I <GRI D_num; I ++) {// draw a canvas. drawline (startx, starty + I * grid_width, startx + (GRID_NUM-1) * grid_width, starty + I * grid_width, paint); // draw a canvas. drawline (startx + I * grid_width, starty, startx + I * grid_width, starty + (GRID_NUM-1) * grid_width, paint);} // draw the pawn for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {// define the position where the winning text is drawn: int textx = screenwidth/2-50; int texty = screenheight-60; If (chess [I] [J] = chess_black) {Pai NT. setcolor (0xff000000); // a black paint brush that draws a Black canvas. drawcircle (startx + I * grid_width, starty + J * grid_width, 15, paint); If (checkwin (chess_black) {paint. setcolor (0xffcd00cd); // The paint color is paint. settextsize (16); // you can specify the font size canvas. drawtext (",! ", Textx, texty, paint); winflag = true ;}} if (chess [I] [J] = chess_white) {paint. setcolor (0 xffffffff); // a white paint brush that draws a white canvas. drawcircle (startx + I * grid_width, starty + J * grid_width, 15, paint); If (checkwin (chess_white) {paint. setcolor (0xffcd00cd); paint. settextsize (16); canvas. drawtext ("Bai fangsheng, black fangyu! ", Textx, texty, paint); winflag = true ;}}}// override the method for listening to touch events of view @ overridepublic Boolean ontouchevent (motionevent event) {float touchx = event. getx (); float touchy = event. gety (); If (touchx <startx | touchx> startx + (GRID_NUM-1) * grid_width | touchy <starty | touchy> starty + (GRID_NUM-1) * grid_width) {// click the system outside the chessboard. out. println ("...... BUDDY: It's incorrect, huh, ");} else {// Based on the clicked position, which is the position on the board, that is, the int index_x = math. Round (touchx-startx)/grid_width); int index_y = math. round (touchy-starty)/grid_width); system. out. println ("... "+ index_x + "... "+ index_y); system. out. println ("... startx "+ startx + "... touchx "+ touchx); If (chess_flag = 0) {// This sentence indicates that at the beginning of the game, chess [index_x] [index_y] = chess_black; chess_flag = chess_black;} else if (chess_flag = chess_black & chess [index_x] [index_y] = 0) {chess [index_x] [index_y] = chess _ White; chess_flag = chess_white;} else if (chess_flag = chess_white & chess [index_x] [index_y] = 0) {chess [index_x] [index_y] = chess_black; chess_flag = chess_black;} If (! Winflag | checkfull () // refresh {invalidate () when there is no fl or wins; // after clicking finish, the ondraw method is re-executed after the notification is re-painted.} return Super. ontouchevent (event);} // determines whether a pawn wins or loses. Private Boolean checkwin (INT chessflag) {for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {if (I + 4 <grid_num & chess [I] [J] = chessflag & chess [I + 1] [J] = chessflag & chess [I + 2] [J] = chessflag & chess [I + 3] [J] = chessflag & chess [I + 4] [J] = chessflag) return true; if (J + 4 <grid_num & chess [I] [J] = chessflag & chess [I] [J + 1] = chessflag & chess [I] [J + 2] = chessflag & chess [I] [J + 3] = chessflag & chess [I] [J + 4] = chessflag) return true; if (I + 4 <grid_num & J + 4 <grid_num & chess [I] [J] = chessflag & chess [I + 1] [J + 1] = = chessflag & chess [I + 2] [J + 2] = chessflag & chess [I + 3] [J + 3] = chessflag & chess [I + 4] [J + 4] = chessflag) return true; if (I-4> 0 & J + 4 <grid_num & chess [I] [J] = chessflag & chess [I-1] [J + 1] = chessflag && chess [I-2] [J + 2] = chessflag & chess [I-3] [J + 3] = chessflag & chess [I-4] [J + 4] = chessflag) return true ;}} return false ;}// determine if the account is full, that is, when a draw occurs, private Boolean checkfull () {for (INT I = 0; I <grid_num; I ++) {for (Int J = 0; j <grid_num; j ++) {If (chess [I] [J] = 0) return false ;}} return true ;}}

Let's take a look at how each scenario wins:

 

Related Article

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.