Пример #1
0
        public void checkLongMoves(List <int[]> directions, PlayingPiece[,] board)
        {
            foreach (int[] dir in directions)
            {
                int[] checkPos = (int[])pos.Clone();
                checkPos [0] += dir [0];
                checkPos [1] += dir [1];
                SquareStatus square = getSquareState(board, checkPos);

                while (square != SquareStatus.outOfBounds)
                {
                    if (square == SquareStatus.occupied)
                    {
                        if (board [checkPos [0], checkPos [1]].getColor() != getColor())
                        {
                            avaliableMoves.Add((int[])checkPos.Clone());
                        }

                        break;
                    }


                    avaliableMoves.Add((int[])checkPos.Clone());

                    checkPos [0] += dir [0];
                    checkPos [1] += dir [1];
                    square        = getSquareState(board, checkPos);
                }
            }
        }
Пример #2
0
        public static void DisplayBoard(Board board)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int k = 0; k < 10; k++)
                {
                    SquareStatus temp = board.BoardState[new Coordinate(i, k)];

                    switch (temp.ToString())
                    {
                    case "Empty":
                        Console.Write("O");
                        break;

                    case "Wall":
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.Write("W");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "BlueBase":
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.Write("B");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "RedBase":
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("B");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "SingleBlue":
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.Write("S");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "DoubleBlue":
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.Write("D");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "SingleRed":
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("S");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;

                    case "DoubleRed":
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("D");
                        Console.ForegroundColor = ConsoleColor.White;
                        break;
                    }
                }
                Console.WriteLine();
            }
        }
Пример #3
0
        public override void calcAvailableMoves(PlayingPiece[,] board)
        {
            avaliableMoves.Clear();
            foreach (int[] dir in moveDirections)
            {
                int[] checkPos = new int[2] {
                    pos [0] + dir [0], pos [1] + dir [1]
                };

                SquareStatus square = getSquareState(board, checkPos);
                if (square == SquareStatus.outOfBounds)
                {
                    continue;
                }

                if (square == SquareStatus.empty)
                {
                    avaliableMoves.Add((int[])checkPos.Clone());
                }
                else if (board[checkPos[0], checkPos[1]].getColor() != playerColor)
                {
                    avaliableMoves.Add((int[])checkPos.Clone());
                }
            }
        }
Пример #4
0
        internal static SquareStatus[,] GenerateBigGridFromFile()
        {
            string[] gridStringArray = System.IO.File.ReadAllLines("../../../grid.txt");

            SquareStatus[,] bigGrid = new SquareStatus[33, 33];
            int c = 0;

            foreach (var line in gridStringArray)
            {
                int r = 0;
                foreach (var zeroOrOne in line)
                {
                    if (zeroOrOne == '1')
                    {
                        bigGrid[c, r] = SquareStatus.wall;
                    }
                    else
                    {
                        bigGrid[c, r] = SquareStatus.free;
                    }

                    r++;
                }

                c++;
            }
            return(bigGrid);
        }
    public override int GetHashCode()
    {
        int hashcode = 157;

        unchecked {
            if (__isset.square)
            {
                hashcode = (hashcode * 397) + Square.GetHashCode();
            }
            if (__isset.squareAuthority)
            {
                hashcode = (hashcode * 397) + SquareAuthority.GetHashCode();
            }
            if (__isset.squareStatus)
            {
                hashcode = (hashcode * 397) + SquareStatus.GetHashCode();
            }
            if (__isset.squareMember)
            {
                hashcode = (hashcode * 397) + SquareMember.GetHashCode();
            }
            if (__isset.squareFeatureSet)
            {
                hashcode = (hashcode * 397) + SquareFeatureSet.GetHashCode();
            }
        }
        return(hashcode);
    }
 void AddQueenToResult(SquareStatus[,] board, int rank, int file, List<Tuple<int, int>> result)
 {
     if (SquareIsOccupied(board[rank, file]))
     {
         result.Add(new Tuple<int, int>(rank, file));
     }
 }
Пример #7
0
 public virtual void RemoveStatus(SquareStatus pStatus)
 {
     if (mStatuses.Contains(pStatus))
     {
         mStatuses.Remove(pStatus);
     }
     UpdateColors();
 }
