Design Candy Crush Saga Game


  • 1
    M

    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


  • 0
    S

    Any proposed solution to this.


  • 0
    P

    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("");
    				}
    	}
    
    

Log in to reply
 

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