def main(win, LOAD, moves=""):
    sound.play_start(LOAD)
    start(win)

    side, board, flags = convertMoves(moves)
    clock = pygame.time.Clock()
    sel = [0, 0]
    prevsel = [0, 0]

    while True:
        clock.tick(25)
        for event in pygame.event.get():
            if event.type == pygame.QUIT and prompt(win):
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 50 < x < 450 and 50 < y < 450:
                    x, y = x // 50, y // 50
                    if LOAD[1] and side:
                        x, y = 9 - x, 9 - y

                    if isOccupied(side, board, [x, y]):
                        sound.play_click(LOAD)

                    prevsel = sel
                    sel = [x, y]
                else:
                    sel = [0, 0]
                    if 350 < x < 500 and 460 < y < 490:
                        if prompt(win, saveGame(moves)):
                            return
                    elif 0 < x < 80 and 0 < y < 50 and LOAD[4]:
                        moves = undo(moves)
                        side, board, flags = convertMoves(moves)

        showScreen(win, side, board, flags, sel, LOAD)
        if isValidMove(side, board, flags, prevsel, sel):
            promote = getPromote(win, side, board, prevsel, sel)

            sound.play_drag(LOAD)
            animate(win, side, board, prevsel, sel, bool(LOAD[1] and side))
            sound.play_move(LOAD)

            side, board, flags = makeMove(side, board, prevsel, sel, flags,
                                          promote)
            moves += " " + encode(prevsel, sel, promote)
Exemple #2
0
    if stok[0] < x < sum(stok[::2]) and stok[1] < y < sum(stok[1::2]):
        win.blit(MAIN.STOCK_H, stok[:2])

    # Begin pygame event loop to catch all events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # User has clicked somewhere, determine which button and
            # call a function to handle the game into a different window.
            x, y = event.pos

            if sngl[0] < x < sum(sngl[::2]) and sngl[1] < y < sum(sngl[1::2]):
                sound.play_click(prefs)
                ret = menus.splayermenu(win)
                if ret == 0:
                    run = False
                elif ret != 1:
                    if ret[0]:
                        run = chess.mysingleplayer(win, ret[1], prefs)
                    else:
                        run = chess.singleplayer(win, ret[1], ret[2], prefs)

            elif mult[0] < x < sum(mult[::2]) and mult[1] < y < sum(
                    mult[1::2]):
                sound.play_click(prefs)
                ret = menus.timermenu(win, prefs)
                if ret == 0:
                    run = False
Exemple #3
0
        win.blit(MAIN.STOCK_H, stok[:2])

    # Begin pygame event loop to catch all events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # User has clicked somewhere, determine which button and
            # Call a function to handle the game into a different window.
            # All functions defined in the chess module open up the chess
            # board and a game begins based on the type of game
            x, y = event.pos

            if sngl[0] < x < sum(sngl[::2]) and sngl[1] < y < sum(sngl[1::2]):
                sound.play_click(LOAD)
                data = menus.splayermenu(win)
                if data is not None:
                    if data[0]:
                        chess.mysingleplayer(win, data[1], LOAD)
                    else:
                        chess.singleplayer(win, data[1], data[2], LOAD)

            elif mult[0] < x < sum(mult[::2]) and mult[1] < y < sum(mult[1::2]):
                chess.multiplayer(win, LOAD)

            elif onln[0] < x < sum(onln[::2]) and onln[1] < y < sum(onln[1::2]):
                sound.play_click(LOAD)
                chess.online(win, menus.onlinemenu(win), LOAD)

            elif load[0] < x < sum(load[::2]) and load[1] < y < sum(load[1::2]):
Exemple #4
0
def chess(win, sock, player, LOAD):
    sound.play_start(LOAD)
    start(win)

    side, board, flags = convertMoves("")

    clock = pygame.time.Clock()
    sel = [0, 0]
    prevsel = [0, 0]

    FLIP = LOAD[1] and player
    while True:
        clock.tick(25)
        for event in pygame.event.get():
            if event.type == pygame.QUIT and prompt(win):
                write(sock, "quit")
                return True

            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 50 < x < 450 and 50 < y < 450:
                    x, y = x // 50, y // 50
                    if FLIP:
                        x, y = 9 - x, 9 - y

                    if isOccupied(side, board, [x, y]):
                        sound.play_click(LOAD)

                    prevsel = sel
                    sel = [x, y]

                elif not (isEnd(side, board, flags)
                          or isEnd(flip(side), board, flags)):
                    if 0 < x < 70 and 0 < y < 50:
                        write(sock, "draw?")
                        if draw(win, sock):
                            return False

                    if 400 < x < 500 and 450 < y < 500:
                        write(sock, "resign")
                        return False

        showScreen(win, side, board, flags, sel, LOAD, player, True)
        if readable():
            msg = read()
            if msg == "close":
                return True

            elif msg == "quit" or msg == "resign":
                popup(win, msg)
                write(sock, "end")
                return False

            elif msg == "draw?":
                if draw(win):
                    write(sock, "draw")
                    while readable():
                        if read() == "close":
                            return True
                    return False

                else:
                    write(sock, "nodraw")

            elif side != player:
                fro, to, promote = decode(msg)
                if isValidMove(side, board, flags, fro, to):
                    sound.play_drag(LOAD)
                    animate(win, side, board, fro, to, FLIP)
                    sound.play_move(LOAD)

                    side, board, flags = makeMove(side, board, fro, to, flags,
                                                  promote)
                    sel = [0, 0]
                else:
                    write(sock, "quit")
                    return True

        if side == player and isValidMove(side, board, flags, prevsel, sel):
            promote = getPromote(win, player, board, prevsel, sel)
            write(sock, encode(prevsel, sel, promote))

            sound.play_drag(LOAD)
            animate(win, player, board, prevsel, sel, FLIP)
            sound.play_move(LOAD)

            side, board, flags = makeMove(side, board, prevsel, sel, flags,
                                          promote)
