def good_start(self, color): board2D = GoBoardUtil.get_twoD_board(self.board) move = None mid = board2D[3][3] op = 3 - color if mid == 0: move = coord_to_point(4, 4, 7) elif (board2D[2][3] == op or board2D[3][4] == op) and board2D[2][4] == 0: move = coord_to_point(3, 5, 7) elif (board2D[4][3] == op or board2D[3][2] == op) and board2D[4][2] == 0: move = coord_to_point(5, 3, 7) elif (board2D[2][2] == op or board2D[4][4] == op) and board2D[4][2] == 0: move = coord_to_point(5, 3, 7) elif (board2D[2][4] == op or board2D[4][2] == op) and board2D[2][2] == 0: move = coord_to_point(3, 3, 7) else: if board2D[2][4] == 0: move = coord_to_point(3, 5, 7) elif board2D[4][2] == 0: move = coord_to_point(5, 3, 7) elif board2D[2][2] == 0: move = coord_to_point(3, 3, 7) elif board2D[4][4] == 0: move = coord_to_point(5, 5, 7) return move
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: if (args[0] not in ['b', 'w', 'B', 'W']): self.error('illegal move: "{}" wrong color'.format(args[0])) return board_color = args[0].lower() board_move = args[1].lower() color = color_to_int(board_color) if board_move[0].isalpha() and board_move[1:].isdigit(): coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error('illegal move: "{}" wrong coordinate'.format( board_move)) return else: self.error( 'illegal move: "{}" wrong coordinate'.format(board_move)) return if not self.board.play_move(move, color): self.error('illegal move: "{}" occupied'.format(board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def genmove_cmd(self, args): """ Modify this function for Assignment 1 """ """ generate a move for color args[0] in {'b','w'} """ board_color = args[0].lower() color = color_to_int(board_color) if self.gogui_rules_final_result_cmd(args) == "unknown": # move = self.go_engine.get_move(self.board, color) legal_moves = self.gogui_rules_legal_moves_cmd(args) length = len(legal_moves) if length > 0: rand_index = random.randint(0, length - 1) self.respond(legal_moves) move = legal_moves[rand_index] # move_coord = point_to_coord(move, self.board.size) # move_as_string = format_point(move_coord) if move: # if self.board.is_legal(move, color): coord = move_to_coord(move, self.board.size) board_coord = coord_to_point(coord[0], coord[1], self.board.size) #self.board.play_move(move, color) #self.board.board self.board.board[board_coord] = color #self.respond(move_as_string) # else: # self.respond("Illegal move: {}".format(move_as_string)) else: pass elif self.gogui_rules_final_result_cmd(args) == "draw": self.respond("pass") elif self.gogui_rules_final_result_cmd(args) != board_color: self.respond("resign")
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0],coord[1], self.board.size) else: self.error("Error executing move {} converted from {}" .format(move, args[1])) return if not self.board.play_move(move, color): self.respond("Illegal Move: {}".format(board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def evaluate_board(board: SimpleGoBoard): init_score_cache(board) # current_player = board.current_player # opponent_player = GoBoardUtil.opponent(current_player) for row in range(1, board.size + 1): for col in range(1, board.size + 1): p = _constrained_index_2d(board, row, col) m = coord_to_point(row, col, board.size) if p == EMPTY: board.board[m] = WHITE score_color[WHITE][row][col] = evaluate_point_dir( board, row, col, WHITE, None) board.board[m] = BLACK score_color[BLACK][row][col] = evaluate_point_dir( board, row, col, BLACK, None) board.board[m] = EMPTY elif p == WHITE: score_color[WHITE][row][col] = evaluate_point_dir( board, row, col, WHITE, None) score_color[BLACK][row][col] = 0 elif p == BLACK: score_color[WHITE][row][col] = 0 score_color[BLACK][row][col] = evaluate_point_dir( board, row, col, BLACK, None) return
def play_cmd(self, args): """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] if board_color != "b" and board_color !="w": self.respond("illegal move: \"{}\" wrong color".format(board_color)) return color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0],coord[1], self.board.size) else: self.error("Error executing move {} converted from {}" .format(move, args[1])) return if not self.board.play_move_gomoku(move, color): self.respond("illegal move: \"{}\" occupied".format(board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) self.toPlay = switchToPlay(args[0].lower()) self.respond() except Exception as e: self.respond('{}'.format(str(e)))
def play_cmd(self, args): """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) if args[1].lower() == "pass": self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.respond("unknown: {}".format(args[1])) return if not self.board.play_move(move, color): self.respond("illegal move: \"{}\" occupied".format( args[1].lower())) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() except Exception as e: self.respond("illegal move: {}".format(str(e).replace('\'', '')))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ # self.respond("error .....") try: board_color = args[0].lower() # check for color if self.has_play_color_error(board_color, args): return board_move = args[1] color = color_to_int(board_color) #check for coordinate coord = self.has_play_coordinate_error(args) # coord = move_to_coord(args, self.board.size) if not coord: self.respond("illegal Move: {}".format("\"" + args[1] + "\" wrong coordinate")) return else: move = coord_to_point(coord[0],coord[1], self.board.size) #check for occupied v = self.board.play_move_gomoku(move, color) if not v: self.respond("illegal Move: {}".format("\"" + args[1] + "\" occupied")) return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def index_cmd(self, args): """ Print the index for a given move args[0] """ coord = move_to_coord(args[0], self.board.size) move = coord_to_point(coord[0], coord[1], self.board.size) self.respond(self.WU.getindex(self.board, move))
def genmove_cmd(self, args): """ Generate a move for the color args[0] in {'b', 'w'}, for the game of gomoku. """ board_color = args[0].lower() color = color_to_int(board_color) game_end, winner = self.board.check_game_end_gomoku() if game_end or len(self.legalMoves()) == 0: if winner == color: self.respond("pass") elif len(self.legalMoves()) == 0: self.respond("pass") else: self.respond("resign") return move_type, pending_moves = self.policy_moves() try: signal.alarm(int(self.timelimit)) move = self.go_engine.genmove(pending_moves, self.board, color) signal.alarm(0) except Exception as e: move = PASS if move == PASS: self.respond("pass") return coord = move_to_coord(move, self.board.size) point = coord_to_point(coord[0], coord[1], self.board.size) if self.board.is_legal_gomoku(point, color): self.board.play_move_gomoku(point, color) self.respond(move)
def play_cmd(self, args): """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] if board_color != "b" and board_color != "w": self.respond( "illegal move: \"{}\" wrong color".format(board_color)) return color = color_to_int(board_color) if args[1].lower() == 'pass': self.respond("illegal move: \"{} {}\" wrong coordinate".format( args[0], args[1])) return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error("Error executing move {} converted from {}".format( move, args[1])) return if not self.board.play_move(move, color): self.respond("illegal move: \"{} {}\" ".format( args[0], board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() except Exception as e: self.respond('illegal move: \"{} {}\" {}'.format( args[0], args[1], str(e)))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) #Test if b or w was entered. if board_color != 'b' and board_color != 'w': self.respond( "illegal move: \"{}\" wrong color".format(board_color)) return #Test if the color entered is the color to play. # if color != self.board.current_player: # self.respond( # "illegal move. \"{}\" wrong color".format(board_color) # ) # return #Test if move is on board. if (ord(board_move.lower()[0]) - ord('a') ) > self.board.size or int(board_move[1:]) > self.board.size: self.respond("illegal move: \"{}\" wrong coordinate".format( board_move).lower()) return if color == 3: self.respond("illegal move: \"{}\" wrong coordinate".format( args[1].lower())) return coord = move_to_coord(args[1], self.board.size) move = coord_to_point(coord[0], coord[1], self.board.size) #Code for passing # if args[1].lower() == "pass": # self.board.play_move(PASS, color) # self.board.current_player = GoBoardUtil.opponent(color) # self.respond() # return #Test if coordinate is occupied. if self.board.board[move] != EMPTY: self.respond("illegal move: \"{}\" occupied".format( board_move.lower())) return else: self.board.board[move] = color self.board.current_player = GoBoardUtil.opponent(color) self.respond() return except Exception as e: self.respond("Error: {}".format(str(e)))
def genmove_cmd(self, args): """ Generate a move for the color args[0] in {'b', 'w'}, for the game of gomoku. """ board_color = args[0].lower() color = color_to_int(board_color) game_end, winner = self.board.check_game_end_gomoku() ########### find_winner = ALPHABETA #### (Need Solver method) if game_end: if winner == color: self.respond("pass") else: self.respond("resign") return else: ############## get the possible winning moves in the minimax solver################ (Need solver method) solveAnswer = solve_in(self) returnSA = solveAnswer.split(" ") if returnSA[0] == 'b' or returnSA[0] == 'w': if returnSA[0] != args[0].lower(): #toplay is losing so play a random move move = GoBoardUtil.generate_random_move_gomoku( self.board ) # generate random move if we cannot get it on time move = self.go_engine.get_move(self.board, color) else: #play a move on the track to a bol_return coord = move_to_coord(returnSA[1], self.board.size) move = coord_to_point(coord[0], coord[1], self.board.size) else: #unknown or draw state so play a random move move = GoBoardUtil.generate_random_move_gomoku( self.board ) # generate random move if we cannot get it on time move = self.go_engine.get_move(self.board, color) move_coord = point_to_coord(move, self.board.size) move_as_string = format_point(move_coord) if self.board.is_legal_gomoku(move, color): self.board.play_move_gomoku(move, color) self.toPlay = switchToPlay(args[0].lower()) self.respond(move_as_string) else: move = self.go_engine.get_move(self.board, color) if move == PASS: self.respond("pass") return move_coord = point_to_coord(move, self.board.size) move_as_string = format_point(move_coord) if self.board.is_legal_gomoku(move, color): self.board.play_move_gomoku(move, color) self.respond(move_as_string) else: self.respond("illegal move: {}".format(move_as_string))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: if (args[0] not in ['b', 'w', 'B', 'W']): self.error('illegal move: "{}" wrong color'.format(args[0])) return board_color = args[0].lower() board_move = args[1].lower() color = color_to_int(board_color) if board_move[0].isalpha() and board_move[1:].isdigit(): if not 2 <= self.board.size <= MAXSIZE: raise ValueError("board_size out of range") try: col_c = board_move[0] if (not "a" <= col_c <= "z") or col_c == "i": self.error( 'illegal move: "{}" wrong coordinate'.format( board_move)) return col = ord(col_c) - ord("a") if col_c < "i": col += 1 row = int(board_move[1:]) if row < 1: self.error( 'illegal move: "{}" wrong coordinate'.format( board_move)) return except (IndexError): self.error('illegal move: "{}" wrong coordinate'.format( board_move)) return if not (col <= self.board.size and row <= self.board.size): self.error('illegal move: "{}" wrong coordinate'.format( board_move)) return coord = (row, col) else: self.error( 'illegal move: "{}" wrong coordinate'.format(board_move)) return move = coord_to_point(coord[0], coord[1], self.board.size) if move not in self.board.get_empty_points(): self.error('illegal move: "{}" occupied'.format(board_move)) return else: self.board.board[move] = color self.current_player = GoBoardUtil.opponent(color) self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1].lower() try: color = color_to_int(board_color) except: reason = "wrong color" self.respond("illegal move: {} {}".format( '"' + board_color + '"', reason)) return if color != WHITE and color != BLACK: reason = "wrong color" self.respond("illegal move: {} {}".format( '"' + board_color + '"', reason)) return # if color != self.board.current_player: # reason = "wrong color" # self.respond("illegal move: {} {}".format(board_color,reason)) # return # if args[1].lower() == "pass": # self.board.play_move(PASS, color) # self.board.current_player = GoBoardUtil.opponent(color) # self.respond() # return coord = move_to_coord(args[1], self.board.size) if coord is not False: move = coord_to_point(coord[0], coord[1], self.board.size) else: reason = "wrong coordinate" self.respond("illegal move: {} {}".format( '"' + board_move + '"', reason)) return if self.board.board[move] != EMPTY: reason = "occupied" self.respond("illegal move: {} {}".format( '"' + board_move + '"', reason)) return # if not self.board.play_move(move, color): # self.respond("Illegal Move: {}".format(board_move)) # return # else: # self.debug_msg( # "Move: {}\nBoard:\n{}\n".format(board_move, self.board2d()) # ) self.board.board[move] = color self.respond() except Exception: return
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) if (color_to_int(board_color) != self.board.current_player): self.error("Illegal Move: wrong color") return if args[1].lower() == 'pass': # passing is illigal self.error("Illegal Move: wrong coordinate") '''self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() ''' return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error("Error executing move {} converted from {}".format( move, args[1])) return legal, reason = self.board.play_move(move, color) if (legal == False): if reason == "occupied": self.error("Illegal Move: The point is occupied.") return elif reason == "capture": self.error("Illegal Move: Capturing is not allowed.") return elif reason == "suicide": self.error("Illegal Move: Suicide is not allowed") return else: self.error("Illegal Move: unknown") else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) """ if not self.board.play_move(move, color): self.respond("Illegal Move: {}".format(board_move)) return """ self.board.current_player = GoBoardUtil.opponent(color) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def find_block_anchors(board, limit): """ Find all blocks with liberty less or equal to limit on the board. Anchors is the smallest point in a block. Return a list of anchors and corresponding liberties Not efficient. One could maintain all blocks and anchors on the board, and update it when a move is played. """ anchors = [] liberty_dic = {} anchor_dic = {} for x in range(1, board.size + 1): for y in range(1, board.size + 1): point = coord_to_point(x, y, board.size) color = board.get_color(point) if point in anchor_dic: continue if color != WHITE and color != BLACK: continue liberty = 0 the_libs = [] group_points = [point] block_points = [point] min_index = point while group_points: current_point = group_points.pop() neighbors = board._neighbors(current_point) for n in neighbors: if n not in block_points: if board.get_color(n) == BORDER: continue if board.get_color(n) == color: group_points.append(n) block_points.append(n) if n < min_index: min_index = n elif board.get_color(n) == EMPTY: if n not in the_libs: the_libs.append(n) liberty += 1 if liberty > limit: break for p in block_points: anchor_dic[p] = min_index if liberty <= limit: anchors.append(min_index) liberty_dic[min_index] = the_libs return anchors, liberty_dic
def update_dir(board: SimpleGoBoard, row, col, direction): current = _constrained_index_2d(board, row, col) m = coord_to_point(row, col, board.size) if current == EMPTY: board.board[m] = WHITE score_color[WHITE][row][col] = evaluate_point_dir( board, row, col, WHITE, direction) board.board[m] = BLACK score_color[BLACK][row][col] = evaluate_point_dir( board, row, col, BLACK, direction) board.board[m] = EMPTY elif current == BLACK or current == WHITE: oppponent = GoBoardUtil.opponent(current) score_color[current][row][col] = evaluate_point_dir( board, row, col, current, direction) score_color[oppponent][row][col] = 0 return
def policy_moves_cmd(self, args): if self.go_engine.random_simulation: #get legal moves first_moves = GoBoardUtil.generate_legal_moves( self.board, self.board.current_player) moves = [] #convert points to formated for move in first_moves: #convert to coords temp = point_to_coord(move, self.board.size) #format version temp = format_point(temp) moves.append(temp) moves.sort() #reverse it new_moves = [] column_letters = "ABCDEFGHJKLMNOPQRSTUVWXYZ" #put format to point for move in moves: temp = str(column_letters.find(move[0]) + 1) temp = coord_to_point(int(move[1]), int(temp), self.board.size) new_moves.append((temp)) movelist = new_moves p = [] for x in range(0, len(movelist)): p.append(round(float(1) / float(len(movelist)), 3)) else: movelist, p = PatternUtil.pattern(self.board) result = '' for move in movelist: temp = point_to_coord(move, self.board.size) temp = format_point(temp) result += temp.lower() + " " for i in p: result += str(i) + " " try: if args[0] == "1": return movelist, p except: self.respond(result)
def score(self, komi): """ Score """ black_score = 0 white_score = komi counted = [] for x in range(1, self.size + 1): for y in range(1, self.size + 1): point = coord_to_point(x, y, self.size) if point in counted: continue color = self.get_color(point) assert color != BORDER if color == BLACK: black_score += 1 continue if color == WHITE: white_score += 1 continue empty_block = where1d(self.connected_component(point)) black_flag = False white_flag = False for p in empty_block: counted.append(p) p_neighbors = self._neighbors(p) found_black = self.board[p_neighbors] == BLACK found_white = self.board[p_neighbors] == WHITE if found_black.any(): black_flag = True if found_white.any(): white_flag = True if black_flag and white_flag: break if black_flag and not white_flag: black_score += len(empty_block) if white_flag and not black_flag: white_score += len(empty_block) if black_score > white_score: return BLACK, black_score - white_score if white_score > black_score: return WHITE, white_score - black_score if black_score == white_score: return None, 0
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] if board_color != 'b' and board_color != 'w': # wrong color self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " wrong color") color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) #self.board.current_player = GoBoardUtil.opponent(color) self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " wrong coordinate") return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0],coord[1], self.board.size) else: # wrong coordinate self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " wrong coordinate") #self.error("Error executing move {} converted from {}" # .format(move, args[1])) return if not self.board.play_move(move, color): neighbors = self.board.neighbors(move) for nb in neighbors: if self.board.board[nb] == GoBoardUtil.opponent(color): if not self.board.detect_capture(nb): # detect capture self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " capture") self.board.board[move] = EMPTY return if self.board.board[move] != EMPTY: # detect occupied self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " occupied") return self.respond("illegal move: \"{}\"".format(board_color + ' ' + board_move) + " suicide") # detect suicide #self.respond("Illegal Move: {}".format(board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def get_move_score_based(self, color): score_c = self.Score(color) score_p = self.Score(3 - color) #self.respond(score_c) score_c = self.Sort(score_c, 1) score_p = self.Sort(score_p, 2) x_P, y_P, max_P = self.Evaluate(score_p) x_C, y_C, max_C = self.Evaluate(score_c) if max_P > max_C and max_C < 1000000: #self.respond("1") row = x_P col = y_P else: row = x_C col = y_C move = coord_to_point(row + 1, col + 1, 7) return move
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] if not board_color == 'w' and not board_color == 'b': self.respond('illegal move: "' + board_color + '" wrong color') return color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error("Error executing move {} converted from {}".format( move, args[1])) return if not self.board.play_move(move, color): self.respond('illegal move: "{}" occupied'.format( board_move.lower())) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) #check to see if the move filled the board check = self.go_engine.get_move(self.board, color) check_coord = point_to_coord(check, self.board.size) check_as_string = format_point(check_coord) if not self.game_status == "black" or not self.game_status == "white": if check == False: self.game_status = "draw" #check to see if move played was a win if self.board.search_for_five(move, color_to_int(board_color)): self.game_status = board_color self.respond() except Exception as e: self.respond('{}'.format(str(e)))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] assert color_to_int_gomoku(board_color), "wc" color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0],coord[1], self.board.size) else: self.error("Error executing move {} converted from {}" .format(move, args[1])) return ''' if not self.board.play_move_gomoku(move, color): self.respond("Illegal Move: \"{}\" wrong color".format(board_move)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) if self.board.play_move_gomoku(move,color) == "wrong color": self.respond("Illegal Move: \"{}\" wrong color returned".format(board_color)) if self.board.play_move_gomoku(move,color) == "occupied": self.respond("Illegal Move: \"{}\" occupied returned".format(board_move))''' self.board.play_move_gomoku(move,color) self.respond() except Exception as e: if str(e) == "occupied": self.respond('illegal move: \"{}\" occupied'.format(args[1].lower())) return if str(e) == "wc": self.respond('illegal move: \"{}\" wrong color'.format(args[0].lower())) return else: self.respond('illegal move: \"{}\" wrong coordinate'.format(args[1].lower()))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) # First check is move is a pass (illegal) if args[1].lower() == 'pass': self.error("illegal move: {} {} pass".format( board_color, board_move)) return # Next check if move is the wrong color if not self.board.current_player == color: self.error("illegal move: {} {} wrong color".format( board_color, board_move)) return # Next validate coordinate try: coord = move_to_coord(args[1], self.board.size) except ValueError: self.error("illegal move: {} {} wrong coordinate".format( board_color, board_move)) return if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error("illegal move: {} {} wrong coordinate".format( board_color, board_move)) return # Attempt to play tryplay = self.board.play_move(move, color) if not tryplay[0]: self.respond("illegal move: {} {} {}".format( board_color, board_move, tryplay[1].name)) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def play_cmd(self, args): try: if self.__wrongColorErr(args): self.respond('illegal move: "{} {}" wrong color'.format( args[0], args[1])) return board_color = args[0].lower() colorAsInt = color_to_int(board_color) #no passing as passing is illegal coords = self.__wrongCoordErr(args) #self.__wrongCoordErr returns True if there is a coord error and the coords otherwise if coords == True: self.respond('illegal move: "{} {}" wrong coordinate'.format( args[0], args[1])) return else: move = coord_to_point(coords[0], coords[1], self.board.size) if self.__occupiedErr(move): self.respond('illegal move: "{} {}" occupied'.format( args[0], args[1])) return if self.__captureErr(move, colorAsInt): self.respond('illegal move: "{} {}" capture'.format( args[0], args[1])) return #unsure here if i should be doing an additional check for suicide to specify the error #pretty sure that the only remaining error should be suicide. if not self.board.play_move(move, colorAsInt): self.respond('illegal move: "{} {}" suicide'.format( args[0], args[1])) return self.respond() except Exception as e: self.respond("Error: {}".format(str(e)))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() board_move = args[1] if board_color not in ['b', 'w']: self.respond( 'illegal move: "{}" wrong color'.format(board_color)) return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.respond('illegal move: "{}" wrong coordinate'.format( self.board.coord(move))) return color = color_to_int(board_color) if args[1].lower() == "pass": self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return if not self.board.play_move(move, color): self.respond('illegal move: "{}" occupied'.format( self.board.coord(move))) return else: self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) self.respond() self.board.current_player = GoBoardUtil.opponent(color) self.update_result(color, move) except Exception: self.respond('illegal move: "{}" wrong coordinate'.format( board_move.lower()))
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ #print(args) # ['B/W', "position"] try: board_color = args[0].lower() board_move = args[1] color = color_to_int(board_color) if args[1].lower() == 'pass': self.board.play_move(PASS, color) self.board.current_player = GoBoardUtil.opponent(color) self.respond() return coord = move_to_coord(args[1], self.board.size) if coord: move = coord_to_point(coord[0], coord[1], self.board.size) else: self.error("Error executing move {} converted from {}".format( move, args[1])) return #do not exist coord!!! # one more evaluation, if one side wins, then play should not continue!!! if self.colorMax['w'] >= 5 or self.colorMax['b'] >= 5: self.respond("One side is win game over!") return # Once executed uptill here, it means , all above conditions passed, # and we should check if that space is empty or not # if yes, then place the chess on that space, otherwise not! if not self.board.play_move(move, color): self.respond("Illegal Move: {}".format(board_move)) return # repeated play in that coord!!! else: #has been successfully executed 'self.board.play_move' self.debug_msg("Move: {}\nBoard:\n{}\n".format( board_move, self.board2d())) # insert the information into dictionary self.insertKeyIntoDict(args[0], coord, move) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))
def _reach_direction_helper(self, point, dir): """ dir: the index offset from point indicating the direction (example: 1 = right, -1 = left) (example: boardsize = down, -boardsize = up) returns the number of stones of colour in direction dir from point """ count = 0 colour = self.get_color(point) coord = point_to_coord(point, self.size) nextpt = add_coord(coord, dir) while 1 <= nextpt[0] <= self.size and 1 <= nextpt[1] <= self.size: if self.get_color(coord_to_point(nextpt[0], nextpt[1], self.size)) != colour: break count += 1 nextpt = add_coord(nextpt, dir) return count
def play_cmd(self, args): """ Modify this function for Assignment 1 """ """ play a move args[1] for given color args[0] in {'b','w'} """ try: board_color = args[0].lower() if board_color != 'b' and board_color != 'w': self.respond('illegal move: \"{}\" wrong color'.format(board_color)) return board_move = args[1] color = color_to_int(board_color) # if args[1].lower() == 'pass': # self.board.play_move(PASS, color) # self.board.current_player = GoBoardUtil.opponent(color) # self.respond() # return coord = move_to_coord(args[1], self.board.size) if coord == WRONGCOORD: self.respond("illegal move: \"{}\" wrong coordinate".format(board_move.lower())) return else: move = coord_to_point(coord[0],coord[1], self.board.size) # else: # self.error("Error executing move {} converted from {}" # .format(move, args[1])) # return if not self.board.play_move(move, color): if self.board.board[move] != EMPTY: self.respond("illegal move: \"{}\" occupied".format(board_move.lower())) return self.respond("illegal move: game is over") return else: self.debug_msg("Move: {}\nBoard:\n{}\n". format(board_move, self.board2d())) self.respond() except Exception as e: self.respond('Error: {}'.format(str(e)))