Exemplo n.º 1
0
def connectAndPlay(modelFrom, modelTo, labelFrom, labelTo, playerName,
                   playerColor, port, championship, lock, baseline_player,
                   folder, enemy_name):
    # playerColor: B or W

    if playerColor == "W":
        # indicates the index that has to be 1 in the FromMove input array
        # white not possible moves removed: a4, a5, a6, b5, d1, e1, f1, e2, i4, i5, i6, h5, d9, e9, f9, e8
        dictFromMoves = {
            'a1': 0,
            'b1': 1,
            'c1': 2,
            'g1': 3,
            'h1': 4,
            'i1': 5,
            'a2': 6,
            'b2': 7,
            'c2': 8,
            'd2': 9,
            'f2': 10,
            'g2': 11,
            'h2': 12,
            'i2': 13,
            'a3': 14,
            'b3': 15,
            'c3': 16,
            'd3': 17,
            'e3': 18,
            'f3': 19,
            'g3': 20,
            'h3': 21,
            'i3': 22,
            'b4': 23,
            'c4': 24,
            'd4': 25,
            'e4': 26,
            'f4': 27,
            'g4': 28,
            'h4': 29,
            'c5': 30,
            'd5': 31,
            'e5': 32,
            'f5': 33,
            'g5': 34,
            'b6': 35,
            'c6': 36,
            'd6': 37,
            'e6': 38,
            'f6': 39,
            'g6': 40,
            'h6': 41,
            'a7': 42,
            'b7': 43,
            'c7': 44,
            'd7': 45,
            'e7': 46,
            'f7': 47,
            'g7': 48,
            'h7': 49,
            'i7': 50,
            'a8': 51,
            'b8': 52,
            'c8': 53,
            'd8': 54,
            'f8': 55,
            'g8': 56,
            'h8': 57,
            'i8': 58,
            'a9': 59,
            'b9': 60,
            'c9': 61,
            'g9': 62,
            'h9': 63,
            'i9': 64
        }
    else:
        # indicates the index that has to be 1 in the FromMove input array
        # black not possible moves removed: e5
        dictFromMoves = {
            'a1': 0,
            'b1': 1,
            'c1': 2,
            'd1': 3,
            'e1': 4,
            'f1': 5,
            'g1': 6,
            'h1': 7,
            'i1': 8,
            'a2': 9,
            'b2': 10,
            'c2': 11,
            'd2': 12,
            'e2': 13,
            'f2': 14,
            'g2': 15,
            'h2': 16,
            'i2': 17,
            'a3': 18,
            'b3': 19,
            'c3': 20,
            'd3': 21,
            'e3': 22,
            'f3': 23,
            'g3': 24,
            'h3': 25,
            'i3': 26,
            'a4': 27,
            'b4': 28,
            'c4': 29,
            'd4': 30,
            'e4': 31,
            'f4': 32,
            'g4': 33,
            'h4': 34,
            'i4': 35,
            'a5': 36,
            'b5': 37,
            'c5': 38,
            'd5': 39,
            'f5': 40,
            'g5': 41,
            'h5': 42,
            'i5': 43,
            'a6': 44,
            'b6': 45,
            'c6': 46,
            'd6': 47,
            'e6': 48,
            'f6': 49,
            'g6': 50,
            'h6': 51,
            'i6': 52,
            'a7': 53,
            'b7': 54,
            'c7': 55,
            'd7': 56,
            'e7': 57,
            'f7': 58,
            'g7': 59,
            'h7': 60,
            'i7': 61,
            'a8': 62,
            'b8': 63,
            'c8': 64,
            'd8': 65,
            'e8': 66,
            'f8': 67,
            'g8': 68,
            'h8': 69,
            'i8': 70,
            'a9': 71,
            'b9': 72,
            'c9': 73,
            'd9': 74,
            'e9': 75,
            'f9': 76,
            'g9': 77,
            'h9': 78,
            'i9': 79
        }

    # connecting to the Tablut server on localhost
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect(("localhost", port))
        # print("[INFO] Connected")

        writePlayerName(sock, playerName)
        # print("[INFO] Sent player name: " + playerName)

        # initial board state
        boardInfo = readBoardInfo(sock)

        total_move = 0
        all_move = []

        while True:

            if (boardInfo["turn"] == "WHITE"
                    and playerColor == "W") or (boardInfo["turn"] == "BLACK"
                                                and playerColor == "B"):
                # player choose move
                # added 'turn' parameter: false white, true black
                turn = (boardInfo["turn"] == "BLACK")

                if not baseline_player:
                    move = generateMove(modelFrom, modelTo, labelFrom, labelTo,
                                        boardInfo, dictFromMoves, turn, lock)
                else:
                    print_debug = False
                    move = generateBaselineMove(boardInfo["board"], turn,
                                                boardInfo["turn"], print_debug)

                total_move += 1
                if baseline_player:
                    all_move.append(move)

                writeMove(sock, move)

                # player read own move
                boardInfo = readBoardInfo(sock)

                if isGameFinished(boardInfo):
                    break

            # player read enemy move
            boardInfo = readBoardInfo(sock)

            if isGameFinished(boardInfo):
                break

        if baseline_player and getPoints(boardInfo["turn"],
                                         playerColor) in [0, -1]:
            if playerColor == "W":
                name = folder + "BLACK__" + str(
                    numpy.random.randint(
                        0, sys.maxsize)) + "__" + str(enemy_name) + ".txt"
            else:
                name = folder + "WHITE__" + str(
                    numpy.random.randint(
                        0, sys.maxsize)) + "__" + str(enemy_name) + ".txt"
            with open(name, "w") as reportFile:
                reportFile.write("Baseline has lost with:\n" + str(all_move))

        championship.calculate(playerName, playerColor == "W",
                               getPoints(boardInfo["turn"], playerColor),
                               total_move)
Exemplo n.º 2
0
        while True:

            if (boardInfo["turn"] == "WHITE"
                    and player == "W") or (boardInfo["turn"] == "BLACK"
                                           and player == "B"):
                # player choose move
                if not baseline_player:
                    move = generateMove(modelFrom, modelTo, labelFrom, labelTo,
                                        boardInfo, dictFromMoves,
                                        boardInfo["turn"] == "BLACK",
                                        threading.Lock())
                else:
                    print_debug = True
                    move = generateBaselineMove(boardInfo["board"],
                                                boardInfo["turn"] == "BLACK",
                                                boardInfo["turn"], print_debug)

                writeMove(sock, move)

                # player read own move
                boardInfo = readBoardInfo(sock)
                input("PRESS ENTER FOR A MOVE !")
                if isGameFinished(boardInfo):
                    break

            # player read enemy move
            boardInfo = readBoardInfo(sock)

            if isGameFinished(boardInfo):
                break