Beispiel #1
0
def test_snake_can_grow_when_feeding():
    board = Board(width=4, height=4)
    snake = Snake(
        [
            Coordinate(x=0, y=0),
            Coordinate(x=3, y=0),
        ],
        'E'
    )
    food = [Coordinate(x=1, y=0)]

    rendered_board = ShowBoard.execute(board, snake, food)
    assert_that(rendered_board[3][0]).is_equal_to('■')
    assert_that(ShowBoard.FRUITS).contains(rendered_board[3][1])
    assert_that(rendered_board[3][2]).is_equal_to(' ')
    assert_that(rendered_board[3][3]).is_equal_to('◙')
    assert_that(len(snake.current_location)).is_equal_to(2)

    MoveSnake().execute(board, snake, food)
    rendered_board = ShowBoard.execute(board, snake, food)
    assert_that(rendered_board[3][0]).is_equal_to('◙')
    assert_that(rendered_board[3][1]).is_equal_to('■')
    assert_that(rendered_board[3][2]).is_equal_to(' ')
    assert_that(rendered_board[3][3]).is_equal_to('◙')
    assert_that(food).is_empty()
    assert_that(len(snake.current_location)).is_equal_to(3)
Beispiel #2
0
def test_can_turn_the_snake_north_when_facing_east():
    current_location = 'Dummy'
    current_heading = 'E'
    snake = Snake('Dummy', current_heading)

    TurnSnake().execute('N', snake)
    assert_that(snake.current_heading).is_equal_to('N')
Beispiel #3
0
def test_can_move_any_size_of_snake_infinitely_across_the_board():
    board = Board(width=6, height=3)
    snake = Snake(
        [
            Coordinate(x=0, y=0),
            Coordinate(x=1, y=0),
            Coordinate(x=2, y=0)
        ],
        'W'
    )
    move_snake = MoveSnake()
    show_board = ShowBoard()

    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' ', ' '],
        ['■', '◙', '◙', ' ', ' ', ' ']
    ])
    for i in range(3):
        move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', '■', '◙', '◙']
    ])
Beispiel #4
0
def test_move_one_unit_towards_east():
    current_location = [Coordinate(x=3, y=0)]
    current_heading = 'E'
    snake = Snake(current_location, current_heading)

    MoveSnake().execute(stub_board, snake)
    assert_that(snake.current_location[0].x).is_equal_to(4)
    assert_that(snake.current_location[0].y).is_equal_to(0)
Beispiel #5
0
def test_can_end_game():
    snake = Snake(
        [
            Coordinate(x=0, y=0),
            Coordinate(x=3, y=0),
        ],
        'E'
    )
    assert_that(EndGame.execute(snake)).is_false()

    dead_snake = Snake(
        [
            Coordinate(x=0, y=0),
            Coordinate(x=3, y=0),
            Coordinate(x=0, y=0),
        ],
        'E'
    )
    assert_that(EndGame.execute(dead_snake)).is_true()
Beispiel #6
0
def test_move_infinitely_towards_west():
    current_location = [Coordinate(x=0, y=0)]
    current_heading = 'W'
    snake = Snake(current_location, current_heading)

    for _ in range(3):
        MoveSnake().execute(stub_board, snake)

    assert_that(snake.current_location[0].x).is_equal_to(3)
    assert_that(snake.current_location[0].y).is_equal_to(0)
Beispiel #7
0
def test_can_turn_the_snake_in_any_direction():
    board = Board(width=4, height=4)
    snake = Snake(
        [
            Coordinate(x=0, y=0),
            Coordinate(x=3, y=0),
        ],
        'E'
    )
    move_snake = MoveSnake()
    turn_snake = TurnSnake()
    show_board = ShowBoard()

    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' '],
        ['■', ' ', ' ', '◙']
    ])
    turn_snake.execute('N', snake)
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' '],
        ['■', ' ', ' ', ' '],
        ['◙', ' ', ' ', ' ']
    ])
    turn_snake.execute('E', snake)
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' '],
        ['◙', '■', ' ', ' '],
        [' ', ' ', ' ', ' ']
    ])
    turn_snake.execute('N', snake)
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' '],
        [' ', '■', ' ', ' '],
        [' ', '◙', ' ', ' '],
        [' ', ' ', ' ', ' ']
    ])
    turn_snake.execute('S', snake)
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', '■', ' ', ' '],
        [' ', '◙', ' ', ' '],
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ']
    ])
