Esempio n. 1
0
        public void PlayTheGame(PlayerInfo player1, PlayerInfo player2, GameBoard gameBoard)
        {
            FireShotResponse response = new FireShotResponse();
            int userTurn = 1;

            //sets to player2 to start
            PlayerInfo opponentsTurn = NextPlayersTurn(player1, player2, userTurn);
            PlayerInfo currentTurn = NextPlayersTurn(player1, player2, opponentsTurn.UserTurn);

            while (response.ShotStatus != ShotStatus.Victory)
            {
                //assigns currentTurn to player1
                Console.WriteLine("\n\t--- {0}'s TURN ---", currentTurn.UserName.ToUpper());

                //get X & Y
                Console.WriteLine("\n**Currently Displaying {0}'s Board**\n", opponentsTurn.UserName);

                DisplayBoardDuringGamePlay(opponentsTurn);

                Console.Write("\nPlayer {0}, Please enter the X & Y coordinate you would like to hit(Ex: A1): ",
                   currentTurn.UserName);

                response = opponentsTurn.MyBoard.FireShot(ConvertX.AcceptUserCoordinate(opponentsTurn, gameBoard));
                Responses(response);

                opponentsTurn = NextPlayersTurn(player1, player2, opponentsTurn.UserTurn);
                currentTurn = NextPlayersTurn(player1, player2, currentTurn.UserTurn);
                Console.WriteLine("It's now {0}'s turn. Press enter to continue", currentTurn.UserName);
                Console.ReadLine();
                Console.Clear();
            }
        }
Esempio n. 2
0
 public void AddMark(Coordinate x, FireShotResponse y)
 {
     if (y.ShotStatus == ShotStatus.Hit || y.ShotStatus == ShotStatus.HitAndSunk || y.ShotStatus == ShotStatus.Victory)
     {
         // string myString;
         boardVisual[(x.XCoordinate - 1), (x.YCoordinate - 1)] = " X";
     }
     else // shot staus miss
     {
         boardVisual[(x.XCoordinate - 1), (x.YCoordinate - 1)] = " O";
     }
 }
Esempio n. 3
0
        public FireShotResponse FireShot(Coordinate coordinate)
        {
            var response = new FireShotResponse();

            // is this coordinate on the board?
            if (!IsValidCoordinate(coordinate))
            {
                response.ShotStatus = ShotStatus.Invalid;
                return response;
            }

            // did they already try this position?
            if (ShotHistory.ContainsKey(coordinate))
            {
                response.ShotStatus = ShotStatus.Duplicate;
                return response;
            }

            CheckShipsForHit(coordinate, response);
            CheckForVictory(response);

            return response;
        }
Esempio n. 4
0
        private void CheckShipsForHit(Coordinate coordinate, FireShotResponse response)
        {
            response.ShotStatus = ShotStatus.Miss;

            foreach (var ship in _ships)
            {
                // no need to check sunk ships
                if (ship.IsSunk)
                    continue;

                ShotStatus status = ship.FireAtShip(coordinate);

                switch (status)
                {
                    case ShotStatus.HitAndSunk:
                        response.ShotStatus = ShotStatus.HitAndSunk;
                        response.ShipImpacted = ship.ShipName;
                        ShotHistory.Add(coordinate, Responses.ShotHistory.Hit);
                        break;
                    case ShotStatus.Hit:
                        response.ShotStatus = ShotStatus.Hit;
                        response.ShipImpacted = ship.ShipName;
                        ShotHistory.Add(coordinate, Responses.ShotHistory.Hit);
                        break;
                }

                // if they hit something, no need to continue looping
                if (status != ShotStatus.Miss)
                    break;
            }

            if (response.ShotStatus == ShotStatus.Miss)
            {
                ShotHistory.Add(coordinate, Responses.ShotHistory.Miss);
            }
        }
Esempio n. 5
0
 private void CheckForVictory(FireShotResponse response)
 {
     if (response.ShotStatus == ShotStatus.HitAndSunk)
     {
         // did they win?
         if (_ships.All(s => s.IsSunk))
             response.ShotStatus = ShotStatus.Victory;
     }
 }
