Пример #1
0
        public void SetBotOpponentSlotsVesselOnEdgeToDot(int row, int col, string result)
        {
            int lengthVessel = BattlefieldElements.GetVesselLength(result);

            if (row == 0)
            {
                for (int rowCurr = 0; rowCurr <= lengthVessel; rowCurr++)
                {
                    this.field[rowCurr, col - 1] = this.field[rowCurr, col + 1] = BattlefieldElements.slotOccuppied;
                    this.field[rowCurr, col]     = result;

                    if (rowCurr == lengthVessel)
                    {
                        this.field[rowCurr, col] = BattlefieldElements.slotOccuppied;
                    }
                }
            } // UPPER EDGE
            else if (row == 9)
            {
                for (int rowCurr = 9; rowCurr >= 9 - lengthVessel; rowCurr--)
                {
                    this.field[rowCurr, col - 1] = this.field[rowCurr, col + 1] = BattlefieldElements.slotOccuppied;
                    this.field[rowCurr, col]     = result;

                    if (rowCurr == 9 - lengthVessel)
                    {
                        this.field[rowCurr, col] = BattlefieldElements.slotOccuppied;
                    }
                }
            } // LOWER EDGE
            else if (col == 0)
            {
                for (int colCurr = 0; colCurr <= lengthVessel; colCurr++)
                {
                    this.field[row - 1, colCurr] = this.field[row + 1, colCurr] = BattlefieldElements.slotOccuppied;
                    this.field[row, colCurr]     = result;

                    if (colCurr == lengthVessel)
                    {
                        this.field[row, colCurr] = BattlefieldElements.slotOccuppied;
                    }
                }
            } // LEFT EDGE
            else if (col == 9)
            {
                for (int colCurr = 9; colCurr >= 9 - lengthVessel; colCurr--)
                {
                    this.field[row - 1, colCurr] = this.field[row + 1, colCurr] = BattlefieldElements.slotOccuppied;
                    this.field[row, colCurr]     = result;

                    if (colCurr == 9 - lengthVessel)
                    {
                        this.field[row, colCurr] = BattlefieldElements.slotOccuppied;
                    }
                }
            } // RIGHT EDGE
        }
