I am supposed to make a game similar to Candy Crush in Java and I'm facing a possible issue.

At first I've thought of simply filling the grid with randomized values (from a specified interval) to represent different candies. For each level I am supposed to specify a value that would represent the number of available moves. However, with this completely randomized approach I'm not sure that I'll always have a feasible move. I haven't faced this in the game itself so I'm not sure how I should handle/solve this.

I would be very grateful if someone could tell me a possible solution!

Edit: Here is the code used to initialize the grid:

        int x; //width of the grid
        int y; //height of the grid
        int candies;
        int[][] grid = new int[x][y];
        for (int i=0; i<x; i++)
            for (int j=0; j<y; j++) {
                int temp = (int)(Math.random()*(candies)+1);
                grid[i][j] = temp;
New contributor
dungelange is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Let us see what you've got so far (e.g, the code you've written to this point) and we'll see what we can do. – Spectric yesterday
  • @Spectric I have added the code to the original question. – dungelange yesterday
  • If there are no possible moves, then your described method should return 0. Am I misunderstanding your question? – Charlie Armstrong yesterday
  • @CharlieArmstrong Well, that is one possible solution. However, in the original game I've never encountered such situation, so I am trying to figure out how they have avoided it. – dungelange yesterday
  • 3
    It seems like your question boils down to "Does Candy Crush allow unsolvable boards?" I'm not sure that SO is a good place to ask a question like that: it would likely require a Candy Crush developer to answer that detail. If that is not your question, can you edit this post to clarify what specifically you are seeking help with? Any algorithm to count the largest number of legal moves? An algorithm to always generate a solvable board? Something else? – chwarr yesterday

Browse other questions tagged or ask your own question.