Test Driven Development w Pythonie

Ostatnio, wraz z kolegą Tomkiem postanowiliśmy pójść na warsztaty z TDD w Pythonie organizowane w ramach BIWAK-u. Zadaniem było napisanie testów (korzystając z nosetests) oraz kilku funkcji związanych z grą Reversi.

from nose.tools import assert_equal

def test_legal_moves():
    board = ["....",
             ".bw.",
             ".wb.",
             "...."]
    expected = [(0, 1), (1, 0), (2, 3), (3, 2)]
    assert_equal(legal_moves(board, 'w'), expected)

    board = ["bw",
             "bw"]
    expected = []
    assert_equal(legal_moves(board, 'w'), expected)

    board = ["w.w",
             "bbb"]
    expected = []
    assert_equal(legal_moves(board, 'w'), expected)

    board = ["bw..",
             "ww..",
             "...."]
    expected = [(0, 2), (2, 0), (2, 2)]
    assert_equal(legal_moves(board, 'b'), expected)

def legal_moves(board, player):
    moves = []
    directions = [(-1,-1), (-1,0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    pieces = find_pieces(board, player)
    for piece in pieces:
        for dir in directions:
            move = valid_move(board, dir, piece, player)
            if move != (-1, -1):
                moves.append(move)
    moves.sort()
    return moves

def test_find_pieces():
    board = ["....",
             ".bw.",
             ".wb.",
             "...."]
    expected = [(1, 2), (2, 1)]
    assert_equal(find_pieces(board, 'w'), expected)

def find_pieces(board, player):
    list = []
    for i,row in enumerate(board):
        for j,col in enumerate(row):
            if col == player:
                list.append((i,j))
    return list

def test_valid_move():
    board = ["....",
             ".bw.",
             ".wb.",
             "...."]
    expected = (0, 2)
    vector = (-1, 0)
    start_point = (2, 2)
    assert_equal(valid_move(board, vector, start_point, "b"), expected)

def valid_move(board, vector, start_point, player):
    if board[start_point[0]][start_point[1]] != player:
        return (-1, -1)

    isEnemyBetween = False
    isMoveValid = False
    i = start_point[0]
    j = start_point[1]

    while True:
        i += vector[0]
        j += vector[1]
        if i not in range(len(board)) or j not in range(len(board)):
            break
        if board[i][j] == player:
            return (-1, -1)
        if board[i][j] == "." and isEnemyBetween:
            return (i, j)
        if board[i][j] == "." and not isEnemyBetween:
            return (-1, -1)
        else:
            isEnemyBetween = True

    return (-1, -1)

Posted on 18/05/2011, in Python. Bookmark the permalink. Dodaj komentarz.

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

%d bloggers like this: