public IEnumerable <Move> GetAllMoves(Piece color, bool earlyGame) { var i = (Board.Round / 2) % 2; foreach (var movablePiece in Board.GetAllPiecePoints().Where(p => p.Value == color)) { // horrible hack to reduce the number of moves by skipping every second move if (earlyGame && i++ % 2 == 0) { continue; } var from = movablePiece.Key; foreach (var direction in BoardPoint.GetAllDirections()) { foreach (var to in Board.GetEmptySpots(from, direction)) { yield return(new Move(from, to)); } } } }
//Stack<string> _hashStack = new Stack<string>(); public float GetScore(Piece color, bool log = false) { var theirColor = color.GetOpponentColor(); var myPieces = 0; var theirPieces = 0; var myValue = 0f; var theirValue = 0f; const float PositionWeight = 0.1f; foreach (var pair in GetAllPiecePoints()) { if (pair.Value == color) { myPieces++; myValue++; myValue += GetScore(pair.Key) * PositionWeight; } if (pair.Value == theirColor) { theirPieces++; theirValue++; theirValue += GetScore(pair.Key) * PositionWeight; } } if (theirPieces == 0 && myPieces > 0) { if (log) { Console.WriteLine("Score: I win"); } return(float.MaxValue); } if (myPieces == 0 && theirPieces > 0) { if (log) { Console.WriteLine("Score: They win"); } return(float.MinValue); } if (Round == EndsAtRound) { if (log) { Console.WriteLine("Score: Tie"); } return(0); } if (theirPieces == 1) { var theirPiece = GetAllPiecePoints(theirColor).First(); foreach (var direction in BoardPoint.GetAllDirections()) { if (GetPieceAt(theirPiece.Key + direction) == color) { // go for the kill already! theirValue /= 2f; break; } } } var score = (myValue - theirValue) / (Width + PositionWeight); if (log) { Console.WriteLine("My Value: " + myValue); Console.WriteLine("Their Value: " + theirValue); Console.WriteLine("Score: " + score); Console.WriteLine("\n(Max Value: " + (Width + PositionWeight) + ")"); } return(score); }
public void PushMove(Move move) { //var hash = GetBoardHash(); //_hashStack.Push(hash); var piece = GetPieceAt(move.From); if (piece == null) { throw new NullReferenceException("Move piece"); } SetPieceAt(move.From, null); SetPieceAt(move.To, piece); var captures = new List <KeyValuePair <BoardPoint, Piece> >(); foreach (var direction in BoardPoint.GetAllDirections()) { // did it chain any combos? var killsThisDirection = CheckForChain(move.To, direction); if (killsThisDirection != null) { captures.AddRange(killsThisDirection); } } //rows { var rowDoubles = CheckForDoubles(move.To, move.To + BoardPoint.Left, move.To + BoardPoint.Right); if (rowDoubles != null) { captures.AddRange(rowDoubles); } } //cols { var colDoubles = CheckForDoubles(move.To, move.To + BoardPoint.Up, move.To + BoardPoint.Down); if (colDoubles != null) { captures.AddRange(colDoubles); } } if (captures.Any()) { // remove duplicated captures var distinctPointPiece = new BoardPoint.ComparePointPiece(); captures = captures.Distinct(distinctPointPiece).ToList(); foreach (var capturePoint in captures.Select(p => p.Key)) { SetPieceAt(capturePoint, null); } _captureHistory.Add(Round, captures); } Round++; _moveStack.Push(move); }