def play_game(side): """ Given a side, black or white, play the game and make moves against an opponent based on states collected. """ while True: npstate, score_state = show_position( ) # Collect game states and scoring state restate = np.reshape(npstate, (-1, 50)) # Convert game state to the right size if VIEW: print() if side == me: """ SELECT AI PLAYER HERE: """ #move = random.choice(list(board)) # Using random - set a flag to choose player later... move = make_predict(restate) # Using Neuronet if VIEW: print("me:", move) # Print my move try: client.make_move(move) # Then attempt to place the pieces grid[me].add(move) # Place on the grid board.remove(move) # Remove from board of available spots except gthclient.MoveError as e: if e.cause == e.ILLEGAL: if VIEW: print("me: made illegal move, passing" ) # Made an illegal move client.make_move("pass") # No move made - so pass else: cont, move = client.get_move( ) # If it's opponent's turn - get their move if VIEW: print("opp:", move) if cont and move == "pass": if VIEW: print("me: pass to end game") client.make_move("pass") # Game end condition break else: if not cont: break # Game end condition board.remove(move) # Remove available board move grid[opp].add(move) # Place on grid side = gthclient.opponent(side) # Who's side is it? # Get the score for winner here: return calc_score(score_state)
#!/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"]:
# Bar Movshovich and Jordan Le # Artificial Intelligence #!/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) #client = gthclient.GthClient(me, "barton.cs.pdx.edu", 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()} # check if a piece is caputred. def check_coordinality(lib1, lib2, lib3, lib4): # all 4 coordinalities are caputred piece is caputred, return True. if(lib1==False and lib2==False and lib3==False and lib4==False):
Main function is where all the good stuff happens, 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()}