Ejemplo n.º 1
0
        public IEnumerable <KeyValuePair <BoardPoint, Piece> > CheckForChain(BoardPoint piecePoint, BoardPoint direction)
        {
            var myColor    = GetPieceAt(piecePoint).Value;
            var theirColor = myColor.GetOpponentColor();

            var chain       = new List <KeyValuePair <BoardPoint, Piece> >();
            var targetPoint = piecePoint;

            targetPoint += direction;
            Piece?targetPiece;

            while (WithinBoard(targetPoint) && (targetPiece = GetPieceAt(targetPoint)).HasValue)
            {
                if (targetPiece == theirColor)
                {
                    chain.Add(new KeyValuePair <BoardPoint, Piece>(targetPoint, targetPiece.Value));
                }
                else if (targetPiece == myColor)
                {
                    if (chain.Any())
                    {
                        return(chain);
                    }
                    else
                    {
                        return(null);
                    }
                }
                targetPoint += direction;
            }

            // Never finished chain fail
            return(null);
        }
Ejemplo n.º 2
0
        public float GetScore(BoardPoint point)
        {
            float score = 0;

            // control the diagonal
            if (point.row == point.col)
            {
                score += 1f;
            }

            if (point.row >= 3 && point.row <= 4)
            {
                score += 0.2f;
            }

            if (point.col >= 3 && point.col <= 4)
            {
                score += 0.2f;
            }

            var shrink = GetShrink(Round + 5);

            if (point.row < shrink || point.row > Width - shrink)
            {
                score -= 2f;
            }

            if (point.col < shrink || point.col > Width - shrink)
            {
                score -= 2f;
            }

            return(score);
        }
Ejemplo n.º 3
0
        public IEnumerable <BoardPoint> GetEmptySpots(BoardPoint piecePoint, BoardPoint direction)
        {
            var point = piecePoint;

            point += direction;
            while (WithinBoard(point) && GetPieceAt(point) == null)
            {
                yield return(point);

                point += direction;
            }
        }
Ejemplo n.º 4
0
        public IEnumerable <KeyValuePair <BoardPoint, Piece?> > GetAllPiecePoints()
        {
            var shrink = GetShrink(Round);

            for (int row = shrink; row < Width - shrink; row++)
            {
                for (int col = shrink; col < Width - shrink; col++)
                {
                    var point = new BoardPoint(row, col);
                    var piece = GetPieceAt(point);

                    yield return(new KeyValuePair <BoardPoint, Piece?>(point, piece));
                }
            }
        }
Ejemplo n.º 5
0
        public IEnumerable <KeyValuePair <BoardPoint, Piece> > CheckForDoubles(BoardPoint piecePoint, BoardPoint sideA, BoardPoint sideB)
        {
            var myColor    = GetPieceAt(piecePoint).Value;
            var theirColor = myColor.GetOpponentColor();

            if (GetPieceAt(sideA) == theirColor && GetPieceAt(sideB) == theirColor)
            {
                return(new[]
                {
                    new KeyValuePair <BoardPoint, Piece>(sideA, theirColor),
                    new KeyValuePair <BoardPoint, Piece>(sideB, theirColor),
                });
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        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));
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public bool WithinBoard(BoardPoint point)
 {
     return(WithinBoard(point.row, point.col));
 }
Ejemplo n.º 8
0
 public void SetPieceAt(BoardPoint point, Piece?piece)
 {
     SetPieceAt(point.row, point.col, piece);
 }
Ejemplo n.º 9
0
 public Piece?GetPieceAt(BoardPoint point)
 {
     return(GetPieceAt(point.row, point.col));
 }
Ejemplo n.º 10
0
        //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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
 public Move(BoardPoint from, BoardPoint to)
 {
     From = from;
     To   = to;
 }