首页 > 解决方案 > Candy Crush 风格分配

问题描述

我目前正在做一个任务,我们需要在 C 语言中创建一个基本的“糖果粉碎”风格游戏。游戏将打印用户规范的板,然后用户可以发出命令来移动角色,以便他们排成一排以及什么时候做3 个或更多字符的行将消失,新字符将替换已删除的字符,或者上面的字符将“下降”并替换那些点。

我被困在试图让数组中的字符消失并替换它们的新字符。

代码很乱,但我是一年级的学生,所以我为此道歉。

关于如何删除字符的任何想法?生病发布我的代码。

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

//global variables
char **board; // This needs to be global so all functions can see it
int gameState = 0;
int n;
int points;

//prototypes
void generateBoard();
int randomiseBoard(int);
void initiateGame();
int printNum(int);
void swapTwoCharacters(int, int, int, int);
int checkIfRow();
void reprintBoard();
bool check_range(int, int);
bool check_adjacent(int, int, int, int);
bool row_check(int, int);
bool find_row_check(int, int);


//main
int main(){
    initiateGame();
    generateBoard();

    while(gameState == 1){
    char input;
    int a,b,c,d;
    printf("\n\nPlease enter a command: ");
    scanf("%c", &input);
    getchar();
    if(input == 's'){
        printf("What do you want to swap?");
        scanf("\n%d %d %d %d", &a,&b,&c,&d);
        getchar();
        if((check_range(a, b) && check_range(c, d)) && check_adjacent(a, b, c, d) == true){
             swapTwoCharacters(a, b, c, d);
             if((find_row_check(a, b) && find_row_check(c, d)) == true){
                board[a][b] == ' ';
                board[c][d] == ' ';
             }
             reprintBoard();
             printf("\n\n");
             getchar();
        }else{
            printf("\nThis is not a valid input, please try again!\n");
            reprintBoard();
            printf("\n\n");
        }
    }
    }

    return 0;
}

//when game is initatied then this script will run
void initiateGame(){
    printf("\n\nWelcome to Character Swap");
    printf("\nHere are some of the commands you can use during your gameplay");
    printf("\ns - swap 2 characters \ne - end current game and save");
}

//swap 2 characters inside the 2 arrays
void swapTwoCharacters(int a, int b, int c, int d){
       char firstSwap = board [a] [b];
       char secondSwap = board [c] [d];
       board [a] [b] = secondSwap;
       board [c] [d] = firstSwap;
}

//check if the swap is valid
bool check_range(int row, int col){
    return row>= 1 && row <= n && col >= 1 && col <= n;
}

//check if the swap is valid
bool check_adjacent(int row1, int col1, int row2, int col2){
    return (row1 == row2 && abs(col1 - col2) == 1) ||
       (col1 == col2 && abs(row1 - row2) == 1);
}


bool row_check(int row, int col){
    return board[row][col] == board[row][col + 1] && 
           board[row][col + 1] == board[row][col + 2];
}

//this function is what I am trying to use to check if characters are the same in a row
bool find_row_check(int row, int col){
    if(col == 1) return row_check(row,col);
    if(col == 2) return row_check(row, col -1) || row_check(row, col);
    if(col == n) return row_check(row, col - 2);
    if(col == n - 1) return row_check(row, col - 2) || row_check(row, col - 1);
    return row_check(row, col - 2) || row_check(row, col-1) || row_check(row, col);
}


bool row_seq(char **board, int row, int col){
    return col > 2 && board[row][col] == board[row][col - 1]
                   && board[row][col] == board[row][col - 2];
}


bool col_seq(char **board, int row, int col){
    return row > 2 && board[row][col] == board[row - 1][col]
                   && board[row][col] == board[row - 2][col];
}

//generates the board using 2 for loops
void generateBoard(){
    char characters[7] = "!@#$&*";

    printf("\n\nPlease enter the board size: ");
    scanf("%d", &n);
    getchar();
    //prints the numbers above board
    printNum(n);
    int k = 1;
    printf("\n%d ", k);

    //generates the array and populates it with random characters
    board = (char **)malloc((n+1) * sizeof(char *));

    for(int i = 1; i <= n; i++){ 
        board[i] = (char *)malloc((n+1) * sizeof(char));
        for(int j = 1; j <= n; j++){
        board[i] [j]= characters[rand() % (sizeof(characters) - 1)]; 
        while (row_seq(board, i , j) || col_seq(board, i, j)){
          board[i][j] = characters[rand() % sizeof(characters) - 1];
        }
        printf("%c ", board[i] [j]);
    }
    //Places array onto a new row and adds the numbers on the side
    printf("\n%d ", k += 1);
    }
    gameState = 1;
}

void reprintBoard(){
    printf("\n");
    printNum(n);
    int k = 1;
    printf("\n%d ", k);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            printf("%c ", board[i] [j]);
        }
        printf("\n%d ", k += 1);
    }

}

//function to add the number to the top of the board
int printNum(int n){
    int *array = (int*)malloc(n * sizeof(int));
    for(int i = 0; i < n; i++){
        array[i] = i + 1;
    }
    printf("  ");
    for(int i = 0; i < n; i++){
        printf("%d ", array[i]);
    }
}

标签: c

解决方案


推荐阅读