public void CanNotOverlapShips()
        {
            Board board = new Board();

            // let's put a carrier at (10,10), (9,10), (8,10), (7,10), (6,10)
            var carrierRequest = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(10, 10),
                Direction = ShipDirection.Left,
                ShipType = ShipType.Carrier
            };

            var carrierResponse = board.PlaceShip(carrierRequest);

            Assert.AreEqual(ShipPlacement.Ok, carrierResponse);

            // now let's put a destroyer overlapping the y coordinate
            var destroyerRequest = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(9, 9),
                Direction = ShipDirection.Down,
                ShipType = ShipType.Destroyer
            };

            var destroyerResponse = board.PlaceShip(destroyerRequest);

            Assert.AreEqual(ShipPlacement.Overlap, destroyerResponse);
        }
        public void CanNotPlaceShipOffBoard()
        {
            Board board = new Board();
            PlaceShipRequest request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(15, 10),
                Direction = ShipDirection.Up,
                ShipType = ShipType.Destroyer
            };

            var response = board.PlaceShip(request);

            Assert.AreEqual(ShipPlacement.NotEnoughSpace, response);
        }
        public void CanNotPlaceShipPartiallyOnBoard()
        {
            Board board = new Board();
            PlaceShipRequest request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(10, 10),
                Direction = ShipDirection.Right,
                ShipType = ShipType.Carrier
            };

            var response = board.PlaceShip(request);

            Assert.AreEqual(ShipPlacement.NotEnoughSpace, response);
        }
Exemple #4
0
        // writes message message revealing the result of a fireshot call/displays board with resort
        public static void ShotMessage(Board playerBoard, string boardLabel, string message)
        {
            // build some suspense by writing * * * across screen before reporting shot status
            ProcessingAnimation();

            // deliver message
            Write($"\n{message}", 30, ConsoleColor.White);

            // refresh the board to show new shot placement
            Console.Clear();
            BoardImage.Present(playerBoard, boardLabel);

            Write($"{message}", 0, ConsoleColor.White);

            Write("\n\n\t\t\tPress any key to continue.", 10);
            Console.ReadKey();
        }
Exemple #5
0
        // writes board to the screen
        public static void Present(Board board, string label, int timeDelay = 0)
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine(AsciiArt.Line + "\n\n\n" + "\n" + AsciiArt.Line + "\n");

            WriteBoard(board.BoardImage);

            // save cursor location to return after text scroll
            int returnLineCursor = Console.CursorTop;

            // scroll/animate board label at the top of the board
            Console.SetCursorPosition(0, Console.CursorTop - 35);
            ConsoleWriter.Write($"\n\t\t\t    {label.ToUpper()}\n\n", timeDelay);

            // return to bottom/original cursor position
            Console.SetCursorPosition(0, returnLineCursor);
        }
Exemple #6
0
        // this was used for testing purposes
        public static void AutoPlaceShips(Board board)
        {
            int i = 1;

            foreach (ShipType ship in Enum.GetValues(typeof(ShipType)))
            {
                PlaceShipRequest request = new PlaceShipRequest()
                {
                    Coordinate = new Coordinate(1, i),
                    Direction = ShipDirection.Right,
                    ShipType = ship,
                };

                i++;

                board.PlaceShip(request);
                board.UpdateBoardImg();
            }
        }
Exemple #7
0
        // prompts user to fire a shot and records results
        public static void PlayerTurn(Board playerBoard, Board opponentBoard, string playerName, string opponentName)
        {
            var validShot = false;

            do
            {
                // Label board with player's name
                string boardLabel = $"   {playerName}'s turn";

                BoardImage.Present(playerBoard, boardLabel, 20);

                // prompt for shot
                int[] coords = Prompt.Coord($"    FIRE AT {opponentName}'S BOARD   ");

                // fire shot
                FireShotResponse response = opponentBoard.FireShot(new Coordinate(coords[0], coords[1]));

                // report what happened to user
                validShot = ReportShotResult(playerBoard, playerName, opponentName, response, coords, boardLabel);

            } while (!validShot);
        }