Пример #8
0
 public virtual bool CanApplyStatus(SquareStatus status)
 {
     if (mStatuses.Contains(status))
     {
         return(false);
     }
     return(true);
 }
Пример #9
0
 /// <summary>
 /// 初始化一个方块区
 /// </summary>
 /// <param name="loc">位置</param>
 /// <param name="mined">是否为雷区</param>
 /// <param name="minesAround">周围雷区数</param>
 public Square(Point loc, bool mined, int minesAround)
 {
     _location    = loc;
     _mined       = mined;
     _minesAround = minesAround;
     _status      = SquareStatus.Idle;
     _isBombPoint = false;
 }
    public override string ToString()
    {
        var  sb      = new StringBuilder("SquareEventNotifiedCreateSquareMember(");
        bool __first = true;

        if (Square != null && __isset.square)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("Square: ");
            sb.Append(Square == null ? "<null>" : Square.ToString());
        }
        if (SquareAuthority != null && __isset.squareAuthority)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareAuthority: ");
            sb.Append(SquareAuthority == null ? "<null>" : SquareAuthority.ToString());
        }
        if (SquareStatus != null && __isset.squareStatus)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareStatus: ");
            sb.Append(SquareStatus == null ? "<null>" : SquareStatus.ToString());
        }
        if (SquareMember != null && __isset.squareMember)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareMember: ");
            sb.Append(SquareMember == null ? "<null>" : SquareMember.ToString());
        }
        if (SquareFeatureSet != null && __isset.squareFeatureSet)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareFeatureSet: ");
            sb.Append(SquareFeatureSet == null ? "<null>" : SquareFeatureSet.ToString());
        }
        sb.Append(")");
        return(sb.ToString());
    }
    public async Task ReadAsync(TProtocol iprot, CancellationToken cancellationToken)
    {
        iprot.IncrementRecursionDepth();
        try
        {
            TField field;
            await iprot.ReadStructBeginAsync(cancellationToken);

            while (true)
            {
                field = await iprot.ReadFieldBeginAsync(cancellationToken);

                if (field.Type == TType.Stop)
                {
                    break;
                }

                switch (field.ID)
                {
                case 1:
                    if (field.Type == TType.String)
                    {
                        SquareMid = await iprot.ReadStringAsync(cancellationToken);
                    }
                    else
                    {
                        await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);
                    }
                    break;

                case 2:
                    if (field.Type == TType.Struct)
                    {
                        SquareStatus = new SquareStatus();
                        await SquareStatus.ReadAsync(iprot, cancellationToken);
                    }
                    else
                    {
                        await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);
                    }
                    break;

                default:
                    await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);

                    break;
                }

                await iprot.ReadFieldEndAsync(cancellationToken);
            }

            await iprot.ReadStructEndAsync(cancellationToken);
        }
        finally
        {
            iprot.DecrementRecursionDepth();
        }
    }
Пример #12
0
 public virtual void ApplyStatus(SquareStatus pStatus)
 {
     if (!CanApplyStatus(pStatus))
     {
         return;
     }
     mStatuses.Add(pStatus);
     UpdateColors();
 }
 void ClearAllThreatenings(SquareStatus[,] board)
 {
     foreach (int rank in GetAllRanks())
     {
         foreach (int file in GetAllFiles())
         {
             ClearNonOccupied(board, rank, file);
         }
     }
 }
 void CalculateAllThreatenings(SquareStatus[,] board)
 {
     foreach (int rank in GetAllRanks())
     {
         foreach (int file in GetAllFiles())
         {
             ThreatenFromOccupied(board, rank, file);
         }
     }
 }
Пример #15
0
    public void Read(TProtocol iprot)
    {
        iprot.IncrementRecursionDepth();
        try
        {
            TField field;
            iprot.ReadStructBegin();
            while (true)
            {
                field = iprot.ReadFieldBegin();
                if (field.Type == TType.Stop)
                {
                    break;
                }
                switch (field.ID)
                {
                case 1:
                    if (field.Type == TType.String)
                    {
                        SquareMid = iprot.ReadString();
                    }
                    else
                    {
                        TProtocolUtil.Skip(iprot, field.Type);
                    }
                    break;

                case 2:
                    if (field.Type == TType.Struct)
                    {
                        SquareStatus = new SquareStatus();
                        SquareStatus.Read(iprot);
                    }
                    else
                    {
                        TProtocolUtil.Skip(iprot, field.Type);
                    }
                    break;

                default:
                    TProtocolUtil.Skip(iprot, field.Type);
                    break;
                }
                iprot.ReadFieldEnd();
            }
            iprot.ReadStructEnd();
        }
        finally
        {
            iprot.DecrementRecursionDepth();
        }
    }
