Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created January 17, 2014 20:46
Show Gist options
  • Save takehiko/8481125 to your computer and use it in GitHub Desktop.
Save takehiko/8481125 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void print_field(char field[4][6])
{
int i;
for (i = 0; i < 4; i++) {
printf("%s\n", field[i]);
}
}
void move_pos(char field[4][6], int x, int y, char *s)
{
int new_x = x, new_y = y;
char tmp;
while (*s != '\0') {
switch (*s) {
case 'u': new_y--; break;
case 'd': new_y++; break;
case 'l': new_x--; break;
case 'r': new_x++; break;
}
s++;
if (new_x < 0 || new_x >= 5 || new_y < 0 || new_y >= 4) {
continue;
}
tmp = field[y][x];
field[y][x] = field[new_y][new_x];
field[new_y][new_x] = tmp;
x = new_x;
y = new_y;
}
}
int check_pos(int checker[4][5], int x, int y)
{
if (checker[y][x] == 0) {
checker[y][x] = 1;
return 1;
}
return 0;
}
void mark_field(char field[4][6], int checker[4][5])
{
int x, y;
for (y = 0; y < 4; y++) {
for (x = 0; x < 5; x++) {
if (checker[y][x] != 0) {
field[y][x] = '*';
}
}
}
}
int test_field(char field[4][6])
{
int checker[4][5] = {0};
int count = 0;
int x, y;
for (x = 0; x < 3; x++) {
for (y = 0; y < 4; y++) {
if (field[y][x] == field[y][x + 1] && field[y][x] == field[y][x + 2]) {
count += check_pos(checker, x, y)
+ check_pos(checker, x + 1, y)
+ check_pos(checker, x + 2, y);
}
}
}
for (x = 0; x < 5; x++) {
for (y = 0; y < 2; y++) {
if (field[y][x] == field[y + 1][x] && field[y][x] == field[y + 2][x]) {
count += check_pos(checker, x, y)
+ check_pos(checker, x, y + 1)
+ check_pos(checker, x, y + 2);
}
}
}
printf("[Result]\n");
mark_field(field, checker);
print_field(field);
if (count < 4 * 5) {
printf("Not cleared\n");
return 0;
}
printf("Cleared\n");
return 1;
}
int main(int argc, char* argv[])
{
char field[4][6] = {
"aaacc",
"abccc",
"abccc",
"bbbba"
};
printf("[1]\n");
print_field(field);
if (argc > 1) {
move_pos(field, 4, 3, argv[1]);
printf("[2]\n");
print_field(field);
}
test_field(field);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment