Beispiel #1
0
        private int ExplodeMine(int[,] field)
        {
            int row = 0, col = 0;
            SetNextMinePosition(field, out row, out col);

            Mine mine = new Mine(field[row, col]);
            int[,] explodeType = mine.ExplodeType();

            int explodeMinesCount = 0;
            for (int i = -2; i < 3; i++)
            {
                for (int j = -2; j < 3; j++)
                {
                    if (row + i >= 0 && row + i < field.GetLength(0) && col + j >= 0 &&
                        col + j < field.GetLength(1))
                    {
                        if (explodeType[i + 2, j + 2] == 1)
                        {
                            if (field[row + i, col + j] > 0)
                            {
                                explodeMinesCount++;
                            }
                            field[row + i, col + j] = -1;
                        }
                    }
                }
            }

            return explodeMinesCount;
        }
        public static char[,] GenerateField(int size)
        {
            char[,] field = new char[size, size];
            int minesCount = DetermineMineCount(size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    field[i, j] = '-';
                }
            }

            List<Mine> mines = new List<Mine>();
            for (int i = 0; i < minesCount; i++)
            {
                int mineX = rand.Next(0, size);
                int mineY = rand.Next(0, size);
                Mine newMine = new Mine() { X = mineX, Y = mineY };

                if (GameServices.Contains(mines, newMine))
                {
                    i--;
                    continue;
                }

                int mineType = rand.Next('1', '6');
                field[mineX, mineY] = Convert.ToChar(mineType);
            }

            return field;
        }
Beispiel #3
0
 public int ExplodeMine(int[,] field, int x, int y)
 {
     Mine mine = new Mine(field[x, y]);
     int[,] explodeType = mine.ExplodeType();
     int explodeMinesCount = 0;
     for (int i = -2; i < 3; i++)
     {
         for (int j = -2; j < 3; j++)
         {
             if (x + i >= 0 && x + i < this.GameField && y + j >= 0 &&
                 y + j < this.GameField)
             {
                 if (explodeType[i + 2, j + 2] == 1)
                 {
                     if (field[x + i, y + j] > 0)
                     {
                         explodeMinesCount++;
                     }
                     field[x + i, y + j] = -1;
                 }
             }
         }
     }
     return explodeMinesCount;
 }
Beispiel #4
0
        public static void Explode(char[,] field, Mine mine)
        {
            char mineType = field[mine.X, mine.Y];

            switch (mineType)
            {
                case '1':
                    {
                        ExplodeOne(field, mine);
                    }
                    break;
                case '2':
                    {
                        ExplodeTwo(field, mine);
                    }
                    break;
                case '3':
                    {
                        ExplodeThree(field, mine);
                    }
                    break;
                case '4':
                    {
                        ExplodeFour(field, mine);
                    }
                    break;
                case '5':
                    {
                        ExplodeFive(field, mine);
                    }
                    break;
            }
        }
Beispiel #5
0
 public void TestMethodExplodeTypeWithCorrectDataTypeOne()
 {
     Mine mine = new Mine(1);
     int[,] expected = {{0,0,0,0,0},
                     {0,1,0,1,0},
                     {0,0,1,0,0},
                     {0,1,0,1,0},
                     {0,0,0,0,0}};
     int[,] actual = mine.ExplodeType();
     CollectionAssert.AreEqual(expected, actual);
 }
Beispiel #6
0
 public void TestMethodExplodeTypeWithInvalidData()
 {
     Mine mine = new Mine(4);
     int[,] expected = {{0,1,1,1,0},
                        {0,1,1,1,1},
                        {1,1,1,1,1},
                        {1,1,1,1,1},
                        {0,1,1,1,0}};
     int[,] actual = mine.ExplodeType();
     CollectionAssert.AreNotEqual(expected, actual);
 }
Beispiel #7
0
 private static void ExplodeFive(char[,] field, Mine mine)
 {
     for (int i = mine.X - 2; i <= mine.X + 2; i++)
     {
         for (int j = mine.Y - 2; j <= mine.Y + 2; j++)
         {
             if (GameField.IsInsideField(field, i, j))
             {
                 field[i, j] = GameField.DetonatedCell;
             }
         }
     }
 }
Beispiel #8
0
        private void StartInteraction()
        {
            string readBuffer = null;
            int    blownMines = 0;

            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine();
            }


            while (GameServices.ContainsMines(gameField))
            {
                GameServices.PokajiMiRezultata(gameField);
                Console.Write("Please enter coordinates: ");
                readBuffer = Console.ReadLine();
                Mine mineToBlow =


                    GameServices.ExtractMineFromString(readBuffer);

                while (mineToBlow == null)
                {
                    Console.Write("Please enter coordinates: ");
                    readBuffer = Console.ReadLine();
                    mineToBlow = GameServices.ExtractMineFromString(readBuffer);
                }

                if (!GameServices.IsValidMove(gameField, mineToBlow.X, mineToBlow.Y))
                {
                    Console.WriteLine("Invalid move!");
                    continue;
                }

                GameServices.Гърми(gameField, mineToBlow);
                blownMines++;
            }

            GameServices.PokajiMiRezultata(gameField);
            Console.WriteLine("Game over. Detonated mines: {0}", blownMines);
        }
Beispiel #9
0
        private static void ExplodeThree(char[,] field, Mine mine)
        {
            ExplodeTwo(field, mine);
            Mine Up = new Mine()
            {
                X = mine.X - 2, Y = mine.Y
            };
            Mine Down = new Mine()
            {
                X = mine.X + 2, Y = mine.Y
            };
            Mine Left = new Mine()
            {
                X = mine.X, Y = mine.Y - 2
            };
            Mine Right = new Mine()
            {
                X = mine.X, Y = mine.Y + 2
            };

            if (VPoletoLiE(field, Up.X, Up.Y))
            {
                field[Up.X, Up.Y] = 'X';
            }

            if (VPoletoLiE(field, Down.X, Down.Y))
            {
                field[Down.X, Down.Y] = 'X';
            }

            if (VPoletoLiE(field, Left.X, Left.Y))
            {
                field[Left.X, Left.Y] = 'X';
            }

            if (VPoletoLiE(field, Right.X, Right.Y))
            {
                field[Right.X, Right.Y] = 'X';
            }
        }
Beispiel #10
0
        private static void ExplodeFour(char[,] field, Mine mine)
        {
            for (int i = mine.X - 2; i <= mine.X + 2; i++)
            {
                for (int j = mine.Y - 2; j <= mine.Y + 2; j++)
                {
                    bool UR = i == mine.X - 2 && j == mine.Y - 2;
                    bool UL = i == mine.X - 2 && j == mine.Y + 2;
                    bool DR = i == mine.X + 2 && j == mine.Y - 2;
                    bool DL = i == mine.X + 2 && j == mine.Y + 2;

                    if (UR) continue;
                    if (UL) continue;
                    if (DR) continue;
                    if (DL) continue;

                    if (GameField.IsInsideField(field, i, j))
                    {
                        field[i, j] = GameField.DetonatedCell;
                    }
                }
            }
        }
Beispiel #11
0
        public static void Гърми(char[,] field, Mine mine)
        {
            char mineType = field[mine.X, mine.Y];

            switch (mineType)
            {
            case '1':
            {
                ExplodeOne(field, mine);
            }
            break;

            case '2':
            {
                ExplodeTwo(field, mine);
            }
            break;

            case '3':
            {
                ExplodeThree(field, mine);
            }
            break;

            case '4':
            {
                ExplodeFour(field, mine);
            }
            break;

            case '5':
            {
                ExplodeFive(field, mine);
            }
            break;
            }
        }
Beispiel #12
0
        public void DetonateMine(Coordinates position)
        {
            Mine mine = this.field.GetMine(position);

            string[] area = detonationAreaOfMine[mine];

            Coordinates center = new Coordinates(area.GetLength(0) / 2, area[0].Length / 2);

            Coordinates topLeft     = position - center;
            Coordinates bottomRight = position + center;

            this.field.ForEach(
                topLeft,
                bottomRight,
                currentPosition =>
            {
                Coordinates areaPosition = currentPosition - topLeft;

                if (area[areaPosition.Row][areaPosition.Col] != Detonator.Empty)
                {
                    this.field.Destroy(currentPosition);
                }
            });
        }
Beispiel #13
0
        public static char[,] GenerateField(int size)
        {
            char[,] field = new char[size, size];
            int minesCount = DetermineMineCount(size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    field[i, j] = '-';
                }
            }

            List <Mine> mines = new List <Mine>();

            for (int i = 0; i < minesCount; i++)
            {
                int  mineX   = rand.Next(0, size);
                int  mineY   = rand.Next(0, size);
                Mine newMine = new Mine()
                {
                    X = mineX, Y = mineY
                };

                if (GameServices.Contains(mines, newMine))
                {
                    i--;
                    continue;
                }

                int mineType = rand.Next('1', '6');
                field[mineX, mineY] = Convert.ToChar(mineType);
            }

            return(field);
        }
Beispiel #14
0
 public void TestCreateMineWithICorrectType()
 {
     Mine mine = new Mine(4);
     Assert.AreEqual(4, mine.Type);
 }
Beispiel #15
0
        // extrated method
        private static char[,] GenerateMines(char[,] field)
        {
            List<Mine> mines = new List<Mine>();

                int minesCount = DetermineMineCount(field.GetLength(0));

                for (int i = 0; i < minesCount; i++)
                {
                    int mineX = rand.Next(0, field.GetLength(0));
                    int mineY = rand.Next(0, field.GetLength(1));

                    Mine newMine = new Mine(mineX, mineY);

                    if (GameServices.CheckIfMineExists(mines, newMine))
                    {
                        i--;
                        continue;
                    }

                    int mineType = rand.Next('1', '6');
                    field[mineX, mineY] = Convert.ToChar(mineType);
                }

                return field;
        }
Beispiel #16
0
 private static void ExplodeTwo(char[,] field, Mine mine)
 {
     for (int i = mine.X - 1; i <= mine.X+1; i++)
     {
         for (int j = mine.Y - 1; j <= mine.Y+1; j++)
         {
             if(IsInsideField(field, i,j))
             {
                 field[i, j] = DetonatedCell;
             }
         }
     }
 }
Beispiel #17
0
 public void TestCreateMineWithILowerType()
 {
     Mine mine = new Mine(0);
 }
 private static void ExplodeTwo(char[,] field, Mine mine)
 {
     for (int i = mine.X - 1; i <= mine.X+1; i++)
     {
         for (int j = mine.Y - 1; j <= mine.Y+1; j++)
         {
             if(VPoletoLiE(field, i,j))
             {
                 field[i, j] = 'X';
             }
         }
     }
 }
        private static void ExplodeOne(char[,] field, Mine mine)
        {
            Mine URcorner = new Mine() { X = mine.X - 1, Y = mine.Y - 1 };
            Mine ULcorner = new Mine() { X = mine.X - 1, Y = mine.Y + 1 };
            Mine DRcorner = new Mine() { X = mine.X + 1, Y = mine.Y - 1 };
            Mine DLcorner = new Mine() { X = mine.X + 1, Y = mine.Y + 1 };

            if (VPoletoLiE(field, mine.X, mine.Y))
            {
                field[mine.X, mine.Y] = 'X';
            }

            if (VPoletoLiE(field, URcorner.X, URcorner.Y))
            {
                field[URcorner.X, URcorner.Y] = 'X';
            }

            if (VPoletoLiE(field, ULcorner.X, ULcorner.Y))
            {
                field[ULcorner.X, ULcorner.Y] = 'X';
            }

            if (VPoletoLiE(field, DRcorner.X, DRcorner.Y))
            {
                field[DRcorner.X, DRcorner.Y] = 'X';
            }

            if (VPoletoLiE(field, DLcorner.X, DLcorner.Y))
            {
                field[DLcorner.X, DLcorner.Y] = 'X';
            }
        }
        private static bool Contains(List<Mine> list, Mine mine)
        {
            //abe tui e malko typo napraveno ama pyk bachka
            foreach (Mine mina in list)
            {
                if (mina.X == mine.X && mina.Y == mine.Y)
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #21
0
 public void TestCreateMineWithIHigherType()
 {
     Mine mine = new Mine(6);
 }
Beispiel #22
0
        private void GenerateMines()
        {
            List<Mine> mines = new List<Mine>();

            int minesCount = this.DetermineMineCount();

            for (int i = 0; i < minesCount; i++)
            {
                int mineX = RandomNumber.Next(0, this.Size);
                int mineY = RandomNumber.Next(0, this.Size);
                Mine newMine = new Mine(mineX, mineY);

                if (this.CheckIfMineExists(newMine, mines))
                {
                    i--;
                    continue;
                }

                mines.Add(newMine);
                int mineType = RandomNumber.Next('1', '6');
                this.Field[mineX, mineY] = Convert.ToChar(mineType);
            }
        }
Beispiel #23
0
        private bool CheckIfMineExists(Mine newMine, List<Mine> mines)
        {
            foreach (Mine currentMine in mines)
            {
                if (currentMine.X == newMine.X && currentMine.Y == newMine.Y)
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #24
0
 public void TestCreateMineWithBorderCase()
 {
     Mine mine = new Mine(1);
 }
        private static void ExplodeFour(char[,] field, Mine mine)
        {
            for (int i = mine.X - 2; i <= mine.X + 2; i++)
            {
                for (int j = mine.Y - 2; j <= mine.Y + 2; j++)
                {
                    bool UR = i == mine.X - 2 && j == mine.Y - 2;
                    bool UL = i == mine.X - 2 && j == mine.Y + 2;
                    bool DR = i == mine.X + 2 && j == mine.Y - 2;
                    bool DL = i == mine.X + 2 && j == mine.Y + 2;

                    if (UR) continue;
                    if (UL) continue;
                    if (DR) continue;
                    if (DL) continue;

                    if (VPoletoLiE(field, i, j))
                    {
                        field[i, j] = 'X';
                    }
                }
            }
        }
Beispiel #26
0
        private static void ExplodeOne(char[,] field, Mine mine)
        {
            Mine URcorner = new Mine(mine.X - 1, mine.Y - 1);
            Mine ULcorner = new Mine(mine.X - 1, mine.Y + 1);
            Mine DRcorner = new Mine(mine.X + 1, mine.Y - 1);
            Mine DLcorner = new Mine(mine.X + 1, mine.Y + 1);

            if (IsInsideField(field, mine.X, mine.Y))
            {
                field[mine.X, mine.Y] = DetonatedCell;
            }

            if (IsInsideField(field, URcorner.X, URcorner.Y))
            {
                field[URcorner.X, URcorner.Y] = DetonatedCell;
            }

            if (IsInsideField(field, ULcorner.X, ULcorner.Y))
            {
                field[ULcorner.X, ULcorner.Y] = DetonatedCell;
            }

            if (IsInsideField(field, DRcorner.X, DRcorner.Y))
            {
                field[DRcorner.X, DRcorner.Y] = DetonatedCell;
            }

            if (IsInsideField(field, DLcorner.X, DLcorner.Y))
            {
                field[DLcorner.X, DLcorner.Y] = DetonatedCell;
            }
        }
        private static void ExplodeThree(char[,] field, Mine mine)
        {
            ExplodeTwo(field, mine);
            Mine Up = new Mine() { X = mine.X - 2, Y = mine.Y };
            Mine Down = new Mine() { X = mine.X + 2, Y = mine.Y };
            Mine Left = new Mine() { X = mine.X, Y = mine.Y - 2 };
            Mine Right = new Mine() { X = mine.X, Y = mine.Y + 2 };

            if (VPoletoLiE(field, Up.X, Up.Y))
            {
                field[Up.X, Up.Y] = 'X';
            }

            if (VPoletoLiE(field, Down.X, Down.Y))
            {
                field[Down.X, Down.Y] = 'X';
            }

            if (VPoletoLiE(field, Left.X, Left.Y))
            {
                field[Left.X, Left.Y] = 'X';
            }

            if (VPoletoLiE(field, Right.X, Right.Y))
            {
                field[Right.X, Right.Y] = 'X';
            }
        }
Beispiel #28
0
        private static void ExplodeThree(char[,] field, Mine mine)
        {
            ExplodeTwo(field, mine);
            Mine Up = new Mine(mine.X - 2, mine.Y);
            Mine Down = new Mine(mine.X + 2, mine.Y);
            Mine Left = new Mine(mine.X, mine.Y - 2);
            Mine Right = new Mine(mine.X, mine.Y + 2);

            if (IsInsideField(field, Up.X, Up.Y))
            {
                field[Up.X, Up.Y] = DetonatedCell;
            }

            if (IsInsideField(field, Down.X, Down.Y))
            {
                field[Down.X, Down.Y] = DetonatedCell;
            }

            if (IsInsideField(field, Left.X, Left.Y))
            {
                field[Left.X, Left.Y] = DetonatedCell;
            }

            if (IsInsideField(field, Right.X, Right.Y))
            {
                field[Right.X, Right.Y] = DetonatedCell;
            }
        }
Beispiel #29
0
        private static bool CheckIfMineExists(IList<Mine> list, Mine mine)
        {
            //renaming
            foreach (Mine currentMine in list)
            {
                if (currentMine.X == mine.X && currentMine.Y == mine.Y)
                {
                    return true;
                }
            }

            list.Add(mine);

            return false;
        }
Beispiel #30
0
 public void TestCreateMineWithNegative()
 {
     Mine mine = new Mine(-1);
 }