Desktop Cottage version 2048-the frame of the moving block of the game Logic Chapter

Source: Internet
Author: User

Yesterday saw the blog (www.richinmemory.com) traffic statistics, incredibly still have a friend commented, touched the eyeful are tears Ah! Thank you for your support! In order to make interactive friends more convenient interaction, today I added a can use micro-blog account login comments plug-ins. Need source of friends can send a letter directly to my mailbox. After the poke (www.richinmemory.com) feel OK, you can try to collect Ah, pro. One day the lucky flow stability, I began to give up this side of the update, but this must be a long time to reach.

Second, desktop Cottage version 2048-game logic to move the block

The basic logic of this little game is: merge similar terms. The player controls the game block by the next key, then controls all adjacent squares with the same number in the direction, and the number on the block will be updated and the color will be changed.

For logical reasoning, I have been deeply convinced that the "from 1 to 2 to infinity" way, this is my high school teacher taught me the way, for any of the rules but complex matters, first from the simple beginning is always not wrong. So, first of all consider only two blocks in one Direction to operate (I chose "down" this direction). According to the principle in the interface chapter, all the game blocks are saved in an array of Itembox and initialized at the beginning of the program, so the logic to be dealt with now is how to control the display of squares in each square.

If there are only two game blocks, the user presses the "down" arrow key, there are two things to consider: First, two game blocks are not in one column, and the other is two game squares in one column. The first thing to think about is that the behavior pattern should be: Two game blocks should move down until they hit the border of the game area. This logic is not difficult to implement, the simplest way, take a loop, traverse all the squares, if the current grid Bshow property is False, then do not operate, otherwise, get the current grid position information (coordinates), text, color. The last line of the current column is given the same text and color, and the information of the game box is emptied (The block color is set to the background color, the text is emptied), and the interface is refreshed, which can cause the current game square to "move" the illusion of the last line. When One Direction is successful, the other three directions are as good as the other. Although this logic and the final logic is still 108,000 miles apart, even for the entire game logic is even incorrect. But if you do not learn to add, then you will not learn triple points it.

Forget the above piece of crap, now to consider the situation two, if two blocks in a column, it is necessary to consider the merging situation (first assume that the text of two blocks is the same). At this point press the arrow keys, the correct expected result is two blocks merged and in the last row of the forefront, there are too many ways of thinking. My most starting first brain idea is from the (0,0) position of the block began to traverse, if the bshow is false, do not do anything, if not, first check the current column the last row of the block bshow properties, if False, and the above situation, Sets the last line of the current column to the same block and clears the current block information. Otherwise, you need to merge, as long as you raise the last row of the current column by one level (such as "2->4") and empty the current block information. So, the movement and merging of two blocks has been done. You can follow this idea first to give a code to try.

Now that the "1" situation has been considered, the following to begin further, consider the "2" situation. Once the two initial blocks have been merged, it is time to think about how to create a new block. There are several things that need to be considered here as well.

First, the first occurrence of the two blocks has been merged, the new block appears and the merged block is not on a column (or just consider a situation in one direction). This is the simplest, that is, two blocks not in a column, it has been said above.

Second, the same two blocks that were originally created have been merged, and the new block is in the same column as the old one. At this point the new block and the merged block text is not the same, it is impossible to merge (temporarily start from the simplest case). So then the idea is an extension above, but also from the upper left corner of the Traverse, in order to determine whether each square bshow is true, if so, then this move is a bit different, because the last line is known to have been merged blocks, So you know that the current block should appear on the penultimate line. But what if we don't know what the last line of the current column, bshow, is false? How to use the program to find it? Since we know the ordinate horizontal axis of the current position, we start from the last line, then walk up, and if we encounter bshow as false, we immediately exit the loop and record the current line coordinates. This will accomplish the task described above, with this coordinate to know where the current game block should be placed, the rest of the operation is consistent with the previous said.

Third, the original two blocks are not merged, so this is certainly the same as one of the two blocks above.

Now that we have dealt with the "2" situation, can this "2" situation be extended to infinity? If a new block appears at this point, can the situation be directly applied to the above ideas? If you think about it, it may not be possible, because the "infinity" situation is too much. I summed up, with a diagram, that although it is not possible to present these four columns at the same time in the actual situation, it is possible to have four columns alone:

The first and fourth columns are the simplest, and you can move the merge directly, as described in the previous steps.

Second column, you need to determine the same column of the next line of text and the current text is not the same, can only move cannot occur merge.

The third column, where two can be merged, column 1 and column 2 "2" can be merged, column 3 and column 4 "4" can be combined, and after merging, two "2" merged "4" to be displayed in the third row, "4" merged "8" to appear in line fourth.

After analyzing the brain's thinking, it is now time to switch to a computer thinking about how to implement it in a program. According to the previous thinking mode, from the top left corner of the first game square to traverse, if the current row of bshow is true, get the current Game box column, starting from the last row of the current column, in turn, to determine whether the current bshow is false, if so, record the current row ordinal and return. After getting this return line ordinal, determine whether the next line of the line ordinal (of course the same column) and the current line of the game box text is consistent (a bit around, meaning you understand), if consistent, then consider merging, and reset the information.

