Beispiel #1
0
def _minimax_score(board, player_to_move, player_to_optimize):
    winner = ttt.get_winner(board)
    if winner is not None:
        if winner == player_to_optimize:
            return 10
        else:
            return -10
    elif ttt.is_board_full(board):
        return 0

    legal_moves = utils.get_all_legal_moves(board)

    scores = []
    for move in legal_moves:
        _board = copy.deepcopy(board)
        ttt.make_move(player_to_move, _board, move)

        opp = utils.get_opponent(player_to_move)
        opp_best_response_score = _minimax_score(_board, opp,
                                                 player_to_optimize)
        scores.append(opp_best_response_score)

    if player_to_move == player_to_optimize:
        return max(scores)
    else:
        return min(scores)
Beispiel #2
0
def _cached_minimax_score(board, player_current, player_main):
    winner = engine.get_winner(board)
    opponent = utils.get_opponent(player_current)

    if winner == player_main:
        return 10
    elif winner == opponent:
        return -10
    elif engine.is_board_full(board):
        return 0

    board_cache_key = str(board)
    is_main_player = player_current == player_main
    if board_cache_key in cache:
        scores = cache[board_cache_key]
        return max(scores) if is_main_player else min(scores)

    legal_moves = utils.get_legal_moves(board, player_current)
    scores = []

    for move in legal_moves:
        _board = copy.deepcopy(board)
        engine.make_move(_board, move, player_current)
        score = _cached_minimax_score(_board, opponent, player_main)
        scores.append(score)

    cache[board_cache_key] = scores
    if player_current == player_main:
        return max(scores)
    else:
        return min(scores)
Beispiel #3
0
 def humanMove(self,row,col):
     move = engine.row_col_to_pos(row,col)
     valid,msg = engine.valid_move(self.state,move,verbose=False,
                                   returnMessage=True)
     if(valid):
         engine.make_move(self.state,move)
         self.play()
     else:
         self.statusBar().showMessage(msg,5000)
Beispiel #4
0
def minimax_ai(board, player):
    legal_moves = utils.get_legal_moves(board, player)
    opponent = utils.get_opponent(player)
    scores = []

    for move in legal_moves:
        _board = copy.deepcopy(board)
        engine.make_move(_board, move, player)
        score = _cached_minimax_score(_board, opponent, player)
        scores.append(score)

    sorted_best_moves = [x for _, x in sorted(zip(scores, legal_moves))]
    return sorted_best_moves[-1]
Beispiel #5
0
def minimax_ai(board, who_am_i):
    best_move = None
    best_score = None

    for move in utils.get_all_legal_moves(board):
        _board = copy.deepcopy(board)
        ttt.make_move(who_am_i, _board, move)

        opp = utils.get_opponent(who_am_i)
        score = _minimax_score(_board, opp, who_am_i)
        if best_score is None or score > best_score:
            best_move = move
            best_score = score

    return best_move
Beispiel #6
0
def minmax_score(board, playing_current, player):
	board_cache_key = hash_the_board(board)
	if board_cache_key in cache:
		return cache.get(board_cache_key)
	else:
		winner = en.get_winner_ai_test(board, playing_current)
		if winner is not None:
			if winner == player:
				return 10
			else:
				return -10
		elif en.is_board_free(board) == False:
				return 0
		else:
			moves = list_moves(board)
			scores = []
			for move in moves:
				new_board = en.make_move(board, move, playing_current) 
				opponent = get_opponent(playing_current)
				score = minmax_score(new_board, opponent, player)
				scores.append(score)
		if playing_current == player:
			cache[board_cache_key] = max(scores)
			# return max(scores)
		else:
			cache[board_cache_key] = min(scores)
			# return min(scores)
		return cache.get(board_cache_key)
Beispiel #7
0
def minmax_ai(board, playing):
	best_move = None
	best_score = None

	for move in list_moves(board):
		_board = copy.deepcopy(board)
		_board = en.make_move(_board, move, playing)
		opp = get_opponent(playing)
		score = minmax_score(_board, opp, playing)
		if best_score is None or score > best_score:
			best_move = move
			best_score = score

	return best_move
Beispiel #8
0
def api():
    state = request.json["state"]
    move = request.json["move"]
    team = request.json["team"]
    if move == 0:
        move = mind.move(state, list(engine.non_check_moves(state, team)))
    result = engine.make_move(state, move, team)
    moves = list(engine.non_check_moves(result, engine.opp(team)))
    win = (1 if engine.is_checked(result, engine.opp(team)) else
           -1) if moves == [] else 0
    return json.dumps({
        "result": result,
        "win": win,
        "move": engine.notate(state, move)
    })
Beispiel #9
0
def _minimax_score(board, player_current, player_main):
    winner = engine.get_winner(board)
    opponent = utils.get_opponent(player_current)

    if winner == player_main:
        return 10
    elif winner == opponent:
        return -10
    elif engine.is_board_full(board):
        return 0

    legal_moves = utils.get_legal_moves(board, player_current)
    scores = []

    for move in legal_moves:
        _board = copy.deepcopy(board)
        engine.make_move(_board, move, player_current)
        score = _minimax_score(_board, opponent, player_main)
        scores.append(score)

    if player_current == player_main:
        return max(scores)
    else:
        return min(scores)
Beispiel #10
0
import engine
import simple_ais as ai
import minimax_ai as mm

try:
    board = engine.new_board()
    move_coords = (2, 0)
    engine.make_move(board, move_coords, "X")
    engine.make_move(board, move_coords, "X")
except Exception as exption:
    print('Threw an exception: {0}'.format(exption))
# => should always throw exception


print()
board = [
  ['X', 'O', '-'],
  ['-', 'O', '-'],
  ['X', '-', '-']
]
print(ai.makes_winning_move_ai(board, 'X'))
print(ai.makes_winning_move_ai(board, 'X'))
# => should always print (1, 0)

print(ai.makes_winning_move_ai(board, 'O'))
print(ai.makes_winning_move_ai(board, 'O'))
# => should always print (2, 1)

print()
board = [
  ['O', '-', '-'],
Beispiel #11
0
 def computerMove(self,move):
     engine.make_move(self.state,move)
     self.play()