Пример #16
0
 public void fireAtSquare()
 {
     if (ship != null)
     {
         ship.takeFire(this);
     }
     else
     {
         status = SquareStatus.miss;
     }
     decideAnim();
     spawnRocket();
 }
        private void ShowGrid(IGrid grid)
        {
            SquareStatus[,] statusGrid = new SquareStatus[_yAxis, _xAxis];
            for (int i = 0; i < _yAxis; i++)
            {
                for (int j = 0; j < _xAxis; j++)
                {
                    statusGrid[i, j] = _checkSquare.Check(grid, (uint)i, (uint)j);
                }
            }

            _userOperations.PrintGrid(statusGrid);
        }
Пример #18
0
    public async Task WriteAsync(TProtocol oprot, CancellationToken cancellationToken)
    {
        oprot.IncrementRecursionDepth();
        try
        {
            var struc = new TStruct("SquareInfo");
            await oprot.WriteStructBeginAsync(struc, cancellationToken);

            var field = new TField();
            if (Square != null && __isset.square)
            {
                field.Name = "square";
                field.Type = TType.Struct;
                field.ID   = 1;
                await oprot.WriteFieldBeginAsync(field, cancellationToken);

                await Square.WriteAsync(oprot, cancellationToken);

                await oprot.WriteFieldEndAsync(cancellationToken);
            }
            if (SquareStatus != null && __isset.squareStatus)
            {
                field.Name = "squareStatus";
                field.Type = TType.Struct;
                field.ID   = 2;
                await oprot.WriteFieldBeginAsync(field, cancellationToken);

                await SquareStatus.WriteAsync(oprot, cancellationToken);

                await oprot.WriteFieldEndAsync(cancellationToken);
            }
            if (SquareNoteStatus != null && __isset.squareNoteStatus)
            {
                field.Name = "squareNoteStatus";
                field.Type = TType.Struct;
                field.ID   = 3;
                await oprot.WriteFieldBeginAsync(field, cancellationToken);

                await SquareNoteStatus.WriteAsync(oprot, cancellationToken);

                await oprot.WriteFieldEndAsync(cancellationToken);
            }
            await oprot.WriteFieldStopAsync(cancellationToken);

            await oprot.WriteStructEndAsync(cancellationToken);
        }
        finally
        {
            oprot.DecrementRecursionDepth();
        }
    }
Пример #19
0
        public void ResetSquare()
        {
            // set type to square
            this.innerType_ = SquareType.SQUARE;

            // set square to empty
            this.innerStatus_ = SquareStatus.EMPTY;

            // no path information
            this.innerPathStatus_ = SquarePathStatus.NONE;

            // also no owners
            this.Owners.Clear();
        }
        public void UpdatePlayerGuess(Location loc)
        {
            SquareStatus square = computerBoard.GetSquareStatus(loc);

            if (square == SquareStatus.Empty)
            {
                computerBoard.squares[loc.Row, loc.Column] = SquareStatus.Guessed;
            }
            else if (square == SquareStatus.Occupied)
            {
                computerBoard.squares[loc.Row, loc.Column] = SquareStatus.Hit;
                square = SquareStatus.Hit;
            }
            computerBoard.SinkShips();
        }
Пример #21
0
        public void CheckSquare_ReturnsValidSquareStatus(bool isMine, bool isUncovered, bool isFlagged, uint adjacentSquaresMines,
                                                         SquareStatus squareStatus)
        {
            var grid = new Grid {
                MineField = new Square[1, 1]
            };
            var square = new Square(isMine, isUncovered, isFlagged)
            {
                AdjacentSquaresWithMines = adjacentSquaresMines
            };

            grid.MineField[0, 0] = square;
            var check = new CheckSquare();

            var status = check.Check(grid, 0, 0);

            Assert.Equal(squareStatus, status);
        }
