Exemplo n.º 1
0
def play():
    req = request.get_json()
    card_played = None
    game_id = req["id"]
    player = req["player"]
    game = Games.query.filter_by(id=game_id).first()
    if game == None:
        flash(f'This game does not exists.', 'danger')
        return redirect(url_for('lobby'))
    if (game.first_player_id == current_user.id or game.second_player_id == current_user.id) \
        and (game.second_player_id != None) and (game.status == 1):
        state = json.loads(game.state)
        if (game.first_player_id == current_user.id and state["current_player"] == 0) \
           or (game.second_player_id == current_user.id and state["current_player"] == 1):
            if req["play"][-1] in ["h", "s", "c"] and int(req["play"][:-1]):
                card_played = foxintheforest.decode_card(req["play"])
                state = foxintheforest.play(state, [player, card_played])
                if len(state["discards"][0]) + len(state["discards"][1]) == 26:
                    game.status = 2
                game.state = json.dumps(state)
                db.session.commit()
        return make_response(
            jsonify(foxintheforest.get_player_state(state, player)), 200)
    else:
        flash(f'You are not a player in this game.', 'danger')
        return redirect(url_for('lobby'))
Exemplo n.º 2
0
def state():
    req = request.get_json()
    game_id = req["id"]
    game = Games.query.filter_by(id=game_id).first()
    if game == None:
        flash(f'This game does not exists.', 'danger')
        return redirect(url_for('lobby'))
    if game.second_player.username == "TheBad":
        state = json.loads(game.state)
        if state["current_player"] == 1:
            state = foxintheforest.play(state, [1, random_ai.ia_play(state)])
            if len(state["discards"][0]) + len(state["discards"][1]) == 26:
                game.status = 2
            game.state = json.dumps(state)
            db.session.commit()
    if game.first_player_id == current_user.id:
        state = json.loads(game.state)
        return make_response(
            jsonify(foxintheforest.get_player_state(state, 0)), 200)
    elif game.second_player_id == current_user.id:
        state = json.loads(game.state)
        return make_response(
            jsonify(foxintheforest.get_player_state(state, 1)), 200)
    else:
        flash(f'You are not a player in this game.', 'danger')
        return redirect(url_for('lobby'))
Exemplo n.º 3
0
def select_play(state, runs):
  """ Select one play based on MCTS simulations """
  allowed = list_allowed(state, state["current_player"])
  if len(allowed) == 1:
    return allowed[0]

  root = Node(None)
  player = state["current_player"]
  k = aquire_knowledge(state)
  for _ in range(runs):
    s = random_state(state, k)
    node = root
    while len(s["hands"][0]) != 0 or len(s["hands"][1]) != 0:
      node = select(node, s, player)
      play(s, node.play)
    while node.parent != None:
      node.visits += 1
      tricks_won = len(s["discards"][0])//2
      if tricks_won <= 3:
        node.outcome_p0["humble"] += 1
        if player == 0:
          node.reward += 1
      elif tricks_won <= 6:
        node.outcome_p0["defeated"] += 1
        if player == 1:
          node.reward += 1
      elif tricks_won <= 9:
        node.outcome_p0["victorious"] += 1
        if player == 0:
          node.reward += 1
      else:
        node.outcome_p0["greedy"] += 1
        if player == 1:
          node.reward += 1
      node = node.parent
    
  maxi = 0
  selected = None
  for n in root.children:
    if n.visits > maxi:
      selected = n
      maxi = n.visits

  return selected.play