Exemplo n.º 1
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([
        [' ', ' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', '■', '◙', '◙']
    ])
Exemplo n.º 2
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)
Exemplo n.º 3
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([
        [' ', '■', ' ', ' '],
        [' ', '◙', ' ', ' '],
        [' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ']
    ])
Exemplo n.º 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)
Exemplo n.º 5
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)
Exemplo n.º 6
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()
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def execute(board: object, snake: object, food: object):
     if not food:
         possible_food_coordinates = []
         for y in range(board.height):
             for x in range(board.width):
                 possible_food_coordinates.append([x, y])
         for coordinate in snake.current_location:
             possible_food_coordinates.remove([coordinate.x, coordinate.y])
         random_coordinate = random.choice(possible_food_coordinates)
         food.append(
             Coordinate(x=random_coordinate[0], y=random_coordinate[1]))
Exemplo n.º 9
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)
Exemplo n.º 10
0
 def move_head(snake):
     head_location = Coordinate(x=snake.current_location[0].x,
                                y=snake.current_location[0].y)
     if snake.current_heading == 'E':
         head_location.x = head_location.x + 1
     elif snake.current_heading == 'N':
         head_location.y = head_location.y + 1
     elif snake.current_heading == 'W':
         head_location.x = head_location.x - 1
     elif snake.current_heading == 'S':
         head_location.y = head_location.y - 1
     return head_location
Exemplo n.º 11
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([
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', '■', ' ', ' ']
    ])
Exemplo n.º 12
0
import pytest
from assertpy import assert_that
from src.use_case.show_board import ShowBoard
from src.domain.snake import Snake
from src.domain.board import Board
from src.domain.coordinate import Coordinate


@pytest.mark.parametrize("current_location,board,expected_board", [
    ([Coordinate(x=2, y=2)], Board(width=5, height=5),
     [[' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', '■', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' ']]),
    ([Coordinate(x=0, y=0)], Board(width=5, height=5),
     [[' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      ['■', ' ', ' ', ' ', ' ']]),
    ([Coordinate(x=0, y=4)], Board(width=5, height=5),
     [['■', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' ']]),
    ([Coordinate(x=3, y=4)], Board(width=5, height=5),
     [[' ', ' ', ' ', '■', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '],
      [' ', ' ', ' ', ' ', ' ']]),
    ([Coordinate(x=0, y=0)], Board(width=1, height=1), [['■']]),
    ([Coordinate(x=2, y=2)], Board(width=3, height=3),
     [[' ', ' ', '■'], [' ', ' ', ' '], [' ', ' ', ' ']]),
    ([Coordinate(x=0, y=0),
      Coordinate(x=1, y=1),
      Coordinate(x=2, y=2)], Board(width=3, height=3),
Exemplo n.º 13
0
def test_coordinate_can_have_an_x_and_y_param():
    point = Coordinate(x=3, y=5)
    assert_that(point.x).is_equal_to(3)
    assert_that(point.y).is_equal_to(5)
Exemplo n.º 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)