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_W 4
#define OUT 0
#define FREE 1
#define TARGET 2
#define WALL 3
#define CRATE_ON_FREE 4
#define CRATE_ON_TARGET 5
#define MAN1_ON_FREE 6
#define MAN1_ON_TARGET 7
#define MAN2_ON_FREE 8
#define MAN2_ON_TARGET 9
#define END_OF_LINE 10
enum board_str {
OUT = ' ',
FREE = '_',
TARGET = '.',
WALL = '#',
CRATE_ON_FREE = '$',
CRATE_ON_TARGET = '*',
MAN1_ON_FREE = '1',
MAN1_ON_TARGET = 'u',
MAN2_ON_FREE = '2',
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"};
struct sok_board_t {

View file

@ -10,7 +10,7 @@ void sok_board_t::print_board() {
for (int j = 0; j < NBC; j++) {
if (board[i][j] == END_OF_LINE)
break;
printf("%c", board_str[board[i][j]]);
printf("%c", board[i][j]);
}
printf("\n");
}
@ -29,41 +29,61 @@ void sok_board_t::load(char *_file) {
if ((static_cast<int>(nread)) > 0) {
bool read_ok = false;
for (int i = 0; i < nread; i++) {
if (line[i] == board_str[FREE]) {
switch (line[i]) {
case (FREE): {
board[board_nbl][i] = FREE;
} else if (line[i] == board_str[TARGET]) {
break;
}
case (TARGET): {
board[board_nbl][i] = TARGET;
} else if (line[i] == board_str[WALL]) {
break;
}
case (WALL): {
read_ok = true;
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;
} else if (line[i] == board_str[CRATE_ON_TARGET]) {
break;
}
case (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_y = i;
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_y = i;
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_y = i;
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_y = i;
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);
}