Game algorithm implementation in "Gemma Maze"

Source: Internet
Author: User

[Qboy] original in March 10, 2013

Recently, I have an idea that I have written everything I have done before in the form of a blog. Whether it is right or not, let the experts give you some advice. I originally wanted to write "Water Pipe cat" first, and then I thought about writing "Gem Maze" first.

I. Game background

Anyone who has played the treasure metal game knows that it is actually a very simple game. By exchanging two adjacent elements (or a gem, the following elements are unified), we can remove three or more identical elements in a row or column from the maze, the elimination element is added, which is the core gameplay of gemdale games. All the gameplay of this type of games is changed above, such as adding items to remove a line and a line, removing the same type of elements, etc. This depends on how various game planners plan the game.

It is a simple gem maze, which uses numbers to represent different elements.

II. In the gameAlgorithm

I am not a game planner. I will not talk about the game perimeter here.

The entire game process should be shown in:

The core of the entire game is under several judgments. Next, I will describe these judgment methods: 1. Whether elements can be eliminated In the game map (represented by a two-dimensional array), traverse each row and each column to see if there are more than three adjacent elements. In order to avoid repetition, we recommend that you only judge right and down.CodeAs follows: -(Nsmutablearray *) getwinstoneinmap { Nsmutablearray * arr = [nsmutablearray array]; For (INT I = 0; I For (Int J = 0; j <wcount; j ++ ){ Stonesprite * curstone = stonearr [I] [J]; Stonetype curtype = curstone. stonetype; // Right Int K = J + 1; If (k <wcount ){ For (; k <wcount; k ++ ){ If (curtype! = Stonearr [I] [K]. stonetype ){ Break; } } If (k-j> = 3) {// whether the same element is greater than 3 For (int m = J; m <K; m ++ ){ If (! [Arr containsobject: stonearr [I] [m]) { [Arr addobject: stonearr [I] [m]; } } } } // Downward K = I + 1; If (k { For (; k If (curtype! = Stonearr [k] [J]. stonetype ){ Break; } } If (k-I> = 3 ){ For (INT n = I; n <K; n ++ ){ If (! [Arr containsobject: stonearr [N] [J]) { [Arr addobject: stonearr [N] [J]; } } } } } } Return arr; }   2. Determine whether there are movable elements   In this method, the above method was called for simplicity. First, exchange Adjacent Elements and call the above method. If yes, return true. If no, continue. -(Bool) checkhasmove { Stonesprite * temp; Bool hasmove = false; For (INT I = 0; I For (Int J = 0; j <wcount &&! Hasmove; j ++ ){ If (j <wCount-1) {// exchange with the right side Temp = stonearr [I] [J]; Stonearr [I] [J] = stonearr [I] [J + 1]; Stonearr [I] [J + 1] = temp; Nsmutablearray * arr = [self getwinstoneinmap]; Temp = stonearr [I] [J]; Stonearr [I] [J] = stonearr [I] [J + 1]; Stonearr [I] [J + 1] = temp; If ([arr count]> 0 ){ Hasmove = yes; Break; } } If (I Temp = stonearr [I] [J]; Stonearr [I] [J] = stonearr [I + 1] [J]; Stonearr [I + 1] [J] = temp; Nsmutablearray * arr = [self getwinstoneinmap]; Temp = stonearr [I] [J]; Stonearr [I] [J] = stonearr [I + 1] [J]; Stonearr [I + 1] [J] = temp; If ([arr count]> 0 ){ Hasmove = yes; Break; } } } } Return hasmove; } 3. Imperfections   Throughout the game, the map of the game is traversed in many places. However, this is not necessary in some cases. For example, after the exchange, we only need to judge the rows and columns where the exchange elements are located. This will greatly improve the game's traversal efficiency.
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.