示例#1
0
def externmodulecall(setting_path="../settings.config"):
    global root, configreader
    root = tkinter.Tk()
    configreader = ConfigReader(setting_path)
    MainScreen(root,
               GameBoard(configreader.retrieve("BOARDSIZE_X"),
                         configreader.retrieve("BOARDSIZE_Y")),
               configreader)  # in settings.config
    root.mainloop()
示例#2
0
class InputHandler:
    def __init__(self, cmdargv, configpath):
        self.AI = None
        self.GameBoard = None
        self.Turn = "player"
        self.Config = ConfigReader(configpath)
        self.MCTSMode = True if self.Config.retrieve(
            "AIMODE") == "MCTS" else False
        self.BoardSize_X = self.Config.retrieve("BOARDSIZE_X")
        self.BoardSize_Y = self.Config.retrieve("BOARDSIZE_Y")
        self.Difficulty = self.Config.retrieve("DIFFICULTY")
        self.TileSearchRange = self.Config.retrieve("SEARCHRANGE")
        self.MCTSTimeLimit = self.Config.retrieve("TIMELIMIT")

        self.Use_XTA = True if self.Config.retrieve(
            "USE_EXTENSIVE_ANALYSIS") == "1" else False
        self.XTA_Coefficient = self.Config.retrieve("EA_COEFFICIENT")

        self.cmdargv = vars(cmdargv)
        self.DebugMode = self.cmdargv["debug"]
        self.MCTSMode = self.cmdargv["mcts"]
        self.HideConsole = self.cmdargv["noconsole"]
        self.HideGUI = self.cmdargv["nogui"]
        self.RunRemote = self.cmdargv["remote"]
        if self.cmdargv["stdcomm"]:
            self.IOMethod = "STDIO"
        elif self.cmdargv["silent"]:
            self.IOMethod = "SOCKET"
            self.socket_addr = self.cmdargv["socket"][0]
            self.socket_port = self.cmdargv["socket"][1]

        self.runlistener()

    def runlistener(self):
        while True:
            inputcmd = input()
            if inputcmd == "help":
                print(helpmsg)
            elif inputcmd == "exit":
                if self.AI:
                    if self.RunRemote:
                        self.AI.killprocess()
                sys.exit(0)
            elif inputcmd == "start":
                self.GameBoard = GameBoard(self.BoardSize_X, self.BoardSize_Y)
                if self.AI and self.RunRemote:
                    self.AI.killprocess()
                if not self.MCTSMode:
                    self.AI = AlphaBeta(self.GameBoard, "white",
                                        self.Difficulty, self.TileSearchRange,
                                        self.Use_XTA, self.XTA_Coefficient,
                                        self.RunRemote)
                elif self.MCTSMode:
                    self.AI = MCTS(self.GameBoard, "white", self.MCTSTimeLimit,
                                   self.RunRemote)
                if self.RunRemote:
                    self.AI.start()
            elif inputcmd == "cpuplay":
                if self.GameBoard:
                    if self.Turn == "cpu":
                        if not self.RunRemote:
                            self.AI.start()
                        self.AI.choosemove()
                        while True:
                            result = self.AI.getresult()
                            if result:
                                movedata, hashdata = result[0], result[1]
                                break
                        self.GameBoard.addstone(movedata[1], "white")
                        print(movedata[1][0], movedata[1][1])
                        sys.stdout.flush()
                        self.Turn = "player"
                    else:
                        print("Invalid turn")
                else:
                    print("No game started")
            elif inputcmd == "stoneinfo":
                if self.GameBoard:
                    bs = []
                    ws = []
                    for x in self.GameBoard.BlackStones:
                        bs.append(str(x))
                    for x in self.GameBoard.WhiteStones:
                        ws.append(str(x))
                    print("".join(bs))
                    print("".join(ws))
                else:
                    print("No game started")
            elif "placestone" in inputcmd or "ps" in inputcmd:
                if len(inputcmd.split()) != 3:
                    print("Missing parameters")
                elif self.Turn == "cpu":
                    print("Invalid Turn")
                elif not self.GameBoard:
                    print("Game does not exist")
                else:
                    x, y = inputcmd.split()[1], inputcmd.split()[2]
                    if x.isdigit() and y.isdigit():
                        if int(x) <= self.BoardSize_X and int(
                                y) <= self.BoardSize_Y and int(x) > 0 and int(
                                    y) > 0:
                            self.GameBoard.addstone((int(x), int(y)), "black")
                            self.Turn = "cpu"
                        else:
                            print("Position out of bounds")
                    else:
                        print("Invalid parameters")
            elif "boardsize" in inputcmd:
                if len(inputcmd.split()) != 3:
                    print("Missing parameters")
                else:
                    x, y = inputcmd.split()[1], inputcmd.split()[2]
                    if x.isdigit() and y.isdigit():
                        self.BoardSize_X = int(x)
                        self.BoardSize_Y = int(y)
                    else:
                        print("Invalid parameters")
            else:
                print('Unknown command. Type "help" for help')