예제 #1
0
파일: proj10.py 프로젝트: pactonal/cse231
def get_all_moves(board, color, is_sorted=False):
    """
    Write something about this function here.
    """
    moves = []
    piece = Piece(color)
    pieceK = Piece(color, True)
    for r in range(8):
        for c in range(8):
            if not board.is_free(r, c):
                if str(board.get(r, c)) is str(piece):
                    valid = tools.get_moves(board, r, c, True)
                    for move in valid:
                        tup = (deindexify(r, c), move)
                        moves.append(tup)
                elif str(board.get(r, c)) is str(pieceK):
                    valid = tools.get_moves(board, r, c, True)
                    for move in valid:
                        tup = (deindexify(r, c), move)
                        moves.append(tup)

    if is_sorted:
        moves.sort()

    return moves
예제 #2
0
파일: tests.py 프로젝트: travcunn/checkers
    def test_possible_moves_and_king(self):
        p = Piece(Checkers.colors['red'])
        self.assertTrue(p.is_king is False)
        self.assertTrue(p.possible_moves == [(1, 1), (-1, 1)])

        p.king()
        self.assertTrue(p.is_king)
        all_moves = [(1, 1), (1, -1), (-1, 1), (-1, -1)]

        self.assertTrue(p.possible_moves == all_moves)

        p = Piece(Checkers.colors['black'])
        self.assertTrue(p.is_king is False)
        self.assertTrue(p.possible_moves == [(-1, -1), (1, -1)])
