帮我写一个消消乐的代码
时间: 2023-04-05 17:03:06 浏览: 151
好的,我可以为您提供一个消消乐的代码。以下是示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 8
#define COLS 8
#define COLORS 5
int board[ROWS][COLS];
void init_board() {
int i, j;
srand(time(NULL));
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
board[i][j] = rand() % COLORS + 1;
}
}
}
void print_board() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int remove_matches() {
int i, j, k, count = 0;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) continue;
for (k = j + 1; k < COLS && board[i][k] == board[i][j]; k++);
if (k - j >= 3) {
count += k - j;
for (k--; k >= j; k--) {
board[i][k] = 0;
}
}
for (k = i + 1; k < ROWS && board[k][j] == board[i][j]; k++);
if (k - i >= 3) {
count += k - i;
for (k--; k >= i; k--) {
board[k][j] = 0;
}
}
}
}
return count;
}
void drop_blocks() {
int i, j, k;
for (j = 0; j < COLS; j++) {
for (i = ROWS - 1, k = ROWS - 1; i >= 0; i--) {
if (board[i][j] != 0) {
board[k--][j] = board[i][j];
}
}
for (; k >= 0; k--) {
board[k][j] = 0;
}
}
}
int main() {
int score = 0;
init_board();
while (1) {
printf("Score: %d\n", score);
print_board();
if (remove_matches() == 0) break;
drop_blocks();
score += 10;
}
printf("Game over! Final score: %d\n", score);
return 0;
}
阅读全文