Пример #1
0
def initialPosition() -> ChessBoard:
    rearRow = [
        PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen,
        PieceType.King, PieceType.Bishop, PieceType.Knight, PieceType.Rook
    ]
    whiteRearRow = list(map(lambda typ: Just(Piece(Color.WHITE, typ)),
                            rearRow))
    whiteFrontRow = [
        Just(Piece(Color.WHITE, PieceType.Pawn)) for i in range(0, 8)
    ]
    emptyRow = [Nothing for i in range(0, 8)]

    blackFrontRow = [
        Just(Piece(Color.BLACK, PieceType.Pawn)) for i in range(0, 8)
    ]
    blackRearRow = list(map(lambda typ: Just(Piece(Color.BLACK, typ)),
                            rearRow))

    cells = whiteRearRow + whiteFrontRow + \
            concat([emptyRow for i in range(0, 4)]) + \
            blackFrontRow + blackRearRow

    return ChessBoard(toVector=lambda: cells,
                      highlights=lambda: [Nothing for i in range(0, 64)],
                      nextPlayer=lambda: Color.WHITE)
Пример #2
0
 def piece():
     return {
         "rook": Just(PieceType.Rook),
         "queen": Just(PieceType.Queen),
         "bishop": Just(PieceType.Bishop),
         "knight": Just(PieceType.Knight)
     }.get(args[1], Nothing)
Пример #3
0
    def test_returns_valid_positions_for_Bishop_at_given_position(self):
        cb = boardForTesting([
            piece(4, 4, Color.BLACK, PieceType.Bishop),
            piece(0, 0, Color.WHITE, PieceType.Pawn),
            piece(2, 2, Color.WHITE, PieceType.Rook)
        ])

        print(showChessBoard(showPossibleMoves(Just((4, 4)), cb)))
Пример #4
0
def fromString(posStr: str) -> Maybe[Position]:
    match = re.match("([abcdefgh])(\d)", posStr)
    if not match:
        return Nothing
    (f, r) = (match[0][0], match[0][1])
    isValid = (f in charRangeInclusive('a', 'h')) and int(r) in range(1, 9)
    if isValid:
        return Just((int(r) - 1, ord(f) - 97))
    return Nothing
Пример #5
0
def game(msg: Maybe[str], cb: ChessBoard):
    if msg == Nothing:
        clearScreen()
        print(showChessBoard(cb))
    else:
        print(msg)

    print(f"input {'WHITE' if cb.nextPlayer() == Color.WHITE else 'BLACK'} > ",
          end="")

    [msg, cb_] = parseInput(fromNullable(input())).flat_map(executeCommand(cb))\
                                                           .either(lambda l: [Just(" ".join(l)), cb],
                                                                   lambda r: [Nothing, r])

    game(msg, cb_)
Пример #6
0
def executeCommand(cb: ChessBoard,
                   cmd: GameCommands) -> Either[List[str], ChessBoard]:
    return when(
        cmd,
        {
            # This is a hack! Python doesn't have switch statements or any elegant form of case matching
            is_(Pick):
            lambda: Right(showPossibleMoves(Just(cmd.position), cb)),
            is_(Move):
            lambda: validateAndMakeMove(cmd, cb),
            is_(Promotion):
            Left("Unimplemented"),
            _:
            Right(cb)
        })
Пример #7
0
    def test_returns_valid_positions_for_Rook_at_given_position(self):
        cb = boardForTesting([
            piece(2, 0, Color.WHITE, PieceType.Rook),
            piece(0, 0, Color.WHITE, PieceType.Pawn),
            piece(3, 0, Color.WHITE, PieceType.Rook)
        ])

        self.assertEqual(False, cellIsEmpty(cb, (0, 0)))
        self.assertEqual(False, cellIsEmpty(cb, (2, 0)))
        self.assertEqual(True, cellIsEmpty(cb, (1, 0)))
        self.assertEqual(False, diffColor(cb, (2, 0), (0, 0)))

        cb = boardForTesting([
            piece(0, 0, Color.WHITE, PieceType.Rook),
            piece(0, 1, Color.WHITE, PieceType.Knight),
            piece(1, 0, Color.BLACK, PieceType.Pawn),
            piece(1, 3, Color.WHITE, PieceType.Pawn)
        ])

        self.assertEqual(False, diffColor(cb, (0, 1), (1, 3)))
        self.maxDiff = None

        print(showChessBoard(showPossibleMoves(Just((0, 0)), cb)))
Пример #8
0
def boardForTesting(pieces: List[Tuple[Position, Piece]]):
    return ChessBoard(
        toVector=lambda: updatedList([Nothing for x in range(
            0, 64)], mapl(lambda pp: (toIndex(pp[0]), Just(pp[1])), pieces)),
        highlights=lambda: [Nothing for _ in range(0, 64)],
        nextPlayer=lambda: Color.WHITE)
Пример #9
0
 def highlightedCells(p: Position):
     cells = mapl(lambda x: toIndex(x), possiblePositionsToMove(cb, p))
     return [
         Just(BackgroundColor.Yellow) if x in cells else Nothing
         for x in range(0, 64)
     ]