Beispiel #1
0
def set_depth():
    config_file = open("go-player.config", "r")
    depth = config_file.readlines()
    depth_info = list(stream(depth))[0]
    print(depth_info)
    global n
    n = depth_info["depth"]
def get_socket_address():
    config_file = open("go.config", "r")
    socket_info = config_file.readlines()
    socket_info = list(stream(socket_info))[0]
    port = socket_info["port"]
    ip = socket_info["IP"]
    return (ip, port)
Beispiel #3
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries player
    :return: list of json objects
    """
    name = "Micah"
    set_depth()
    output = []
    proxy = proxy_remote_player(black, name)
    server_response = proxy.client("WITNESS ME")
    if len(server_response) < 1:
        proxy.client("done")
        return
    try:
        lst = list(stream(server_response))[0]  # parse json objects
    except:
        output.append(crazy)
    finally:
        for query in lst:
            result = proxy.player.query(query)
            if result and not isinstance(result, bool):
                output.append(result)
    proxy.client(json.dumps(output))
    proxy.client("done")
Beispiel #4
0
def read_input_from_file():
    file_contents = ""  # read in all json objects to a string
    file_contents_so_far = ""
    special_json = sys.stdin.readline()
    while special_json:
        file_contents += special_json
        special_json = sys.stdin.readline()
        decoded = ""
        # try to decode into json as we go
        # because if something later breaks the json formatting
        # we still want to be able to run all prior valid json
        try:
            decoded = list(stream(file_contents))
        except:
            continue
        if len(decoded) > 0:
            file_contents_so_far = list(stream(file_contents))
    try:
        return list(stream(file_contents))  # parse json objects
    except:
        if len(file_contents_so_far) > 0:
            return file_contents_so_far
        return [crazy]
Beispiel #5
0
def main():
    """
    Test Driver reads 10 json objects from stdin
    Uses the streamy library to parse
    Returns sorted list to stdout
    :return: list of json objects
    """
    file_contents = ""  # read in all 10 json objects to a string
    special_json = sys.stdin.readline()
    while special_json:
        file_contents += special_json
        special_json = sys.stdin.readline()
    lst = list(stream(file_contents))  # parse json objects
    lst = backend().sort(lst)
    print(json.dumps(lst))
Beispiel #6
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries game board
    :return: list of json objects
    """
    output = []
    file_contents = ""  # read in all json objects to a string
    special_json = sys.stdin.readline()
    while special_json:
        file_contents += special_json
        special_json = sys.stdin.readline()
    lst = list(stream(file_contents))  # parse json objects
    for query in lst:
        output.append(board(query[0]).query(query[1]))
    print(json.dumps(output))
Beispiel #7
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries player
    :return: list of json objects
    """
    # create server (simulate referee)
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_address = player.get_socket_address()
    sock.bind(server_address)
    sock.settimeout(60)
    sock.listen(1)
    output = ""
    client_done_flag = False
    while not client_done_flag:
        connection, client_address = sock.accept()
        try:
            # Receive the data in small chunks and collect it
            while True:
                data = connection.recv(64)
                if data:
                    data = data.decode()
                    output += data
                else:
                    break
                if data == "done":
                    connection.sendall("done".encode())
                    client_done_flag = True
                    break
                if data == "WITNESS ME":
                    lst = read_input_from_file()
                    connection.sendall(json.dumps(lst).encode())
                    output = ""
                    break
        finally:
            # Clean up the connection
            connection.close()
    # done shouldn't be part of the game-play output, it is just a client-server acknowledgement
    output = output.replace("done", "")
    output = list(stream(output))
    output = output[0]
    print(json.dumps(output))
Beispiel #8
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries player
    :return: list of json objects
    """
    file_contents = ""  # read in all json objects to a string
    special_json = sys.stdin.readline()
    while special_json:
        file_contents += special_json
        special_json = sys.stdin.readline()
    lst = list(stream(file_contents))  # parse json objects
    # assuming input is correctly formatting,
    # the second item in the second input obj should contain the stone
    player1 = player(black, lst[0])
    player2 = player(white, lst[1])
    ref = referee(player1, player2)
    for query in lst[2:]:
        result = ref.handle_move(query)
        if not result:
            break
    print(json.dumps(ref.game_output))
Beispiel #9
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries player
    :return: list of json objects
    """
    output = []
    file_contents = ""  # read in all json objects to a string
    special_json = sys.stdin.readline()
    while special_json:
        file_contents += special_json
        special_json = sys.stdin.readline()
    lst = list(stream(file_contents))  # parse json objects
    # assuming input is correctly formatting,
    # the second item in the second input obj should contain the stone
    stone = lst[1][1]
    curr_player = player(stone)
    for query in lst:
        result = curr_player.query(query)
        if result:
            output.append(result)
    print(json.dumps(output))
Beispiel #10
0
def main():
    """
    Test Driver reads json objects from stdin
    Uses the streamy library to parse
    Queries player
    :return: list of json objects
    """
    name = "Micah"
    set_depth()
    output = []
    proxy = proxy_remote_player(black, name)
    proxy.player.register_flag = True
    proxy.player.receive_flag = True
    server_response = proxy.client("WITNESS ME")
    while server_response and len(server_response) > 1:
        server_response = str(server_response)
        try:
            boards = list(stream(server_response))[0]  # parse json objects
            play = proxy.player.make_a_move_random_maybe_illegal(boards)
            server_response = proxy.client(play)
        except:
            return
    return
Beispiel #11
0
import sys
import socket
import json
from streamy import stream
from board import make_point, board, get_board_length, make_empty_board, parse_point
from referee import referee

# configuration
config_file = open("go.config", "r")
info = list(stream(config_file.readlines()))[0]
default_player_file_path = info["default-player"]
player_pkg = __import__(default_player_file_path)
from player_pkg import proxy_remote_player, player
default_player = player

maxIntersection = get_board_length()
empty = " "
black = "B"
white = "W"
n = 1
crazy = "GO has gone crazy!"
history = "This history makes no sense!"
empty_board = make_empty_board()
recv_size = 64


def read_input_from_file():
    file_contents = ""  # read in all json objects to a string
    file_contents_so_far = ""
    special_json = sys.stdin.readline()
    while special_json:
Beispiel #12
0
 def stream_fails(self, s):
     try:
         list(streamy.stream(s))
     except ValueError:
         return
     raise Exception
Beispiel #13
0
 def assert_stream(self, s, *expected):
     self.assertEqual(list(streamy.stream(s)), list(expected))
Beispiel #14
0
 def test_chunk_size(self):
     for i in range(16):
         with open('test_file.txt') as fp:
             result = list(streamy.stream(fp, chunk_size=1 + i))
             self.assertEqual(result, FILE_EXPECTED)
Beispiel #15
0
 def stream_fails(self, s):
     with self.assertRaises(ValueError):
         list(streamy.stream(s))
Beispiel #16
0
def split(file_contents):
    # Decode input into a list of special JSON objects
    lst = list(stream(file_contents))
    # Split list into sub-lists of length 10
    return populate(lst)