1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <raylib.h>
#include <stdlib.h>
#include <stdio.h>
const int WindowHeight = 600;
const int WindowWidth = 800;
int rows;
int cols;
int cellSize;
enum color {
red, blue, green, violet, black
};
typedef char byte;
typedef struct Tile {
byte number;
enum color color;
} Tile;
Tile* board;
Color colorFromEnum(enum color c){
switch(c){
case red:
return RED;
break;
case blue:
return BLUE;
break;
case green:
return GREEN;
break;
case violet:
return VIOLET;
break;
case black:
default:
return BLACK;
}
}
void drawBoard(){
int ymax = cols * cellSize;
int xmax = rows * cellSize;
int offset = 2;
for (int i = 1; i <= rows; i++){
DrawLine(i * cellSize, 0, i * cellSize, ymax, BLACK);
}
for (int j = 1; j <= cols; j++){
DrawLine(0 , j * cellSize, xmax, j * cellSize, BLACK);
}
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
char text[16];
Tile tile = board[i * cols + j];
snprintf(text, 16, "%d", tile.number);
DrawText(text, j * cellSize + offset, i * cellSize + offset, cellSize - 2 * offset, colorFromEnum(tile.color));
}
}
}
int selectTile(int x, int y){
int r = y / cellSize;
int c = x / cellSize;
return r * cols + c;
}
void explode(int r, int c, enum color color){
int index = r * cols + c;
board[index].number++;
board[index].color = color;
if (board[index].number > 3){
board[index].number = 0;
board[index].color = black;
if (r > 0) explode(r - 1, c, color);
if (r < rows - 1) explode(r + 1, c, color);
if (c > 0) explode(r, c - 1, color);
if (c < cols - 1) explode(r, c + 1, color);
}
}
int main(){
InitWindow(WindowHeight, WindowWidth, "Explode");
rows = cols = 6;
cellSize = 60;
board = (Tile*) calloc(sizeof(Tile), rows * cols);
Color turns[] = {RED, BLUE, GREEN, VIOLET};
int alive[] = {1, 1, 1, 1};
int turn_index = 0;
for (int i = 0; i < rows * cols; i++){
board[i].color = black;
}
board[1 + 1 * cols].color = red;
board[1 + 4 * cols].color = blue;
board[4 + 1 * cols].color = green;
board[4 + 4 * cols].color = violet;
while (!WindowShouldClose()){
BeginDrawing();
ClearBackground(WHITE);
drawBoard();
if (!alive[turn_index]){
turn_index = (turn_index + 1) % 4;
}
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
int index = selectTile(GetMouseX(), GetMouseY());
if (index < rows * cols && ColorIsEqual(colorFromEnum(board[index].color), turns[turn_index])){
explode(index / cols, index % cols, board[index].color);
int number_sums[] = {0, 0, 0, 0};
for (int i = 0; i < rows * cols; i++){
number_sums[board[i].color]++;
}
for (int i = 0; i < 4; i++){
if (number_sums[i] == 0){
alive[i] = 0;
}
}
turn_index = (turn_index + 1) % 4;
}
}
EndDrawing();
}
free(board);
}
|