Exemplo n.º 1
0
        public bool PlayerFire(Player pTurn, Player pOpponent)
        {
            int          playerShotX, playerShotY;
            bool         tryAgain = true;
            bool         wonOrNot = false;
            ConsoleColor preColor = Console.ForegroundColor;

            while (tryAgain)
            {
                Console.WriteLine($"\t***********\\/ Opponent's Board \\/***********");
                _grid.Draw(pOpponent._board);

                Console.WriteLine($"\n\n\t*************\\/ Your Board \\/*************");
                _grid.DrawShips(pTurn._board);

                Console.Write($"\n\t {pTurn.name}, please choose a coordinate to fire at: ");
                string playerShot = Console.ReadLine();

                SoundPlayer fireSound = new SoundPlayer(@".\Sound\fireSound.wav");
                fireSound.Play();
                Thread.Sleep(1500);
                fireSound.Stop();

                TransformCoords shotCoords = new TransformCoords();
                playerShotX = shotCoords.Conversion(playerShot, out playerShotY);
                FireShotResponse _response = pOpponent._board.FireShot(new Coordinate(playerShotY, playerShotX));

                Console.Clear();
                switch (_response.ShotStatus)
                {
                case ShotStatus.Duplicate:
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n\a****************Duplicate shot! Please try again.****************");
                    Console.ForegroundColor = preColor;
                    break;

                case ShotStatus.Hit:
                    Console.Clear();
                    SoundPlayer hitSound = new SoundPlayer(@".\Sound\hitSound1.wav");
                    hitSound.Play();
                    _grid.Draw(pOpponent._board);
                    Console.WriteLine($"\n\tOh you hit their ship!");
                    Thread.Sleep(3000);
                    tryAgain = false;
                    hitSound.Stop();
                    break;

                case ShotStatus.HitAndSunk:
                    Console.Clear();
                    SoundPlayer hitAndSunkSound = new SoundPlayer(@".\Sound\HitAndSink.wav");
                    hitAndSunkSound.Play();
                    _grid.Draw(pOpponent._board);
                    Console.WriteLine($"\n\tYou just sunk their {_response.ShipImpacted}! Good going!");
                    Thread.Sleep(4500);
                    tryAgain = false;
                    hitAndSunkSound.Stop();
                    break;

                case ShotStatus.Invalid:
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n\a****************Invalid shot! Please try again.****************");
                    Console.ForegroundColor = preColor;
                    break;

                case ShotStatus.Victory:
                    tryAgain = false;
                    wonOrNot = true;
                    break;

                default:
                    Console.Clear();
                    SoundPlayer missSound = new SoundPlayer(@".\Sound\MissSound.wav");
                    missSound.Play();
                    _grid.Draw(pOpponent._board);
                    Console.WriteLine("\n\tAww! You missed! Try harder next time.");
                    Thread.Sleep(3500);
                    tryAgain = false;
                    missSound.Stop();
                    break;
                }
            }
            return(wonOrNot);
        }
        public void SetShip(Board _board, ShipType type)
        {
            PlaceShipRequest _request = new PlaceShipRequest();
            bool             valid    = false;

            do
            {
                _request.ShipType = type;
                int num1, num2;

                //Choosing Coordinates
                Console.WriteLine();
                Console.Write("\n{0} (5 Spaces): \n\tPlease Choose Starting Coordinate: ", Enum.GetName(typeof(ShipType), type));
                string          coordinateString = Console.ReadLine();
                TransformCoords coords           = new TransformCoords();
                num1 = coords.Conversion(coordinateString, out num2);

                _request.Coordinate = new Coordinate(num2, num1);

                //Choosing Direction
                Console.Write("\n\tPlease Choose The Direction (Up/Down/Right/Left): ");
                string directionString = Console.ReadLine();

                //validating direction chosen
                if (directionString.ToUpper() == Enum.GetName(typeof(ShipDirection), (int)0).ToUpper())
                {
                    _request.Direction = ShipDirection.Up;
                }
                else if (directionString.ToUpper() == Enum.GetName(typeof(ShipDirection), (int)1).ToUpper())
                {
                    _request.Direction = ShipDirection.Down;
                }
                else if (directionString.ToUpper() == Enum.GetName(typeof(ShipDirection), (int)2).ToUpper())
                {
                    _request.Direction = ShipDirection.Left;
                }
                else if (directionString.ToUpper() == Enum.GetName(typeof(ShipDirection), (int)3).ToUpper())
                {
                    _request.Direction = ShipDirection.Right;
                }

                //ouput error message if position is invalid
                else
                {
                    ConsoleColor preColor = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("\n********Please Choose A Valid Direction********");
                    Console.ForegroundColor = preColor;
                    continue;
                }
                var request = _board.PlaceShip(_request);
                if (ShipPlacement.Ok != request)
                {
                    ConsoleColor preColor = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("\n********P'lease Choose A Valid Location********");
                    Console.ForegroundColor = preColor;
                }
                else if (ShipPlacement.Ok == request)
                {
                    valid = true;
                }
            } while (!valid);
        }