use switch

This commit is contained in:
Mylloon 2023-01-27 17:37:08 +01:00
parent 1e990a60a5
commit 8e3803695a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 49 additions and 29 deletions

View file

@ -15,20 +15,20 @@
#define MOVE_R 3 #define MOVE_R 3
#define MOVE_W 4 #define MOVE_W 4
#define OUT 0 enum board_str {
#define FREE 1 OUT = ' ',
#define TARGET 2 FREE = '_',
#define WALL 3 TARGET = '.',
#define CRATE_ON_FREE 4 WALL = '#',
#define CRATE_ON_TARGET 5 CRATE_ON_FREE = '$',
#define MAN1_ON_FREE 6 CRATE_ON_TARGET = '*',
#define MAN1_ON_TARGET 7 MAN1_ON_FREE = '1',
#define MAN2_ON_FREE 8 MAN1_ON_TARGET = 'u',
#define MAN2_ON_TARGET 9 MAN2_ON_FREE = '2',
#define END_OF_LINE 10 MAN2_ON_TARGET = 'd',
END_OF_LINE = 'a'
};
const char board_str[] = {' ', '_', '.', '#', '$', '*',
'1', 'u', '2', 'd', 'a'};
const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"}; const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"};
struct sok_board_t { struct sok_board_t {

View file

@ -10,7 +10,7 @@ void sok_board_t::print_board() {
for (int j = 0; j < NBC; j++) { for (int j = 0; j < NBC; j++) {
if (board[i][j] == END_OF_LINE) if (board[i][j] == END_OF_LINE)
break; break;
printf("%c", board_str[board[i][j]]); printf("%c", board[i][j]);
} }
printf("\n"); printf("\n");
} }
@ -29,41 +29,61 @@ void sok_board_t::load(char *_file) {
if ((static_cast<int>(nread)) > 0) { if ((static_cast<int>(nread)) > 0) {
bool read_ok = false; bool read_ok = false;
for (int i = 0; i < nread; i++) { for (int i = 0; i < nread; i++) {
if (line[i] == board_str[FREE]) { switch (line[i]) {
case (FREE): {
board[board_nbl][i] = FREE; board[board_nbl][i] = FREE;
} else if (line[i] == board_str[TARGET]) { break;
}
case (TARGET): {
board[board_nbl][i] = TARGET; board[board_nbl][i] = TARGET;
} else if (line[i] == board_str[WALL]) { break;
}
case (WALL): {
read_ok = true; read_ok = true;
board[board_nbl][i] = WALL; board[board_nbl][i] = WALL;
} else if (line[i] == board_str[CRATE_ON_FREE]) { break;
}
case (CRATE_ON_FREE): {
board[board_nbl][i] = CRATE_ON_FREE; board[board_nbl][i] = CRATE_ON_FREE;
} else if (line[i] == board_str[CRATE_ON_TARGET]) { break;
}
case (CRATE_ON_TARGET): {
board[board_nbl][i] = CRATE_ON_TARGET; board[board_nbl][i] = CRATE_ON_TARGET;
} else if (line[i] == board_str[MAN1_ON_FREE]) { break;
}
case (MAN1_ON_FREE): {
man1_x = board_nbl; man1_x = board_nbl;
man1_y = i; man1_y = i;
board[board_nbl][i] = MAN1_ON_FREE; board[board_nbl][i] = MAN1_ON_FREE;
} else if (line[i] == board_str[MAN1_ON_TARGET]) { break;
}
case (MAN1_ON_TARGET): {
man1_x = board_nbl; man1_x = board_nbl;
man1_y = i; man1_y = i;
board[board_nbl][i] = MAN1_ON_TARGET; board[board_nbl][i] = MAN1_ON_TARGET;
} else if (line[i] == board_str[MAN2_ON_FREE]) { break;
}
case (MAN2_ON_FREE): {
man2_x = board_nbl; man2_x = board_nbl;
man2_y = i; man2_y = i;
board[board_nbl][i] = MAN2_ON_FREE; board[board_nbl][i] = MAN2_ON_FREE;
} else if (line[i] == board_str[MAN2_ON_TARGET]) { break;
}
case (MAN2_ON_TARGET): {
man2_x = board_nbl; man2_x = board_nbl;
man2_y = i; man2_y = i;
board[board_nbl][i] = MAN2_ON_TARGET; board[board_nbl][i] = MAN2_ON_TARGET;
break;
}
}
if (read_ok) {
board[board_nbl][nread - 1] = END_OF_LINE;
board_nbl++;
} }
} }
if (read_ok) {
board[board_nbl][nread - 1] = END_OF_LINE;
board_nbl++;
}
} }
free(line);
fclose(fp);
} }
free(line);
fclose(fp);
} }