Snake: Difference between revisions

Content deleted Content added
Trizen (talk | contribs)
m →‎{{header|Sidef}}: minor code simplification
Trizen (talk | contribs)
m →‎{{header|Sidef}}: simpler code
Line 201: Line 201:
const ansi = frequire('Term::ANSIColor')
const ansi = frequire('Term::ANSIColor')


enum(
enum (VOID, HEAD, BODY, TAIL, FOOD)
VOID,
HEAD,
BODY,
TAIL,
FOOD,
)


define (
define (
Line 216: Line 210:
)
)


const BG_COLOR = "on_black"
define BG_COLOR = "on_black"
const FOOD_COLOR = ("red" + " " + BG_COLOR)
define FOOD_COLOR = ("red" + " " + BG_COLOR)
const SNAKE_COLOR = ("bold green" + " " + BG_COLOR)
define SNAKE_COLOR = ("bold green" + " " + BG_COLOR)
const SLEEP_SEC = 0.02
define SLEEP_SEC = 0.02


const (
const (
U_HEAD = ansi.colored('', SNAKE_COLOR),
A_VOID = ansi.colored(' ', BG_COLOR),
D_HEAD = ansi.colored('', SNAKE_COLOR),
A_FOOD = ansi.colored('', FOOD_COLOR),
L_HEAD = ansi.colored('', SNAKE_COLOR),
A_BLOCK = ansi.colored('', SNAKE_COLOR),
R_HEAD = ansi.colored('▶', SNAKE_COLOR),
)
)


const (
has dir = LEFT
U_BODY = ansi.colored('╹', SNAKE_COLOR),
D_BODY = ansi.colored('╻', SNAKE_COLOR),
L_BODY = ansi.colored('╴', SNAKE_COLOR),
R_BODY = ansi.colored('╶', SNAKE_COLOR),
)

const (
U_TAIL = ansi.colored('╽', SNAKE_COLOR),
D_TAIL = ansi.colored('╿', SNAKE_COLOR),
L_TAIL = ansi.colored('╼', SNAKE_COLOR),
R_TAIL = ansi.colored('╾', SNAKE_COLOR),
)

const A_VOID = ansi.colored(' ', BG_COLOR)
const A_FOOD = ansi.colored('❇', FOOD_COLOR)

has grid = [[]]
has grid = [[]]
has head_pos = [0, 0]
has head_pos = [0, 0]
has tail_pos = [0, 0]
has tail_pos = [0, 0]
has dir = LEFT


method init {
method init {
Line 274: Line 250:


method display {
method display {
static i = 0

print("\033[H", grid.map { |row|
print("\033[H", grid.map { |row|
row.map { |cell|
row.map { |cell|
if (cell[0] != VOID) {
i = [UP, DOWN, LEFT, RIGHT].index(cell[1])
}
given (cell[0]) {
given (cell[0]) {
when (VOID) { A_VOID }
when (VOID) { A_VOID }
when (FOOD) { A_FOOD }
when (FOOD) { A_FOOD }
when (BODY) { [U_BODY, D_BODY, L_BODY, R_BODY][i] }
default { A_BLOCK }
when (HEAD) { [U_HEAD, D_HEAD, L_HEAD, R_HEAD][i] }
when (TAIL) { [U_TAIL, D_TAIL, L_TAIL, R_TAIL][i] }
}
}
}.join('')
}.join('')