Exemple #8
0
        private FireShotResponse SinkCruiser(Board board)
        {
            var coordinate = new Coordinate(3, 1);
            board.FireShot(coordinate);

            coordinate = new Coordinate(3, 2);
            board.FireShot(coordinate);

            coordinate = new Coordinate(3, 3);
            return board.FireShot(coordinate);
        }
Exemple #9
0
        private FireShotResponse SinkCarrier(Board board)
        {
            var coordinate = new Coordinate(4, 4);
            board.FireShot(coordinate);

            coordinate = new Coordinate(5, 4);
            board.FireShot(coordinate);

            coordinate = new Coordinate(6, 4);
            board.FireShot(coordinate);

            coordinate = new Coordinate(7, 4);
            board.FireShot(coordinate);

            coordinate = new Coordinate(8, 4);
            return board.FireShot(coordinate);
        }
Exemple #10
0
        private FireShotResponse SinkBattleship(Board board)
        {
            var coordinate = new Coordinate(10, 6);
            board.FireShot(coordinate);

            coordinate = new Coordinate(10, 7);
            board.FireShot(coordinate);

            coordinate = new Coordinate(10, 8);
            board.FireShot(coordinate);

            coordinate = new Coordinate(10, 9);
            return board.FireShot(coordinate);
        }
Exemple #11
0
        /// <summary>
        /// Let's set up a board as follows:
        /// Destroyer: (1,8) (2,8)
        /// Cruiser: (3,1) (3,2) (3,3)
        /// Sub: (1,5) (2,5) (3,5)
        /// Battleship: (10,6) (10,7) (10,8) (10, 9)
        /// Carrier: (4,4) (5,4) (6,4) (7,4) (8,4)
        /// 
        ///    1 2 3 4 5 6 7 8 9 10
        ///  1     R
        ///  2     R
        ///  3     R
        ///  4       C C C C C
        ///  5 S S S
        ///  6                   B
        ///  7                   B
        ///  8 D D               B
        ///  9                   B
        /// 10
        /// </summary>
        /// <returns>A board that is ready to play</returns>
        private Board SetupBoard()
        {
            Board board = new Board();

            PlaceDestroyer(board);
            PlaceCruiser(board);
            PlaceSubmarine(board);
            PlaceBattleship(board);
            PlaceCarrier(board);

            return board;
        }
Exemple #12
0
        private void PlaceSubmarine(Board board)
        {
            var request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(3, 5),
                Direction = ShipDirection.Left,
                ShipType = ShipType.Submarine
            };

            board.PlaceShip(request);
        }
Exemple #13
0
        private void PlaceDestroyer(Board board)
        {
            var request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(1,8),
                Direction = ShipDirection.Right,
                ShipType = ShipType.Destroyer
            };

            board.PlaceShip(request);
        }
Exemple #14
0
        public static void DisplayShipBoard(Board playerBoard)
        {
            string Displaychar = "~";

            for (int i = 0; i < 10; i++)
            {
                Console.Write("    {0}", _aToJ[i]);
            } //Generating across board.
            Console.Write("\n");

            for (int i = 0; i < 35; i++)
            {
                Console.Write("__");
            } //Generating Border.

            // loop to get all values (including row 10) for values to fill coordinate object (necessary?)

            for (int i = 0; i < 9; i++) //9
            {
                Console.Write("\n" + (i + 1) + " |");
                for (int j = 0; j < 10; j++) //10
                {
                    Coordinate coord = new Coordinate(j + 1, i + 1);
                    if (playerBoard.ShipHistory.ContainsKey(coord) &&
                        playerBoard.ShipHistory[coord].Equals(ShipType.Battleship))
                        //Make sure Shot is valid && is a hit.
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "B";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;

                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Carrier))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "C";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Cruiser))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "C";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Destroyer))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "D";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Submarine))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "S";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Displaychar = "~";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                }
                Console.Write("\n  |");
            }
            // loop again for row 10 because of the extra char in the label
            for (int i = 9; i < 10; i++) //10
            {
                Console.Write("\n" + (i + 1) + "|");
                for (int j = 0; j < 10; j++) //10
                {
                    Coordinate coord = new Coordinate(j + 1, i + 1);
                    if (playerBoard.ShipHistory.ContainsKey(coord) &&
                        playerBoard.ShipHistory[coord].Equals(ShipType.Battleship))
                        //Make sure Shot is valid && is a hit.
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "B";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;

                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Carrier))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "C";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Cruiser))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "C";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Destroyer))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "D";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (playerBoard.ShipHistory.ContainsKey(coord) &&
                             playerBoard.ShipHistory[coord].Equals(ShipType.Submarine))
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Displaychar = "S";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Displaychar = "~";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                }

                Console.Write("\n  |");
            }
        }
