Exemplo n.º 1
0
def _alpha_beta(board: Connect4, depth: int, alpha: int, beta: int,
                is_maximising_player: bool, player: bool):
    if depth == 0:
        return board.value  # Opposing player has the current turn

    if board.is_game_over:
        return board.value - depth

    if is_maximising_player:
        score = LOW_BOUND
        for move in board.legal_moves:
            board.make_move(move)
            score = max(
                _alpha_beta(board, depth - 1, alpha, beta,
                            not is_maximising_player, player), score)
            alpha = max(alpha, score)
            board.unmake_move()
            if alpha >= beta:
                return score
    else:
        score = HIGH_BOUND
        for move in board.legal_moves:
            board.make_move(move)
            score = min(
                _alpha_beta(board, depth - 1, alpha, beta,
                            not is_maximising_player, player), score)
            beta = min(beta, score)
            board.unmake_move()
            if alpha >= beta:
                return score
    return score
Exemplo n.º 2
0
def _minimax(board: Connect4, depth: int, is_maximising_player: bool,
             player: bool):
    if depth == 0:
        return board.value

    if board.is_game_over:
        return board.value - depth

    if is_maximising_player:
        score = LOW_BOUND
        for move in board.legal_moves:
            board.make_move(move)
            score = max(
                _minimax(board, depth - 1, not is_maximising_player, player),
                score)
            board.unmake_move()
    else:
        score = HIGH_BOUND
        for move in board.legal_moves:
            board.make_move(move)
            score = min(
                _minimax(board, depth - 1, not is_maximising_player, player),
                score)
            board.unmake_move()
    return score
Exemplo n.º 3
0
def _json_board(board: Connect4, params: Optional[Dict] = None):
    by_rank = {}
    for slot in SLOTS_VFLIP:
        rank = int(slot / 7)
        file = slot % 7

        by_rank[rank] = by_rank.get(rank, [])

        _square = {
            'rank': rank,
            'file': file,
            'index': int(slot),
        }

        piece = board.piece_at(slot)
        if piece:
            _square['piece'] = 'red' if piece.colour else 'yellow'

        by_rank[rank].append(_square)

    by_rank_reverse = {}
    for i, rank in enumerate(sorted(by_rank.keys(), reverse=True)):
        by_rank_reverse[i] = by_rank[rank]

    payload = {
        'game': by_rank_reverse,
        'turn': board.turn_name,
        'mhn': board.mhn
    }
    if params:
        payload.update(params)
    return payload
Exemplo n.º 4
0
def minimax(board: Connect4, depth: int):
    score = LOW_BOUND
    best_move = None
    player = board.turn  # Current player is the AI player

    for move in board.legal_moves:
        board.make_move(move)
        value = _minimax(board, depth - 1, True, player)
        board.unmake_move()

        if not player:
            value *= -1

        if value >= score:
            score = value
            best_move = move
    return best_move
Exemplo n.º 5
0
def c4_new_game():
    data = request.get_json()
    c4 = Connect4()
    cache['game'] = c4

    if not data['player']:
        return c4_make_move_ai()  # Make first move

    return _json_board(c4)
Exemplo n.º 6
0
from flask import request
from typing import Optional, Dict

from connect_4.game import Connect4, IllegalMove, SLOTS_VFLIP
from connect_4.ai import minimax, alpha_beta
from web.server import app

cache = {
    'game': Connect4(),
}


def _end_message(result: int) -> str:
    if result == 1:
        return 'Game over: Red wins!'
    elif result == -1:
        return 'Game over: Yellow wins!'
    elif result == 0:
        return 'Game over: Draw'


def _json_board(board: Connect4, params: Optional[Dict] = None):
    by_rank = {}
    for slot in SLOTS_VFLIP:
        rank = int(slot / 7)
        file = slot % 7

        by_rank[rank] = by_rank.get(rank, [])

        _square = {
            'rank': rank,