Exemple #5
0
def main(win, player, LOAD, moves=""):
    sound.play_start(LOAD)
    start(win)

    side, board, flags = convertMoves(moves)

    clock = pygame.time.Clock()
    sel = [0, 0]
    prevsel = [0, 0]

    FLIP = LOAD[1] and player

    while True:
        clock.tick(25)
        end = isEnd(side, board, flags)
        for event in pygame.event.get():
            if event.type == pygame.QUIT and prompt(win):
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 50 < x < 450 and 50 < y < 450:
                    x, y = x // 50, y // 50
                    if FLIP:
                        x, y = 9 - x, 9 - y

                    if isOccupied(side, board, [x, y]):
                        sound.play_click(LOAD)

                    prevsel = sel
                    sel = [x, y]
                elif side == player or end:
                    sel = [0, 0]
                    if 350 < x < 500 and 460 < y < 490:
                        if prompt(win, saveGame(moves, "mysingle", player)):
                            return
                    elif 0 < x < 80 and 0 < y < 50 and LOAD[4]:
                        moves = undo(moves,
                                     2) if side == player else undo(moves)
                        side, board, flags = convertMoves(moves)

        showScreen(win, side, board, flags, sel, LOAD, player)
        if side != player:
            if not end:
                fro, to = miniMax(side, board, flags)

                sound.play_drag(LOAD)
                animate(win, side, board, fro, to, FLIP)
                sound.play_move(LOAD)

                promote = getPromote(win, side, board, fro, to, True)
                side, board, flags = makeMove(side, board, fro, to, flags)

                moves += " " + encode(fro, to, promote)
                sel = [0, 0]

        elif isValidMove(side, board, flags, prevsel, sel):
            promote = getPromote(win, side, board, prevsel, sel)

            sound.play_drag(LOAD)
            animate(win, side, board, prevsel, sel, FLIP)
            sound.play_move(LOAD)

            side, board, flags = makeMove(side, board, prevsel, sel, flags,
                                          promote)
            moves += " " + encode(prevsel, sel, promote)
Exemple #6
0
def main(win, player, level, LOAD, moves=""):
    fish = StockFish(getSFpath(), level)

    if not fish.isActive():
        rmSFpath()
        return

    sound.play_start(LOAD)
    start(win)

    fish.startGame(moves)
    side, board, flags = convertMoves(moves)

    if player == 1 and not moves.strip():
        fish.startEngine()

    clock = pygame.time.Clock()
    sel = [0, 0]
    prevsel = [0, 0]

    FLIP = LOAD[1] and player

    while True:
        clock.tick(25)
        end = isEnd(side, board, flags)
        for event in pygame.event.get():
            if event.type == pygame.QUIT and prompt(win):
                fish.close()
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 50 < x < 450 and 50 < y < 450:
                    x, y = x // 50, y // 50
                    if FLIP:
                        x, y = 9 - x, 9 - y

                    if isOccupied(side, board, [x, y]):
                        sound.play_click(LOAD)

                    prevsel = sel
                    sel = [x, y]

                elif side == player or end:
                    sel = [0, 0]
                    if 350 < x < 500 and 460 < y < 490:
                        msg = saveGame(fish.moveSequence, "single", player,
                                       level)
                        if prompt(win, msg):
                            fish.close()
                            return
                    elif 0 < x < 80 and 0 < y < 50 and LOAD[4]:
                        if side == player:
                            fish.undo(2)
                        else:
                            fish.undo()
                        side, board, flags = convertMoves(fish.moveSequence)

        showScreen(win, side, board, flags, sel, LOAD, player)
        if side != player:
            if not end and fish.hasMoved():
                fro, to, promote = decode(fish.getMove())

                sound.play_drag(LOAD)
                animate(win, side, board, fro, to, FLIP)
                sound.play_move(LOAD)

                side, board, flags = makeMove(side, board, fro, to, flags,
                                              promote)
                sel = [0, 0]

        elif isValidMove(side, board, flags, prevsel, sel):
            promote = getPromote(win, side, board, prevsel, sel)

            sound.play_drag(LOAD)
            animate(win, side, board, prevsel, sel, FLIP)
            sound.play_move(LOAD)

            side, board, flags = makeMove(side, board, prevsel, sel, flags,
                                          promote)
            fish.makeMove(encode(prevsel, sel, promote))