def play(initialPlayer, player1, player2): # game = games.TicTacToe(h=3,v=3,k=3) game = games.ConnectFour() game.initial.to_move = initialPlayer state = game.initial player = initialPlayer while True: print "Jugador a mover:", game.to_move(state) game.display(state) if player == player1.chip: move = player1.move(game, state) state = game.make_move(move, state) player = player2.chip else: move = player2.move(game, state) state = game.make_move(move, state) player = player1.chip print "\n-------------------" if game.terminal_test(state): game.display(state) print "\r\n GAME OVER" \ "\r\n Winner:", \ if_(state.utility, if_(player == 'O', 'X', 'O'), if_(not state.moves, '-', if_(player == 'O', 'X', 'O'))) + "\r\n" break
def human_vs_computer(): player_start = True while True: player_start = raw_input("Do you want to start? [y/n]: ") if player_start == 'y': player_start = True break elif player_start == 'n': player_start = False break print("Please answer with 'y' or 'n'") computer_chip = 'O' player_chip = 'X' while True: player_chip = raw_input("Do you want to be X or O ? : ") if player_chip == 'O': computer_chip = 'X' break elif player_chip == 'X': break print("Please enter 'X' or 'O'") difficulty = 1 while True: print("1. Regalado\r\n2. Easy\r\n3. Medium\r\n4. I don't want to win [And you won't]\r\n5. Custom") try: difficulty = int(raw_input("Select AI difficulty: ")) if difficulty > 0 and difficulty < 6: break print("Invalid difficulty") except ValueError: print("Please enter a number [1-5]\r\n") player = Human(player_chip) computer = Computer(computer_chip, difficulty - 1) play(if_(player_start, player_chip, computer_chip), player, computer)
def make_move(self, move, state): if move not in state.moves: return state # Illegal move has no effect board = state.board.copy(); board[move] = state.to_move moves = list(state.moves); moves.remove(move) return Struct(to_move=if_(state.to_move == 'X', 'O', 'X'), utility=self.compute_utility(board, move, state.to_move), board=board, moves=moves)
def custom(state, player): if state.utility: return state.utility * if_(player == 'X', 10000, -10000) # External heuristic code goes here print("bleh") return 0
def compute_utility(self, board, move, player): "If X wins with this move, return 1; if O return -1; else return 0." if (self.k_in_row(board, move, player, (0, 1)) or self.k_in_row(board, move, player, (1, 0)) or self.k_in_row(board, move, player, (1, -1)) or self.k_in_row(board, move, player, (1, 1))): return if_(player == 'X', +1, -1) else: return 0
def create_int_validator(min_: int = None, max_: int = None, nullable: bool = False) -> Callable[[int], str]: validators = [ lambda obj: if_(not isinstance(obj, int), TYPE_ERROR_MSG % INT_TYPE) ] validators = apply_min_max_validator(validators, int, min_, max_, create_limit_validator, INT_TYPE) return create_validator(validators, nullable, int)
def make_move(self, move, state): if move not in state.moves: return state # Illegal move has no effect board = state.board.copy() board[move] = state.to_move moves = list(state.moves) moves.remove(move) return Struct(to_move=if_(state.to_move == 'X', 'O', 'X'), utility=self.compute_utility(board, move, state.to_move), board=board, moves=moves)
def create_str_validator(min_len: int = None, max_len: int = None, nullable: bool = False) -> Callable[[str], str]: validators = [ lambda obj: if_(not isinstance(obj, str), TYPE_ERROR_MSG % STR_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_len, int), create_len_limit_validator, lt, min_len, MIN_LEN_ERROR_MSG, STR_TYPE) validators = append_validator_if_set(validators, isinstance(max_len, int), create_len_limit_validator, gt, max_len, MAX_LEN_ERROR_MSG, STR_TYPE) return create_validator(validators, nullable)
def create_date_validator(min_: date = None, max_: date = None, nullable: bool = False) -> Callable[[date], str]: validators = [ lambda obj: if_(not isinstance(obj, date), TYPE_ERROR_MSG % DATE_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_, date), create_limit_validator, lt, min_, MIN_ERROR_MSG, DATE_TYPE) validators = append_validator_if_set(validators, isinstance(max_, date), create_limit_validator, gt, max_, MAX_ERROR_MSG, DATE_TYPE) return create_validator(validators, nullable, to_date)
def h3(state, player): if state.utility: return state.utility * if_(player == 'X', 10000, -10000) h = 0 for x in state.moves: for b in range(1, 4): if x == (4, b): h -= 5 for x in state.board: for a in range(1, 7): for a2 in range(3, 6): if (x == (a2, a)) & (state.board.get(x) == "X"): h += 80 return h
def recursive_dls(node, problem, limit): if problem.goal_test(node.state): return node elif node.depth == limit: return 'cutoff' else: cutoff_occurred = False for child in node.expand(problem): result = recursive_dls(child, problem, limit) if result == 'cutoff': cutoff_occurred = True elif result is not None: return result return if_(cutoff_occurred, 'cutoff', None)
def result(self, state, move): if move not in state.moves: print("Illegal Move") return state # Illegal move has no effect board = state.board.copy() board[move] = state.to_move moves = list(state.moves) moves.remove(move) if move[0] > 1: moves.append((move[0] - 1, move[1])) return Struct(to_move=if_(state.to_move == 'X', 'O', 'X'), utility=self.compute_utility(board, move, state.to_move), board=board, moves=moves)
def create_dict_validator( nullable: bool = False, element_validators: Dict[str, Callable] = None) -> Callable[[dict], str]: validators = [ lambda obj: if_(not isinstance(obj, dict), TYPE_ERROR_MSG % DICT_TYPE) ] validate_dict = create_validator(validators, nullable) def super_func(obj: dict) -> str: message = validate_dict(obj) if message != VALID: return message return filter_msgs(obj, validate_as_dict, element_validators) return super_func
def create_float_validator(min_: float = None, max_: float = None, nullable: bool = False) -> Callable[[float], str]: # validate_min_max(min_, max_, float) validators = [ lambda obj: if_(not isinstance(obj, float), TYPE_ERROR_MSG % FLOAT_TYPE ) ] validators = append_validator_if_set(validators, isinstance(min_, float), create_limit_validator, lt, min_, MIN_ERROR_MSG, FLOAT_TYPE) validators = append_validator_if_set(validators, isinstance(max_, float), create_limit_validator, gt, max_, MAX_ERROR_MSG, FLOAT_TYPE) validators = apply_min_max_validator(validators, float, min_, max_, create_limit_validator, INT_TYPE) return create_validator(validators, nullable, float)
def computer_vs_computer(): difficulty1 = 1 difficulty2 = 1 print("1. Regalado\r\n2. Easy\r\n3. Medium\r\n4. Hard\r\n5. Custom") while True: try: difficulty1 = int(raw_input("Select Computer1 difficulty: ")) if difficulty1 > 0 and difficulty1 < 6: break print("Invalid difficulty") except ValueError: print("Please enter a number [1-5]\r\n") while True: try: difficulty2 = int(raw_input("Select Computer2 difficulty: ")) if difficulty2 > 0 and difficulty2 < 6: break print("Invalid difficulty") except ValueError: print("Please enter a number [1-5]\r\n") computer1_start = True while True: computer1_start = raw_input("Wich one should start Computer1 or Computer2 ? : ") if computer1_start == '1': computer1_start = True break elif computer1_start == '2': computer1_start = False break print("Please answer with '1' or '2'") computer1_chip = 'X' computer2_chip = 'O' while True: computer1_chip = raw_input("Assign Computer1 X or O " "(the remainig one will be automatically assigned to Computer2): ") if computer1_chip == 'O': computer2_chip = 'X' break elif computer1_chip == 'X': break print("Please enter 'X' or 'O'") computer1 = Computer(computer1_chip, difficulty1 - 1) computer2 = Computer(computer2_chip, difficulty2 - 1) play(if_(computer1_start, computer1_chip, computer2_chip), computer1, computer2)
def create_list_validator( min_len: int = None, max_len: int = None, nullable: bool = False, validate_element: Callable = False) -> Callable[[list], str]: validators = [ lambda obj: if_(not isinstance(obj, list), TYPE_ERROR_MSG % LIST_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_len, int), create_len_limit_validator, lt, min_len, MIN_LEN_ERROR_MSG, LIST_TYPE) validators = append_validator_if_set(validators, isinstance(max_len, int), create_len_limit_validator, gt, max_len, MAX_LEN_ERROR_MSG, LIST_TYPE) validate_list = create_validator(validators, nullable) def super_func(obj: list) -> str: message = validate_list(obj) if message != VALID: return message return filter_msgs(enumerate(obj), validate_as_list, validate_element) return super_func
def create_bool_validator(nullable: bool = False) -> Callable[[bool], str]: return create_validator([ lambda obj: if_(not isinstance(obj, bool), TYPE_ERROR_MSG % BOOL_TYPE) ], nullable)
def to_move(self, state): return if_(state in 'BCD', 'MIN', 'MAX')
def exp_schedule(k=20, lam=0.005, limit=100): "One possible schedule function for simulated annealing" return lambda t: if_(t < limit, k * math.exp(-lam * t), 0)
def h6(state, player): he = 1000 if state.utility: return state.utility * if_(player == 'X', 10000, -10000) for x in state.moves: for b in range(1, 4): if x == (4, b): he -= 100 for x in state.board: for a in range(1, 7): for a2 in range(4, 6): if (x == (a2, a)) & (state.board.get(x) == player): he += 20 for a in range(1, 6): he1 = 0 linea = 0 for a1 in range(1, 7): if state.board.get((a1, a)) == if_(player == 'X', 'O', 'X'): if linea == 0: linea = 1 he1 = 10 else: he1 = linea * 50 linea += 1 else: linea = 0 he -= he1 he1 = 0 for a in range(1, 7): he1 = 0 linea = 0 for a1 in range(1, 6): if state.board.get((a1, a)) == if_(player == 'X', 'O', 'X'): if linea == 0: linea = 1 he1 = 10 else: he1 = linea * 50 linea += 1 else: linea = 0 he -= he1 he1 = 0 for a in range(1, 6): he1 = 0 linea = 0 for a1 in range(1, 7): if state.board.get((a1, a)) == player: if linea == 0: linea = 1 he1 = 10 else: he1 = linea * 10 linea += 1 else: linea = 0 he += he1 he1 = 0 for a in range(1, 7): he1 = 0 linea = 0 for a1 in range(1, 6): if state.board.get((a1, a)) == player: if linea == 0: linea = 1 he1 = 10 else: he1 = linea * 10 linea += 1 else: linea = 0 he += he1 he1 = 0 return he
def h1(state, player): if state.utility: return state.utility * if_(player == 'X', 10000, -10000) return randint(-100,100)
def h7(state, player): if state.utility: return state.utility * if_(player == 'X', 10000, -10000) h = 0 for (x, y) in state.moves: if x == 4: h -= 20 elif x == 3 or x == 5: h -= 10 if y == 1 or (x, y -1) in state.board: ch = chips_in_row((x, y + 1), state.board, player, (1, 0)) cds = chips_in_row((x, y + 1), state.board, player, (1, -1)) cdi = chips_in_row((x, y + 1), state.board, player, (1, 1)) h += 1.15 * ((100 * if_(ch == 3, ch, 0)) + (((90 * if_(cds == 3, cds, 0)) + (y * 2)) * if_((x >= 5 & y in range(11 - x, 7)) or (x <= 3 & y in range(1, 5 - x)), 0, 1)) + (((90 * if_(cdi == 3, cdi, 0)) + (y * 2)) * if_((x <= 3 & y in range(11 - x, 7)) or (x >= 5 & y in range(1, 5 - x)), 0, 1))) ch = chips_in_row((x, y + 1), state.board, if_(player == 'X', 'O', 'X'), (1, 0)) cds = chips_in_row((x, y + 1), state.board, if_(player == 'X', 'O', 'X'), (1, -1)) cdi = chips_in_row((x, y + 1), state.board, if_(player == 'X', 'O', 'X'), (1, 1)) h -= 100 * if_(ch == 3, ch, 0) + \ (((90 * if_(cds == 3, cds, 0)) + (y * 2)) * if_((x >= 5 & y in range(11 - x, 7)) or (x <= 3 & y in range(1, 5 - x)), 0, 1)) \ + (((90 * if_(cdi == 3, cdi, 0)) + (y * 2)) * if_((x <= 3 & y in range(11 - x, 7)) or (x >= 5 & y in range(1, 5 - x)), 0, 1)) return h
def utility(self, state, player): "Return the value to player; 1 for win, -1 for loss, 0 otherwise." return if_(player == 'X', state.utility, -state.utility)
def h4(state, player): if state.utility: return state.utility * if_(player == 'X', 10000, -10000) h = 0 for (x, y) in state.moves: if x == 4: h -= 20 elif x == 3 or x == 5: h -= 10 cv = chips_in_row((x, y), state.board, player, (0, 1)) ch = chips_in_row((x, y), state.board, player, (1, 0)) cds = chips_in_row((x, y), state.board, player, (1, -1)) cdi = chips_in_row((x, y), state.board, player, (1, 1)) h += ((90 * if_(cv > 1, cv, 0)) - (y * 2)) \ + (100 * if_(ch > 1, ch, 0)) \ + (((95 * if_(cds > 1, cds, 0)) + (y * 2)) * if_((x >= 5 & y in range(11 - x, 7)) or (x <= 3 & y in range(1, 5 - x)), 0, 1)) \ + (((95 * if_(cdi > 1, cdi, 0)) + (y * 2)) * if_((x <= 3 & y in range(11 - x, 7)) or (x >= 5 & y in range(1, 5 - x)), 0, 1)) cv = chips_in_row((x, y), state.board, if_(player == 'X', 'O', 'X'), (0, 1)) ch = chips_in_row((x, y), state.board, if_(player == 'X', 'O', 'X'), (1, 0)) cds = chips_in_row((x, y), state.board, if_(player == 'X', 'O', 'X'), (1, -1)) cdi = chips_in_row((x, y), state.board, if_(player == 'X', 'O', 'X'), (1, 1)) h -= ((90 * if_(cv > 1, cv, 0)) - (y * 2)) \ + (100 * if_(ch > 1, ch, 0)) \ + (((95 * if_(cds > 1, cds, 0)) + (y * 2)) * if_((x >= 5 & y in range(11 - x, 7)) or (x <= 3 & y in range(1, 5 - x)), 0, 1)) \ + (((95 * if_(cdi > 1, cdi, 0)) + (y * 2)) * if_((x <= 3 & y in range(11 - x, 7)) or (x >= 5 & y in range(1, 5 - x)), 0, 1)) return h
" [Enter '0' for random selection]: ") if player1_chip == 'O': player2_chip = 'X' break elif player1_chip == 'X': break elif player1_chip == '0': player1_chip = randint(0, 2) if player1_chip == 0: player1_chip = 'X' else: player1_chip = 'O' print "Player1 is ", player1_chip break print("Please enter 'X' or 'O' or '0'") player1 = Human(player1_chip) player2 = Human(player2_chip) play(if_(player1_start, player1_chip, player2_chip), player1, player2) while True: operations = (human_vs_computer, computer_vs_computer, human_vs_human) option = 5 print " CONNECT - 4\r\n FSI EDITION" \ "\r\n1. Human VS Computer\r\n2. Computer VS Computer\r\n3. Human VS Human\r\n4. Exit" while True: try: option = int(raw_input("Select operation: ")) if option > 0 and option < 5: break print("Invalid mode!") except ValueError: print("Please introduce a number! [1-4]") if (option) == 4: break operations[option - 1]()