예제 #3
0
def initialize(board):
    """
    This function puts white and black pieces according to the checkers
    game positions. The black pieces will be on the top three rows and
    the white pieces will be on the bottom three rows (for an 8x8 board).
    The first row for the black pieces will be placed as a2, a4, a6, ...
    etc. and the next rows will be b1, b3, b5, ... etc. For the white
    rows, the placement patterns will be opposite of those of blacks.
    This must work for any even length board size.
    """
    row = col = board.get_length()
    initrows = (row // 2) - 1
    for r in range(row - 1, row - (initrows + 1), -1):
        for c in range(0 if r % 2 == 1 else 1, col, 2):
            board.place(r, c, Piece('white'))
    for r in range(0, initrows):
        for c in range(0 if r % 2 == 1 else 1, col, 2):
            board.place(r, c, Piece())
예제 #4
0
파일: proj10.py 프로젝트: pactonal/cse231
def count_pieces(board):
    """
    Write something about this function here.
    """
    wp = Piece('white')
    bp = Piece()
    wk = Piece('white', True)
    bk = Piece(is_king=True)
    white = 0
    black = 0
    for r in range(8):
        for c in range(8):
            if not board.is_free(r, c):
                if (str(board.get(r, c)) is str(wp)
                        or str(board.get(r, c)) is str(wk)):
                    white += 1
                elif (str(board.get(r, c)) is str(bp)
                      or str(board.get(r, c)) is str(bk)):
                    black += 1

    return (black, white)
예제 #5
0
파일: proj10.py 프로젝트: pactonal/cse231
def get_all_captures(board, color, is_sorted=False):
    """
    Write something about this function here.
    """
    captures = []
    piece = Piece(color)
    pieceK = Piece(color, True)
    for r in range(8):
        for c in range(8):
            if not board.is_free(r, c):
                if str(board.get(r, c)) is str(piece):
                    valid = tools.get_captures(board, r, c, True)
                    for lst in valid:
                        captures.append(lst)
                elif str(board.get(r, c)) is str(pieceK):
                    valid = tools.get_captures(board, r, c, True)
                    for lst in valid:
                        captures.append(lst)
    if is_sorted:
        sort_captures(captures, True)
    return captures
예제 #6
0
def board_initial():
    """
    An example initial board.
    """
    Piece.symbols = ['b', 'w']
    Piece.symbols_king = ['B', 'W']
    board = Board()
    cells = eval("[[None, 'b', None, 'b', None, 'b', None, 'b'], " + \
        "['b', None, 'b', None, 'b', None, 'b', None], " + \
        "[None, 'b', None, 'b', None, 'b', None, 'b'], " + \
        "[None, None, None, None, None, None, None, None], " + \
        "[None, None, None, None, None, None, None, None], " + \
        "['w', None, 'w', None, 'w', None, 'w', None], " + \
        "[None, 'w', None, 'w', None, 'w', None, 'w'], " + \
        "['w', None, 'w', None, 'w', None, 'w', None]]")
    for row, lst in enumerate(cells):
        for col, item in enumerate(lst):
            if item is not None:
                if item == 'w':
                    board.place(row, col, Piece('white'))
                else:
                    board.place(row, col, Piece())
    return board
예제 #7
0
def initialize(board, b):
    """
    This function puts white and black pieces according to the checkers
    game positions. The black pieces will be on the top three rows and
    the white pieces will be on the bottom three rows (for an 8x8 board).
    The first row for the black pieces will be placed as a2, a4, a6, ...
    etc. and the next rows will be b1, b3, b5, ... etc. For the white
    rows, the placement patterns will be opposite of those of blacks.
    This must work for any even length board size.
    """

    row = col = board.get_length()
    initrows = (row // 2) - 1
    for r in range(0, row):
        for c in range(0, col):
            if b[r][c] == 'w':
                board.place(r, c, Piece('white'))
            if b[r][c] == 'b':
                board.place(r, c, Piece('black'))
            if b[r][c] == 'W':
                board.place(r, c, Piece('white', True))
            if b[r][c] == 'B':
                board.place(r, c, Piece('black', True))
예제 #8
0
파일: proj10.py 프로젝트: pactonal/cse231
def apply_capture(board, capture_path):
    """
    Write something about this function here.

    Raise this exception below:
        raise RuntimeError("Invalid jump/capture, please type" \
                         + " \'hints\' to get suggestions.")
    If,
        a. there is no jump found from any position in capture_path, i.e. use
            tools.get_jumps() function to get all the jumps from a certain
            position in capture_path
        b. the destination position from a jump is not in the jumps list found
            from tools.get_jumps() function.
    """
    wp = Piece('white')
    bp = Piece()
    wk = Piece('white', True)
    bk = Piece('black', True)
    loc = indexify(capture_path[0])
    if str(board.get(loc[0], loc[1])) is str(wp):
        for idx in range(0, len(capture_path) - 1):
            oldloc = indexify(capture_path[idx])
            if capture_path[idx + 1] in tools.get_jumps(
                    board, oldloc[0], oldloc[1]):
                newloc = indexify(capture_path[idx + 1])
                caploc = ((oldloc[0] + newloc[0]) // 2,
                          (oldloc[1] + newloc[1]) // 2)

                if newloc[0] == 0:
                    board.place(newloc[0], newloc[1], Piece('white', True))
                    board.remove(oldloc[0], oldloc[1])
                    board.remove(caploc[0], caploc[1])
                else:
                    board.place(newloc[0], newloc[1], Piece('white'))
                    board.remove(oldloc[0], oldloc[1])
                    board.remove(caploc[0], caploc[1])

    elif str(board.get(loc[0], loc[1])) is str(bp):
        for idx in range(0, len(capture_path) - 1):
            oldloc = indexify(capture_path[idx])
            if capture_path[idx + 1] in tools.get_jumps(
                    board, oldloc[0], oldloc[1]):
                newloc = indexify(capture_path[idx + 1])
                caploc = ((oldloc[0] + newloc[0]) // 2,
                          (oldloc[1] + newloc[1]) // 2)

                if newloc[0] == 7:
                    board.place(newloc[0], newloc[1], Piece(is_king=True))
                    board.remove(oldloc[0], oldloc[1])
                    board.remove(caploc[0], caploc[1])
                else:
                    board.place(newloc[0], newloc[1], Piece())
                    board.remove(oldloc[0], oldloc[1])
                    board.remove(caploc[0], caploc[1])

    elif str(board.get(loc[0], loc[1])) is str(wk):
        for idx in range(0, len(capture_path) - 1):
            oldloc = indexify(capture_path[idx])
            if capture_path[idx + 1] in tools.get_jumps(
                    board, oldloc[0], oldloc[1]):
                newloc = indexify(capture_path[idx + 1])
                caploc = ((oldloc[0] + newloc[0]) // 2,
                          (oldloc[1] + newloc[1]) // 2)

                board.place(newloc[0], newloc[1], Piece('white', True))
                board.remove(oldloc[0], oldloc[1])
                board.remove(caploc[0], caploc[1])

    elif str(board.get(loc[0], loc[1])) is str(bk):
        for idx in range(0, len(capture_path) - 1):
            oldloc = indexify(capture_path[idx])
            if capture_path[idx + 1] in tools.get_jumps(
                    board, oldloc[0], oldloc[1]):
                newloc = indexify(capture_path[idx + 1])
                caploc = ((oldloc[0] + newloc[0]) // 2,
                          (oldloc[1] + newloc[1]) // 2)

                board.place(newloc[0], newloc[1], Piece(is_king=True))
                board.remove(oldloc[0], oldloc[1])
                board.remove(caploc[0], caploc[1])

    else:
        raise RuntimeError(jump_error)
예제 #9
0
파일: proj10.py 프로젝트: pactonal/cse231
def apply_move(board, move):
    """
    Write something about this function here.

    Raise this exception below:
        raise RuntimeError("Invalid move, please type" \
                         + " \'hints\' to get suggestions.")
    If,
        a. there is no move from move[0], i.e. use tools.get_moves() function
           to get all the moves from move[0]
        b. the destination position move[1] is not in the moves list found
            from tools.get_moves() function.
    """
    wp = Piece('white')
    bp = Piece()
    wk = Piece('white', True)
    bk = Piece('black', True)
    loc = indexify(move[0])
    if str(board.get(loc[0], loc[1])) is str(wp):
        if move not in get_all_moves(board, 'white', True):
            raise RuntimeError(move_error)
        else:
            oldloc = loc
            newloc = indexify(move[1])
            if newloc[0] == 0:
                board.place(newloc[0], newloc[1], Piece('white', True))
                board.remove(oldloc[0], oldloc[1])
            else:
                board.place(newloc[0], newloc[1], Piece('white'))
                board.remove(oldloc[0], oldloc[1])
            board.remove(oldloc[0], oldloc[1])

    elif str(board.get(loc[0], loc[1])) is str(bp):
        if move not in get_all_moves(board, 'black', True):
            raise RuntimeError(move_error)
        else:
            oldloc = loc
            newloc = indexify(move[1])
            if newloc[0] == 7:
                board.place(newloc[0], newloc[1], Piece(is_king=True))
                board.remove(oldloc[0], oldloc[1])
            else:
                board.place(newloc[0], newloc[1], Piece())
                board.remove(oldloc[0], oldloc[1])

    elif str(board.get(loc[0], loc[1])) is str(wk):
        if move not in get_all_moves(board, 'white', True):
            raise RuntimeError(move_error)
        else:
            oldloc = loc
            newloc = indexify(move[1])
            board.place(newloc[0], newloc[1], Piece('white', True))
            board.remove(oldloc[0], oldloc[1])

    elif str(board.get(loc[0], loc[1])) is str(bk):
        if move not in get_all_moves(board, 'black', True):
            raise RuntimeError(move_error)
        else:
            oldloc = loc
            newloc = indexify(move[1])
            board.place(newloc[0], newloc[1], Piece(is_king=True))
            board.remove(oldloc[0], oldloc[1])
    else:
        raise RuntimeError(move_error)
예제 #10
0
def board_capture_whites():
    """
    A board where a black captures a series of white.
    """
    Piece.symbols = ['b', 'w']
    Piece.symbols_king = ['B', 'W']
    board = Board()
    board.place(7, 0, Piece(is_king=True))
    board.place(1, 6, Piece())
    board.place(2, 1, Piece('white'))
    board.place(2, 3, Piece('white'))
    board.place(2, 5, Piece('white'))
    board.place(4, 1, Piece('white'))
    board.place(4, 3, Piece('white'))
    board.place(4, 5, Piece('white'))
    board.place(6, 1, Piece('white'))
    board.place(6, 3, Piece('white'))
    board.place(6, 5, Piece('white'))
    return board
예제 #11
0
def board_figure1():
    """
    The board from Figure 1.
    """
    Piece.symbols = ['b', 'w']
    Piece.symbols_king = ['B', 'W']
    board = Board()
    board.place(0, 7, Piece('white', is_king=True))
    board.place(1, 2, Piece('white', is_king=True))
    board.place(1, 4, Piece('white', is_king=True))
    board.place(3, 0, Piece('white'))
    board.place(3, 2, Piece('white'))
    board.place(4, 7, Piece('white'))
    board.place(5, 0, Piece('white'))
    board.place(5, 2, Piece('white'))
    board.place(6, 3, Piece('white'))
    board.place(6, 5, Piece('white'))
    board.place(6, 7, Piece('white'))
    board.place(7, 0, Piece('white'))
    board.place(2, 5, Piece())
    board.place(2, 7, Piece())
    board.place(3, 6, Piece())
    board.place(4, 3, Piece())
    board.place(4, 5, Piece())
    return board
예제 #12
0
def board_capture_transition():
    board = Board()
    board.place(1, 0, Piece())
    board.place(1, 2, Piece())
    board.place(1, 4, Piece())
    board.place(1, 6, Piece())
    board.place(2, 1, Piece())
    board.place(2, 5, Piece())
    board.place(3, 0, Piece('white'))
    board.place(3, 2, Piece())
    board.place(3, 6, Piece())
    board.place(4, 1, Piece('white'))
    board.place(4, 7, Piece('white'))
    board.place(5, 0, Piece('white'))
    board.place(5, 2, Piece('white'))
    board.place(5, 6, Piece())
    board.place(6, 1, Piece('white'))
    board.place(6, 3, Piece())
    board.place(7, 4, Piece('black', True))
    board.place(7, 6, Piece('black', True))
    return board
예제 #13
0
def board_capture_blacks2():
    """
    A board where a white captures a series of blacks.
    Version 2
    """
    Piece.symbols = ['b', 'w']
    Piece.symbols_king = ['B', 'W']
    board = Board()
    board.place(7, 0, Piece('white'))
    board.place(7, 6, Piece('white'))
    board.place(1, 6, Piece('white', is_king=True))
    board.place(1, 0, Piece('white', is_king=True))
    board.place(2, 1, Piece())
    board.place(2, 3, Piece())
    board.place(2, 5, Piece())
    board.place(4, 1, Piece())
    board.place(4, 3, Piece())
    board.place(4, 5, Piece())
    board.place(6, 1, Piece())
    board.place(6, 3, Piece())
    board.place(6, 5, Piece())
    return board
예제 #14
0
#!/usr/bin/env python3
from checkers import Piece
import examples, tools
from proj10 import apply_move, indexify

Piece.symbols = ['b', 'w']
Piece.symbols_king = ['B', 'W']

board = examples.board_figure1()
board.place(1, 2, Piece('white'))
board.place(6, 1, Piece('black'))
print("Figure 1 board:")
board.display()

errmsg = "Invalid move, please type 'hints' to get suggestions."

#row,col = indexify(moves[0][0])
#moves = tools.get_moves(board, row, col)
#print("MOVES:",moves)
print("trying white pawn to king-row move: ('b3','a2')")
apply_move(board, ('b3', 'a2'))
row, col = indexify('a2')
piece = board.get(row, col)
assert piece.is_king()
print("Success: white pawn made into king\n")

print("trying black pawn to king-row move: ('g2','h3')")
apply_move(board, ('g2', 'h3'))
row, col = indexify('h3')
piece = board.get(row, col)
assert piece.is_king()