连接2d数组中的随机数

时间:2016-05-27 03:20:46

标签: c

我已经在2d数组中生成了随机数。我现在想要的是通过在游戏板内移动来连接相同的数字。问题是它不会移动。

我真的需要帮助!!!我是c编程的新手。提前谢谢。

这是我的代码:

void playgame(char box[ROW][COL])
{
  int x, y, choice2,num,direction=0;
  char input;
  do{
     printf("Please select a number (1-7) : \n");
     scanf("%i",&num);
     if(x==0 || x==ROW-1 ||y==0 || y==COL-1)
     {

     printf("Invalid!\n");

     }
     else
     {

     printf("Numer %i is currently selected!\n", num);
     }
    }while(x==0 || x==ROW-1 ||y==0 || y==COL-1);

    printf("[1]Move\n[2]Sign out\n");
    printf("Enter choice: ");
    scanf("%d", &choice2);


    switch(choice2)
    {
      case 1: 
                {
                    printf("Press 'E' to go up\n");
                    /*codes for moving the character up.....*/
                }

                {
                    printf("Press 'D' to go right\n");
                    /*codes for moving the character down.....*/
                }

                {
                    printf("Press 'S' to go left\n");
                    /*codes for moving the character left.....*/
                }

                {
                    printf("Press 'X' to go down\n");
                    /*codes for moving the character up.....*/
                }

                {
                    printf("Press 'R' to remove the existing path\n");
                }
                    fflush(stdin);

                    scanf("%c", &input);
                break;

        case 2: printf("Bye!\n");
    }



  for(x=0; x<9; x++)

  for(y=0; y<9; y++)
  {     
    if(input == 'E')
      if(box[x][y]==num)
      {
       box[--x][y]==num;
       }

     if(input == 'D')
      if(box[x][y]==num)
      {
       box[x][y++]==num;
       }


     if(input == 'S')
        if(box[x][y]== num)
        {
            box[x][--y]== num;
        }

     if(input == 'X')
      if(box[x][y]==num)
      {
       box[--x][y]==num;
       }


     }


}

1 个答案:

答案 0 :(得分:0)

试试这个:

#include <stdio.h>
#include <stdlib.h>

#define ROW 9
#define COL 9

void display(char box[ROW][COL]);
int validMove(int r, int c, char box[ROW][COL]);

void playgame(char box[ROW][COL]){
    int r, c, num;
    int choice, nr, nc;
    char input, n;

    for(;;){
        printf("Please select a number (1-7) : \n");
        if(1 == scanf("%i", &num) && 1<= num && num <= 7)
            break;
        printf("Invalid!\n");
        while(getchar() != '\n');
    }
    printf("Numer %i is currently selected!\n", num);
    n = num + '0';

    for(r = 1; r < ROW-1; ++r)
        for(c = 1; c < COL-1; ++c)
            if(box[r][c] == n)
                goto find;
find:

    for(;;){
        printf("[1]Move\n"
               "[2]Sign out\n"
               "Enter choice: ");
        scanf("%d", &choice);

        switch(choice){
        case 1:
            printf("Press 'E' to go up\n"
                   "Press 'D' to go right\n"
                   "Press 'S' to go left\n"
                   "Press 'X' to go down\n"
                   "Press 'R' to remove the existing path\n");

            scanf(" %c", &input);
            nr = r;
            nc = c;
            switch(input){
            case 'E':
                nr = r - 1;
                break;
            case 'D':
                nc = c + 1;
                break;
            case 'S':
                nc = c - 1;
                break;
            case 'X':
                nr = r + 1;
                break;
            case 'R':
                break;
            default:
                break;
            }
            if(validMove(nr, nc, box)){
                box[nr][nc] = box[r][c];
                box[r][c]   = ' ';
                r = nr;
                c = nc;
                display(box);
            } else if(input != 'R'){
                printf("invalid move!\n");
            }
            break;
        case 2: printf("Bye!\n");
            exit(0);
        }
    }
}

int validMove(int r, int c, char box[ROW][COL]){
    if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
        return 0;
    if(box[r][c] != ' ')
        return 0;
    return 1;
}

void initBoard(char box[ROW][COL]){
    int r, c;
    for(r = 0; r < ROW; ++r){
        for(c = 0; c < COL; ++c){
            if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
                box[r][c] = '#';
            else
                box[r][c] = ' ';
        }
    }
    for(int i = 1; i <= 7; ++i){
        int r = rand() % (ROW-1-1) + 1;
        int c = rand() % (COL-1-1) + 1;
        if(box[r][c] == ' ')
            box[r][c] = i + '0';
        else
            --i;
    }
}

void display(char box[ROW][COL]){
    for(int r = 0; r < ROW; ++r){
        for(int c = 0; c < COL; ++c){
            putchar(box[r][c]);
        }
        putchar('\n');
    }
}

int main(void){
    char board[ROW][COL];
    initBoard(board);
    display(board);
    playgame(board);

    return 0;
}