private string ChangeName(Player opponent) { if (GameEngine.NumberOfPlayers == 0) { return("Sorry, you cannot change computer player names."); } while (true) { Console.Write("Your current name is: "); CX.Print(Name, Colour); Console.WriteLine(".\nYour opponent is named: "); CX.Print(opponent.Name, opponent.Colour); Console.WriteLine("Please choose a name."); string newName; try { newName = CX.Read(); } catch (Exception e) { Console.WriteLine(e); continue; } if (newName == "") { Console.WriteLine("You have not entered a name, please do that."); } else if (opponent.IsRobot) { if (newName.ToLower().Contains("computer") || newName.ToLower().Contains("a.i.")) { Console.WriteLine("Sorry, this name is unavailable. Please try something that isn't related to being a computer player."); } else if (Name == opponent.Name) { Console.WriteLine("This name is reserved for the computer player."); } else { Name = newName; return(Name); } } else if (Name == opponent.Name) { Console.WriteLine("This name is taken by the other player."); } else { return(Name); } } }
public static byte ChooseNumberOfPlayers(Player player1, Player player2) { // Giving the players names based on possibility they may be A.I. // TODO: Code the computer player's moves. player2.Name = "Computer A.I."; byte numberOfPlayers = 2; do { // Asking the user how many players there will be in the game? Console.Write("How many players will be playing: "); CX.Print("1", player1.Colour); Console.Write(" or "); CX.Print("2", player2.Colour); Console.WriteLine("?"); Console.WriteLine("Please enter your answer as a whole number."); try { numberOfPlayers = CX.GetKey(limit: 2); } catch { CX.Print("PLEASE ENTER YOUR ANSWER AS AN INTEGER. THIS MEANS A NUMBER WITH NO DECIMAL POINTS.", ConsoleColor.DarkRed); } if (numberOfPlayers == 0) { while (true) { Console.Clear(); Console.WriteLine("This will result in the computer versing itself while you watch. Are you sure?"); Console.WriteLine("1 - Yes\n2 - No"); byte answer = CX.GetKey(limit: 2); if (answer == 1) { player1.Name = "Computer A.I. 1"; player2.Name = "Computer A.I. 2"; Console.Write("Okay, enjoy..."); break; } else if (answer == 2) { numberOfPlayers = 99; // This will force the outer loop to continue. 99 was arbitrarily chosen. Console.Clear(); Console.WriteLine("Please choose again."); break; } else // This shouldn't run but just incase it is here to catch anything unexpected. { CX.Print("Sorry, I don't understand, please try again.", ConsoleColor.Red); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); } } } else if (numberOfPlayers == 1 || numberOfPlayers == 2) { player1.IsRobot = false; while (true) { Console.Clear(); Console.WriteLine("Enter the name for Player 1..."); player1.Name = CX.Read(player1.Colour).Trim(); if (player1.Name.Length < 1 || (player1.Name.ToLower().Contains("computer") || player1.Name.ToLower().Contains("a.i") && numberOfPlayers == 1)) { Console.Clear(); if (player1.Name.Length < 1) { Console.WriteLine("You must give Player 1 a name."); } else { Console.WriteLine("That name is reserved... Please choose something else."); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); continue; } Console.Write($"\nPlayer 1's name is set to "); CX.Print(player1.Name, player1.Colour); Console.WriteLine("!\n"); break; } if (numberOfPlayers == 2) { player2.IsRobot = false; while (true) { Console.Clear(); Console.Write($"Player 1's name is set to "); CX.Print(player1.Name, player1.Colour); Console.WriteLine("!\n"); Console.WriteLine("Enter the name for Player 2..."); player2.Name = CX.Read(player2.Colour).Trim(); if (player2.Name.Length < 1 || player2.Name.ToLower() == player1.Name.ToLower()) { Console.Clear(); if (player2.Name.Length < 0) { Console.WriteLine("You must give Player 2 a name."); } if (player2.Name.ToLower().Trim() == player1.Name.ToLower().Trim()) { Console.WriteLine("The player names cannot be the same."); } continue; } Console.Write("\nPlayer 2's name is set to "); CX.Print(player2.Name, player2.Colour); Console.WriteLine("!\n"); break; } } } else // This shouldn't run but just incase it is here to catch anything unexpected. { numberOfPlayers = 99; // Resets the outer loop. Console.Clear(); Console.WriteLine("Sorry, I don't understand, please make another attempt.\nI can only accept numerical answers.\n"); } } while (numberOfPlayers < 0 || numberOfPlayers > 2); NumberOfPlayers = (byte)(numberOfPlayers < 0 || numberOfPlayers > 2 ? 2 : numberOfPlayers); // Guaranteeing that there are no conversion issues that could cause an exception. return(NumberOfPlayers); }