// Copyright 2019 DeepMind Technologies Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "open_spiel/algorithms/kriegspiel/common.h"
#include "open_spiel/abseil-cpp/absl/random/distributions.h"

namespace open_spiel {
namespace algorithms {



bool IsTerminal(const chess::ChessBoard &board) {
  if (board.IrreversibleMoveCounter() > 50) return true;
  if (!board.HasSufficientMaterial()) return true;
  if (!board.HasLegalMoves()) return true;
  return false;
}

double GetReturn(const chess::ChessBoard& board, chess::Color color) {
  if (board.IrreversibleMoveCounter() > 50) return 0.5;
  if (!board.HasSufficientMaterial()) return 0.5;
  if (!board.HasLegalMoves()) {
    if (!board.InCheck()) {
      return 0.5;
    } else {
      auto next_to_play = board.ToPlay();
      return next_to_play == color ? 0.0 : 1.0;
    }
  }
  // todo should not happen but we should check it
  return 0.5;
}

Action RandomStep(const State &state, std::mt19937 rng_, Player player) {

  std::vector<Action> legal_actions = state.LegalActions(player);
  int r = absl::Uniform(rng_, 0u, legal_actions.size());

  return legal_actions[r];
}


}  // namespace algorithms
}  // namespace open_spiel