Esempio n. 6
0
        // reports the results of the shot
        private static bool ReportShotResult(Board playerBoard, string playerName, string opponentName, 
            FireShotResponse response, int[] coords, string boardLabel)
        {
            switch (response.ShotStatus)
            {
                case ShotStatus.Invalid:
                    ConsoleWriter.Write("Invalid coord", 0);
                    return false;

                case ShotStatus.Duplicate:
                    Console.SetCursorPosition(0, Console.CursorTop - 3);
                    ConsoleWriter.Write("\t\t\t*** You already shot there. ***", 10, ConsoleColor.Red);
                    ConsoleWriter.Write("\n\n\t\t\t  Press any key to try again.              \n", 10, ConsoleColor.White);
                    Console.ReadKey();

                    return false;

                case ShotStatus.Miss:
                    playerBoard.BoardImage[coords[0], coords[1]] = " |M| "; // Record miss on player's board
                    ConsoleWriter.ShotMessage(playerBoard, boardLabel, "\t\t    YOU MISSED! BETTER LUCK NEXT TIME");
                    return true;

                case ShotStatus.Hit:
                    playerBoard.BoardImage[coords[0], coords[1]] = " |H| "; // Record hit on player's board
                    ConsoleWriter.ShotMessage(playerBoard, boardLabel,
                        $"\t\t\t   YOU HIT SOMETHING!");
                    return true;

                case ShotStatus.HitAndSunk:
                    playerBoard.BoardImage[coords[0], coords[1]] = " |H| "; // Record hit on player's board
                    ConsoleWriter.ShotMessage(playerBoard, boardLabel,
                        $"\t\t\t  YOU SUNK {opponentName.ToUpper()}'s {response.ShipImpacted.ToUpper()}!");
                    return true;

                case ShotStatus.Victory:
                    playerBoard.BoardImage[coords[0], coords[1]] = " |H| "; // Record hit on player's board
                    ConsoleWriter.ShotMessage(playerBoard, boardLabel,
                        $"\t\t\t  YOU SUNK {opponentName.ToUpper()}'s {response.ShipImpacted}!\n\t\t\t  YOU ARE THE WINNER!");
                    Synth.WinnerAnnouncement(playerName);
                    _playerHasWon = true;
                    return true;
            }
            return false;
        }
Esempio n. 7
0
        public void Responses(FireShotResponse response)
        {
            switch (response.ShotStatus)
            {
                case ShotStatus.Victory:
                    Console.WriteLine("You win!");
                    break;

                case ShotStatus.HitAndSunk:
                    Console.WriteLine("{0} Hit & Sunk, Congrats", response.ShipImpacted);
                    break;

                case ShotStatus.Hit:
                    Console.WriteLine("{0} Hit, Congrats", response.ShipImpacted);
                    break;
                case ShotStatus.Duplicate:
                    Console.WriteLine("Duplicate shot try again");
                    break;

                case ShotStatus.Invalid:
                    Console.WriteLine("Invalid shot, try again");
                    break;
                default:
                    Console.WriteLine("Miss shot, sux2bu");
                    break;
            }
        }
Esempio n. 8
0
 private void ReportStatus(FireShotResponse response)
 {
     if (response.ShotStatus == ShotStatus.Duplicate)
         Console.WriteLine("\nYou already tried that position, try again!");
     if (response.ShotStatus == ShotStatus.Hit)
         Console.WriteLine("It's a hit!");
     if (response.ShotStatus == ShotStatus.HitAndSunk)
         Console.WriteLine("Hit and sunk!!");
     if (response.ShotStatus == ShotStatus.Invalid)
         Console.WriteLine("\nNot a valid coordinate, try again!");
     if (response.ShotStatus == ShotStatus.Miss)
         Console.WriteLine("You missed.");
     if (response.ShotStatus == ShotStatus.Victory)
     {
         Console.WriteLine("You win!!!");
         endGame = true;
     }
 }
