Cocos2d Simple elimination Game algorithm (i)

Source: Internet
Author: User

1. Video game Demo


2. Tri-elimination Game I understand
the game in the above video, I did 2 weeks, can only be counted as a simple demo, as well as bugs, special effects are almost no. I feel three games mainly by grinding, more grinding more fine. In the market, the three-elimination game has been super. The mainstream is map-type, almost endless mode, a variety of elimination of special effects, a variety of customs clearance, play up or good, is to encounter more difficult levels, to try several times, good luck when the time passed, otherwise card dead. The real extension of this game is the clearance mode, but also need the entire special map editor, with planning, and constantly upgrade the game.
3. Eliminate the simple algorithms involved
3.1 generating Random map algorithms



There are all kinds of maps, here take the simplest rectangle. Requirements:1. The algorithm is to generate a random map that cannot have 3 horizontal or 3 vertical. 2. This map user move one step can be eliminated (not a death map )
At first see this demand feel still very difficult, and then think of the next 2nd needs should forget, if it is dead map, and then regenerate a map on it. Under test, the probability of generating a death graph is very low.
Description of the algorithm implementation:Suppose the map (0,0) is in the upper-left corner. very simple x is generated from the leftmost top to the right, and y from top to bottom. Each time first determine whether its left two is already the same color, there are two above whether the same color, if the same color, to remove this color. Suppose the map that has been generated is:2, 3, 3, 4, 1, 3, 21, 2, 3, 4, 4, 3, 31, 2, 4, 2, 2, X because X left two is 2, so x can no longer be 2, its top two is 3, so x can no longer be 3. So the result of X can only be a random one in 0,1,4.
The following is a pseudo-code (a real code that cannot be run):
Enum matchitemtype{kreditem = 0,//0 kgreenitem,//1 Kblueitem,//2 Kwhiteitem, 3 Korangeitem//4}; Matchitemtype getonerandomtypeexceptparameter (const matchitemtype& type) {Matchitemtype allType[5] = {KRedItem, KG    Reenitem, Kblueitem, Kwhiteitem, Korangeitem}; Std::vector
  
   
 Resttype;        for (int i = 0; i < 5; ++i) {if (Alltype[i]! = type) {Resttype.push_back (alltype[i]);    }} int restsize = Resttype.size ();        int randomindex = rand ()% Restsize; return Resttype[randomindex];} Array2d<matchitemtype> Getonerandommaparray () {array2d<matchitemtype> map = Array2D<MatchItemType    > (7, 7);    BOOL Findthreesameinx = false;        BOOL Findthreesameiny = false; for (int y = 0, y < 7; ++y) {for (int x = 0; x < 7; ++x) {Matchitemtype Randomtype = (matchitemtype                        ) (rand ()% 5); if (x >= 2) {//The left two is the same color if (map. Get (x-1, y) = = map.                Get (x-2, y)) {//need Find a new type Findthreesameinx = true;                }else{Findthreesameinx = false;            }}else{Findthreesameinx = false;        } if (y >= 2) {//top two are the same color        if (map. Get (x, y-1) = = map.                    Get (x, y-2)) {//need find a new type;                Findthreesameiny = true;                }else{Findthreesameiny = false;            }}else{Findthreesameiny = false; } if (Findthreesameinx = = False && Findthreesameiny = = False) {//do Nothing}el Se if (Findthreesameinx = = True && Findthreesameiny = = False) {Randomtype = Getonerandomtypeexceptpa Rameter (map.            Get (x-1, y)); }else if (Findthreesameinx = = False && Findthreesameiny = = True) {Randomtype = Getonerandomtypeexcep Tparameter (map.            Get (x, y-1)); }else{Randomtype = getonerandomtypeexceptparameter (map. Get (x-1, y), map.            Get (x, y-1)); } map.        Set (x, y, Randomtype); }} return map;}
  



3.2 Judging if the map is a dead map
if the entire map, the user moved any step can not be eliminated, is dead map, to regenerate the map.

Case 1/////[x]//////[x]//////////[x][o][x][x][o][x]//////////[x]//////[x]/////////////////////////////////// Case 2////////[x]///////////////////[x][o][x]///////////////////[x]//////////////////////[x]/////////////////// [x] [O] [x]///////////////////[x]//////////////

Here are two simple cases with annotations, note the position of X. Case1 is a two-color case, moving one step can eliminate only 6 possible, 3 on the left, 3 species on the right. The following is the case of vertical with two colors, moving one step can be eliminated is also 6 cases. Above 3 kinds, below 3 kinds. Knowing this, the code is easy. Remember to find one and return directly.

Vector<matchitem*> getthreematchitemcanremovebyonestep (const array2d<matchitem*> & map) {vector<        matchitem*> result;    int MaxX = 7;            int maxy = 7;                for (int y = 0, y < Maxy; ++y) {for (int x = 0; x < MaxX; ++x) {if (x + 1 < MaxX) { Case 1 if (map. Get (x, y)->gettype () = = map. Get (x + 1, y)->gettype ()) {Matchitemtype Currenttype = map.                    Get (x, y)->gettype ();                        Check 6 item, one move one step can combine three same item if (x-2 >= 0) { if (map. Get (x-2, y)->gettype () = = Currenttype) {//find one result.push_ Back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (x-2, y));                        return result;             }                    }       if (x-1 >= 0 && y-1 >= 0) {if (map. Get (x-1, Y-1)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (X-1, y-1));                        return result; }} if (X + 2 < MaxX && y-1 >= 0) {if (map. Get (x + 2, y-1)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (x + 2, y-1));                        return result; }} if (X + 3 < MaxX) {if (map. Get (x + 3, y)->geTtype () = = Currenttype) {//find one result.push_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (x + 3, y));                        return result; }} if (X + 2 < MaxX && y + 1 < Maxy) {if (map. Get (x + 2, y + 1)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (x + 2, y + 1));                        return result; }} if (x-1 >= 0 && y + 1 < Maxy) {if (map.                Get (x-1, y + 1)->gettype () = = Currenttype) {//find One            Result.push_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x + 1, y)); Result.push_back (map.                            Get (x-1, y + 1));                        return result; }}}} if (Y + 1 < Maxy) {Matchitemtype Currentt ype = map.                Get (x, y)->gettype (); Case 2 if (map. Get (x, y)->gettype () = = map. Get (x, y + 1)->gettype ()) {if (y-2 >= 0) {if (map. Get (x, Y-2)->gettype () = = Currenttype) {//find one result.push_ Back (map.                            Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (x, y-2));                        return result;   }} if (x + 1 < maxX && y-1 >= 0) {                     if (map. Get (x + 1, y-1)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (x + 1, y-1));                        return result; }} if (x + 1 < MaxX && y + 2 < Maxy) {if (map. Get (x + 1, y + 2)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (x + 1, y + 2));                        return result; }} if (Y + 3 < Gameglobal::xmapcount) {if (map.          Get (x, y + 3)->gettype () = = Currenttype) {                  Find one result.push_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (x, y + 3));                        return result; }} if (x-1 >= 0 && y + 2 < Maxy) {if (map. Get (x-1, y + 2)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map.                            Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (x-1, y + 2));                        return result; }} if (x-1 >= 0 && y-1 >= 0) {if (map. Get (x-1, y + 1)->gettype () = = Currenttype) {//find one RESULT.P Ush_back (map. Get (x, y)); Result.push_back (map.                            Get (x, y + 1)); Result.push_back (map.                            Get (X-1, y-1));                        return result; }}}}} Retu RN result;}

It seems to be a bit complicated, poor up to 12 cases, the algorithm should be very fast. There is a place to use this algorithm, is in the elimination of the game, the user has not been eliminated for a long time, to give hints. Use this algorithm to find which 3 can be moved one step to eliminate.


Algorithm first to here ... Follow up with time to update ...

http://www.waitingfy.com/archives/1335


Cocos2d Simple elimination Game algorithm (i)

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.