Exemplo n.º 1
0
        public bool CheckForAttackerVictory()
        {
            //Check to see if King is surrounded on 4 sides
            try
            {
                SimpleSquare kingSquare = FindTheKing(this.board, true);
                if (kingSquare == null)
                {
                    return(false);
                }
                SimpleSquare up    = GetSquare(kingSquare.Row - 1, kingSquare.Column);
                SimpleSquare down  = GetSquare(kingSquare.Row + 1, kingSquare.Column);
                SimpleSquare left  = GetSquare(kingSquare.Row, kingSquare.Column - 1);
                SimpleSquare right = GetSquare(kingSquare.Row, kingSquare.Column + 1);

                if (up != null && down != null && left != null && right != null)
                {
                    if ((up.AttackerPresent || up.SquareType == Square.square_type.Throne) && (down.AttackerPresent || down.SquareType == Square.square_type.Throne) && (left.AttackerPresent || left.SquareType == Square.square_type.Throne) && (right.AttackerPresent || right.SquareType == Square.square_type.Throne))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(false);
        }
Exemplo n.º 2
0
        public SimpleSquare FindTheKing(SimpleBoard board, bool quick)
        {
            if (quick)
            {
                return(board.kingSquare);
            }
            else
            {
                SimpleSquare kingSquare = null;
                for (int i = 0; i < board.OccupationArray.GetLength(0); i++)     //Rows
                {
                    for (int j = 0; j < board.OccupationArray.GetLength(1); j++) //Columns
                    {
                        if (board.OccupationArray[j, i] == Square.occupation_type.King)
                        {
                            kingSquare = new SimpleSquare(j, i, Square.occupation_type.King, board.SquareTypeArray[j, i]);
                            return(kingSquare);
                        }
                    }
                }
                if (kingSquare == null)
                {
                    return(null);
                }

                return(kingSquare);
            }
        }
Exemplo n.º 3
0
        public int NumberOfAttackersAroundKing()
        {
            int          numberOfAttackers = 0;
            SimpleSquare kingSquare        = FindTheKing(board, true);
            SimpleSquare up    = GetSquare(kingSquare.Row - 1, kingSquare.Column);
            SimpleSquare down  = GetSquare(kingSquare.Row + 1, kingSquare.Column);
            SimpleSquare left  = GetSquare(kingSquare.Row, kingSquare.Column - 1);
            SimpleSquare right = GetSquare(kingSquare.Row, kingSquare.Column + 1);

            if (up != null && up.AttackerPresent)
            {
                numberOfAttackers++;
            }
            if (down != null && down.AttackerPresent)
            {
                numberOfAttackers++;
            }
            if (left != null && left.AttackerPresent)
            {
                numberOfAttackers++;
            }
            if (right != null && right.AttackerPresent)
            {
                numberOfAttackers++;
            }


            return(numberOfAttackers);
        }
Exemplo n.º 4
0
        private void CheckAndProcessTake()
        {
            SimpleSquare endSquare = GetSquare(this.endRow, this.endColumn);
            //Check UP 1 ROW

            SimpleSquare squareToCheck = GetSquare(endSquare.Row - 1, endSquare.Column);

            if (squareToCheck != null) //Is a valid square
            {
                SearchAroundForTake(squareToCheck, endSquare, direction.FromBelow);
            }

            //Check DOWN 1 ROW
            squareToCheck = GetSquare(endSquare.Row + 1, endSquare.Column);
            if (squareToCheck != null) //Is a valid square
            {
                SearchAroundForTake(squareToCheck, endSquare, direction.FromAbove);
            }

            //Check LEFT 1 COLUMN
            squareToCheck = GetSquare(endSquare.Row, endSquare.Column - 1);
            if (squareToCheck != null) //Is a valid square
            {
                SearchAroundForTake(squareToCheck, endSquare, direction.FromRight);
            }

            //Check RIGHT 1 COLUMN
            squareToCheck = GetSquare(endSquare.Row, endSquare.Column + 1);
            if (squareToCheck != null) //Is a valid square
            {
                SearchAroundForTake(squareToCheck, endSquare, direction.FromLeft);
            }
        }
Exemplo n.º 5
0
        public SimpleSquare GetSquare(int row, int column)   //Need to be careful throughout as have a habit of swapping these.  Using row, column (y,x) here and in Check and process take
        {
            SimpleSquare retSquare = new SimpleSquare();

            try
            {
                if (column >= board.OccupationArray.GetLength(0) || column < 0 || row >= board.OccupationArray.GetLength(1) || row < 0)
                {
                    return(null);
                }
                retSquare.Occupation = board.OccupationArray[column, row];
                retSquare.SquareType = board.SquareTypeArray[column, row];
                retSquare.Row        = row;
                retSquare.Column     = column;
            }
            catch (Exception ex)
            {
                return(null);
            }

            return(retSquare);
        }
Exemplo n.º 6
0
        //Copy constructor
        public SimpleBoard(SimpleBoard input)
        {
            int SizeX = input.OccupationArray.GetLength(0);
            int SizeY = input.OccupationArray.GetLength(1);

            Square.occupation_type[,] newOcc = new Square.occupation_type[SizeX, SizeY];
            Square.square_type[,] newTypes   = new Square.square_type[SizeX, SizeY];

            for (int i = 0; i < SizeY; i++)     //Rows
            {
                for (int j = 0; j < SizeX; j++) //Columns
                {
                    newOcc[j, i] = input.OccupationArray[j, i];
                    if (input.OccupationArray[j, i] == Square.occupation_type.King)
                    {
                        this.kingSquare = new SimpleSquare(j, i, Square.occupation_type.King, input.SquareTypeArray[j, i]);
                    }
                    newTypes[j, i] = input.SquareTypeArray[j, i];
                }
            }

            this.OccupationArray = newOcc;
            this.SquareTypeArray = newTypes;
        }
Exemplo n.º 7
0
        private void SearchAroundForTake(SimpleSquare squareToCheck, SimpleSquare endSquare, direction dir)
        {
            //squareToCheck is the square with the possible piece to be taken,  endSquare is the square into which the possible taker moved, direction is the direction that the taker moved w.r.t takee.
            SimpleSquare squareTwoAway = null;

            if (squareToCheck.Occupation != Square.occupation_type.Empty && squareToCheck.Occupation != Square.occupation_type.King) //Something in the square
            {
                if (squareToCheck.AttackerPresent && (endSquare.KingPresent || endSquare.DefenderPresent))
                {
                    //Defender or King moved next to Attacker
                    //Look 2 squares away in given direction for defender or King
                    switch (dir)
                    {
                    case direction.FromAbove:
                        squareTwoAway = GetSquare(endSquare.Row + 2, endSquare.Column);
                        break;

                    case direction.FromBelow:
                        squareTwoAway = GetSquare(endSquare.Row - 2, endSquare.Column);
                        break;

                    case direction.FromLeft:
                        squareTwoAway = GetSquare(endSquare.Row, endSquare.Column + 2);
                        break;

                    case direction.FromRight:
                        squareTwoAway = GetSquare(endSquare.Row, endSquare.Column - 2);
                        break;
                    }
                    if (squareTwoAway != null)
                    {
                        if (squareTwoAway.DefenderPresent || squareTwoAway.KingPresent || squareTwoAway.SquareType == Square.square_type.Corner)
                        {
                            board.OccupationArray[squareToCheck.Column, squareToCheck.Row] = Square.occupation_type.Empty;
                            numberTakesDefender++;
                        }
                    }
                }
                if (squareToCheck.DefenderPresent && endSquare.AttackerPresent)
                {
                    //Attacker moved next to defender
                    switch (dir)
                    {
                    case direction.FromAbove:
                        squareTwoAway = GetSquare(endSquare.Row + 2, endSquare.Column);
                        break;

                    case direction.FromBelow:
                        squareTwoAway = GetSquare(endSquare.Row - 2, endSquare.Column);
                        break;

                    case direction.FromLeft:
                        squareTwoAway = GetSquare(endSquare.Row, endSquare.Column + 2);
                        break;

                    case direction.FromRight:
                        squareTwoAway = GetSquare(endSquare.Row, endSquare.Column - 2);
                        break;
                    }
                    if (squareTwoAway != null)
                    {
                        if (squareTwoAway.AttackerPresent || squareTwoAway.SquareType == Square.square_type.Corner)
                        {
                            board.OccupationArray[squareToCheck.Column, squareToCheck.Row] = Square.occupation_type.Empty;
                            numberTakesAttacker++;
                        }
                    }
                }
            }
        }