Esempio n. 9
0
        //GameWorkFlow Start Methods
        public static void StartGame(Player[] players)
        {
            Console.Clear();

            bool player1Turn = false;

            FireShotResponse playerShotFired = new FireShotResponse();

            while (playerShotFired.ShotStatus != ShotStatus.Victory)
            {
                for (int i = 0; i < players.Length; i++)
                {
                    do
                    {
                        if (i == 0)
                        {
                            DrawBoard(players[1]);
                        }
                        else
                        {
                            DrawBoard(players[0]);
                        }

                        Console.WriteLine("{0}, get ready to fire your shot?\n", players[i].Name);
                        Console.WriteLine("Enter a coordinate to fire your missile!\n");
                        string shotFired = Console.ReadLine();
                        Coordinate missle = Convert(shotFired);
                        //FireShotResponse playerShotFired = new FireShotResponse();

                        if (i == 0)
                        {
                            playerShotFired = Player2.PlayerBoard.FireShot(missle);
                            player1Turn = false;
                        }
                        else
                        {
                            playerShotFired = Player1.PlayerBoard.FireShot(missle);
                            player1Turn = true;
                        }
                        if (playerShotFired.ShotStatus == ShotStatus.Duplicate)
                        {
                            Console.WriteLine(
                                "\nThat is a duplicate shot. We will have to take in a new coordinate.\nPress Enter to continue...");
                            Console.ReadLine();
                            Console.Clear();
                        }
                    } while (playerShotFired.ShotStatus == ShotStatus.Duplicate);

                    switch (playerShotFired.ShotStatus)
                    {
                        case ShotStatus.Hit:
                            Console.WriteLine("\nYou hit something!");
                            break;
                        case ShotStatus.Miss:
                            Console.WriteLine("\nYour projectile splashes into the ocean, you missed!");
                            break;
                        case ShotStatus.HitAndSunk:
                            Console.WriteLine("\nYou sank your opponent's {0}!", playerShotFired.ShipImpacted);
                            break;
                    }

                    if (playerShotFired.ShotStatus == ShotStatus.Victory)
                    {
                        Console.WriteLine("You sank your opponent't {0}, the final ship!!", playerShotFired.ShipImpacted);
                        break;
                    }
                    Console.WriteLine(
                        "\nIt is now the next player's turn. Press enter to clear the screen and continue to the next turn.");
                    Console.ReadLine();
                    Console.Clear();
                }
            }

            if (player1Turn == false)
            {
                Console.WriteLine("\n\n{0} has won the game and beaten {1}!! Congratulations.", players[0].Name, players[1].Name);
                Console.WriteLine(@"  ______                       _____");
                Console.WriteLine(@" / _____)                     / ___ \\");
                Console.WriteLine(@"| /  ___  ____ ____   ____   | |   | |_   _ ____  ____");
                Console.WriteLine(@"| | (___)/ _  |    \ / _  )  | |   | | | | / _  )/ ___)");
                Console.WriteLine(@"| \____/( ( | | | | ( (/ /   | |___| |\ V ( (/ /| |");
                Console.WriteLine(@" \_____/ \_||_|_|_|_|\____)   \_____/  \_/ \____)_|");
                Console.WriteLine();
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("\n\n{0} has won the game and beaten {1}!! Congratulations.", players[1].Name, players[0].Name);
                Console.WriteLine(@"  ______                       _____");
                Console.WriteLine(@" / _____)                     / ___ \\");
                Console.WriteLine(@"| /  ___  ____ ____   ____   | |   | |_   _ ____  ____");
                Console.WriteLine(@"| | (___)/ _  |    \ / _  )  | |   | | | | / _  )/ ___)");
                Console.WriteLine(@"| \____/( ( | | | | ( (/ /   | |___| |\ V ( (/ /| |");
                Console.WriteLine(@" \_____/ \_||_|_|_|_|\____)   \_____/  \_/ \____)_|");
                Console.WriteLine();
                Console.ReadLine();
            }

            Console.ReadLine();
        }