Exemple #15
0
        // Prompt user to place a ship on their board.
        public static void Place(Board board, string playerName, ShipType ship)
        {
            BoardImage.Present(board, playerName + "'s setup turn");
            ShipPlacement placementResult;

            do
            {
                // prompt for coord and direction while listing ship and ship size
                int[] coords = Prompt.Coord($"{ship} ({_shipDict[ship]})\t");

                ShipDirection direction = DirectionPrompt();

                // create ship request
                var request = new PlaceShipRequest()
                {
                    Coordinate = new Coordinate(coords[0], coords[1]),
                    Direction = direction,
                    ShipType = ship,
                };

                // request a ship placement
                placementResult = board.PlaceShip(request);
                Console.Clear();

                if (placementResult == ShipPlacement.NotEnoughSpace)
                {
                    BoardImage.Present(board, playerName + "'s setup turn");
                    ConsoleWriter.Write("\t\t\t*** Not enough space. Try again. ***",
                                        10, ConsoleColor.Red);
                }
                else if (placementResult == ShipPlacement.Overlap)
                {
                    BoardImage.Present(board, playerName + "'s setup turn");
                    ConsoleWriter.Write("\t\t*** Another ship is already there. Try again. ***",
                                         10, ConsoleColor.Red);
                }

            } while (placementResult != ShipPlacement.Ok); // loop until ship placement is valid

            board.UpdateBoardImg();    // update board image to reflect placed ships
        }
Exemple #16
0
        // Prompts a player to place ships on his/her board
        public static void ShipPlacementProcedure(Board board, string playerName)
        {
            Display.NextPlayerScreen(playerName);

            // Loop through each ship type, prompting player to place them.
            foreach (ShipType ship in Enum.GetValues(typeof(ShipType)))
            {
                PlaceShips.Place(board, playerName, ship);
                Console.Clear();
            }

            BoardImage.Present(board, playerName + "'s setup turn");

            ConsoleWriter.Write("\t\t\t  Your ships are all set!\n\n", 10);
            ConsoleWriter.Write("\t\t\t Press any key to continue", 10, ConsoleColor.White);

            Console.ReadKey();
            Console.Clear();
        }
Exemple #17
0
 public Player()
 {
     Name = "New Player";
     PlayerBoard = new Board();
 }
Exemple #18
0
        public void DisplayGameGrid()
        {
            Board board = new Board();

            if (_player1Turn)
            {
                board = player2Board;
            }
            else
            {
                board = player1Board;
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\t    A    B    C    D    E    F    G    H    I    J  \n");
            Console.ResetColor();

            for (int i = 1; i < 11; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("\t" + i);
                Console.ResetColor();

                for (int j = 1; j < 11; j++)
                {
                    Coordinate shotCoordinate = new Coordinate(j,i);
                    if (board.ShotHistory.ContainsKey(shotCoordinate))
                    {
                        if (board.ShotHistory[shotCoordinate] == ShotHistory.Miss)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            if (i == 10 && j == 1)
                            {
                                Console.Write("  M ");
                            }
                            else
                            {
                                Console.Write("   M ");
                            }
                        }

                        else if (board.ShotHistory[shotCoordinate] == ShotHistory.Hit)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            if (i == 10 && j == 1)
                            {
                                Console.Write("  H ");
                            }
                            else
                            {
                                Console.Write("   H ");
                            }
                        }
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        if (i == 10 && j == 1)
                        {
                            Console.Write("  _ ");
                        }
                        else
                        {
                            Console.Write("   _ ");
                        }
                        Console.ResetColor();
                    }
                }
                Console.WriteLine("\n");
            }
            Console.ResetColor();
        }
Exemple #19
0
 public Player(string inName, Board inBoard, int inScore)
 {
     name = inName;
     board = inBoard;
 }
