#!/usr/bin/python3 # Random-move Gothello player. import random import sys import gthclient me = sys.argv[1] opp = gthclient.opponent(me) client = gthclient.GthClient(me, "localhost", 0) def letter_range(letter): for i in range(5): yield chr(ord(letter) + i) board = {letter + digit for letter in letter_range('a') for digit in letter_range('1')} grid = {"white": set(), "black": set()} def show_position(): for digit in letter_range('1'): for letter in letter_range('a'): pos = letter + digit if pos in grid["white"]: piece = "O" elif pos in grid["black"]:
#!/usr/bin/python3 # Random-move Gothello player. import random import gthclient client = gthclient.GthClient("black", "localhost", 0) def letter_range(letter): for i in range(5): yield chr(ord(letter) + i) board = { letter + digit for letter in letter_range('a') for digit in letter_range('1') } grid = {"white": set(), "black": set()} def show_position(): for digit in letter_range('1'): for letter in letter_range('a'): pos = letter + digit if pos in grid["white"]: piece = "O" elif pos in grid["black"]:
# Minimax player implementation import gthclient from minimax import * import sys if len(sys.argv) != 5: print("usage: python3 player.py <color> <host_name> <host_number> <depth>") exit(0) color = sys.argv[1] host_name = sys.argv[2] host_number = sys.argv[3] depth = int(sys.argv[4]) client = gthclient.GthClient(color, host_name, int(host_number)) if 'black' in color: ma = Minimax(depth, 1) else: ma = Minimax(depth, 0) s = State() # actual game sim = State() # simulation of actual, checking the rules i = 1 while True: if 'black' in color: move = ma.choose_action(s, sim)[1] print("{}. me:{}".format(i, move)) if move != 'pass': try:
play x amount of games against bart's computer opponent to see how well each approach does. New players can be used via the move variable in the play_game function. """ wins = 0 # Keep track of my wins losses = 0 # Keep track of my losses draws = 0 # Keep track of draws total_games = 100 # Play x games and see how I do i = 0 # Just a counter while i < total_games: # Loop for specified amount of games i += 1 try: opp = gthclient.opponent(me) # Set opponent client = gthclient.GthClient(me, "barton.cs.pdx.edu", 0) # Connect to server (Bart's) #client = gthclient.GthClient(me, "localhost", 0) side = "black" # Set up available board spots board = { letter + digit for letter in letter_range('a') for digit in letter_range('1') } # Set the grid grid = {"white": set(), "black": set()} win, loss, draw = play_game( side) # Check if I won, loss, or tied a game
if not self.make_player_move(): break else: if not self.get_opp_move(): break if self.client.who == "white": if not self.make_player_move(): break else: if not self.get_opp_move(): break if self.client.winner == "black": print("black win") if self.client.winner == "white": print("white win") def usage(): print("usage: python3 gthplayer.py color server_name server_number depth") exit(0) if len(sys.argv) != 5: usage() my_color = sys.argv[1] server_name = sys.argv[2] server_number = int(sys.argv[3]) depth = int(sys.argv[4]) client = gthclient.GthClient(my_color, server_name, server_number) p = Player(depth, client) p.play()
def main(): sides = ["black", "white"] eval_methods = ["number", "eye"] parser = argparse.ArgumentParser(description='gothello') parser.add_argument('--side', '-s', type=str, choices=sides, default=sides[0], help="choose a side to play") parser.add_argument('--depth', '-d', type=int, default=4, help="depth limitation for minimax search \ , not applied to iter deepening") parser.add_argument('--evaluate', '-e', type=str, choices=eval_methods, default=eval_methods[0], help="choose a static evaluate function: (1)\ \"number\" counts number of stones on each side; (2)\ \"eye\" counts number of eyes and stones on each side" ) parser.add_argument('--stonescore', '-S', type=int, default=1, help="assign a score for stone") parser.add_argument('--blackeyescore', '-b', type=int, default=1, help="assign a score for black eye") parser.add_argument('--whiteeyescore', '-w', type=int, default=1, help="assign a score for white eye") parser.add_argument('--iterdeepening', '-i', action='store_true', help="enable iterative deepening \ default maximum number of states \ to visit is 1000") parser.add_argument('--moveselection', '-M', action='store_true', help="select move with the largest liberties \ when multiple moves with same evalutated\ score encountered") parser.add_argument('--maxnstate', '-m', type=int, default=1000, help="assign a number for maximum number \ of states to visit in iterative deepening") parser.add_argument('--stats', action='store_true', help="enable printing states info") args = parser.parse_args() side = args.side depth = args.depth eval_function = args.evaluate scoring = { 'stone': args.stonescore, 'black eye': args.blackeyescore, 'white eye': args.whiteeyescore } iterdeepening = args.iterdeepening maximum_visit = args.maxnstate move_selection = args.moveselection print_stats = args.stats client = gthclient.GthClient(side, "localhost", 0) method = AlphaBetaPruning(side, depth=depth, iterdeepening=iterdeepening, maximum_visited=maximum_visit, eval_method=eval_function, scoring=scoring, move_selection=move_selection, print_stats=print_stats) game = Gothelo(method, client, side=side) game.play() game.client.closeall()