Esempio n. 10
0
        public static void PlayGame(GameWorkflow game)
        {
            string coordinateInput = "";
            char[] coordinateOutput;
            bool isValidInput;
            Coordinate coordinate;
            FireShotResponse response = new FireShotResponse();

            Console.WriteLine("Your ships are set, let's start the game!");

            do
            {
                Console.WriteLine("{0}, it is now your turn.", game.CurrentPlayer.Name);
                Console.WriteLine();
                do
                {
                    do
                    {
                        Console.WriteLine("This is Your Shot History...");
                        Console.WriteLine();
                        BoardDrawer.DrawShotHistoryBoard(game.OtherPlayer);
                        Console.WriteLine();
                        Console.WriteLine("Here are your ships that got hit.");
                        Console.WriteLine();
                        BoardDrawer.DrawShotHistoryBoard(game.CurrentPlayer);
                        Console.WriteLine();
                        Console.WriteLine("{0}, pick a coordinate to fire at: ", game.CurrentPlayer.Name);
                        coordinateInput = Console.ReadLine().ToUpper();

                        isValidInput = InputChecker.CheckInput(coordinateInput);

                        if (!isValidInput)
                        {
                            Console.WriteLine();
                            Console.WriteLine("{0}, that was not a valid shot coordinate.  Please try again!",
                                game.CurrentPlayer.Name);
                            ScreenCleaner.ClearBoard();
                        }
                        else
                        {
                            coordinateOutput = coordinateInput.ToCharArray();
                            coordinate = GridConversion.CoordinateConversion(coordinateOutput);
                            response = game.OtherPlayer.PlayerBoard.FireShot(coordinate);
                        }
                    } while (!isValidInput);

                    switch (response.ShotStatus)
                    {
                        case ShotStatus.Invalid:
                            Console.WriteLine();
                            Console.WriteLine("Not a valid coordinate.");
                            ScreenCleaner.ClearBoard();
                            break;
                        case ShotStatus.Duplicate:
                            Console.WriteLine();
                            Console.WriteLine("Duplicate coordinate.");
                            ScreenCleaner.ClearBoard();
                            break;
                        case ShotStatus.Miss:
                            Console.WriteLine();
                            Console.WriteLine("That was a miss!");
                            ScreenCleaner.ClearBoard();
                            break;
                        case ShotStatus.Hit:
                            Console.WriteLine();
                            Console.WriteLine("You hit the {0} ship!", response.ShipImpacted);
                            ScreenCleaner.ClearBoard();
                            break;
                        case ShotStatus.HitAndSunk:
                            Console.WriteLine();
                            Console.WriteLine("You sank the {0} ship!", response.ShipImpacted);
                            ScreenCleaner.ClearBoard();
                            break;
                        case ShotStatus.Victory:
                            Console.WriteLine();
                            Console.WriteLine("Congratulations, {0}, you won!", game.CurrentPlayer.Name);
                            ScreenCleaner.ClearBoard();
                            break;
                    }

                } while (response.ShotStatus == ShotStatus.Invalid || response.ShotStatus == ShotStatus.Duplicate);

                game.NextTurn();

            } while (response.ShotStatus != ShotStatus.Victory);
        }
Esempio n. 11
0
        public bool player2Turn()
        {
            bool isValid = false;
            FireShotResponse response = new FireShotResponse();

            do
            {
                Console.Clear();
                player1Board.DisplayBoard();
                Console.Write("{0}, where would you like to fire?", player2.name);
                string input = Console.ReadLine().ToUpper();
                Coordinate player2Coord = ValidateCoord.getValidCoord(input);
                if (player2Coord == null)
                {
                    Console.WriteLine("That was not a valid coordinate.");
                }
                else
                {
                    response = player1Board.FireShot(player2Coord);
                    switch (response.ShotStatus)
                    {
                        case ShotStatus.Duplicate:
                            Console.Write("You've already shot there. Try again.");
                            break;
                        case ShotStatus.Hit:
                            Console.Write("YOU HAVE HIT A TARGET!");
                            isValid = true;
                            break;
                        case ShotStatus.HitAndSunk:
                            Console.Write("You sunk a ship!!");
                            isValid = true;
                            break;
                        case ShotStatus.Invalid:
                            Console.Write("Sorry, that is not a valid Coordinate. Try again.");
                            break;
                        case ShotStatus.Miss:
                            Console.Write("Awwww no ship was hit this turn.");
                            isValid = true;
                            break;
                        case ShotStatus.Victory:
                            Console.Write("Great Job!! You Won!!");
                            isValid = true;
                            isVictory = true;
                            break;
                    }
                    Console.ReadLine();
                }
            } while (!isValid);

            return isVictory;
        }