Exemple #20
0
        private FireShotResponse SinkDestroyer(Board board)
        {
            var coordinate = new Coordinate(1, 8);
            board.FireShot(coordinate);

            coordinate = new Coordinate(2, 8);
            return board.FireShot(coordinate);
        }
Exemple #21
0
        private void PlaceCruiser(Board board)
        {
            var request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(3, 3),
                Direction = ShipDirection.Up,
                ShipType = ShipType.Cruiser
            };

            board.PlaceShip(request);
        }
Exemple #22
0
        private FireShotResponse SinkSubmarine(Board board)
        {
            var coordinate = new Coordinate(1, 5);

            board.FireShot(coordinate);

            coordinate = new Coordinate(2, 5);
            board.FireShot(coordinate);

            coordinate = new Coordinate(3, 5);
            return board.FireShot(coordinate);
        }
Exemple #23
0
        public static void DisplayGameBoard(Board playerBoard)
        {
            //Board newBoard = new Board();

            string Displaychar = "~";
            //get Displaychar to change based on ship placement & shot history

            for (int i = 0; i < 10; i++)
            {
                Console.Write("    {0}", _aToJ[i]);
            } //Generating across board.

            Console.Write("\n");

            for (int i = 0; i < 35; i++)
            {
                Console.Write("__");
            } //Generating Border.

            // loop to get all values (including row 10) for values to fill coordinate object (necessary?)

            for (int i = 0; i < 9; i++) //9
            {
                Console.Write("\n" + (i + 1) + " |");
                for (int j = 0; j < 10; j++) //10
                {
                    Coordinate coord = new Coordinate(j + 1, i + 1);
                    if (playerBoard.ShotHistory.ContainsKey(coord) &&
                        playerBoard.ShotHistory[coord].Equals(ShotHistory.Hit)) //Make sure Shot is valid && is a hit.
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Displaychar = "H";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;

                    }
                    else if (playerBoard.ShotHistory.ContainsKey(coord) &&
                             playerBoard.ShotHistory[coord].Equals(ShotHistory.Miss))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Displaychar = "M";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    //(playerBoard.ShotHistory.ContainsKey(coord) && playerBoard.ShotHistory.ContainsValue(ShotHistory.Unknown))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Displaychar = "~";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                }
                Console.Write("\n  |");
            }
            // loop again for row 10 because of the extra char in the label
            for (int i = 9; i < 10; i++) //10
            {
                Console.Write("\n" + (i + 1) + "|");
                for (int j = 0; j < 10; j++) //10
                {
                    Coordinate coord = new Coordinate(j + 1, i + 1);
                    if (playerBoard.ShotHistory.ContainsKey(coord) &&
                        playerBoard.ShotHistory.ContainsValue(ShotHistory.Hit))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Displaychar = "H";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;

                    }
                    if (playerBoard.ShotHistory.ContainsKey(coord) &&
                        playerBoard.ShotHistory.ContainsValue(ShotHistory.Miss))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Displaychar = "M";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    //(playerBoard.ShotHistory.ContainsKey(coord) && playerBoard.ShotHistory.ContainsValue(ShotHistory.Unknown))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Displaychar = "~";
                        Console.Write("  {0}    ", Displaychar);

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    //Console.Write("  {0}    ", Displaychar);
                }
                Console.WriteLine("\n  |");

            }
        }
Exemple #24
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;
        }
Exemple #25
0
        private void PlaceBattleship(Board board)
        {
            var request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(10, 6),
                Direction = ShipDirection.Down,
                ShipType = ShipType.Battleship
            };

            board.PlaceShip(request);
        }
Exemple #26
0
        // Workflow for setting up a new game
        public static void SetupGame()
        {
            _board1 = new Board();
            _board2 = new Board();

            ShipPlacementProcedure(_board1, _player1);
            ShipPlacementProcedure(_board2, _player2);

            // refresh the displays so that the ship placements don't show
            _board1.CreateBoardImg();
            _board2.CreateBoardImg();
        }
Exemple #27
0
        private void PlaceCarrier(Board board)
        {
            var request = new PlaceShipRequest()
            {
                Coordinate = new Coordinate(4,4),
                Direction = ShipDirection.Right,
                ShipType = ShipType.Carrier
            };

            board.PlaceShip(request);
        }