Пример #22
0
        /// <summary>
        /// 左键单击
        /// </summary>
        /// <returns>是否踩到雷</returns>
        public bool LeftClick()
        {
            switch (_status)
            {
            case SquareStatus.Idle:
            case SquareStatus.Queried:
                _status = SquareStatus.Opened;
                return(_mined);

            case SquareStatus.Opened:
            case SquareStatus.Marked:
            case SquareStatus.GameOver:
                return(false);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #23
0
        public override void calcAvailableMoves(PlayingPiece[,] board)
        {
            avaliableMoves.Clear();
            int yDir = playerColor == Color.white ? 1 : -1;

            //Forward Walk
            SquareStatus forwardSquare = getSquareState(board, new int[2] {
                pos[0], pos[1] + yDir
            });

            if (forwardSquare == SquareStatus.empty)
            {
                avaliableMoves.Add(new int[2] {
                    pos [0], pos [1] + yDir
                });
                //Double step
                if (canDoubleStep && getSquareState(board, new int[2] {
                    pos[0], pos[1] + yDir * 2
                }) == SquareStatus.empty)
                {
                    avaliableMoves.Add(new int[2] {
                        pos [0], pos [1] + yDir * 2
                    });
                }
            }

            //Attack Squares
            for (int x = -1; x < 2; x += 2)
            {
                int[]        checkPos = new int[] { pos [0] + x, pos [1] + yDir };
                SquareStatus square   = getSquareState(board, checkPos);
                if (square == SquareStatus.occupied && board[pos[0] + x, pos[1] + yDir].getColor() != playerColor)
                {
                    avaliableMoves.Add((int[])checkPos.Clone());
                }
            }

            //EnPeasant
            if (canEnPeasant)
            {
                avaliableMoves.Add(enPeasantPos);
                canEnPeasant = false;
            }
        }
Пример #24
0
 public void Write(TProtocol oprot)
 {
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct("SquareInfo");
         oprot.WriteStructBegin(struc);
         TField field = new TField();
         if (Square != null && __isset.square)
         {
             field.Name = "square";
             field.Type = TType.Struct;
             field.ID   = 1;
             oprot.WriteFieldBegin(field);
             Square.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (SquareStatus != null && __isset.squareStatus)
         {
             field.Name = "squareStatus";
             field.Type = TType.Struct;
             field.ID   = 2;
             oprot.WriteFieldBegin(field);
             SquareStatus.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (SquareNoteStatus != null && __isset.squareNoteStatus)
         {
             field.Name = "squareNoteStatus";
             field.Type = TType.Struct;
             field.ID   = 3;
             oprot.WriteFieldBegin(field);
             SquareNoteStatus.Write(oprot);
             oprot.WriteFieldEnd();
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }
Пример #25
0
    public override int GetHashCode()
    {
        int hashcode = 157;

        unchecked {
            if (__isset.square)
            {
                hashcode = (hashcode * 397) + Square.GetHashCode();
            }
            if (__isset.squareStatus)
            {
                hashcode = (hashcode * 397) + SquareStatus.GetHashCode();
            }
            if (__isset.squareNoteStatus)
            {
                hashcode = (hashcode * 397) + SquareNoteStatus.GetHashCode();
            }
        }
        return(hashcode);
    }
Пример #26
0
        public void fireAtOponentSquare(char targetStatus, ShipType targetType, Vector3 spawnPos, bool horizontal)
        {
            bool spawnSunkShip = false;

            if (targetStatus == GameOverlord.singleton.hitSign || targetStatus == GameOverlord.singleton.sunkSign)
            {
                anim     = GameOverlord.singleton.hitAnim;
                animTime = animHitTime;
                status   = SquareStatus.hit;

                spawnSunkShip = targetStatus == GameOverlord.singleton.sunkSign;
            }
            else
            {
                anim     = GameOverlord.singleton.missAnim;
                animTime = animMissTime;
                status   = SquareStatus.miss;
            }

            spawnRocket(targetType, spawnPos, horizontal, spawnSunkShip);
        }
Пример #27
0
        public void giveAnPeasant(PlayingPiece[,] board)
        {
            for (int i = -1; i <= 1; i += 2)
            {
                int[] sidePos = new int[2] {
                    pos [0] + i, pos [1]
                };
                SquareStatus temp = getSquareState(board, sidePos);
                if (temp != SquareStatus.occupied)
                {
                    continue;
                }

                PlayingPiece p = board[sidePos[0], sidePos[1]];
                if (p.type != PieceType.pawn || p.getColor() == playerColor)
                {
                    continue;
                }

                (p as Pawn).enableEnPeasant(this);
            }
        }
Пример #28
0
    public override string ToString()
    {
        var  sb      = new StringBuilder("SquareInfo(");
        bool __first = true;

        if (Square != null && __isset.square)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("Square: ");
            sb.Append(Square == null ? "<null>" : Square.ToString());
        }
        if (SquareStatus != null && __isset.squareStatus)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareStatus: ");
            sb.Append(SquareStatus == null ? "<null>" : SquareStatus.ToString());
        }
        if (SquareNoteStatus != null && __isset.squareNoteStatus)
        {
            if (!__first)
            {
                sb.Append(", ");
            }
            __first = false;
            sb.Append("SquareNoteStatus: ");
            sb.Append(SquareNoteStatus == null ? "<null>" : SquareNoteStatus.ToString());
        }
        sb.Append(")");
        return(sb.ToString());
    }
Пример #29
0
        private void BoardSquare_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            Console.WriteLine(label.Name);
            string[] split = label.Name.Split(',');
            if (_game.MakeMove(Convert.ToInt32(split[0]), Convert.ToInt32(split[1])))
            {

                RedrawBoard();

                SquareStatus win = _game.CheckWin();
                if (win != SquareStatus.None)
                {
                    MessageBox.Show("The winner is: " + win);
                }
            }
            else
            {
                MessageBox.Show("Invalid move!");
            }

            uxTurnLabel.Text = "Turn: " + _game.Turn;
        }
Пример #30
0
        /// <summary>
        /// this updates the button color if its on the player's side.
        /// </summary>
        /// <param name="btn"></param>
        private void UpdateButtonPlayerSide(Button btn)
        {
            Location     btnLoc    = (Location)btn.Tag;
            SquareStatus btnSquare = ctrl.playerBoard.GetSquareStatus(btnLoc);

            if (btnSquare == SquareStatus.Guessed)
            {
                btn.Background = Brushes.DeepSkyBlue;
                btn.Content    = "G";
            }
            else if (btnSquare == SquareStatus.Hit)
            {
                btn.Background = Brushes.Firebrick;
            }
            else if (btnSquare == SquareStatus.Occupied)
            {
                btn.Background = Brushes.DarkGray;
            }
            else
            {
                btn.Background = Brushes.DeepSkyBlue;
            }
        }
Пример #31
0
        internal static SquareStatus[,] GetGrid(Kordinat cord)
        {
            SquareStatus[,] bigGrid = GenerateBigGridFromFile();

            SquareStatus[,] gameGrid = new SquareStatus[cord.X, cord.Y];

            Random rnd     = new Random();
            int    randomX = rnd.Next(0, 33 - cord.X);
            int    randomY = rnd.Next(0, 33 - cord.Y);

            for (int i = 0; i < cord.X; i++)
            {
                for (int j = 0; j < cord.Y; j++)
                {
                    gameGrid[i, j] = bigGrid[i + randomX, j + randomY];
                }
            }
            gameGrid[0, 0]                   = SquareStatus.free;
            gameGrid[0, cord.Y - 1]          = SquareStatus.free;
            gameGrid[cord.X - 1, 0]          = SquareStatus.free;
            gameGrid[cord.X - 1, cord.Y - 1] = SquareStatus.free;
            return(gameGrid);
        }
Пример #32
0
        /// <summary>
        /// This function attempts to make a legal move in the target row/column and returns true if a move is made.
        /// </summary>
        /// <param name="targetRow">row of the selected tile</param>
        /// <param name="targetCol">column of the selected tile</param>
        /// <returns></returns>
        public bool MakeMove(int targetRow, int targetCol)
        {
            BoardTile target = SelectSquare(targetRow, targetCol);

            if (SelectedTile == null)
            {
                return(false);
            }
            if (SelectedTile == target)
            {
                return(true);
            }
            else if (SelectedTile != null && target.State == SquareStatus.None)
            {
                if (SelectedTile.State == SquareStatus.Enemy)
                {
                    target.State = SelectedTile.State;
                }
                SelectedTile.State    = SquareStatus.None;
                SelectedTile.Selected = false;
                SelectedTile          = null;

                Turn = Turn == SquareStatus.Enemy ? SquareStatus.Enemy : SquareStatus.Player;

                return(true);
            }
            else if (SelectedTile != null && target.State != SquareStatus.None)
            {
                BoardTile end;
                //if(CheckAttack())
                return(false);
            }
            else
            {
                return(false);
            }
        }
Пример #33
0
        /// <summary>
        /// 右键单击
        /// </summary>
        public void RightClick()
        {
            switch (_status)
            {
            case SquareStatus.Idle:
                _status = SquareStatus.Marked;
                break;

            case SquareStatus.Opened:
            case SquareStatus.GameOver:
                break;

            case SquareStatus.Marked:
                _status = SquareStatus.Queried;
                break;

            case SquareStatus.Queried:
                _status = SquareStatus.Idle;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 void MarkThreatenedSquares(SquareStatus[,] board, int rankToClear, int fileToClear)
 {
     ThreatenSameRank(board, rankToClear);
     ThreatenSameFile(board, fileToClear);
     ThreatenRightToLeftDiagonal(board, rankToClear, fileToClear);
     ThreatenLeftToRightDiagonal(board, rankToClear, fileToClear);
 }
        int RemoveQueenOnRank(SquareStatus[,] board, int rank)
        {
            int startingfile = 0;
            foreach (int file in GetAllFiles())
            {
                if (SquareIsOccupied(board[rank, file]))
                {
                    ClearSquare(board, rank, file);
                    startingfile = file + 1;
                }
            }

            return startingfile;
        }
 int RevertLastQueenPlacement(SquareStatus[,] board, ref int rank)
 {
     rank = MoveToPreviousRank(rank);
     var startingFile = RemoveQueenOnRank(board, rank);
     rank = MoveToPreviousRank(rank);
     ClearAllThreatenings(board);
     CalculateAllThreatenings(board);
     return startingFile;
 }
 static void ClearSquare(SquareStatus[,] board, int rank, int file)
 {
     board[rank, file] = SquareStatus.Empty;
 }
 static void ThreatenSquare(SquareStatus[,] board, int rank, int file)
 {
     board[rank, file] = SquareStatus.Threatened;
 }
 public void ThreatenSquare()
 {
     status = SquareStatus.Threatened;
 }
 public void ClearSquare()
 {
     status = SquareStatus.Empty;
 }
 void ThreatenNonOccupiedSquare(SquareStatus[,] board, int rank, int file)
 {
     if (SquareIsNotOccupied(board[rank, file]))
     {
         ThreatenSquare(board, rank, file);
     }
 }
 void ThreatenRightToLeftDiagonal(SquareStatus[,] board, int rankToClear, int fileToClear)
 {
     ThreatenUpperLeftDiagonal(board, rankToClear, fileToClear);
     ThreatenLowerRightDiagonal(board, rankToClear, fileToClear);
 }
 void ThreatenSameRank(SquareStatus[,] board, int rank)
 {
     foreach(int file in GetAllFiles())
     {
         ThreatenNonOccupiedSquare(board, rank, file);
     }
 }
 void ClearNonOccupied(SquareStatus[,] board, int rank, int file)
 {
     if (SquareIsNotOccupied(board[rank, file]))
     {
         ClearSquare(board, rank, file);
     }
 }
 void ThreatenUpperRightDiagonal(SquareStatus[,] board, int rankToClear, int fileToClear)
 {
     for (int fileToUpdate = fileToClear, rankToUpdate = rankToClear;
         fileToUpdate >= 0 && rankToUpdate < boardSize;
         fileToUpdate--, rankToUpdate++)
     {
         ThreatenNonOccupiedSquare(board, rankToUpdate, fileToUpdate);
     }
 }
        bool TryPlaceQueenOnFile(SquareStatus[,] board, int rank, int startingFile)
        {
            var queenIsPlaced = false;
            for (int file = startingFile; file < boardSize; file++)
            {
                queenIsPlaced = TryPlaceQueenOnSquare(board, rank, file);
                if (queenIsPlaced)
                {
                    break;
                }
            }

            return queenIsPlaced;
        }
 void FindSolution(SquareStatus[,] board)
 {
     var startingfile = 0;
     for (int rank = 0; rank < 8; rank++)
     {
         TryPlaceQueenOnRank(board, ref rank, ref startingfile);
     }
 }
 void ThreatenFromOccupied(SquareStatus[,] board, int rank, int file)
 {
     if (SquareIsOccupied(board[rank, file]))
     {
         MarkThreatenedSquares(board, rank, file);
     }
 }
 List<Tuple<int, int>> ExtractSolution(SquareStatus[,] board)
 {
     List<Tuple<int, int>> result = new List<Tuple<int, int>>();
     foreach (int rank in GetAllRanks())
     {
         foreach (int file in GetAllFiles())
         {
             AddQueenToResult(board, rank, file, result);
         }
     }
     return result;
 }
 SquareStatus[,] CreateBoard()
 {
     SquareStatus[,] board = new SquareStatus[boardSize, boardSize];
     return board;
 }
 public void OccupySquare()
 {
     status = SquareStatus.Occupied;
 }
        public List<Tuple<int, int>> Solve()
        {
            SquareStatus[,] board = new SquareStatus[boardSize, boardSize];
            for (int rank = initialRank; rank < boardSize; rank++)
            {
                for (int file = initialFile; file < boardSize; file++)
                {
                    board[rank, file] = SquareStatus.Empty;
                }
            }

            var startingFile = 0;
            for (int rank = initialRank; rank < boardSize; rank++)
            {
                var queenIsPlaced = false;
                for (int file = startingFile; file < boardSize; file++)
                {
                    if (board[rank, file] == SquareStatus.Empty)
                    {
                        queenIsPlaced = true;
                        board[rank, file] = SquareStatus.QueenPlaced;
                        for (int fileToUpdate = initialFile; fileToUpdate < boardSize; fileToUpdate++)
                        {
                            if (board[rank, fileToUpdate] != SquareStatus.QueenPlaced)
                            {
                                board[rank, fileToUpdate] = SquareStatus.Threatened;
                            }
                        }

                        for (int rankToUpdate = initialRank; rankToUpdate < boardSize; rankToUpdate++)
                        {
                            if (board[rankToUpdate, file] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, file] = SquareStatus.Threatened;
                            }
                        }

                        for (int fileToUpdate = file, rankToUpdate = rank; fileToUpdate < boardSize && rankToUpdate < boardSize; fileToUpdate++, rankToUpdate++)
                        {
                            if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                            }
                        }

                        for (int fileToUpdate = file, rankToUpdate = rank; fileToUpdate < boardSize && rankToUpdate >= 0; fileToUpdate++, rankToUpdate--)
                        {
                            if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                            }

                        }

                        for (int fileToUpdate = file, rankToUpdate = rank; fileToUpdate >= 0 && rankToUpdate < boardSize; fileToUpdate--, rankToUpdate++)
                        {
                            if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                            }
                        }

                        for (int fileToUpdate = file, rankToUpdate = rank; fileToUpdate >= 0 && rankToUpdate >= 0; fileToUpdate--, rankToUpdate--)
                        {
                            if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                            }
                        }

                        break;
                    }
                }
                if (!queenIsPlaced)
                {
                    rank = rank - 1;
                    for (int file = initialFile; file < boardSize; file++)
                    {
                        if (board[rank, file] == SquareStatus.QueenPlaced)
                        {
                            board[rank, file] = SquareStatus.Empty;
                            startingFile = file + 1;
                        }
                    }

                    rank = rank - 1;
                    for (int rankToUpdate = initialRank; rankToUpdate < boardSize; rankToUpdate++)
                    {
                        for (int file = initialFile; file < boardSize; file++)
                        {
                            if (board[rankToUpdate, file] != SquareStatus.QueenPlaced)
                            {
                                board[rankToUpdate, file] = SquareStatus.Threatened;
                            }
                        }
                    }

                    for (int rankToCheck = initialRank; rankToCheck < boardSize; rankToCheck++)
                    {
                        for (int fileToCheck = initialFile; fileToCheck < boardSize; fileToCheck++)
                        {
                            if (board[rankToCheck, fileToCheck] == SquareStatus.QueenPlaced)
                            {
                                for (int fileToUpdate = initialFile; fileToUpdate < boardSize; fileToUpdate++)
                                {
                                    if (board[rankToCheck, fileToUpdate] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToCheck, fileToUpdate] = SquareStatus.Threatened;
                                    }
                                }

                                for (int rankToUpdate = initialRank; rankToUpdate < boardSize; rankToUpdate++)
                                {
                                    if (board[rankToUpdate, fileToCheck] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToUpdate, fileToCheck] = SquareStatus.Threatened;
                                    }
                                }

                                for (int fileToUpdate = fileToCheck, rankToUpdate = rankToCheck; fileToUpdate < boardSize && rankToUpdate < boardSize; fileToUpdate++, rankToUpdate++)
                                {
                                    if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                                    }
                                }

                                for (int fileToUpdate = fileToCheck, rankToUpdate = rankToCheck; fileToUpdate < boardSize && rankToUpdate >= 0; fileToUpdate++, rankToUpdate--)
                                {
                                    if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                                    }
                                }

                                for (int fileToUpdate = fileToCheck, rankToUpdate = rankToCheck; fileToUpdate >= 0 && rankToUpdate < boardSize; fileToUpdate--, rankToUpdate++)
                                {
                                    if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                                    }
                                }

                                for (int fileToUpdate = fileToCheck, rankToUpdate = rankToCheck; fileToUpdate >= 0 && rankToUpdate >= 0; fileToUpdate--, rankToUpdate--)
                                {
                                    if (board[rankToUpdate, fileToUpdate] != SquareStatus.QueenPlaced)
                                    {
                                        board[rankToUpdate, fileToUpdate] = SquareStatus.Threatened;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    startingFile = 0;
                }
            }

            // The below code gets all the found cells
            // in b to return as the solution to the problem
            List<Tuple<int,int>> result = new List<Tuple<int,int>>();
            for (int rank = initialRank; rank < boardSize; rank++)
            {
                for (int file = initialFile; file < boardSize; file++)
                {
                    if (board[rank, file] == SquareStatus.QueenPlaced)
                    {
                        result.Add(new Tuple<int, int>(rank, file));
                    }
                }
            }

            return result;
        }
 void ThreateLowerLeftDiagonal(SquareStatus[,] board, int rankToClear, int fileToClear)
 {
     for (int fileToUpdate = fileToClear, rankToUpdate = rankToClear;
         fileToUpdate >= 0 && rankToUpdate >= 0;
         fileToUpdate--, rankToUpdate--)
     {
         ThreatenNonOccupiedSquare(board, rankToUpdate, fileToUpdate);
     }
 }
 static bool ICanPlaceQueen(SquareStatus square)
 {
     return square == SquareStatus.Empty;
 }
 bool SquareIsOccupied(SquareStatus square)
 {
     return square == SquareStatus.Occupied;
 }
 bool SquareIsNotOccupied(SquareStatus square)
 {
     return square != SquareStatus.Occupied;
 }
        SquareStatus[,] CreateInitializedBoard()
        {
            SquareStatus[,] board = new SquareStatus[boardSize, boardSize];
            for (int rank = initialRank; rank < boardSize; rank++)
            {
                for (int file = initialFile; file < boardSize; file++)
                {
                    board[rank, file] = SquareStatus.Empty;
                }
            }

            return board;
        }
        void TryPlaceQueenOnRank(SquareStatus[,] board, ref int rank, ref int startingFile)
        {
            var queenIsPlaced = TryPlaceQueenOnFile(board, rank, startingFile);

            if (queenIsPlaced)
            {
                startingFile = 0;
            }
            else
            {
                startingFile = RevertLastQueenPlacement(board, ref rank);
            }
        }
        bool TryPlaceQueenOnSquare(SquareStatus[,] board, int rank, int file)
        {
            if (ICanPlaceQueen(board[rank, file]))
            {
                board[rank, file] = SquareStatus.Occupied;
                MarkThreatenedSquares(board, rank, file);
                return true;
            }

            return false;
        }
        List<Tuple<int, int>> ExtractSolutionFromBoardState(SquareStatus[,] board)
        {
            List<Tuple<int, int>> result = new List<Tuple<int, int>>();
            for (int rank = initialRank; rank < boardSize; rank++)
            {
                for (int file = initialFile; file < boardSize; file++)
                {
                    AddQueenToResult(board, rank, file, result);
                }
            }

            return result;
        }