# Design Candy Crush Saga Game

• Design the Candy Crush Saga game, there are five kinds of candy, the goal is to initialize the game and the same candy will not appear three times continuously in a row or column. and the board size is 10 * 10
e.g. (1,2,3,4,5 mean different types of candy)
1 2 2 1 2 2 3 3 5 1
2 2 1 2 2 3 3 5 1 4
3 3 5 1 4 5 4 1 2 2
1 2 3 5 1 1 4 1 2 2
4 2 3 3 5 1 2 3 4 4
1 4 2 1 2 2 3 3 5 1
1 2 2 1 4 1 1 2 5 1
5 2 5 4 2 2 3 3 4 2

• Any proposed solution to this.

• Here's the code to initialize the boards with random numbers. Runtime O(n2) for a board of size n*n.

``````private static int[][] initializeBoard(int[][] board) {
// TODO Auto-generated method stub
Random r = new Random();
int p1 = 0;
int p2 = 0;
for(int row=0; row<board.length; row++){
for(int col=0; col<board[0].length; col++){
int curr = r.nextInt(5)+1;
while((curr==p1 && curr==p2) || (row>2 && curr == board[row-1][col] && curr == board[row-2][col])){
curr = r.nextInt(5)+1;
}
board[row][col] = curr;
p1 = curr;
p2 = p1;
}
}

printBoard(board);
return board;
}

private static void printBoard(int[][] board) {
// TODO Auto-generated method stub
//Print board
for(int row=0; row<board.length; row++){
for(int col=0; col<board[0].length; col++){
System.out.print(board[row][col] +" ");
}
System.out.println("");
}
}

``````

• With little Modification

``````int[][] initializeBoard(int[][] board) {
int next = 0, pc1 = 0, pc2 = 0, pr1 = 0, pr2 = 0;
Random random = new Random();

for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board.length; c++) {
next = random.nextInt(5) + 1; // random return between 0 -4
while ((next == pc1 && next == pc2) || (next == pr1 && next == pr2)) {
next = random.nextInt(5) + 1;
}
board[r][c] = next;
pc1 = pc2;
pc2 = next;
}
pr1 = pr2;
pr2 = next;
}
return board;
}

void printBoard(int[][] board)
{

Arrays.stream(board).map(Arrays::toString).forEach(System.out::println);

}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.