summaryrefslogtreecommitdiff
path: root/snakegame/common.py
blob: a44219bfc2a89f348ea10c141236192f6d46f5e0 (about) (plain)
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
from string import ascii_lowercase as lowercase, ascii_uppercase as uppercase
alphabet = lowercase + uppercase

directions = {
    'U': (0, -1),
    'D': (0, 1),
    'L': (-1, 0),
    'R': (1, 0),
}

EMPTY = '.'
APPLE = '*'
WALL = '#'

is_empty = EMPTY.__eq__
is_apple = APPLE.__eq__
is_wall = WALL.__eq__

def is_vacant(cell):
    return cell in (EMPTY, APPLE)

def is_blocking(cell):
    return cell not in (EMPTY, APPLE)

def is_snake(cell):
    return cell in alphabet

def is_snake_head(cell):
    return cell in uppercase

def is_snake_body(cell):
    return cell in lowercase

def is_enemy_snake(cell, me):
    assert me.isupper()
    return is_snake(cell) and cell.upper() != me

def is_my_snake(cell, me):
    assert me.isupper()
    return cell.upper() == me

def get_size(board):
    height = len(board)
    width = len(board[0])
    return width, height

def in_bounds(x, y, width, height):
    return (
        x >= 0 and x < width and
        y >= 0 and y < height
    )

def get_cell(board, x, y, wrap=True):
    width, height = get_size(board)
    if wrap:
        x %= width
        y %= height
    elif not in_bounds(x, y, width, height):
        return None
    return board[y][x]