void up(int n){
	int i,j;
	for(j=0;j<n;j++){//每一列 
		for(i=0;i<n-1;i++){
			if(Array2048[i][j]==Array2048[i+1][j]){
				Array2048[i][j]=Array2048[i][j]+Array2048[i+1][j];
				Array2048[i+1][j]=0;
				//移除空格
				up_remove_blank(n); 
			}
		} 
	}
}
int main(){
	int i,j;
	bool mark;
	int n =8;
    fillBox(n);
	fillBox(n);
    output(n);

    printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);

    while (true){
		char ch;
		scanf("%c%*c", &ch);
		if (ch == 'a'){
			// mark = left(n);
			left_remove_blank(n);
			left(n);
		}
		else if (ch == 'd'){
			// mark = right(n);
			right_remove_blank(n);
			right(n);
		}
		else if (ch == 'w'){
			// mark = up(n);
			up_remove_blank(n);
			up(n);
		}
		else if (ch == 's'){
			// mark = down(n);
			down_remove_blank(n);
			down(n);
		}
		else{
			continue;
		}
		system("clear");
		//printf("Move:\n");
		//output();
		// if (!mark){
		// 	continue;
		// }
		fillBox(n);
		printf("Fill:\n");
		output(n);

		if (isOver(n)){
			printf("\n\nGame Over!\n\n");
			break;
		}
		if (isWin(n))
		{
			printf("\n\n Wow You Win!\n\n");
			break;
			/* code */
		}
	}
	
	return 0;
}
/*
 *  ======== consoleTaskFxn ========
 *  Console task
 *
 *  This task listens to the key pressed in the keyboard through USBCDC.
 *  The string ended with return character '\n' will trigger the task
 *  to send this string to the mailbox.
 *  For example, when the user enter "ls\n", this task will scan all the
 *  files in the root of SD card and send the file list to the mailbox to
 *  inform the drawing task to display on the screen.
 *  The up/down arrow can be used to scroll up/down to display more files
 *  in the SD card.
 */
Void consoleTaskFxn (UArg arg0, UArg arg1)
{
    unsigned int count;
    unsigned int cpuLoad;
    char input[128];
    UInt key;
    DrawMessage drawMsg;
    count = 1;

    /* printf goes to the UART com port */
    printf("\f======== Welcome to the Console ========\n");
    printf("Enter a command followed by return.\n"
           "Type help for a list of commands.\n\n");

    printf("%d %% ", count++);
    fflush(stdout);

    /* Loop forever receiving commands */
    while(true) {
        /* Get the user's input */
        scanf("%s", input);
        /* Flush the remaining characters from stdin since they are not used. */
        fflush(stdin);

        if (!strcmp(input, "a")) {
            /* Print the CPU load */
            cpuLoad = Load_getCPULoad();
            printf("CPU Load: %d\n", cpuLoad);
            drawMsg.drawCommand = KeyBoadCOMMAND;


            up_remove_blank(4);
            up(4);


            // copy the data to message
            key = Gate_enterSystem();
            copyFrom2048ArrayToDrawMessage(4,drawMsg.draw2048 , Array2048);
            Gate_leaveSystem(key);

            Mailbox_post(mailboxHandle, &drawMsg, BIOS_WAIT_FOREVER);
        }
        else if (!strcmp(input, "d")) {
            /* Print the CPU load */
            cpuLoad = Load_getCPULoad();
            printf("CPU Load: %d\n", cpuLoad);
            drawMsg.drawCommand = KeyBoadCOMMAND;

            down_remove_blank(4);
            down(4);


            // copy the data to message
            key = Gate_enterSystem();
            copyFrom2048ArrayToDrawMessage(4,drawMsg.draw2048 , Array2048);
            Gate_leaveSystem(key);

            Mailbox_post(mailboxHandle, &drawMsg, BIOS_WAIT_FOREVER);
        } else if (!strcmp(input, "w")) {
            /* Print the CPU load */
            cpuLoad = Load_getCPULoad();
            printf("CPU Load: %d\n", cpuLoad);
            drawMsg.drawCommand = KeyBoadCOMMAND;

            left_remove_blank(4);
            left(4);


            // copy the data to message
            key = Gate_enterSystem();
            copyFrom2048ArrayToDrawMessage(4,drawMsg.draw2048 , Array2048);
            Gate_leaveSystem(key);

            Mailbox_post(mailboxHandle, &drawMsg, BIOS_WAIT_FOREVER);
        } else if (!strcmp(input, "s")) {
            /* Print the CPU load */
            cpuLoad = Load_getCPULoad();
            printf("CPU Load: %d\n", cpuLoad);
            drawMsg.drawCommand = KeyBoadCOMMAND;

            right_remove_blank(4);
            right(4);


            // copy the data to message
            key = Gate_enterSystem();
            copyFrom2048ArrayToDrawMessage(4,drawMsg.draw2048 , Array2048);
            Gate_leaveSystem(key);

            Mailbox_post(mailboxHandle, &drawMsg, BIOS_WAIT_FOREVER);
        }
        else if (!strcmp(input, "exit")) {
            /* Exit the console task */
            printf("Are you sure you want to exit the console? Y/N: ");
            fflush(stdout);
            scanf("%s", input);
            fflush(stdin);
            if ((input[0] == 'y' || input[0] == 'Y') && input[1] == 0x00) {
                printf("Exiting console, goodbye.\n");
                Task_exit();
            }
        }
        else {
            /* Print a list of valid commands. */
            printf("Valid commands:\n"
                   "- w: move up.\n"
                   "- a: move left.\n"
                   "- s: move down.\n"
                   "- a: move right.\n"
                   "- exit: Exit the console task.\n");
        }

        fillBox(4);
//	        printf("%d %% ", count++);
        fflush(stdout);
    }

}
/*
 *  ======== grlibTaskFxn ========
 *  Drawing task
 *
 *  It is pending for the message either from console task or from button ISR.
 *  Once the messages received, it draws to the screen based on information
 *  contained in the message.
 */
