309 lines
6.8 KiB
C
309 lines
6.8 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
|
|
#define CANVAS_HEIGHT 20
|
|
#define CANVAS_WIDTH 10
|
|
|
|
uint8_t canvas[CANVAS_WIDTH][CANVAS_HEIGHT];
|
|
|
|
#define O_HEIGHT 2
|
|
#define O_WIDTH 2
|
|
#define O_COLOR 1
|
|
|
|
#define I_HEIGHT 4
|
|
#define I_WIDTH 1
|
|
#define I_COLOR 1
|
|
|
|
typedef enum {e_O, e_I} t_shape;
|
|
|
|
typedef struct {
|
|
t_shape shape;
|
|
|
|
// flip width and height when rotating
|
|
uint8_t width;
|
|
uint8_t height;
|
|
|
|
uint8_t initial; // 1 = initial appearing, no wipe necessary when drawing
|
|
|
|
uint8_t x;
|
|
uint8_t y;
|
|
uint8_t rotation; // 0 = 12 o'clock, 1 = 3 o'clock, 2 = 6 o'clock, 3 = 9 o'clock
|
|
|
|
uint8_t last_x;
|
|
uint8_t last_y;
|
|
uint8_t last_rotation;
|
|
|
|
union {
|
|
uint8_t O[O_WIDTH][O_HEIGHT];
|
|
uint8_t I[I_WIDTH][I_HEIGHT];
|
|
};
|
|
} t_object;
|
|
|
|
t_object object;
|
|
|
|
typedef enum {e_Down, e_Left, e_Right} t_direction;
|
|
|
|
|
|
void showCanvas() {
|
|
for (uint8_t y = 0; y < CANVAS_HEIGHT; y++) {
|
|
for (uint8_t x = 0; x < CANVAS_WIDTH; x++) {
|
|
printf("%02x ", canvas[x][y]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void initObject(t_shape shape, uint8_t x, uint8_t y) {
|
|
object.shape = shape;
|
|
object.x = x;
|
|
object.y = y;
|
|
object.rotation = 0;
|
|
object.last_x = 0;
|
|
object.last_y = 0;
|
|
object.last_rotation = 0;
|
|
object.initial = 1;
|
|
|
|
switch (shape) {
|
|
case e_O:
|
|
object.width = O_WIDTH;
|
|
object.height = O_HEIGHT;
|
|
memset(object.O, O_COLOR, O_HEIGHT * O_WIDTH);
|
|
break;
|
|
case e_I:
|
|
object.width = I_WIDTH;
|
|
object.height = I_HEIGHT;
|
|
memset(object.I, I_COLOR, I_HEIGHT * I_WIDTH);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void drawObject() {
|
|
if (object.initial == 0) {
|
|
// wipe
|
|
switch (object.shape) {
|
|
case e_O:
|
|
switch (object.last_rotation) {
|
|
case 0:
|
|
canvas[object.last_x][object.last_y] = 0;
|
|
canvas[object.last_x][object.last_y+1] = 0;
|
|
canvas[object.last_x+1][object.last_y] = 0;
|
|
canvas[object.last_x+1][object.last_y+1] = 0;
|
|
break;
|
|
}
|
|
break;
|
|
case e_I:
|
|
switch (object.last_rotation) {
|
|
case 0:
|
|
canvas[object.last_x][object.last_y] = 0;
|
|
canvas[object.last_x][object.last_y+1] = 0;
|
|
canvas[object.last_x][object.last_y+2] = 0;
|
|
canvas[object.last_x][object.last_y+3] = 0;
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
// draw
|
|
switch (object.shape) {
|
|
case e_O:
|
|
switch (object.rotation) {
|
|
case 0:
|
|
canvas[object.x][object.y] = object.O[0][0];
|
|
canvas[object.x][object.y+1] = object.O[0][1];
|
|
canvas[object.x+1][object.y] = object.O[1][0];
|
|
canvas[object.x+1][object.y+1] = object.O[1][1];
|
|
break;
|
|
}
|
|
break;
|
|
case e_I:
|
|
switch (object.rotation) {
|
|
case 0:
|
|
canvas[object.x][object.y] = object.O[0][0];
|
|
canvas[object.x][object.y+1] = object.O[0][1];
|
|
canvas[object.x][object.y+2] = object.O[0][2];
|
|
canvas[object.x][object.y+3] = object.O[0][3];
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
// first draw has been done, next time, wipe is necessary
|
|
object.initial = 0;
|
|
}
|
|
|
|
void moveObject(t_direction direction) {
|
|
object.last_x = object.x;
|
|
object.last_y = object.y;
|
|
object.last_rotation = object.rotation;
|
|
|
|
switch (direction) {
|
|
case e_Down:
|
|
object.y = object.y + 1;
|
|
break;
|
|
case e_Left:
|
|
object.x = object.x - 1;
|
|
break;
|
|
case e_Right:
|
|
object.x = object.x + 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void revokeObjectMove() {
|
|
object.x = object.last_x;
|
|
object.y = object.last_y;
|
|
object.rotation = object.last_rotation;
|
|
}
|
|
|
|
int checkCanvasBordersForObject() {
|
|
// return true if borders are violated
|
|
return ((object.y > CANVAS_HEIGHT - object.height) || (object.x > CANVAS_WIDTH - object.width));
|
|
}
|
|
|
|
int checkOtherObjectsTouchedForObject() {
|
|
// return true if another object is touched
|
|
int res = 0;
|
|
|
|
switch (object.shape) {
|
|
case e_O:
|
|
switch (object.rotation) {
|
|
case 0:
|
|
res = (canvas[object.x][object.y] != 0) ||
|
|
(canvas[object.x][object.y+1] != 0) ||
|
|
(canvas[object.x+1][object.y] != 0) ||
|
|
(canvas[object.x+1][object.y+1] != 0);
|
|
break;
|
|
}
|
|
break;
|
|
case e_I:
|
|
switch (object.rotation) {
|
|
case 0:
|
|
res = (canvas[object.x][object.y] != 0) ||
|
|
(canvas[object.x][object.y+1] != 0) ||
|
|
(canvas[object.x][object.y+2] != 0) ||
|
|
(canvas[object.x][object.y+3] != 0);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
memset(canvas, 0, CANVAS_HEIGHT * CANVAS_WIDTH);
|
|
|
|
showCanvas();
|
|
|
|
initObject(e_O, 2, 0);
|
|
|
|
drawObject();
|
|
showCanvas();
|
|
|
|
while (1) {
|
|
moveObject(e_Down);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
while (1) {
|
|
moveObject(e_Right);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
while (1) {
|
|
moveObject(e_Left);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
|
|
initObject(e_I, 1, 0);
|
|
|
|
drawObject();
|
|
showCanvas();
|
|
|
|
while (1) {
|
|
moveObject(e_Down);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
while (1) {
|
|
moveObject(e_Right);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
while (1) {
|
|
moveObject(e_Left);
|
|
if (checkCanvasBordersForObject()) {
|
|
printf("object would stick out canvas\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
if (checkOtherObjectsTouchedForObject()) {
|
|
printf("object would touch other object\n");
|
|
revokeObjectMove();
|
|
break;
|
|
}
|
|
drawObject();
|
|
showCanvas();
|
|
}
|
|
|
|
}
|
|
|