Recently, I successfully recreated the 2048 game in Java, which was a pleasant surprise to me as I didn't expect to create something this "advanced."
During the game, when tiles are moved, a new square/tile/number needs to be generated. To find an available spot for the new tile, my code randomly selects a number between 0 and 15 (tiles are numbered from 0 to 15 using a multidimensional array with 4 columns and 4 rows). If the chosen spot is already occupied, the function recalls itself by returning the existing tile. In cases where the spot is unoccupied, I simply return a number that is not utilized later. Can you help me identify what am I doing wrong?
Another question I have is related to starting a new game. Currently, I reset all variables in the newGame() function. Is there a way to restart the application instead? I couldn't find any information on the website regarding this.
I would also appreciate feedback on whether I followed programming conventions and your overall thoughts on my approach to recreating this game for practice. Thank you for taking the time to review this.
Below is the code snippet:
// Code goes here