Beispiel #8
0
def test_body_can_follow_the_head():
    current_location = [
        Coordinate(x=0, y=0),
        Coordinate(x=1, y=0),
        Coordinate(x=2, y=0)
    ]
    current_heading = 'W'
    snake = Snake(current_location, current_heading)

    MoveSnake().execute(stub_board, snake)

    assert_that(snake.current_location[0].x).is_equal_to(5)
    assert_that(snake.current_location[0].y).is_equal_to(0)
    assert_that(snake.current_location[1].x).is_equal_to(0)
    assert_that(snake.current_location[1].y).is_equal_to(0)
    assert_that(snake.current_location[2].x).is_equal_to(1)
    assert_that(snake.current_location[2].y).is_equal_to(0)
Beispiel #9
0
def test_can_move_the_snake_infinitely_across_the_board():
    board = Board(width=5, height=5)
    snake = Snake([Coordinate(x=0, y=0)], 'W')
    move_snake = MoveSnake()
    show_board = ShowBoard()

    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        ['■', ' ', ' ', ' ', ' ']
    ])
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', '■']
    ])
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', '■', ' ']
    ])
    move_snake.execute(board, snake)
    assert_that(show_board.execute(board, snake)).is_equal_to([
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', '■', ' ', ' ']
    ])
Beispiel #10
0
def test_can_generate_food():
    board = Board(width=4, height=4)
    snake_coordinates = [[0, 0], [3, 0]]
    possible_food_coordinates = [
        [1, 0], [2, 0],
        [0, 1], [1, 1], [2, 1], [3, 1],
        [0, 2], [1, 2], [2, 2], [3, 2],
        [0, 3], [1, 3], [2, 3], [3, 3]
    ]
    snake = Snake(
        [
            Coordinate(x=snake_coordinates[0][0], y=snake_coordinates[0][1]),
            Coordinate(x=snake_coordinates[1][0], y=snake_coordinates[1][1]),
        ],
        'E'
    )
    food = []

    GenerateFood().execute(board=board, snake=snake, food=food)

    assert_that(food).is_not_empty()
    for food_coordinate in [[food[0].x, food[0].y]]:
        assert_that(possible_food_coordinates).contains(food_coordinate)
        assert_that(snake_coordinates).does_not_contain(food_coordinate)
Beispiel #11
0
def test_can_draw_a_board_with_snake(current_location, board, expected_board):
    snake = Snake(current_location, 'Dummy')

    board = ShowBoard().execute(board, snake)
    assert_that(board).is_equal_to(expected_board)
Beispiel #12
0
def test_can_turn_the_snake_south_when_facing_west():
    current_heading = 'W'
    snake = Snake('Dummy', current_heading)

    TurnSnake().execute('S', snake)
    assert_that(snake.current_heading).is_equal_to('S')
Beispiel #13
0
def test_cannot_turn_the_snake_west_when_facing_east():
    current_heading = 'E'
    snake = Snake('Dummy', current_heading)

    TurnSnake().execute('W', snake)
    assert_that(snake.current_heading).is_equal_to('E')
Beispiel #14
0
import time
import curses
from src.domain.snake import Snake
from src.domain.board import Board
from src.domain.coordinate import Coordinate
from src.use_case.move_snake import MoveSnake
from src.use_case.turn_snake import TurnSnake
from src.use_case.show_board import ShowBoard
from src.use_case.generate_food import GenerateFood
from src.use_case.end_game import EndGame

# Init game objects
board = Board(width=10, height=10)
snake = Snake([Coordinate(x=0, y=0), Coordinate(
    x=1, y=0), Coordinate(x=2, y=0)], 'N')
food = []
move_snake = MoveSnake()
show_board = ShowBoard()
turn_snake = TurnSnake()
generate_food = GenerateFood()
end_game = EndGame()

# initialize application
stdscr = curses.initscr()
# get non-blocking user input
stdscr.timeout(100)

# tweak terminal settings
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
Beispiel #15
0
def test_snake_can_be_initialised_with_a_starting_location():
    expeteced_snake_coordinates = [[0, 1], [0, 0]]
    snake = Snake(expeteced_snake_coordinates, 'S')
    assert_that(
        snake.current_location).is_equal_to(expeteced_snake_coordinates)
Beispiel #16
0
def test_snake_can_be_initialised_with_a_heading():
    expeteced_heading = 'N'
    snake = Snake([[0, 0]], expeteced_heading)
    assert_that(snake.current_heading).is_equal_to(expeteced_heading)