Void grlibTaskFxn(UArg arg0, UArg arg1)
{
    DrawMessage curMsg;
    const UChar *pucCurImage;
    UInt key;
    UInt fontHeight = GrStringHeightGet(&context);

    while (TRUE) {
        Mailbox_pend(mailboxHandle, &curMsg, BIOS_WAIT_FOREVER);

        fillBox(4);
        /* Clear screen before drawing */
        clearDisplay();

        /* Parse the message and draw */
        switch (curMsg.drawCommand) {
        case IMAGE:
            pucCurImage = image_Gallery[curMsg.drawImageIndex];

            /* Draw image at (0,0) */
            GrImageDraw(&context, pucCurImage, 0, 0);
            break;

        case KeyBoadCOMMAND:
        	switch(curMsg.MoveCommand){
        	case UP:
        		 // copy the data to message
        			           	key = Gate_enterSystem();
        			            up_remove_blank(4);
        			            up(4);



        			            copyFrom2048ArrayToDrawMessage(4,curMsg.draw2048 , Array2048);
        			            Gate_leaveSystem(key);
        		            break;
        	case DOWN:
        		// copy the data to message
        						key = Gate_enterSystem();
        			        	down_remove_blank(4);
        						down(4);



        						copyFrom2048ArrayToDrawMessage(4,curMsg.draw2048 , Array2048);
        						Gate_leaveSystem(key);
        	        		break;
        	case LEFT:
        		// copy the data to message
        						key = Gate_enterSystem();
        						left_remove_blank(4);
        						left(4);


        						copyFrom2048ArrayToDrawMessage(4,curMsg.draw2048 , Array2048);
        						Gate_leaveSystem(key);
        	        		break;
        	case RIGHT:
        		// copy the data to message
        						key = Gate_enterSystem();
        						right_remove_blank(4);
        						right(4);

        						copyFrom2048ArrayToDrawMessage(4,curMsg.draw2048 , Array2048);
        						Gate_leaveSystem(key);
        	        		break;
        	default:
        	            break;
        	}
        	output(4,curMsg.draw2048);

            break;

        case ScreenCOMMAND:
        	// copy the data to message
        	key = Gate_enterSystem();
        	fillBox(4);
        	Gate_leaveSystem(key);
        	output(4,curMsg.draw2048);
        	System_printf("here\n");
//        	printf("data X %d , data Y %d /n",curMsg.touchPosition[0],curMsg.touchPosition[1]);
        	break;

        default:
            break;
        }
    }
}