private static void displayShotStatus(Player otherPlayer, FireShotResponse response) { switch (response.ShotStatus) { case ShotStatus.Invalid: Console.WriteLine("Invalid shot location, try again."); break; case ShotStatus.Duplicate: Console.WriteLine("Duplicate shot locaion, try again."); break; case ShotStatus.Miss: Console.WriteLine("Miss!"); break; case ShotStatus.Hit: Console.WriteLine("Hit!"); break; case ShotStatus.HitAndSunk: Console.WriteLine($"Hit and sunk {otherPlayer.Name}'s {response.ShipImpacted}!"); break; case ShotStatus.Victory: Console.WriteLine("You win!"); break; } Console.WriteLine("Press enter to end your turn."); Console.ReadKey(); Console.Clear(); }
// Prints a message to the screen saying if that shot missed, hit, etc public static void PrintFireShotStatus(FireShotResponse fireShotResponse) { switch (fireShotResponse.ShotStatus.ToString()) { case "Invalid": PrintError("SOMEHOW AN INVALID COORDINATE GOT THROUGH"); break; case "Duplicate": PrintError("You've already fired at that location."); break; case "Miss": Console.WriteLine("You missed!"); break; case "Hit": Console.WriteLine("You hit a ship!"); break; case "HitAndSunk": Console.WriteLine("You sunk a ship!"); break; case "Victory": Console.WriteLine("You win!"); break; } }
private void CheckShipsForHit(Coordinates coordinate, FireShotResponse response) { response.ShotStatus = ShotStatus.Miss; foreach (var ship in Ships) { if (ship.IsSunk) { continue; } ShotStatus status = ship.FireAtShip(coordinate); switch (status) { case ShotStatus.Hit: response.ShotStatus = ShotStatus.Hit; response.ShipImpacted = ship.ShipName; ShotHistorys.Add(coordinate, ShotHistory.Hit); break; } if (status != ShotStatus.Miss) { break; } } if (response.ShotStatus == ShotStatus.Miss) { response.ShotStatus = ShotStatus.Miss; ShotHistorys.Add(coordinate, ShotHistory.Miss); } }
public void CanSinkShip() { Board board = SetupBoard(); Coordinate coordinate = new Coordinate(6, 10); FireShotResponse response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Hit, response.ShotStatus); Assert.AreEqual("Battleship", response.ShipImpacted); coordinate = new Coordinate(7, 10); response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Hit, response.ShotStatus); Assert.AreEqual("Battleship", response.ShipImpacted); coordinate = new Coordinate(8, 10); response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Hit, response.ShotStatus); Assert.AreEqual("Battleship", response.ShipImpacted); coordinate = new Coordinate(9, 10); response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.HitAndSunk, response.ShotStatus); Assert.AreEqual("Battleship", response.ShipImpacted); }
/// <summary> /// Check if a ship is hit /// </summary> /// <param name="coordinate"></param> /// /// <param name="response"></param> private void CheckShipsForHit(Coordinate coordinate, FireShotResponse response) { response.ShotStatus = ShotStatus.Miss; ShotStatus status = Ships[0].FireAtShip(coordinate); switch (status) { case ShotStatus.HitAndSunk: response.ShotStatus = ShotStatus.HitAndSunk; response.ShipImpacted = Ships[0].ShipName; ShotHistory.Add(coordinate, Responses.ShotHistory.Hit); break; case ShotStatus.Hit: response.ShotStatus = ShotStatus.Hit; response.ShipImpacted = Ships[0].ShipName; ShotHistory.Add(coordinate, Responses.ShotHistory.Hit); break; } if (response.ShotStatus == ShotStatus.Miss) { ShotHistory.Add(coordinate, Responses.ShotHistory.Miss); } }
//Gives responses to the player informing them of what their shot did private void ResponseToShot(FireShotResponse shotResponse) { if (shotResponse.ShotStatus == ShotStatus.Miss) { Console.WriteLine("That shot did not hit a ship."); } else if (shotResponse.ShotStatus == ShotStatus.Duplicate) { Console.WriteLine("You've already tried this coordinate. Press any key to go again"); Console.ReadLine(); StartFire(); } else if (shotResponse.ShotStatus == ShotStatus.Hit) { Console.WriteLine("You hit a ship!"); } else if (shotResponse.ShotStatus == ShotStatus.HitAndSunk) { Console.WriteLine("You hit and sunk the {0}", shotResponse.ShipImpacted); } else if (shotResponse.ShotStatus == ShotStatus.Invalid) { Console.WriteLine("That shot was invalid"); StartFire(); } }
public static void DisplayShotStatus(Player user, FireShotResponse response) { switch (response.ShotStatus) { case ShotStatus.Invalid: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Duplicate: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Miss: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Hit: Console.WriteLine($"\n{response.ShotStatus}!"); break; case ShotStatus.HitAndSunk: Console.WriteLine($"\n{response.ShotStatus}!!"); break; case ShotStatus.Victory: Console.WriteLine($"\n{response.ShotStatus}!!!!\n{user.Name} has Won!!!!"); break; } }
public static void FireShotReponse(FireShotResponse response) { //Display's output to user response from the FireShot function string output = ""; switch (response.ShotStatus) { case ShotStatus.Duplicate: output = "Duplicate"; // Attempt to FireShot at previouse location, try again break; case ShotStatus.Hit: output = "Hit"; // Fire shot and hit a Ship break; case ShotStatus.HitAndSunk: output = $"Hit And Sunk {response.ShipImpacted}"; // Fire shot and new Ship and sunk it. break; case ShotStatus.Invalid: // Invalid input when FireShot was called output = "Invalid"; break; case ShotStatus.Miss: output = "Miss"; // Fire shot and missed a Ship break; case ShotStatus.Victory: output = "Victory"; // Last Shot has been fired and all opponent's have been sunk. break; } Console.WriteLine(); Console.WriteLine("Result from Attack : " + output); }
private void CheckShipsForHit(Coordinate coordinate, FireShotResponse response) { ShotHistory shotHistory = Responses.ShotHistory.Miss; response.ShotStatus = ShotStatus.Miss; // check only un-sunk ships foreach (var ship in Ships.Where(s => !s.IsSunk)) { response.ShotStatus = ship.FireAtShip(coordinate); // if a hit, set response, history type if (response.ShotStatus != ShotStatus.Miss) { // Only return ship if sunk if (response.ShotStatus == ShotStatus.HitAndSunk) { response.ShipImpacted = ship.ShipName; } shotHistory = Responses.ShotHistory.Hit; // hit can only be 1 ship, all done break; } } // Save history _shotHistory.Add(coordinate, shotHistory); }
public void Attack(Player attacker) { bool turn = true; while (turn) { Console.WriteLine($"{attacker.Name}, your turn"); Output.BoardOutput(this); turn = true; Coordinate atckCord = Input.GetCoordinate(); FireShotResponse verify = GameBoard.FireShot(atckCord); Output.AttackMessage(this, verify); if (verify.ShotStatus == ShotStatus.Victory) { attacker.Victory = true; } if (verify.ShotStatus != ShotStatus.Duplicate && verify.ShotStatus != ShotStatus.Invalid) { turn = false; } Console.ReadLine(); Console.Clear(); } }
/// <summary> /// Method gives Shot Responce /// </summary> /// <param name="Coordinate"></param> public FireShotResponse FireShot(Coordinate coordinate) { var response = new FireShotResponse(); try { // 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); } catch (Exception) { throw; } return(response); }
private void CheckShipsForHit(Coordinate coordinate, FireShotResponse response) { ShotHistory shotHistory = Responses.ShotHistory.Miss; response.ShotStatus = ShotStatus.Miss; foreach (Ship ship in Ships) { // check only un-sunk ships if (!ship.IsSunk) { response.ShotStatus = ship.FireAtShip(coordinate); // if a hit, set response, history type if (response.ShotStatus != ShotStatus.Miss) { response.ShipImpacted = ship.ShipName; shotHistory = Responses.ShotHistory.Hit; // hit can only be 1 ship, all done break; } } } // Save history ShotHistory.Add(coordinate, shotHistory); }
private static void GetPlayerShot(Game game) { Player activePlayer; Player enemyPlayer; if (game.Player1Turn) { activePlayer = game.Player1; enemyPlayer = game.Player2; } else { activePlayer = game.Player2; enemyPlayer = game.Player1; } FireShotResponse shot = new FireShotResponse() { ShotStatus = ShotStatus.Invalid }; while (shot.ShotStatus == ShotStatus.Invalid || shot.ShotStatus == ShotStatus.Duplicate) { shot = enemyPlayer.PlayerBoard.FireShot(CoordinateWorkflow.GetCoordinate(game, "take your shot")); ReportShot(shot, activePlayer, enemyPlayer); if (shot.ShotStatus == ShotStatus.Victory) { game.GameOver = true; } } }
public void FireShot(Coordinate coord) { FireShotResponse response = game.Players[game.OtherPlayer].PlayerBoard.FireShot(coord); switch (response.ShotStatus) { case ShotStatus.HitAndSunk: Console.WriteLine($"{response.ShipImpacted} Hit and Sunk!"); DisplayShotBoard(); break; case ShotStatus.Duplicate: Console.WriteLine("You've already hit that square. Press any key to continue..."); DisplayShotBoard(); break; case ShotStatus.Victory: game.Winner = game.CurrentPlayer; DisplayShotBoard(); Console.WriteLine($"Congratulations, {game.Players[game.CurrentPlayer].Name}! You win!"); Console.ReadLine(); break; default: Console.WriteLine($"{response.ShotStatus}!"); DisplayShotBoard(); break; } Console.WriteLine("Press any key."); Console.ReadLine(); }
private void PlayerTurn(Board board, string player) { do { DisplayMap(board); Console.WriteLine($"{player}, choose coordinates to fire on your enemies map!"); int[] coordinates = Players.GetUserCoordinates(); Coordinate Coordinate = new Coordinate(coordinates[0], coordinates[1]); FireShotResponse yourTurn = board.FireShot(Coordinate); if (yourTurn.ShotStatus.Equals(ShotStatus.Invalid)) { Console.WriteLine("Coordinates were invalid, repeat turn."); continue; } else if (yourTurn.ShotStatus.Equals(ShotStatus.Duplicate)) { Console.WriteLine("Shot was a dupliacte, repeat turn."); continue; } else if (yourTurn.ShotStatus.Equals(ShotStatus.Hit)) { Console.WriteLine("Direct Hit!"); Console.WriteLine("Click any button to go to continue to next turn."); Console.ReadKey(); Console.Clear(); break; } else if (yourTurn.ShotStatus.Equals(ShotStatus.HitAndSunk)) { Console.WriteLine($"Direct Hit and {SunkenShip(Coordinate, board)} was sunk!"); Console.WriteLine("Click any button to go to continue to next turn."); Console.ReadKey(); Console.Clear(); break; } else if (yourTurn.ShotStatus.Equals(ShotStatus.Miss)) { Console.WriteLine("Miss!"); Console.WriteLine("Click any button to go to continue to next turn."); Console.ReadKey(); Console.Clear(); break; } else { Console.WriteLine("You have won the game! Congratulations!"); } winnerInt++; Console.WriteLine("Click any button to go to continue to next turn."); Console.ReadKey(); Console.Clear(); break; } while (true); }
public static void ShowShotResult(FireShotResponse shotresponse, Coordinates c, string playername) { String str = ""; switch (shotresponse.ShotStatus) { case ShotStatus.Duplicate: Console.ForegroundColor = ConsoleColor.Red; str = "Shot location: " + Board.GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Duplicate shot location!"; break; case ShotStatus.Hit: Console.ForegroundColor = ConsoleColor.Green; str = "Shot location: " + Board.GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Hit!"; break; case ShotStatus.Invalid: Console.ForegroundColor = ConsoleColor.Red; str = "Shot location: " + Board.GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Invalid hit location!"; break; case ShotStatus.Miss: Console.ForegroundColor = ConsoleColor.White; str = "Shot location: " + Board.GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Miss!"; break; } Console.WriteLine(str); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); }
//Invalid, Duplicate, Miss, Hit, HitAndSunk, Victory public static void DisplayShotResult(FireShotResponse response) { if (response.ShotStatus == ShotStatus.Invalid) { Console.WriteLine("Coordinate not on board, press any key to try again"); Console.ReadKey(); } else if (response.ShotStatus == ShotStatus.Duplicate) { Console.WriteLine("You've already shot there, please try again"); Console.ReadKey(); } else if (response.ShotStatus == ShotStatus.Miss) { Console.WriteLine("You Missed!"); Console.ReadKey(); } else if (response.ShotStatus == ShotStatus.Hit) { Console.WriteLine("Their Battleship was hit!"); Console.ReadKey(); } else if (response.ShotStatus == ShotStatus.HitAndSunk) { Console.WriteLine($"You Sunk their {response.ShipImpacted} battleship!"); Console.ReadKey(); } else if (response.ShotStatus == ShotStatus.Victory) { Console.WriteLine("You Are Victorious!"); Console.ReadKey(); } }
internal static void AttackMessage(Player player, FireShotResponse verify) { switch (verify.ShotStatus) { case (ShotStatus.Duplicate): Console.WriteLine("Invalid input. Already attacked specified coordinate"); break; case (ShotStatus.Invalid): Console.WriteLine("Invalid input. Attack must remain on the board"); break; case (ShotStatus.Hit): Console.WriteLine("Enemy ship hit!"); break; case (ShotStatus.HitAndSunk): Console.WriteLine($"Enemy {verify.ShipImpacted} hit and sunk!"); break; case (ShotStatus.Miss): Console.WriteLine("Attack missed!"); break; case (ShotStatus.Victory): Console.WriteLine($"The final ship has been sunk!"); break; } }
public static bool OpponentFleetSunk(FireShotResponse response, User player) { switch (response.ShotStatus) { case ShotStatus.Victory: { Console.WriteLine("\nHit! You sunk the last ship in your enemy's fleet!"); Console.WriteLine($"\nVICTORY IS YOURS, {player.Name}!"); return(true); } case ShotStatus.HitAndSunk: { Console.WriteLine($"\nHit! The enemy's {response.ShipImpacted} has sunk!"); return(false); } case ShotStatus.Hit: { Console.Write("\nHit! There are reports of major damage from the enemy front."); return(false); } case ShotStatus.Miss: { Console.WriteLine("\nMiss! Your precious munitions have splashed into the ocean."); return(false); } default: return(false); } }
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; } }
//this method checks: //this method returns a FireShotResponse object which contains a //shot status and name of ship 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); } //if the coord entered is on the board (is valid) and player has not tried that before //then checkships for hit will take in both coord and a response new Fireshot reponse object // will check all ships on the players board //// is the ship it is currently is sunk, it will skip it and move to the next one in the array of ships on the board /// it will check each ship on board /// CheckShipsForHit(coordinate, response); CheckForVictory(response); return(response); }
public static void DisplayShotStatus(Player player, FireShotResponse response) { //display result switch (response.ShotStatus) { case ShotStatus.Invalid: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Duplicate: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Miss: Console.WriteLine($"\n{response.ShotStatus}."); break; case ShotStatus.Hit: Console.WriteLine($"\n{response.ShotStatus}!"); break; // need to add which ship sunk case ShotStatus.HitAndSunk: Console.WriteLine($"\n{response.ShotStatus}!!"); break; //check for victory and play again / exit case ShotStatus.Victory: Console.WriteLine($"\n{response.ShotStatus}!!!!\n{player.Name} has Won!!!!"); break; } }
public FireShotResponse firePlayerShot(int[] shotFired, Board b) { Coordinate shotCoord = new Coordinate(shotFired[0], shotFired[1]); FireShotResponse response = new FireShotResponse(); response = b.FireShot(shotCoord); return(response); }
public FireShotResponse Fire() { Coordinate newShotCoor = ConsoleIO.GetCoord(); FireShotResponse CkShot = this.board.FireShot(newShotCoor); return(CkShot); }
public void SetupBoard() { Player player1 = new Player(); Player player2 = new Player(); player1.Name = UserInput.GetUserName(1); player2.Name = UserInput.GetUserName(2); bool playAgain = true; bool player1Turn = RandomGenerator.WhosTurnFirst(); while (playAgain) { player1.playerBoard = SetUpBoard(player1.Name); player2.playerBoard = SetUpBoard(player2.Name); bool gameStillGoing = true; while (gameStillGoing) { bool failedShotTurn1 = true; bool failedShotTurn2 = true; if (player1Turn) { UserOutput.BoardDisplay(player1.playerBoard); Coordinate attackCoordinate = UserInput.GetCoordinateForPlacement(player1.Name); FireShotResponse response = player1.playerBoard.FireShot(attackCoordinate); failedShotTurn1 = UserInput.AttackAndCheckShotStatus(response, player1.Name); if (response.ShotStatus == ShotStatus.Victory) { gameStillGoing = false; } else { player1Turn = false; } } else { while (failedShotTurn2) { UserOutput.BoardDisplay(player2.playerBoard); Coordinate attackCoordinate = UserInput.GetCoordinateForPlacement(player2.Name); FireShotResponse response = player2.playerBoard.FireShot(attackCoordinate); failedShotTurn2 = UserInput.AttackAndCheckShotStatus(response, player2.Name); if (response.ShotStatus == ShotStatus.Victory) { gameStillGoing = false; } else { player1Turn = true; } } } } playAgain = UserInput.PlayAgain(); } }
public void CanMissShip() { Board board = SetupBoard(); Coordinate coordinate = new Coordinate(5, 5); FireShotResponse response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Miss, response.ShotStatus); }
private void CheckForVictory(FireShotResponse response) { if (response.Status == ShotStatus.HitAndSunk) { if (Ships.All(s => s.isSunk)) { response.Status = ShotStatus.Victory; } } }
public void CanNotFireOffBoard() { Board board = SetupBoard(); Coordinate coordinate = new Coordinate(15, 20); FireShotResponse response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Invalid, response.ShotStatus); }
public void CanHitShip() { Board board = SetupBoard(); Coordinate coordinate = new Coordinate(1, 3); FireShotResponse response = board.FireShot(coordinate); Assert.AreEqual(ShotStatus.Hit, response.ShotStatus); Assert.AreEqual("Cruiser", response.ShipImpacted); }
public void Start(Board b1, Board b2, Player[] playerArray) { int[] shot = new int[2]; FireShotResponse response = new FireShotResponse(); ResponseDisplayer ResponseWriter = new ResponseDisplayer(); setPlayers(playerArray); do { ShotStatus responseType = new ShotStatus(); if (TurnCounter % 2 == 0) { do { showShotHistory(player2, b1); shot = getUserShot(player2); response = firePlayerShot(shot, b1); Console.Clear(); showShotHistory(player2, b1); responseType = ResponseWriter.DisplayResponse(response); } while (responseType == ShotStatus.Invalid || responseType == ShotStatus.Duplicate); TurnCounter++; } else { do { showShotHistory(player1, b2); shot = getUserShot(player1); response = firePlayerShot(shot, b2); Console.Clear(); showShotHistory(player1, b2); responseType = ResponseWriter.DisplayResponse(response); } while (responseType == ShotStatus.Invalid || responseType == ShotStatus.Duplicate); TurnCounter++; } } while (response.ShotStatus != ShotStatus.Victory); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Game Over. Would you like to play again? (y/n)"); Console.ForegroundColor = ConsoleColor.Gray; string playagain = Console.ReadLine(); if (playagain == "y") { Console.Clear(); Program p = new Program(); p.playAgain(); } else { Console.WriteLine("Thanks for playing. Bye."); Thread.Sleep(3000); } }