correct and format this C code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#define WIDTH 10
#define HEIGHT 24
int board[HEIGHT][WIDTH] = {0};
const int shapes[7][4][2] = {
{{0, 2}, {0, 1}, {0, 0}, {0, -1}},
{{0, 1}, {0, 0}, {1, 0}, {1, -1}},
{{0, 0}, {0, 1}, {1, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 1}, {1, 2}},
{{0, 0}, {0, -1}, {1, 0}, {1, 1}},
{{0, 1}, {0, 0}, {0, -1}, {1, 0}},
{{0, 1}, {0, 0}, {0, -1}, {1, -1}}
};
int get_char() {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
void print_board() {
printf(“\n”);
for (int j = 0; j < HEIGHT; j++) {
for (int i = 0; i < WIDTH; i++) {
if (board[j][i]) printf(“[]”);
else printf(" “);
}
printf(”\n");
}
}
int check_collision(int piece[4][2], int x, int y) {
for (int i = 0; i < 4; i++) {
int px = piece[i][0] + x;
int py = piece[i][1] + y;
if (px < 0 || px >= WIDTH || py < 0 || py >= HEIGHT || board[py][px]) return 1;
}
return 0;
}
void place_piece(int piece[4][2], int x, int y, int color) {
for (int i = 0; i < 4; i++) {
int px = piece[i][0] + x;
int py = piece[i][1] + y;
board[py][px] = color;
}
}
void remove_piece(int piece[4][2], int x, int y) {
for (int i = 0; i < 4; i++) {
int px = piece[i][0] + x;
int py = piece[i][1] + y;
board[py][px] = 0;
}
}
int main(int argc, char const *argv[]) {
srand(time(NULL));
int piece[4][2] = {0};
int x = WIDTH / 2;
int y = 2;
int shape = rand() % 7;
int rotation = 0;
int color = rand() % 7 + 1;
int score = 0;
while (1) {
printf(“\033[H\033[J”); // clear screen
printf(“Score: %d\n”, score);
// move piece down
if (check_collision(shapes[shape], x, y + 1)) {
place_piece(shapes[shape], x, y, color);
y = 2;
shape = rand() % 7;
rotation = 0;
color = rand() % 7 + 1;
// check for full rows
for (int j = 0; j < HEIGHT; j++) {
int full_row = 1;
for (int i = 0; i < WIDTH; i++) {
if (!board[j][i]) full_row = 0;
}
if (full_row) {
score++;
for (int k = j; k > 0; k–) {
for (int i = 0; i < WIDTH; i++) {
board[k][i] = board[k-1][i];
}
}
}
}
if (check_collision(shapes[shape], x, y)) break; // game over
}
else y++;
// print board
print_board();
// get input
int ch = 0;
if (kbhit()) ch = get_char();
switch (ch) {
case ‘a’: if (!check_collision(shapes[shape], x - 1, y)) { remove_piece(shapes[shape], x, y); x–; } break;
case ‘d’: if (!check_collision(shapes[shape], x + 1, y)) { remove_piece(shapes[shape], x, y); x++; } break;
case ‘s’: while (!check_collision(shapes[shape], x, y + 1)) { remove_piece(shapes[shape], x, y); y++; } break;
case ‘w’: {
int next_rotation = rotation + 1;
if (next_rotation > 3) next_rotation = 0;
int temp[4][2];
for (int i = 0; i < 4; i++) {
int px = shapes[shape][i][0];
int py = shapes[shape][i][1];
temp[i][0] = px * cos(next_rotation * M_PI / 2) - py * sin(next_rotation * M_PI / 2);
temp[i][1] = py * cos(next_rotation * M_PI / 2) + px * sin(next_rotation * M_PI / 2);
}
if (!check_collision(temp, x, y)) {
remove_piece(shapes[shape], x, y);
memcpy(piece, temp, sizeof(piece));
rotation = next_rotation;
}
break;
}
case ‘q’: return 0;
}
// sleep 100ms
usleep(100000);
}
printf(“\nGame Over!\n”);
return 0;
}