Esempio n. 12
0
 private void ShotStatusMessaging(FireShotResponse _shotResponse)
 {
     switch (_shotResponse.ShotStatus)
     {
         case ShotStatus.Invalid:
             Console.WriteLine("That isn't a valid coordinate.");
             break;
         case ShotStatus.Duplicate:
             Console.WriteLine("You have already fired at that coordinate. Press any key to shoot again.");
             break;
         case ShotStatus.Miss:
             Console.WriteLine("Shot missed.");
             break;
         case ShotStatus.Hit:
             Console.WriteLine("Your shot hit a ship!");
             break;
         case ShotStatus.HitAndSunk:
             Console.WriteLine("Your shot hit and sunk {0}'s {1}!", (_player1Turn) ? _player2Name : _player1Name,_shotResponse.ShipImpacted);
             break;
         case ShotStatus.Victory:
             Console.WriteLine("Your shot sunk the last ship! Victory is yours!");
             break;
     }
 }
Esempio n. 13
0
        public void PlayGame()
        {
            DisplayGameGrid();
            Console.WriteLine("Now that the fleets are in place, it's time to start shooting!\n");

            do
            {

                if (_player1Turn)
                {
                    Console.Write("{0}, fire a shot at a coordinate in your opponent's fleet: ", _player1Name);
                }
                else
                {
                    Console.Write("{0}, fire a shot at a coordinate in your opponent's fleet: ", _player2Name);
                }

                _userShot = Console.ReadLine().ToUpper();

                while (_userShot.Length < 2)
                {
                    Console.WriteLine("That is not a valid coordinate. Please enter a letter A through J followed by a");
                    Console.Write("number 1 through 10: ");
                    _userShot = Console.ReadLine().ToUpper();
                }

                ValidateCoordinateLetterNumber(_userShot);

                response = (_player1Turn)
                    ? player2Board.FireShot(new Coordinate(_xCoordinate, _yCoordinate))
                    : player1Board.FireShot(new Coordinate(_xCoordinate, _yCoordinate));

                if (response.ShotStatus != ShotStatus.Invalid && response.ShotStatus != ShotStatus.Duplicate)
                {
                    Console.Clear();
                    DisplayGameGrid();
                }

                ShotStatusMessaging(response);

                if (response.ShotStatus == ShotStatus.Duplicate || response.ShotStatus == ShotStatus.Invalid)
                {
                    Console.ReadKey();
                    continue;
                }

                if (response.ShotStatus != ShotStatus.Victory)
                {
                    Console.Write("Press any key to start a new player shot turn.");
                    Console.ReadKey();
                    Console.Clear();
                    _player1Turn = !_player1Turn;
                    DisplayGameGrid();
                }
            } while (response.ShotStatus != ShotStatus.Victory);

            Console.Write("Press any key.");
            Console.ReadKey();
            player1Board.ShotHistory.Clear();
            player2Board.ShotHistory.Clear();
            Console.Clear();
        }
