public static bool GetShipAlignment(string shipType, int i) { bool isHorizontal = false; string answer; bool correctAnswer = false; while (!correctAnswer) { UI.PrintMessage($"Do you want to place {numerals[i]} \"{shipType}\" horizontally? (y/n)"); answer = Console.ReadLine().ToLower(); if (answer == "y") { isHorizontal = true; correctAnswer = true; } else if (answer == "n") { isHorizontal = false; correctAnswer = true; } else { UI.PrintMessage("Wrong answer! You have to pick yes-(y) or no-(n)."); } } return(isHorizontal); }
public static bool AskIfRebellion() { string answer; bool isRebellion = false; bool correctAnswer = false; while (!correctAnswer) { UI.PrintMessage("\nDo you want to play Rebellion (r) or Empire (e)?"); // answer = Console.ReadLine().ToLower(); answer = "r"; if (answer == "r") { isRebellion = true; correctAnswer = true; UI.PrintMessage("Yay! You play the Rebellion! You are breathtaking!"); } else if (answer == "e") { isRebellion = false; correctAnswer = true; UI.PrintMessage("Seriously? You play the Empire. You will lose anyway..."); } else { UI.PrintMessage("Wrong answer! You have to pick a \"light\"-(r) or \"dark\"-(e) side."); } } return(isRebellion); }
public void HandleActions() { while (!Player.IsLost && !AIOpponent.IsLost) { UI.PrintTwoBoards(Player, AIOpponent); UI.PrintComments(); UI.PrintMessage("\nWhich field do you want to shoot?"); bool correctCoordinates = false; bool isShip = true; int[] coordinates; char charRepresentation; while (!correctCoordinates && isShip && !AIOpponent.IsLost) { coordinates = UI.GetPairCoordinates(); correctCoordinates = AIOpponent.IsAlreadyUsed(coordinates); if (correctCoordinates) { AIOpponent.HandleShooting(coordinates); isShip = AIOpponent.Board.CheckIfShip(coordinates); correctCoordinates = false; AIOpponent.CheckIfLost(); if (isShip) { UI.PrintTwoBoards(AIOpponent, AIOpponent); UI.PrintComments(); } } } isShip = true; correctCoordinates = false; while (!correctCoordinates && isShip && !AIOpponent.IsLost && !Player.IsLost) { coordinates = Handler.GetRandomCoordinates(); while (Player.alreadySelected.Any(x => x[0] == coordinates[0] && x[1] == coordinates[1])) { coordinates = Handler.GetRandomCoordinates(); } charRepresentation = Convert.ToChar(('A' + coordinates[1])); UI.AddComment($"\nAI shoots at: {charRepresentation}{coordinates[0] + 1}\n"); Player.alreadySelected.Add(coordinates); correctCoordinates = Player.IsAlreadyUsed(coordinates); if (correctCoordinates) { Player.HandleShooting(coordinates); isShip = Player.Board.CheckIfShip(coordinates); correctCoordinates = false; Player.CheckIfLost(); } } } }
public static void PrintTwoBoards(Player player, Player anotherPlayer) { string yourBoardMessage = "Your board:"; string opponendBoardMessage = "Your opponent's board:"; int columnOffset = 5; string spacesOffset = new string (' ', columnOffset); string yourBoardOffset = new string(' ', separatorLine.Length - yourBoardMessage.Length); int rowNumber = 1; string rowToPrint; Player currentPlayer = player; Console.Clear(); UI.PrintMessage(yourBoardMessage + yourBoardOffset + spacesOffset + opponendBoardMessage); UI.PrintMessage(topLine + spacesOffset + topLine); UI.PrintMessage(separatorLine + spacesOffset + separatorLine); for (int e = 0; e < currentPlayer.Board.board.Count; e++) { rowToPrint = ""; for (int i = 0; i < 2; i++) { if (rowNumber < 10) { rowToPrint += rowNumber.ToString() + longSeparator; } else { rowToPrint += rowNumber.ToString() + shortSeparator; } foreach (Square square in currentPlayer.Board.board[e]) { square.updateVisualRepresentation(); if (!currentPlayer.IsActivePlayer && square.visualRepresentation == notHitShipMark) { rowToPrint += notHitEmptyMark; } else { rowToPrint += square.visualRepresentation; } } rowToPrint += (i == 0) ? spacesOffset : ""; currentPlayer = (i == 0) ? anotherPlayer : player; } UI.PrintMessage(rowToPrint); UI.PrintMessage(separatorLine + spacesOffset + separatorLine); rowNumber++; } }
public bool IsAlreadyUsed(int[] coordinates) { bool isCorrect = false; Square square = board[coordinates[0]][coordinates[1]]; if (square.IsHit) { UI.PrintMessage("You have shot this field already!"); } else { isCorrect = true; } return(isCorrect); }
public static string AskName() { string nameAnswer = ""; string nameQuestion = "\nWhat is your name, challenger?"; string feedback; string notCorrectAnswerFeedback = "Ooops! It looks like you have not provided any answer. Try again!"; while (!Validation.CheckName(nameAnswer)) { UI.PrintMessage(nameQuestion); //nameAnswer = Console.ReadLine(); nameAnswer = "AAA"; feedback = (!Validation.CheckName(nameAnswer)) ? notCorrectAnswerFeedback : $"Welcome captain {nameAnswer}!"; UI.PrintMessage(feedback); } return(nameAnswer); }
public static List <int[]> GetFullCoordinatesFromShipHead(Ship ship, Space space, int i) { int[] coordinates = { -1, -1 }; bool correctAnswer = false; while (!correctAnswer) { UI.AskForPlacement(ship, i); coordinates = UI.GetPairCoordinates(); if (Validation.IsAnswerValid(coordinates) && !Validation.IsThereAShip(space, ship, coordinates)) { correctAnswer = true; } else { UI.PrintMessage("Please enter valid coordinates. You are too close!"); } } return(Handler.GetFullShipCoordinates(ship, coordinates)); }
public static void PrintBoard(bool isActivePlayer, List <List <Square> > board) { int rowNumber = 1; string rowToPrint; UI.PrintMessage(UI.topLine); UI.PrintMessage(UI.separatorLine); foreach (List <Square> element in board) { if (rowNumber < 10) { rowToPrint = rowNumber.ToString() + UI.longSeparator; } else { rowToPrint = rowNumber.ToString() + UI.shortSeparator; } foreach (Square square in element) { square.updateVisualRepresentation(); if (!isActivePlayer && square.visualRepresentation == UI.notHitShipMark) { rowToPrint += UI.notHitEmptyMark; } else { rowToPrint += square.visualRepresentation; } } UI.PrintMessage(rowToPrint); UI.PrintMessage(UI.separatorLine); rowNumber++; } }
public static int[] GetPairCoordinates() { int[] coordinates = { -1, -1 }; bool correctAnswer = false; while (!correctAnswer) { string userPlacement = Console.ReadLine(); try { coordinates[0] = int.Parse(userPlacement.Substring(1)) - 1; coordinates[1] = Validation.TranslateCoordinates(userPlacement[0]); } catch (FormatException) { UI.PrintMessage("Wrong coordinates format. First enter column letter, than row number. For example \"a1\"."); continue; } catch (ArgumentException error) { UI.PrintMessage(error.Message); continue; } if (Validation.IsAnswerValid(coordinates)) { correctAnswer = true; } else { UI.PrintMessage("You are out of range!"); } } return(coordinates); }
public Game() { UI.PrintMessage(UI.welcomeMessage); Player = new Player(UI.AskName(), UI.AskIfRebellion(), true); AIOpponent = new Player("AI", !Player.IsRebellion, false); }