Пример #1
0
        static Field CreateField(string name)
        {
            Console.Clear();

            Console.CursorVisible = true;

            //placement

            Field field = FieldCreator.Create();

            field.Size = size;
            var shipsBuffer = FieldGenerator.CreateBlankShips();

            int selected = 1;

            while (shipsBuffer.Count > 0)
            {
                Console.SetCursorPosition(8, 0);
                Console.Write($"{name}'s field");
                Console.SetCursorPosition(0, 2);
                Console.Write(" \t");
                for (int i = 0; i < field.Size; i++)
                {
                    Console.Write(((char)(65 + i)));
                    Console.Write(" ");
                }

                Console.WriteLine("\n");
                for (int i = 0; i < field.Size; i++)
                {
                    Console.Write(i + 1 + "\t");
                    for (int j = 0; j < field.Size; j++)
                    {
                        bool b = false;
                        foreach (var ship in field.Ships)
                        {
                            if (ship.Points.Contains(new Point(j, i)))
                            {
                                Console.Write("■ ");
                                b = true;
                                break;
                            }
                        }
                        if (!b)
                        {
                            Console.Write(". ");
                        }
                    }

                    Console.WriteLine();
                }



                var groupped    = shipsBuffer.GroupBy(x => x.Size);
                int added       = 4;
                int selectedKey = 0;
                for (int i = 0; i < groupped.Count(); i++)
                {
                    Console.SetCursorPosition(field.Size + 30, added++);
                    if (selected == i + 1)
                    {
                        Console.BackgroundColor = ConsoleColor.White;
                        Console.ForegroundColor = ConsoleColor.Black;
                        selectedKey             = groupped.ToList()[i].Key;
                    }
                    else
                    {
                        Console.ResetColor();
                    }
                    Console.Write(groupped.ToList()[i].Count() + " ");
                    var str = new string('■', groupped.ToList()[i].Key * 2);
                    Console.Write(str);
                    Console.ResetColor();
                }

                var k = Console.ReadKey().Key;
                if (k == ConsoleKey.DownArrow)
                {
                    if (selected < groupped.Count())
                    {
                        selected++;
                    }
                }
                else if (k == ConsoleKey.UpArrow)
                {
                    if (selected > 1)
                    {
                        selected--;
                    }
                }
                else if (k == ConsoleKey.Enter)
                {
                    var ship = shipsBuffer.Find(x => x.Size == selectedKey);
                    Console.SetCursorPosition(field.Size + 30, added++);
                    Console.Write("Direction? h/v");
                    Console.SetCursorPosition(field.Size + 30, added++);
                    var key = Console.ReadKey().Key;
                    if (key == ConsoleKey.H)
                    {
                        ship.Direction = ShipDirection.Horizontal;
                    }

                    if (key == ConsoleKey.V)
                    {
                        ship.Direction = ShipDirection.Vertical;
                    }
                    Console.SetCursorPosition(field.Size + 30, added++);
                    Console.Write("Enter start's position");
                    Console.SetCursorPosition(field.Size + 30, added++);
                    var pos = Console.ReadLine().Split(' ');
                    Console.SetCursorPosition(field.Size + 30, added++);

                    try
                    {
                        var x = Convert.ToChar(pos[0]) - 65;
                        var y = Convert.ToInt32(pos[1]) - 1;

                        ship.Points = new List <Point>();
                        for (int i = 0; i < ship.Size; i++)
                        {
                            if (ship.Direction == ShipDirection.Horizontal)
                            {
                                ship.Points.Add(new Point(x++, y));
                            }
                            else
                            {
                                ship.Points.Add(new Point(x, y++));
                            }
                        }

                        var b = FieldCreator.Place(ship, field);
                        if (b)
                        {
                            shipsBuffer.Remove(ship);
                        }
                    }
                    catch
                    {
                    }
                    Console.Clear();
                }
                else if (k == ConsoleKey.Escape)
                {
                    Console.CursorVisible = false;
                    Menu();
                }
            }

            Console.CursorVisible = false;
            return(field);
        }
Пример #2
0
        public void CreateBlankShips_Invoke_ReturnsListOfShips()
        {
            var list = FieldGenerator.CreateBlankShips();

            Assert.AreEqual(10, list.Count);
        }