2048 AI

       2048 is a simplistic game with simple enough rules, combine the tiles together until you obtain the 2048 tile. The game ends when the board is filled and there are no more combos that you can do. That being said if you don't start the game with a game plan it can quickly become a game over. After playing the game I realized that creating an AI to play the game would be relatively straight forward. Unlike the miss pac-man AI I had to create for school, there were no enemies to avoid. All you had to do was determine the best possible move out of the four possible moves.


        The hardest part of this whole project was creating the 2048 game itself. To start off task I created a general layout of how the code was going to play out. Because my final goal was to create a game that an AI could play I needed to make sure my code and functions could be integrated into the AI's core functions. I decided that the best way to go about the AI was to do a weighted move system based on the moves that could be made. So the AI would have an int[4] MoveWeight array, then after the moves were calculated whatever move had the highest number would be used. 


        In addition to the weight system if I wanted my bot to have any chance of actually winning I would also need the weight system to be recursive in some way. So while I was creating the core game I kept that fact in my mind, that way I would not have to waste my time refactoring huge parts of the code just to be able to integrate the AI. 

Core Game

        To start off the project I needed to create the game board as well as some way to be able to track the board pieces.  After debating it for a while to try and find the best solution I decided that creating two seperate arrays would be the best way to do this. I created an in[4,4] to track the actual state of the game board and an GameTile[4,4] to track the actual Gameobjects on the screen. I then created a prefab for the Tiles with a custom GameTile script. The script was relativly simple, it has a constructor that takes a Value and position of the tile. 

         The most complex part of it was the image selector. I wanted to be able to swap out the images on the tiles without having to do to much work so I decided to create a script that pulls images outside of the unity project. But in doing so the images were far to large for the 100x100 tiles. So I created a quick function that resized the gameobject scale so that it was always 100x100. Also by scaling down the gameobject instead of the image itself less detail was loss in the process.   

          Once I got the tile images working and to the correct size it I started to work on the sliding aspect of the game. When you play the game you have 4 possible moves, up, down, left and right. When you move all the tiles slide int that direction as far as they can. If a tile collides with another tile with the same value they become one tile with double the value of the two 2+2 => 4. When playing it seems like a fairly simple thing to do when when actually trying to calculate it in code it becomes a little tedious. The difficult part is that after two tiles combine they cant combine again that same turn. 


         So if you have a line of [2,2,2,2] it is supposed to become [0,0,4,4]. But if you code it incorrect it might become [0,0,0,8]. Some sources online stated that using a bool[4,4] array would work , using the bools to determine if a tiles had been joined that turn and then ignoring it, but I opted for a different approach.