private static string AddHit(string renderedBoard, Utils.Utils.Point coords, int value = 0, int range = 1)
        {
            var sb         = new StringBuilder();
            int outOfArea  = coords.X - (range - 1);
            int totalRange = (range - 1) * 2 + 1 + (outOfArea < 0 ? outOfArea : 0);

            range--;
            foreach (var line in renderedBoard.Split('\n').Select((s, i) => new { i, s }))
            {
                var lineSb = new StringBuilder();
                if (line.i >= (coords.Y - range) * (BOX_HEIGHT - 1) && line.i <= (coords.Y + range + 1) * (BOX_HEIGHT - 1))
                {
                    lineSb.Append(line.s.Substring(0, Math.Max(0, (coords.X - range) * (BOX_WIDTH - 1))));
                    lineSb.Append(new String((value == 0 ? ' ' : '█'), totalRange * (BOX_WIDTH - 1) + 1));
                    if (line.s.Length > lineSb.Length)
                    {
                        lineSb.Append(line.s.Substring(lineSb.Length));
                    }
                    else
                    {
                        lineSb.Remove(line.s.Length, lineSb.Length - line.s.Length);
                    }
                    sb.Append(lineSb.ToString() + '\n');
                }
                else
                {
                    sb.Append(line.s + '\n');
                }
            }
            while (sb[sb.Length - 1] == '\n')
            {
                sb.Remove(sb.Length - 1, 1);
            }
            return(sb.ToString());
        }
        public static bool IsValidEndPoint(Utils.Utils.Point end)
        {
            if (Start == null)
            {
                throw new NullReferenceException("Ship start point not set!");
            }
            int ySize = Math.Abs(end.Y - Start.Y) + 1;
            int xSize = Math.Abs(end.X - Start.X) + 1;

            if (!((ySize == CurrentShip.Size.Item1 && xSize == CurrentShip.Size.Item2) || (ySize == CurrentShip.Size.Item2 && xSize == CurrentShip.Size.Item1)))
            {
                return(false);
            }
            int offset = (Options.OPTIONS["Ships may touch"] == 0 ? 1 : 0);

            for (int y = Math.Min(Start.Y, end.Y) - offset; y <= Math.Max(Start.Y, end.Y) + offset; y++)
            {
                for (int x = Math.Min(Start.X, end.X) - offset; x <= Math.Max(Start.X, end.X) + offset; x++)
                {
                    if (x < 0 || y < 0 || x >= Options.OPTIONS["Board width"] || y >= Options.OPTIONS["Board height"])
                    {
                        continue;
                    }
                    if (Players.CurrentPlayer.Board.Map[y][x].Ship != null)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #3
0
        private static void HandleKey(ConsoleKeyInfo key)
        {
            var CB     = Boards.CurrentBoard;
            var coords = new Utils.Utils.Point(CB.SelectedTile.Coords);

            switch (key.Key)
            {
            case (ConsoleKey.UpArrow):
                coords.Y--;
                goto SelectTile;

            case (ConsoleKey.DownArrow):
                coords.Y++;
                goto SelectTile;

            case (ConsoleKey.LeftArrow):
                coords.X--;
                goto SelectTile;

            case (ConsoleKey.RightArrow):
                coords.X++;
                goto SelectTile;
SelectTile:
                CB.SelectTile(coords);
                break;

            case (ConsoleKey.Spacebar):
                Players.CurrentPlayer.ShowEnemyBoard = !Players.CurrentPlayer.ShowEnemyBoard;
                break;

            case (ConsoleKey.Enter):
                if (State.Turn == 0)
                {
                    ShipPlacerController.Select();
                }
                else
                {
                    CB.SelectedTile.OnEnter();
                }
                break;

            case (ConsoleKey.Escape):
                if (State.Turn == 0 && ShipPlacerController.Start != null)
                {
                    ShipPlacerController.Start = null;
                }
                else
                {
                    Menu.CurrentNode = PauseMenu.GetPauseMenu();
                    Menu.Run();
                }
                break;

            default:
                break;
            }
        }
Beispiel #4
0
        static void AnimateHit(string board, Utils.Utils.Point point, int hitRange)
        {
            for (int i = 0; i < 8; i++)
            {
                board = AddHit(board, point, (i + 1) % 2, hitRange);
                ShowRenderedBoard(board);

                System.Threading.Thread.Sleep(100);
            }
        }
Beispiel #5
0
        public static void ShowGame(Utils.Utils.Point animateHit = null, int hitRange = 1, Players.Player player = null)
        {
            string board = GameText.RenderBoard();

            if (animateHit == null)
            {
                ShowRenderedBoard(board);
            }
            else
            {
                AnimateHit(board, animateHit, hitRange);
            }
        }
Beispiel #6
0
        public static void MoveTo(AI.IAI AI, Utils.Utils.Point p)
        {
            Func <Utils.Utils.Point> currentP = () => Boards.CurrentBoard.SelectedTile.Coords;

            while (!currentP().Equals(p))
            {
                var move = new Utils.Utils.Point(currentP());

                switch (rnd.Next(2))
                {
                case 0:
                    if (p.Y != currentP().Y)
                    {
                        move.Y += (p.Y > currentP().Y ? 1 : -1);
                    }
                    else
                    {
                        goto case 1;
                    }
                    break;

                case 1:
                    if (p.X != currentP().X)
                    {
                        move.X += (p.X > currentP().X ? 1 : -1);
                    }
                    else
                    {
                        goto case 0;
                    }
                    break;
                }

                while (Console.KeyAvailable)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Enter)
                    {
                        Boards.CurrentBoard.SelectTile(p);
                    }
                }

                Boards.CurrentBoard.SelectTile(move);
                GameUI.ShowGame();
                System.Threading.Thread.Sleep(rnd.Next(AI.MoveTimeRange.Item1, AI.MoveTimeRange.Item2));
            }
        }
        public static bool IsValidStartPoint(Utils.Utils.Point start)
        {
            int offset = (Options.OPTIONS["Ships may touch"] == 0 ? 1 : 0);

            for (int y = start.Y - offset; y <= start.Y + offset; y++)
            {
                for (int x = start.X - offset; x <= start.X + offset; x++)
                {
                    if (x < 0 || y < 0 || x >= Options.OPTIONS["Board width"] || y >= Options.OPTIONS["Board height"])
                    {
                        continue;
                    }
                    if (Players.CurrentPlayer.Board.Map[y][x].Ship != null)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #8
0
        public static void GameWinScreen()
        {
            Players.CurrentPlayer.ShowEnemyBoard = true;
            Players.CurrentPlayer.Messages.Add($"\n\t CUNGRADULATION {Players.CurrentPlayer}! YOU ARE WINNER!");

            var rnd = new Random();

            while (!EnterPressed())
            {
                int range = (rnd.Next(3) == 0 ? 2 : 1);
                var p     = new Utils.Utils.Point(rnd.Next(Options.OPTIONS["Board height"]), rnd.Next(Options.OPTIONS["Board width"]));
                Boards.CurrentBoard.Explode(p, range);
                string board = GameText.RenderBoard();
                AnimateHit(board, p, range);
                ShowRenderedBoard(board);

                System.Threading.Thread.Sleep(rnd.Next(500));
            }

            Stop = true;
        }
 public static bool Select(Utils.Utils.Point point, bool AI = false)
 {
     if (Start == null)
     {
         if (!IsValidStartPoint(point))
         {
             return(false);
         }
         Start = point;
         if (!AI && CurrentShip.HP == 1)
         {
             Select(point);
         }
     }
     else
     {
         if (IsValidEndPoint(point))
         {
             new Ships.Ship(Players.CurrentPlayer, CurrentShip, Start, point);
             CurrentShipCount++;
             Start = null;
             if (!AI)
             {
                 PrepareShip();
             }
         }
         else
         {
             if (!AI)
             {
                 Start = null;
             }
             return(false);
         }
     }
     return(true);
 }
Beispiel #10
0
 public Move(Utils.Utils.Point point, int range)
 {
     R = range;
     P = point;
 }