private static IEnumerable <Move.Play> GetPossibleMoves(GameState gameState, List <Card> playersHand, PlayerIdentityEnum player) { //return GetPossibleMoves2(playersHand, player).ToList().OrderByDescending(x => x. Item1).Select(x => x.Item2); //} // private IEnumerable<Tuple<int, Move.Play>> GetPossibleMoves2(List<Card> playersHand, PlayerIdentityEnum player) { foreach (var piece in gameState.Pieces.Where(p => p.Owner == player)) { foreach (var card in playersHand) { foreach (var target in card.Targets) { var newPosition = piece.PositionOnBoard + target; var potentialMove = new Move.Play(card.Type, piece.PositionOnBoard, newPosition); if (IsPlayValid(gameState, potentialMove)) { //var score = Evaluate(createGameStateWith(potentialMove), maximizingPlayer); //yield return new Tuple<int, Move.Play>(score, potentialMove); yield return(potentialMove); } } } } }
public int Minimax(int depth, int min, int max) { if (depth == 0 || IsLeaf()) { return(Evaluate(this.gameState, this.maximizingPlayer)); } if (IsMaxNode()) { var v = min; // TODO invert dependency in v2 foreach (var move in GetPossibleMoves(this.gameState, this.gameState.MyHand, maximizingPlayer)) { if (BestMove == null) { BestMove = move; } var v2 = new Node(createGameStateWith(move), maximizingPlayer).Minimax(depth - 1, v, max); if (v2 > v) { v = v2; BestMove = move; } if (v > max) { return(max); } } // TODO add pass plays???? (if so remove passing code in MyBot:GetNextMove) return(v); } else { var v = max; foreach (var move in GetPossibleMoves(this.gameState, this.gameState.OpponentsHand, Opponent(maximizingPlayer))) { var v2 = new Node(createGameStateWith(move), maximizingPlayer).Minimax(depth - 1, min, v); if (v2 < v) { v = v2; } if (v < min) { return(min); } } // TODO add pass plays???? (if so remove passing code in MyBot:GetNextMove) return(v); } }
private IEnumerable <Move> GetPossibleMoves() { foreach (var piece in this.gameState.MyPieces) { foreach (var card in this.gameState.MyHand) { foreach (var target in card.Targets) { var newPosition = piece.PositionOnBoard + target; var potentialMove = new Move.Play(card.Type, piece.PositionOnBoard, newPosition); if (this.IsPlayValid(potentialMove)) { yield return(potentialMove); } } } } }
private bool IsPlayValid(Move.Play move) { //out of bounds if (move.To.X < 0 || move.To.X > 4 || move.To.Y < 0 || move.To.Y > 4) { return(false); } //if there's a piece on the target position, only allow if piece is of the enemy var maybePiece = this.gameState.Pieces.Find(p => p.PositionOnBoard == move.To); if (maybePiece != null) { return(maybePiece.Owner != this.gameState.CurrentlyPlaying); } // if is in bounds and noone is on the target position, go ahead. return(true); }