示例#1
0
        private void placeShips(bool placeMode, BattleShip current) // Function which handles placement of ships
        {
            BattleBoard Dummy = new BattleBoard();
            int         y = 0, x = 0;
            bool        vertical = true;

            while (placeMode)
            {
                Dummy.GameBoard = GameBoard.Clone() as char[, ];

                for (int i = 0; i < current.Length; i++)
                {
                    if (vertical)
                    {
                        Dummy.GameBoard[y + i, x] = Convert.ToChar(counter + 48);
                    }
                    else
                    {
                        Dummy.GameBoard[y, x + i] = Convert.ToChar(counter + 48);
                    }
                }
                Console.Clear();
                Console.WriteLine(Dummy.getplayerview());
                Console.WriteLine();
                Console.WriteLine("Use arrows to move ship.");
                Console.WriteLine("Press R to rotate ship.");
                Console.WriteLine("Press ENTER to place ship.");

                var ch = Console.ReadKey(false).Key;
                switch (ch)
                {
                case ConsoleKey.R:
                    vertical = !vertical;     // Switch between true and false
                    y        = 0;
                    x        = 0;
                    break;

                case ConsoleKey.UpArrow:
                    if (y > 0)
                    {
                        y -= 1;
                    }
                    break;

                case ConsoleKey.DownArrow:
                    if (vertical)
                    {
                        if (y < 10 - current.Length)
                        {
                            y += 1;
                        }
                    }
                    else
                    {
                        if (y < 9)
                        {
                            y += 1;
                        }
                    }
                    break;

                case ConsoleKey.LeftArrow:
                    if (x > 0)
                    {
                        x -= 1;
                    }
                    break;

                case ConsoleKey.RightArrow:
                    if (vertical)
                    {
                        if (x < 9)
                        {
                            x += 1;
                        }
                    }
                    else
                    {
                        if (x < 10 - current.Length)
                        {
                            x += 1;
                        }
                    }
                    break;

                case ConsoleKey.Enter:

                    bool valid = false;

                    for (int i = 0; i < current.Length; i++)
                    {
                        if (vertical)
                        {
                            if (GameBoard[y + i, x] == ' ')
                            {
                                valid = true;
                            }
                            else
                            {
                                valid = false;
                                break;
                            }
                        }
                        else
                        {
                            if (GameBoard[y, x + i] == ' ')
                            {
                                valid = true;
                            }
                            else
                            {
                                valid = false;
                                break;
                            }
                        }
                    }
                    if (valid)
                    {
                        for (int i = 0; i < current.Length; i++)
                        {
                            if (vertical)
                            {
                                GameBoard[y + i, x] = Convert.ToChar(counter + 48);
                            }
                            else
                            {
                                GameBoard[y, x + i] = Convert.ToChar(counter + 48);
                            }
                        }
                        counter++;
                        placeMode = false;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine();
                        Console.WriteLine("Placement not valid.");
                        Thread.Sleep(2000);
                    }
                    //Console.WriteLine(y.ToString() +", "+ x.ToString());
                    break;
                }
            }
        }
示例#2
0
 private void DoActionFor1()
 {
     battleShip = new BattleShip();
 }