Example #1
0
        public void HumanPlayerMove(Point to)
        {
            if (IsGameOver ||
                CurrentPosition.Equals(to) ||
                Field.IsStalemate(to, CurrentPosition))
            {
                return;
            }

            var from = CurrentPosition;

            Field.RemoveEdge(Field.Vertex(from), Field.Vertex(to));
            CurrentPosition = to;
            LastMoves.Clear();
            LastMoves.Add(new Move(PlayerTurn, to, from));
            MovesHistory.AddRange(LastMoves);
            VictoryConditions(to);

            if (IsGameOver)
            {
                NumberOfMoves++;
                return;
            }

            if (Field.IsMoveIntoTheVoid(to))
            {
                Flip();
                NumberOfMoves++;
            }
        }
Example #2
0
        public void ComputerMove()
        {
            if (IsGameOver)
            {
                return;
            }

            var bottomGoal = new Point(Width / 2, Height + 1);
            var topGoal    = new Point(Width / 2, -1);

            var goal = PlayerTurn == PlayerOrder.First ? topGoal : bottomGoal;

            IComputer computer;

            if (CurrentPlayer.Name == "Watson")
            {
                computer = new Watson(this);
            }
            else
            {
                computer = new Walter();
            }

            var pathToGoal = computer.GetPathToGoal(Field, CurrentPosition, goal);

            if (pathToGoal == null)
            {
                Flip();
                WonBy      = WonBy.Block;
                IsGameOver = true;
                return;
            }

            int goTo = pathToGoal.Pop(); // First item is current position - ignoring

            goTo = pathToGoal.Pop();

            LastMoves.Clear();
            MoveComputer(CurrentPosition, Field.Position(goTo), pathToGoal);
            MovesHistory.AddRange(LastMoves);

            if (IsGameOver)
            {
                return;
            }

            Flip();
            NumberOfMoves++;
        }
Example #3
0
        public bool Place(Figure f, int row, int column, FigureType pawnReplacement = FigureType.Queen)
        {
            if (GameOver || !GameStarted ||
                !IsValidPosition(row, column) ||
                f == null ||
                f.Color != CurrentColor ||
                !IsValidPosition(f.Row, f.Column))
            {
                return(false); // invalid, no change
            }
            LastMoves.Clear();
            LastStroke = null;
            // disallow castling
            if (f.Type == FigureType.Rook && f.MoveCount == 0)
            {
                var king = GetKing(f.Color);
                if (king != null)
                {
                    if (f.Column == 7)
                    {
                        king.AllowCastlingKingSide = false;
                    }
                    else if (f.Column == 0)
                    {
                        king.AllowCastlingQueenSide = false;
                    }
                }
            }
            // castling
            if (f.Type == FigureType.King && Math.Abs(f.Column - column) == 2)
            {
                // king size
                if (column > f.Column)
                {
                    var rook = Get(f.Row, 7);
                    if (rook != null)
                    {
                        LastMoves.Add(new Move(rook, f.Row, f.Column + 1));
                        board[f.Row, 7]            = null;
                        board[f.Row, f.Column + 1] = rook;
                        rook.Column = f.Column + 1;
                    }
                }
                // queen size
                else
                {
                    var rook = Get(f.Row, 0);
                    if (rook != null)
                    {
                        LastMoves.Add(new Move(rook, f.Row, f.Column - 1));
                        board[f.Row, 0]            = null;
                        board[f.Row, f.Column - 1] = rook;
                        rook.Column = f.Column - 1;
                    }
                }
            }
            if (f.Type == FigureType.Pawn)
            {
                // enpassent strike
                var dir = f.Color == FigureColor.White ? 1 : -1;
                if (enpassentFigure != null &&
                    f.Color != enpassentFigure.Color &&
                    row == enpassentFigure.Row + dir &&
                    column == enpassentFigure.Column)
                {
                    LastStroke = new Figure(enpassentFigure);
                    board[enpassentFigure.Row, enpassentFigure.Column] = null;
                    enpassentFigure.Column = -1;
                    enpassentFigure.Row    = -1;
                }
                // pawn replacement
                if (f.Color == FigureColor.White && row == 7 ||
                    f.Color == FigureColor.Black && row == 0)
                {
                    f.Type = pawnReplacement;
                }
            }
            board[f.Row, f.Column] = null;
            if (f.Type == FigureType.Pawn &&
                (f.Row == 6 && f.Color == FigureColor.Black && row == 4 ||
                 f.Row == 1 && f.Color == FigureColor.White && row == 3))
            {
                enpassentFigure = f;
            }
            else
            {
                enpassentFigure = null;
            }
            var strike = Get(row, column);

            if (strike != null)
            {
                LastStroke    = new Figure(strike);
                strike.Row    = -1;
                strike.Column = -1;
                if (strike.Type == FigureType.King)
                {
                    KingStrike = true;
                    Winner     = f.Color;
                }
            }
            LastMoves.Add(new Move(f, row, column));
            f.Row    = row;
            f.Column = column;
            board[f.Row, f.Column] = f;
            f.MoveCount           += 1;
            UpdateClocks();
            CurrentColor = GetOpponentColor(CurrentColor);
            return(true);
        }