public PositionContext position() { PositionContext _localctx = new PositionContext(Context, State); EnterRule(_localctx, 2, RULE_position); int _la; try { EnterOuterAlt(_localctx, 1); { State = 15; _la = TokenStream.LA(1); if (!((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << T__1) | (1L << T__2))) != 0))) { ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { ExitRule(); } return(_localctx); }
private MoveCandidate analyze(int depth, int field, PositionContext position) { ConsoleLogger.LogDepth(depth); ConsoleLogger.LogMove(position); if (depth == Settings.MaxDepth) { MoveCandidate candidate = new MoveCandidate(field, position); PositionController.MakeMove(position, field); candidate.Evaluation = evaluate(position); ConsoleLogger.Log(candidate.Evaluation.ToString()); computedVariation++; return(candidate); } States move = position.Move; List <int> candidates = FindCandidates(position); if (candidates.Count == 0) { ConsoleLogger.Log("! nie znaleziono ruchow kandydatow!"); } ConsoleLogger.Log("Ilość kandydatów: " + candidates.Count); List <MoveCandidate> moveCandidates = new List <MoveCandidate>(); foreach (var i in candidates) { PositionContext newPosition = PositionContextFactory.CreatePositionContext(position); PositionController.MakeMove(newPosition, i); MoveCandidate candidate = analyze(depth + 1, i, newPosition); moveCandidates.Add(candidate); } var bestCandidate = getBest(moveCandidates, move); ConsoleLogger.Log("Z " + candidates.Count + " wariantow wybrano: " + bestCandidate.Evaluation.ToString()); return(bestCandidate); }
public static bool IsValid(PositionContext position, int index) { Field field = position.GetField(index); if (field.SurroundingEmptySquares != 0) { if ((position.GetLeft(index) != null && PositionHelper.AreEnemies(field, position.GetLeft(index))) && (position.GetRight(index) != null && PositionHelper.AreEnemies(field, position.GetRight(index))) && (position.GetLower(index) != null && PositionHelper.AreEnemies(field, position.GetLower(index))) && (position.GetUpper(index) != null && PositionHelper.AreEnemies(field, position.GetUpper(index)))) { // TODO // if we are putting the stone between all the enemies pawn // there must be beating move } } return(true); }
/// <summary> /// Gets the current position evaluation. /// </summary> /// <returns>The position computer's evaluation.</returns> private double evaluate(PositionContext posContext) { return(posContext.WhitePoints - posContext.BlackPoints); }
/// <summary> /// </summary> /// <param name="positionContext"></param> /// <returns></returns> private List <int> FindCandidates(PositionContext positionContext) { List <int> candidates = new List <int> (); List <Chain> chainsToBeat = new List <Chain>(); int biggestSizeToBeat = 0; Chain biggestToBeat = null; // find chain to beat foreach (var chain in positionContext.Chains.Where(x => x.Color != positionContext.Move && !x.Beated)) { if (chain.SurroundingEmptyFields.Count == 1) { chainsToBeat.Add(chain); if (chain.Count > biggestSizeToBeat) { biggestSizeToBeat = chain.Count; biggestToBeat = chain; } } } // try to defend List <Chain> chainsToDefend = new List <Chain>(); int biggestLost = 0; Chain biggestChainToLost = null; foreach (var chain in positionContext.Chains.Where(x => x.Color == positionContext.Move)) { if (chain.SurroundingEmptyFields.Count == 1) { chainsToDefend.Add(chain); if (chain.Count > biggestLost) { biggestLost = chain.Count; biggestChainToLost = chain; } } } /* * // TODO defence by beating * if (biggestSizeToBeat > 0 && biggestSizeToBeat >= biggestLost) * { * //return biggestToBeat.SurroundingEmptyFields.First(); * } * else * { * if (biggestLost > 0) * { * //return biggestChainToLost.SurroundingEmptyFields.First(); * } * * } * * // TODO assert is not deadly move * if (biggestLost > 0) * { * // TODO has surrounding empty spaces/ could be connected to another chain without self-killing * //this.gameContext.GetField(biggestChainToLost.SurroundingEmptyFields.First()). * //return biggestChainToLost.SurroundingEmptyFields.First(); * } * * * // find biggest chain and try to surround * int biggestSizeToSurround = 0; * Chain biggest = null; * foreach (var chain in positionContext.Chains.Where(x => x.Color != positionContext.Move)) * { * if (chain.Count > biggestSizeToSurround) * { * biggestSizeToSurround = chain.Count; * biggest = chain; * } * } * * // todo * if (biggestSizeToSurround > 0) * { * return biggest.SurroundingEmptyFields.First(); * } * * int dimension = (int)Settings.BoardSize; * // TODO * // very, very stupid clause * while (true) * { * int fieldIndex = this.randomNumber(0, dimension * dimension - 1); * * if (positionContext.GetField(fieldIndex).FieldState.Equals(States.Empty)) * { * return fieldIndex; * } * } * * */ if (chainsToBeat.Count > 0) { chainsToBeat.OrderByDescending(x => x.Count); } if (chainsToDefend.Count > 0) { chainsToDefend.OrderByDescending(x => x.Count); } candidates.AddRange(chainsToBeat.Select(x => x.SurroundingEmptyFields.First())); candidates.AddRange(chainsToDefend.Select(x => x.SurroundingEmptyFields.First())); if (candidates.Count == 0) { // find biggest chain and try to Chain biggest = AlgorithmHelper.GetBiggestUnbeatenChain(positionContext); if (biggest != null) { candidates.AddRange(biggest.SurroundingEmptyFields); } } if (candidates.Count == 0) { //TODO Michal to jest blad petli powyzej~! } return(candidates); }
public PositionUnitOfWork(string connection) { context = new PositionContext(connection); PositionRepository = new PositionRepository(context); }
/// <summary> /// </summary> /// <param name="positionContext"></param> /// <returns></returns> private MoveCandidate FindCandidate(PositionContext positionContext) { List <int> candidates = new List <int> (); List <Chain> chainsToBeat = new List <Chain>(); int biggestSizeToBeat = 0; Chain biggestToBeat = null; // find chain to beat foreach (var chain in positionContext.Chains.Where(x => x.Color != positionContext.Move)) { if (chain.SurroundingEmptyFields.Count == 1) { chainsToBeat.Add(chain); if (chain.Count > biggestSizeToBeat) { biggestSizeToBeat = chain.Count; biggestToBeat = chain; } } } // try to defend List <Chain> chainsToDefend = new List <Chain>(); int biggestLost = 0; Chain biggestChainToLost = null; foreach (var chain in positionContext.Chains.Where(x => x.Color == positionContext.Move)) { if (chain.SurroundingEmptyFields.Count == 1) { chainsToDefend.Add(chain); if (chain.Count > biggestLost) { biggestLost = chain.Count; biggestChainToLost = chain; } } } // TODO defence by beating if (biggestSizeToBeat > 0 && biggestSizeToBeat >= biggestLost) { return(new MoveCandidate(biggestToBeat.SurroundingEmptyFields.First())); } else { if (biggestLost > 0) { return(new MoveCandidate(biggestChainToLost.SurroundingEmptyFields.First())); } } // TODO assert is not deadly move if (biggestLost > 0) { // TODO has surrounding empty spaces/ could be connected to another chain without self-killing //this.gameContext.GetField(biggestChainToLost.SurroundingEmptyFields.First()). return(new MoveCandidate(biggestChainToLost.SurroundingEmptyFields.First())); } // find biggest chain and try to surround int biggestSizeToSurround = 0; Chain biggest = null; foreach (var chain in positionContext.Chains.Where(x => x.Color != positionContext.Move)) { if (chain.Count > biggestSizeToSurround) { biggestSizeToSurround = chain.Count; biggest = chain; } } // TODO if (biggestSizeToSurround > 0) { return(new MoveCandidate(biggest.SurroundingEmptyFields.First())); } int dimension = (int)Settings.BoardSize; // TODO // very, very stupid clause while (true) { int fieldIndex = this.randomNumber(0, dimension * dimension - 1); if (positionContext.GetField(fieldIndex).FieldState.Equals(States.Empty)) { return(new MoveCandidate(fieldIndex)); } } // TODO return(new MoveCandidate(4)); }
public Dao() { context = new PositionContext(); }
public PositionRepository(PositionContext context) { this.context = context; }
public Maintanence(PositionContext context) { _context = context; }
public Updates(PositionContext context) { _context = context; }