Considering here, it seems that logic has been repeated, I was thinking anyway. So the first time I wrote the code is this,getcoordsfromindex (i,it) is a function I wrote, the purpose is to get the game box according to the current sequence number of the coordinates and horizontal axis, the use of division and take the remainder of the operation is easy to do:

    if(NChar = =Vk_down) {         for(intI=0; i<ntotalgrids-1; i++)        {            if(m_itemboxarray[i].bshow) {getcoordsfromindex (i,it);  for(intJ=ntotalgrids-(m_nrowandcol-it.ncolindex); j>i;j-=M_nrowandcol) {                    //find the maximum line ordinal of the current column bshow to False                    if(!m_itemboxarray[j].bshow) {M_itemboxarray[i].bshow=false; M_itemboxarray[j].bshow=true; M_itemboxarray[j].stritemtext=M_itemboxarray[i].stritemtext; M_itemboxarray[i].stritemtext= _t ("");                        Getcoordsfromindex (J,it);  Break; }                }                //Merging                if(It.nrowindex < M_nrowandcol-1) {Itembox itcurrent=m_itemboxarray[getindexfromcoords (IT)]; ++It.nrowindex; Itembox Itnextrow=m_itemboxarray[getindexfromcoords (IT)]; if(Itnextrow.bshow &&Itnextrow.stritemtext==itcurrent.stritemtext) {M_ITEMBOXARRAY[GETINDEXFR Omcoords (IT)].stritemtext= M_arrstritemtexts[getindex (Itnextrow.stritemtext) +1]; --It.nrowindex; M_itemboxarray[getindexfromcoords (IT)].bshow=false; M_itemboxarray[getindexfromcoords (IT)].stritemtext= _t (""); }                }            }        }    }

But a test found that the situation did not go in the direction I imagined. I observed a phenomenon that, according to this idea, the behavior of column 3 is completely incorrect. Then I went back to this code, after all, go to this step, from the point of view of the code easier to identify the problem, which does not violate the principles of mathematical induction. If you follow this code, column 3 is the case, because we are traversing from the upper left corner, then the first row of 2 and the second row of 2 merged into the second row of 4, the traversal continues, when traversing to the third row of 4 o'clock, decided to merge with the fourth row of 4, which formed the fourth row 8, At this point, however, 4 of the second row should be moved to the third row. However, according to our code, we have traversed the second line, it is impossible to go back, so caused in the error, will result in the merger of 4 and merge 8 respectively in the second row and the fourth row, the third line is empty, which is obviously incorrect. How to solve this problem? Through the analysis can be found that the problem is because of our traversal sequence is problematic, then it is simple, in the downward direction, as long as the last game from the square to the first square to traverse the problem is solved, the specific logic comrades can think again.

After this, with a complacent mood to compile again, try logic is correct, the result is still too naive, the problem arises, if the same column appears 4 "2", then click "Down" Direction key, these 4 "2" will be directly merged into a "8", according to the original game rules, which is not right, After all, we are the cottage, the cottage must be a little sincerity, can not tamper with the original rules of the game. How can this problem be solved? First of all to find out the cause of this problem, careful thinking is not difficult to find that the original game rules is an operation, the merged game Block can not be merged again, then, this problem is easy to solve, using another member variable in the package Bjoin, identify the merged block. So the code for the final "down" direction looks like this:

    if(NChar = =Vk_down) {         for(inti=ntotalgrids-1; i>=0; i--)//traverse forward from backward        {            if(m_itemboxarray[i].bshow) {getcoordsfromindex (i,it); //Maximum row ordinal that queries the same column for bshow to False                 for(intJ=ntotalgrids-(m_nrowandcol-it.ncolindex); j>i;j-=M_nrowandcol) {                    if(!m_itemboxarray[j].bshow) {M_itemboxarray[i].bshow=false; M_itemboxarray[j].bshow=true; M_itemboxarray[j].stritemtext=M_itemboxarray[i].stritemtext; M_itemboxarray[i].stritemtext= _t ("");                        Getcoordsfromindex (J,it);  Break; }                }                //Merging                if(It.nrowindex < M_nrowandcol-1) {Itembox itcurrent=m_itemboxarray[getindexfromcoords (IT)]; ++It.nrowindex; Itembox Itnextrow=m_itemboxarray[getindexfromcoords (IT)]; if(Itnextrow.bshow &&Itnextrow.stritemtext= = Itcurrent.stritemtext &&!itnextrow.bjoin//determine if the current block has been merged or not) {m_itemboxarray[getindexfromcoords (IT)].stritemte XT= M_arrstritemtexts[getindex (Itnextrow.stritemtext) +1]; M_nscore+=2*pow (2.0, GetIndex (Itnextrow.stritemtext) +1 ); M_itemboxarray[getindexfromcoords (IT)].bjoin=true;//set the current block has been merged--It.nrowindex; M_itemboxarray[getindexfromcoords (IT)].bshow=false; M_itemboxarray[getindexfromcoords (IT)].stritemtext= _t (""); }                }            }        }    }

The remaining three directions of the code in accordance with the "next" direction, but to pay attention to the order of traversal, specific code can enter the blog, where there is my mailbox, if you need source code, you can leave a message or send me. Here, for a complete game, logic can only say that the completion of a framework, not to become a qualified cottage, so please look at the next "logical surface of the slowly appearing details like the wind leaf."

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.