Пример #2
0
        public void MarkShipOnEdgeAsDestroyed(int row, int col, string vessel)
        {
            int lengthVessel = BattlefieldElements.GetVesselLength(vessel);

            if (BattlefieldElements.slotsVessels.Contains(vessel))
            {
                // TOP EDGE
                if (row == 0 && (col > 0 && col < 9))
                {
                    for (int i = 0; i < lengthVessel; i++)
                    {
                        this.playerBattlefield.SetSlot(i, col, BattlefieldElements.slotHit);
                    }
                }
                // BOTTOM EDGE
                else if (row == 9 && (col > 0 && col < 9))
                {
                    for (int i = 9; i > 9 - lengthVessel; i--)
                    {
                        this.playerBattlefield.SetSlot(i, col, BattlefieldElements.slotHit);
                    }
                }
                // LEFT EDGE
                else if (col == 0 && (row > 0 && row < 9))
                {
                    for (int i = 0; i < lengthVessel; i++)
                    {
                        this.playerBattlefield.SetSlot(row, i, BattlefieldElements.slotHit);
                    }
                }
                // RIGHT EDGE
                else if (col == 9 && (row > 0 && row < 9))
                {
                    for (int i = 9; i > 9 - lengthVessel; i--)
                    {
                        this.playerBattlefield.SetSlot(row, i, BattlefieldElements.slotHit);
                    }
                }
            }
        }
        private static void PlaceVesselInTheMiddle(string[,] field, int row, int col, int orientation, string vessel)
        {
            int vesselLength = BattlefieldElements.GetVesselLength(vessel);

            //HORIZONTAL ORIENTATION
            if (orientation == 0)
            {
                // LEFT HALF GOING RIGHT
                if (col >= 0 && col <= 4)
                {
                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = col - 1; j < col + vesselLength; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied || field[i, j] != BattlefieldElements.slotHidden))
                            {
                                field[i, j] = BattlefieldElements.slotOccuppied;
                            }

                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (i == row) && (j != col - 1 && j != col + vesselLength))
                            {
                                field[i, j] = vessel;
                            }
                        }
                    }
                }
                // RIGHT HALF GOING LEFT
                else
                {
                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = col + 1; j >= col - vesselLength; j--)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied || field[i, j] != BattlefieldElements.slotHidden))
                            {
                                field[i, j] = BattlefieldElements.slotOccuppied;
                            }

                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && i == row && (j != col + 1 && j != col - vesselLength))
                            {
                                field[i, j] = vessel;
                            }
                        }
                    }
                }
            }
            // VERTICAL ORIENTATION
            else
            {
                // UPPER PART GOING DOWN
                if (row >= 0 && row <= 4)
                {
                    for (int i = row - 1; i <= row + vesselLength; i++)
                    {
                        for (int j = col - 1; j <= col + 1; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied || field[i, j] != BattlefieldElements.slotHidden))
                            {
                                field[i, j] = BattlefieldElements.slotOccuppied;
                            }

                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && j == col && (i != row - 1 && i != row + vesselLength))
                            {
                                field[i, j] = vessel;
                            }
                        }
                    }
                }
                // LOWER PART GOING UP
                else
                {
                    for (int i = row + 1; i >= row - vesselLength; i--)
                    {
                        for (int j = col - 1; j <= col + 1; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied || field[i, j] != BattlefieldElements.slotHidden))
                            {
                                field[i, j] = BattlefieldElements.slotOccuppied;
                            }

                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (j == col) && (i != row + 1 && i != row - vesselLength))
                            {
                                field[i, j] = vessel;
                            }
                        }
                    }
                }
            }
        }
        private static void PlaceVesselOnEdge(string[,] field, int row, int col, string vessel)
        {
            int lengthVessel = BattlefieldElements.GetVesselLength(vessel);

            // UPPER EDGE
            if (row == 0 && (col > 0 && col < 9))
            {
                for (int i = 0; i <= lengthVessel; i++)
                {
                    for (int j = col - 1; j <= col + 1; j++)
                    {
                        field[i, j] = BattlefieldElements.slotOccuppied;
                        if (j == col && i != lengthVessel)
                        {
                            field[i, j] = vessel;
                        }
                    }
                }
            }
            // LOWER EDGE
            else if (row == 9 && (col > 0 && col < 9))
            {
                for (int i = 9; i >= 9 - lengthVessel; i--)
                {
                    for (int j = col - 1; j <= col + 1; j++)
                    {
                        field[i, j] = BattlefieldElements.slotOccuppied;

                        if (j == col && i != 9 - lengthVessel)
                        {
                            field[i, j] = vessel;
                        }
                    }
                }
            }
            // LEFT EDGE
            else if ((row > 0 && row < 9) && col == 0)
            {
                for (int i = row - 1; i <= row + 1; i++)
                {
                    for (int j = 0; j <= lengthVessel; j++)
                    {
                        field[i, j] = BattlefieldElements.slotOccuppied;
                        if (i == row && j != lengthVessel)
                        {
                            field[i, j] = vessel;
                        }
                    }
                }
            }
            // RIGHT EDGE
            else if ((row > 0 && row < 9) && col == 9)
            {
                for (int i = row - 1; i <= row + 1; i++)
                {
                    for (int j = 9; j >= 9 - lengthVessel; j--)
                    {
                        field[i, j] = BattlefieldElements.slotOccuppied;
                        if (i == row && j != 9 - lengthVessel)
                        {
                            field[i, j] = vessel;
                        }
                    }
                }
            }
        }
        private static bool CheckIfPlacingVesselInTheMiddleIsPossible(string[,] field, int row, int col, int orientation, string vessel)
        {
            int vesselLength = BattlefieldElements.GetVesselLength(vessel);

            //HORIZONTAL ORIENTATION
            if (orientation == 0)
            {
                // LEFT HALF GOING RIGHT
                if (col >= 0 && col <= 4)
                {
                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = col - 1; j <= col + vesselLength; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden))
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
                // RIGHT HALF GOING LEFT
                else
                {
                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = col + 1; j >= col - vesselLength; j--)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden))
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
            }
            // VERTICAL ORIENTATION
            else
            {
                // UPPER PART GOING DOWN
                if (row >= 0 && row <= 4)
                {
                    for (int i = row - 1; i <= row + vesselLength; i++)
                    {
                        for (int j = col - 1; j <= col + 1; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden))
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
                // LOWER PART GOING UP
                else
                {
                    for (int i = row + 1; i >= row - vesselLength; i--)
                    {
                        for (int j = col - 1; j <= col + 1; j++)
                        {
                            if ((i >= 0 && i <= 9 && j >= 0 && j <= 9) && (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden))
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
            }
        }
        private static bool CheckIfPlacingVesselOnEdgeIsPossible(string[,] field, int row, int col, string vessel)
        {
            int lengthVessel = BattlefieldElements.GetVesselLength(vessel);

            // IF ON TOP EDGE
            if (row == 0 && (col > 0 && col < 9))
            {
                for (int i = 0; i <= lengthVessel; i++)
                {
                    for (int j = col - 1; j <= col + 1; j++)
                    {
                        if (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            // IF ON BOTTOM EDGE
            else if (row == 9 && (col > 0 && col < 9))
            {
                for (int i = 9; i >= 9 - lengthVessel; i--)
                {
                    for (int j = col - 1; j <= col + 1; j++)
                    {
                        if (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            // IF ON LEFT EDGE
            else if ((row > 0 && row < 9) && col == 0)
            {
                for (int i = row - 1; i <= row + 1; i++)
                {
                    for (int j = 0; j <= lengthVessel; j++)
                    {
                        if (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            // IF ON RIGHT EDGE
            else if ((row > 0 && row < 9) && col == 9)
            {
                for (int i = row - 1; i <= row + 1; i++)
                {
                    for (int j = 9; j >= 9 - lengthVessel; j--)
                    {
                        if (field[i, j] != BattlefieldElements.slotOccuppied && field[i, j] != BattlefieldElements.slotHidden)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }