Exemplo n.º 1
0
        static List <Coordenate> SearchNeighboring(Data data)
        {
            Coordenate        temp     = data.DecksShip[0];
            List <Coordenate> tempList = new List <Coordenate>();

            foreach (Coordenate c in data.FieldForEnemy)
            {
                if (data.Dir == Direction.None && CheckNeighborhoodWithPoint(temp, c) && !tempList.Contains(c))
                {
                    tempList.Add(c);
                }
            }

            for (int i = 0; i < 2; i++)
            {
                foreach (Coordenate c in data.FieldForEnemy)
                {
                    if (CheckExtremePoint(temp, c, data.Dir) && !tempList.Contains(c))
                    {
                        tempList.Add(c);
                    }
                }
                temp = data.DecksShip[data.DecksShip.Count - 1];
            }

            return(tempList);
        }
Exemplo n.º 2
0
        static void StepEnemy(int[,] enemyFieldForPlayer, int[,] enemyField, int[,] playerField, Data data)
        {
            if (CheckWiner(data))
            {
                return;
            }

            Coordenate coord = new Coordenate(0, 0);
            Random     rand  = new Random();

            if (data.Touch)
            {
                data.LikelyCoordinatesForEnemy = SearchNeighboring(data);

                if (data.LikelyCoordinatesForEnemy.Count == 0)
                {
                    data.Touch = false;
                }

                coord = data.LikelyCoordinatesForEnemy[rand.Next(0, data.LikelyCoordinatesForEnemy.Count)];
                data.LikelyCoordinatesForEnemy.Remove(coord);
                data.FieldForEnemy.Remove(coord);
            }
            else
            {
                coord = data.FieldForEnemy[rand.Next(0, data.FieldForEnemy.Count)];
                data.FieldForEnemy.Remove(coord);
            }

            AttackEnemy(enemyFieldForPlayer, enemyField, playerField, data, coord);
        }
Exemplo n.º 3
0
        static List <Coordenate> DecksCheck(int[,] field, int countDeck, Coordenate coord, Direction dir)
        {
            List <Coordenate> coordenatesDecks = new List <Coordenate>();

            switch (dir)
            {
            case Direction.Up:
                for (int i = coord.X - countDeck - 1; i <= coord.X + countDeck - 1; i++)
                {
                    if ((i >= 0 && i < 10) && field[i, coord.Y] == countDeck)
                    {
                        coordenatesDecks.Add(new Coordenate(i, coord.Y));
                    }
                }
                break;

            case Direction.Left:
                for (int i = coord.Y - countDeck - 1; i <= coord.Y + countDeck - 1; i++)
                {
                    if ((i >= 0 && i < 10) && field[coord.X, i] == countDeck)
                    {
                        coordenatesDecks.Add(new Coordenate(coord.X, i));
                    }
                }
                break;
            }

            return(coordenatesDecks);
        }
Exemplo n.º 4
0
        static bool CheckExtremePoint(Coordenate mainPoint, Coordenate checkPoint, Direction dir)
        {
            if (dir == Direction.Up && (checkPoint.Y == mainPoint.Y && (checkPoint.X == mainPoint.X + 1 || checkPoint.X == mainPoint.Y - 1)))
            {
                return(true);
            }
            if (dir == Direction.Left && (checkPoint.X == mainPoint.X && (checkPoint.Y == mainPoint.Y + 1 || checkPoint.Y == mainPoint.Y - 1)))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
        static int[,] MarkBorders(int[,] field, Coordenate start, Coordenate end, Data data)
        {
            for (int i = data.FieldForEnemy.Count - 1; i > 0; i--)
            {
                if ((data.FieldForEnemy[i].X >= start.X - 1 && data.FieldForEnemy[i].X <= end.X + 1) &&
                    (data.FieldForEnemy[i].Y >= start.Y - 1 && data.FieldForEnemy[i].Y <= end.Y + 1))
                {
                    field[data.FieldForEnemy[i].X, data.FieldForEnemy[i].Y] = MissValue;
                    data.FieldForEnemy.Remove(data.FieldForEnemy[i]);
                }
            }

            return(field);
        }
Exemplo n.º 6
0
        static List <Coordenate> KillCheckForPlayer(Coordenate coord, int[,] field)
        {
            int       countDeck = field[coord.X, coord.Y];
            Direction dir       = Direction.None;

            if (coord.X + 1 < 10 && field[coord.X, coord.Y] == field[coord.X + 1, coord.Y])
            {
                dir = Direction.Up;
            }
            else if (coord.X - 1 >= 0 && field[coord.X, coord.Y] == field[coord.X - 1, coord.Y])
            {
                dir = Direction.Up;
            }
            else if (coord.Y + 1 < 10 && field[coord.X, coord.Y] == field[coord.X, coord.Y + 1])
            {
                dir = Direction.Left;
            }
            else if (coord.Y - 1 >= 0 && field[coord.X, coord.Y] == field[coord.X, coord.Y - 1])
            {
                dir = Direction.Left;
            }

            return(DecksCheck(field, countDeck, coord, dir));
        }
Exemplo n.º 7
0
 static bool CheckNeighborhoodWithPoint(Coordenate mainPoint, Coordenate checkPoint)
 {
     return((checkPoint.X == mainPoint.X && (checkPoint.Y == mainPoint.Y + 1 || checkPoint.Y == mainPoint.Y - 1)) ||
            (checkPoint.Y == mainPoint.Y && (checkPoint.X == mainPoint.X + 1 || checkPoint.X == mainPoint.X - 1)));
 }
Exemplo n.º 8
0
        static void AttackEnemy(int[,] enemyFieldForPlayer, int[,] enemyField, int[,] playerField, Data data, Coordenate hitCoord)
        {
            if (playerField[hitCoord.X, hitCoord.Y] == 0)
            {
                playerField[hitCoord.X, hitCoord.Y] = MissValue;
                FieldOutput(playerField, enemyFieldForPlayer);
                Console.WriteLine("Fortunately, the enemy missed!");
                StepPlayer(enemyFieldForPlayer, enemyField, playerField, data);
            }
            else
            {
                int countDecks = 0;
                if (data.Touch)
                {
                    data.TouchCount++;
                    if (data.TouchCount == playerField[hitCoord.X, hitCoord.Y])
                    {
                        data.Touch = false;
                        data.Dir   = data.Dir == Direction.None ? FindDirection(data.DecksShip) : data.Dir;
                        data.DecksShip.Add(hitCoord);
                        data.DecksShip = StartEndShip(data.Dir, data.DecksShip);
                        countDecks     = data.DecksShip.Count;
                        playerField    = MarkBorders(playerField, data.DecksShip[0], data.DecksShip[data.DecksShip.Count - 1], data);
                    }
                    else
                    {
                        data.Dir = data.Dir == Direction.None ? FindDirection(data.DecksShip) : data.Dir;
                        data.DecksShip.Add(hitCoord);
                        data.DecksShip = StartEndShip(data.Dir, data.DecksShip);
                    }
                }
                else if (IntegrityCheck(playerField, hitCoord.X, hitCoord.Y))
                {
                    data.Touch      = true;
                    data.TouchCount = 1;
                    data.DecksShip  = new List <Coordenate>();
                    data.DecksShip.Add(hitCoord);
                    data.Dir = Direction.None;
                }
                else
                {
                    countDecks  = 1;
                    playerField = MarkBorders(playerField, hitCoord, hitCoord, data);
                }

                playerField[hitCoord.X, hitCoord.Y] = HitValue;
                FieldOutput(playerField, enemyFieldForPlayer);

                if (countDecks != 0)
                {
                    WriteHitEnemyPLeyersShip(countDecks);
                }
                else
                {
                    Console.WriteLine("You hit!");
                }

                data.CountEnemy++;
                StepEnemy(enemyFieldForPlayer, enemyField, playerField, data);
            }
        }