我目前正在做一项作业,需要在C中创建一个基本的“ Candy Crush” Style游戏。该游戏将打印用户指定的棋盘,然后用户可以发出命令移动字符,以便它们排成一行并在何时排好队。他们确实会删除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]);
}
}
您得到这种排序了吗?我正在做同样的作业,遇到麻烦。