Esempio n. 14
0
        public static void AlternatePlayerGame(Player player1, Player player2)
        {
            bool isVictory = false;
            Player currentPlayer = player1;
            Player otherPlayer = player2;
            string userInput = "";

            while (!isVictory)
            {
                Console.Clear();
                DrawBoard.DrawBoardShotHistory(otherPlayer);
                DrawBoard.DrawBoardShips(currentPlayer);
                Coordinate coord;
                bool isValidCoordinate = false;
                FireShotResponse response = new FireShotResponse();

                do
                {
                    Console.ResetColor();
                    //Talking---this is repeated a bunch...possible loop to change/alternate the saying?? Or will have to leave out.
                    SpeechSynthesizer talk = new SpeechSynthesizer();
                    talk.SetOutputToDefaultAudioDevice();
                    talk.Speak(String.Format("{0}, it's your turn. Stay on target.", currentPlayer.Name));//maybe just say FIRE!

                    Console.WriteLine("\n\n{0}, enter a coordinate to fire a shot.", currentPlayer.Name);
                    userInput = Console.ReadLine();
                    coord = ConvertInputToCoordinate(userInput);
                    isValidCoordinate = CheckValidCoord(coord);
                    if (!isValidCoordinate)
                    {
                        //Talking
                        talk.SetOutputToDefaultAudioDevice();
                        talk.Speak("  Those are not the coordinates we were looking for! ");
                        Console.WriteLine("\nInvalid Coordinate - Repeat Turn");
                    }
                    else
                    {
                        response = otherPlayer.PlayerBoard.FireShot(coord);
                        Console.Beep();
                        Console.Beep();
                        Console.Beep();
                        switch (response.ShotStatus)
                        {
                            case ShotStatus.Hit:
                                //Talking
                                talk.SetOutputToDefaultAudioDevice();
                                talk.Speak(" It's a hit! ");
                                GameWorkflow.DisplayMessages.DisplayHitMessage();
                                Console.WriteLine("\nYou hit something!, Press Enter to continue....");
                                Console.ReadLine();
                                break;
                            case ShotStatus.HitAndSunk:
                                //Talking
                               // SpeechSynthesizer talk = new SpeechSynthesizer();
                                talk.SetOutputToDefaultAudioDevice();
                                talk.Speak(string.Format("  With great power comes great responsiblity. You sank your opponents {0}",response.ShipImpacted));
                                GameWorkflow.DisplayMessages.DisplaySinkingShip();
                                Console.WriteLine("\nYou sank your opponent's {0}!, Press Enter to continue....", response.ShipImpacted);
                                Console.ReadLine();
                                break;
                            case ShotStatus.Miss:
                                //Talking
                                //SpeechSynthesizer talk = new SpeechSynthesizer();
                                talk.SetOutputToDefaultAudioDevice();
                                talk.Speak("  These are not the ships you are looking for. You didn't hit squat!");
                                GameWorkflow.DisplayMessages.DisplayMissMessage();
                                Console.WriteLine("\nYour projectile splashes into the ocean, you missed! \n\nPress Enter to continue....");
                                Console.ReadLine();
                                break;
                            case ShotStatus.Duplicate:
                                //talking
                                //SpeechSynthesizer talk = new SpeechSynthesizer();
                                talk.SetOutputToDefaultAudioDevice();
                                talk.Speak("   Hey genius!  You already tried that!");

                                Console.WriteLine("\nDuplicate shot - repeat turn.  Press Enter to continue....");
                                Console.ReadLine();
                                break;
                            case ShotStatus.Victory:
                                //Talking
                                //SpeechSynthesizer talk = new SpeechSynthesizer();
                                talk.SetOutputToDefaultAudioDevice();
                                talk.Speak(String.Format("May the force be with {0}! You sank all of your opponents ships. You won the game!", currentPlayer.Name));
                                GameWorkflow.DisplayMessages.DisplayVictoryMessage();
                                Console.WriteLine("\n{0}, you have sunk all of your opponents ships, you win!- End Game", currentPlayer.Name);
                                System.Threading.Thread.Sleep(2000);
                                break;
                        }
                    }
                } while (!isValidCoordinate || response.ShotStatus == ShotStatus.Duplicate);

                if (response.ShotStatus == ShotStatus.Victory)
                {
                    isVictory = true;
                }
                else
                {
                    isVictory = false;
                    Player tempPlayer = currentPlayer;
                    currentPlayer = otherPlayer;
                    otherPlayer = tempPlayer;

                }
            }
        }