Tile Generation: manipulating strings with child figures. - acdop100/Matlab-2048 GitHub Wiki

Alright this will move a little faster than the previous sections as there isn't a whole lot that is new.

First you'll see the function header. I have a ~ in the event slot because I don't use it in this function. Honestly I can't remember why I would have it as an input anyway except that I think Matlab requires you to input both the figure and the event or don't input them at all.

Next is another if statement. There are 2 cases in which this function would be run:

  • At the start of the game to determine the two tiles that are placed and what number they're assigned.
  • After each move to generate a new tile and which number it is assigned.

I will go over the first option mainly since the second option is essentially the same.

  • First, you will see the if statement checks to see if the array we declared at the beginning of this guide is empty (it will be since we initialized an empty array). This will show that it is a new game.
  • Assuming the array is empty, the function will then initialize a 4x4 array of all zeroes.
  • Next I created an 2x1 array with the numbers 2 and 4. Then the program chooses randomly between the two numbers twice to decide the two values of the boxes.
  • Next, I put a 16x1 array with values ranging from 1-16. The program then chooses a random spot in the game's array that has a value of 0 (which is all of them, currently). Then it sorts out which spots have 0 within the 16x1 array we made to get all "open" spots to assign. It also then samples out two available spots when we generate two boxes for the game start.
  • Now we get all current children of the figure again.
  • Next we assign the values of the chosen numbers to the chosen boxes in the game array.
  • The function then injects the array into the child figure "KeyReleaseFcn" at spot 2.
  • Next, since the child values are all +1 from the values in our place array, I just made it add +1 to all values in the place variable.
  • This is where the function updates the figure with new text. We call the "childs" var at the box the function chose and select the string attribute, then assign the number(s). In my case, this looks like "childs(childplace(1)).String = num(1);"

Next the function updates the background color of the different boxes. Each number has a different color assigned to it.

  • I use a switch here to explicitly define the 3 options to use
  • To change the colors, call the children and select the BackgroundColor attribute. It looks like "childs(x).BackgroundColor"
  • If you notice, all of the selectors for children are just attributes that can be assigned in uicontrol creation.

Congrats! You've reached the end of this guide. I won't go over the actual game logic file because 1: its like over 300 lines or something and 2: It won't cover anything new.

I hope this guide has been useful. Please don't just copy my code! I don't need to get in trouble with GT for ethics violations.