# ──────────────────────────────────────────────
# NAJPROSTSZE SZACHY W PYTHONIE – DZIAŁAJĄCE
# bez bibliotek, z prostym botem
# ──────────────────────────────────────────────

import random

# plansza 8x8
board = [
    list("rnbqkbnr"),
    list("pppppppp"),
    list("........"),
    list("........"),
    list("........"),
    list("........"),
    list("PPPPPPPP"),
    list("RNBQKBNR")
]

def print_board():
    for r in board:
        print(" ".join(r))
    print()

# sprawdzanie, czy ruch jest w granicach
def in_bounds(r, c):
    return 0 <= r < 8 and 0 <= c < 8

# zwraca listę wszystkich możliwych ruchów danej strony (B/W)
def get_all_moves(white=True):
    moves = []
    pieces = "PRNBQK" if white else "prnbqk"

    directions_pawn = [(-1,0), (-1,1), (-1,-1)] if white else [(1,0), (1,1), (1,-1)]

    for r in range(8):
        for c in range(8):
            piece = board[r][c]
            if piece not in pieces:
                continue

            if piece.upper() == "P":
                for dr, dc in directions_pawn:
                    nr, nc = r + dr, c + dc
                    if not in_bounds(nr, nc): 
                        continue
                    if dc == 0:  # ruch do przodu
                        if board[nr][nc] == ".":
                            moves.append(((r, c), (nr, nc)))
                    else:  # bicie
                        if board[nr][nc] != "." and white != board[nr][nc].isupper():
                            moves.append(((r, c), (nr, nc)))

            # bardzo uproszczone ruchy wieży/hetmana
            if piece.upper() in "RQ":
                for dr, dc in [(1,0),(-1,0),(0,1),(0,-1)]:
                    nr, nc = r + dr, c + dc
                    while in_bounds(nr, nc):
                        if board[nr][nc] == ".":
                            moves.append(((r,c),(nr,nc)))
                        else:
                            if white != board[nr][nc].isupper():
                                moves.append(((r,c),(nr,nc)))
                            break
                        nr += dr
                        nc += dc

            # uproszczone ruchy gońca/hetmana
            if piece.upper() in "BQ":
                for dr, dc in [(1,1),(1,-1),(-1,1),(-1,-1)]:
                    nr, nc = r + dr, c + dc
                    while in_bounds(nr, nc):
                        if board[nr][nc] == ".":
                            moves.append(((r,c),(nr,nc)))
                        else:
                            if white != board[nr][nc].isupper():
                                moves.append(((r,c),(nr,nc)))
                            break
                        nr += dr
                        nc += dc

            # uproszczony skoczek
            if piece.upper() == "N":
                for dr, dc in [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]:
                    nr, nc = r + dr, c + dc
                    if in_bounds(nr,nc):
                        if board[nr][nc] == "." or (white != board[nr][nc].isupper()):
                            moves.append(((r,c),(nr,nc)))

            # uproszczony król
            if piece.upper() == "K":
                for dr in [-1,0,1]:
                    for dc in [-1,0,1]:
                        if dr == 0 and dc == 0: 
                            continue
                        nr, nc = r + dr, c + dc
                        if in_bounds(nr,nc):
                            if board[nr][nc] == "." or (white != board[nr][nc].isupper()):
                                moves.append(((r,c),(nr,nc)))

    return moves

def make_move(move):
    (r1, c1), (r2, c2) = move
    board[r2][c2] = board[r1][c1]
    board[r1][c1] = "."

print("SZACHY — grasz białymi. Ruch np. e2e4")

print_board()

turn_white = True

while True:
    if turn_white:
        move_str = input("Twój ruch: ").strip()
        if len(move_str) != 4:
            print("Zły format!")
            continue

        c1 = ord(move_str[0]) - ord('a')
        r1 = 8 - int(move_str[1])
        c2 = ord(move_str[2]) - ord('a')
        r2 = 8 - int(move_str[3])

        moves = get_all_moves(True)
        chosen = None
        for m in moves:
            if m == ((r1, c1), (r2, c2)):
                chosen = m
                break

        if chosen is None:
            print("Niedozwolony ruch!")
            continue

        make_move(chosen)
        print_board()
        turn_white = False

    else:
        print("Bot myśli...")
        moves = get_all_moves(False)
        if not moves:
            print("Bot nie ma ruchu — wygrana!")
            break
        m = random.choice(moves)
        make_move(m)
        print("Bot gra:", m)
        print_board()
        turn_white = True
