Ejemplo n.º 1
0
        public static string DrawSwitcher(Player player, Ship selectedShip)
        {
            StringBuilder message = new StringBuilder();

            message.Append("   1)Switch a Ship\n   2)Rotate\n   3)Remove a Ship\n   4)Random(BETA)\n" +
                           "   --------------\n   Available Ships:\n");
            for (int i = 0; i < player.Ships.Count; i++)
            {
                if (player.Ships[i] == selectedShip)
                {
                    message.Append("-> ");
                }
                else
                {
                    message.Append("   ");
                }
                message.Append(player.Ships[i] + "\n");
            }

            message.Append("   --------------\n");

            return(message.ToString());
        }
Ejemplo n.º 2
0
        public static void Placing(Player player, GameBoard board, int[] coords = null)
        {
            player.Ships = new List <Ship>(Rules.Ships);
            if (coords == null)
            {
                coords = new[] { 0, 0 };
            }
            bool         rotation     = false;
            var          shipsAmount  = player.Ships.Count;
            ConsoleColor shipcolor    = ConsoleColor.Green;
            Ship         selectedShip = player.Ships[0];

            DrawPlacing(player, selectedShip.Length, rotation, coords, shipcolor, DrawSwitcher(player, selectedShip));
            while (shipsAmount > 0)
            {
                if (Abort)
                {
                    break;
                }
                shipsAmount = player.Ships.Count;
                if (shipsAmount == 0)
                {
                    break;
                }
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.UpArrow:
                    if (rotation)
                    {
                        if (coords[0] == 0)
                        {
                            coords[0] = board.Board.Count - 1 - selectedShip.Length + 1;
                        }
                        else
                        {
                            coords[0]--;
                        }
                    }
                    else
                    {
                        if (coords[0] == 0)
                        {
                            coords[0] = board.Board.Count - 1;
                        }
                        else
                        {
                            coords[0]--;
                        }
                    }

                    break;

                case ConsoleKey.DownArrow:
                    coords[0]++;
                    break;

                case ConsoleKey.RightArrow:
                    coords[1]++;
                    break;

                case ConsoleKey.LeftArrow:
                    if (!rotation)
                    {
                        if (coords[1] == 0)
                        {
                            coords[1] = (board.Board[0].Count - 1) - selectedShip.Length + 1;
                        }
                        else
                        {
                            coords[1]--;
                        }
                    }
                    else
                    {
                        if (coords[1] == 0)
                        {
                            coords[1] = (board.Board[0].Count - 1);
                        }
                        else
                        {
                            coords[1]--;
                        }
                    }


                    break;

                case ConsoleKey.Enter:
                    if (shipcolor == ConsoleColor.Green)
                    {
                        AI.SetPlace(board, coords, selectedShip.Length, rotation);
                        player.Ships.Remove(selectedShip);
                        if (player.Ships.Count != 0)
                        {
                            selectedShip = player.Ships[0];
                        }
                    }
                    break;

                case ConsoleKey.X:
                    Abort = true;
                    break;

                case ConsoleKey.D1:    //switch
                    if (player.Ships.IndexOf(selectedShip) == player.Ships.Count - 1)
                    {
                        selectedShip = player.Ships[0];
                    }
                    else
                    {
                        selectedShip = player.Ships[player.Ships.IndexOf(selectedShip) + 1];
                    }
                    break;

                case ConsoleKey.D2:    //rotate
                    if (rotation)
                    {
                        rotation = false;
                    }
                    else
                    {
                        rotation = true;
                    }
                    break;

                case ConsoleKey.D3:
                    try
                    {
                        var shiptoremove = AI.FindShip(player.Board, Target("Use the arrow keys to select\nEnter - remove", player, coords));
                        foreach (var point in shiptoremove.Locations)
                        {
                            board.Board[point[0]][point[1]] = BoardSquareState.Empty;
                        }
                        player.Ships.Add(shiptoremove);
                        board.Ships.Remove(shiptoremove);
                        selectedShip = shiptoremove;
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    break;

                case ConsoleKey.D4:
                    AI.AIPlacing(player.Ships, board);
                    break;
                }

                if (rotation == false)
                {
                    if (coords[0] > (board.Board.Count - 1))
                    {
                        coords[0] = 0;
                    }
                    if (coords[1] > (board.Board[0].Count - 1) - selectedShip.Length + 1)
                    {
                        coords[1] = 0;
                    }
                }

                if (rotation)
                {
                    if (coords[0] > (board.Board.Count - 1) - selectedShip.Length + 1)
                    {
                        coords[0] = 0;
                    }
                    if (coords[1] > (board.Board[0].Count - 1))
                    {
                        coords[1] = 0;
                    }
                }
                if (AI.CheckPlace(board, coords, selectedShip.Length, rotation))
                {
                    shipcolor = ConsoleColor.Green;
                }
                else
                {
                    shipcolor = ConsoleColor.Red;
                }
                var menu = DrawSwitcher(player, selectedShip);

                DrawPlacing(player, selectedShip.Length, rotation, coords, shipcolor, menu);
            }

            if (!Abort)
            {
                var sb = new StringBuilder();
                sb.Append("PRESS ANY KEY TO CONFIRM");
                Draw(player, GetBoardString(board), sb.ToString());
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.Enter:
                    break;

                case ConsoleKey.X:
                    break;
                }
            }
        }