public static Gameboard.GameMode ChooseGameMode(Player player1, Player player2) { do { Console.WriteLine("Choose a gamemode!"); CX.Print("1 - Classic Mode", player1.Colour, null, endLine: true); CX.Print("2 - Infinity Mode\n", player2.Colour, null, endLine: true); try { Gameboard.Mode = (Gameboard.GameMode)CX.GetKey() - 1; Console.Clear(); } catch { CX.Catch(false); } if (Gameboard.Mode == Gameboard.GameMode.Classic) { Console.WriteLine("You have chosen Classic Mode!\n"); } else if (Gameboard.Mode == Gameboard.GameMode.Infinity) { Console.WriteLine("You have chosen Infinity Mode!\n"); } else { CX.Print("Please try again.", ConsoleColor.Red); } } while (Gameboard.Mode != Gameboard.GameMode.Classic && Gameboard.Mode != Gameboard.GameMode.Infinity); return(Gameboard.Mode); }
public static void ModifyGame(Player player1, Player player2) { Console.Clear(); if (NumberOfPlayers < 2) { Console.WriteLine("Would you like to modify the game board or change the A.I. difficulty?"); Console.WriteLine("Please select from the options below..."); Console.WriteLine("1 - Modify Board and Game Parameters"); Console.WriteLine("2 - Change A.I. Difficulty"); Console.WriteLine("0 - Return without changing anything"); byte answer = CX.GetKey(limit: 2); if (answer == 0) { return; } if (answer == 1) { Gameboard.CustomiseGame(player1, player2); } if (answer == 2) { if (player1.IsRobot && player2.IsRobot) { Console.WriteLine("Who would you like to change the difficulty of?"); Console.Write("1 - "); CX.Print(player1.Name, player1.Colour, null, true); Console.Write("2 - "); CX.Print(player2.Name, player2.Colour, null, true); Console.WriteLine("0 - I changed my mind and don't want to change any difficulties."); byte secondAnswer = CX.GetKey(limit: 2); if (secondAnswer == 0) { return; } if (secondAnswer == 0) { player1.ChangeDifficulty(); } if (secondAnswer == 0) { player2.ChangeDifficulty(); } } else { player2.ChangeDifficulty(); } } } else { Gameboard.CustomiseGame(player1, player2); } }
// * MAIN MENU * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- static void Main() { // Initialising the player objects Player player1 = new Player("Player 1", ConsoleColor.Red); Player player2 = new Player("Player 2", ConsoleColor.Yellow); // The game should only end when the player decides so, thus a while loop keeping the game alive is required. while (true) { // The print method is used to change the colour of onscreen text should it be supported. // The console needs the ability to change the colour of text to differentiate counters. Console.Write("* CONNECT X * "); CX.Print("O ", player1.Colour); Console.Write("| "); CX.Print("O ", player1.Colour); Console.Write("| "); CX.Print("O ", player2.Colour); Console.Write("| "); CX.Print("O", player2.Colour, null, true); Console.WriteLine("Make a selection:"); Console.WriteLine("1 - Play Game\n2 - Instructions\n3 - Choose Player Colours\n0 - Quit"); //try // { byte Option = CX.GetKey(); Console.Clear(); switch (Option) { case 1: int score = PlayGame(player1, player2); break; case 2: Instructions(); break; case 3: Player.ColourChanger(player1, player2); break; case 0: Console.WriteLine("Thank you for playing, press any key to quit!"); Console.ReadKey(); return; } //} //catch(Exception e) { // Console.WriteLine(e); // ;} //finally {Console.Clear();} } }
public static void ColourChanger(Player player1, Player player2) { while (true) { // Declaring the player objects that determine which player has been selected. Player chosenPlayer = player1, otherPlayer = player2; sbyte playerColourChoice = 0; // Declaring the variable for user input and giving it a default that triggers the question . Console.Clear(); // Choosing the player to change the colour of. while (playerColourChoice != 1 && playerColourChoice != 2) { Console.WriteLine("What player would you like to change the colour of?"); CX.Print($"1 - {player1.Name}", player1.Colour, null, true); CX.Print($"2 - {player2.Name}", player2.Colour, null, true); Console.WriteLine("Please enter your choice..."); Console.WriteLine("Enter \"-1\" to exit without changing any player colours..."); try { playerColourChoice = sbyte.Parse(Console.ReadLine()); if (playerColourChoice == 2) { // Swapping the initialised variables values around should the user choose so. chosenPlayer = player2; otherPlayer = player1; } else if (playerColourChoice == -1) { Console.Clear(); return; } else { Console.WriteLine("That wasn't a valid option, please try again...\n"); } } catch { CX.Catch(false); } finally { Console.Clear(); } } // Selecting a colour to set for the chosen user. while (true) { Console.Clear(); Console.WriteLine("Please select a colour"); // We get the colours from the Print method. ConsoleColor?[] colourChoices = new ConsoleColor?[12]; colourChoices[0] = CX.Print("1 - Dark Red", ConsoleColor.DarkRed, null, true); colourChoices[1] = CX.Print("2 - Red", ConsoleColor.Red, null, true); colourChoices[2] = CX.Print("3 - Dark Yellow", ConsoleColor.DarkYellow, null, true); colourChoices[3] = CX.Print("4 - Yellow", ConsoleColor.Yellow, null, true); colourChoices[4] = CX.Print("5 - Green", ConsoleColor.Green, null, true); colourChoices[5] = CX.Print("6 - Dark Green", ConsoleColor.DarkGreen, null, true); colourChoices[6] = CX.Print("7 - Dark Blue", ConsoleColor.DarkBlue, null, true); colourChoices[7] = CX.Print("8 - Blue", ConsoleColor.Blue, null, true); colourChoices[8] = CX.Print("9 - Dark Cyan", ConsoleColor.DarkCyan, null, true); colourChoices[9] = CX.Print("10 - Cyan", ConsoleColor.Cyan, null, true); colourChoices[10] = CX.Print("11 - Pink", ConsoleColor.Magenta, null, true); colourChoices[11] = CX.Print("12 - Dark Pink", ConsoleColor.DarkMagenta, null, true); Console.WriteLine("Chose the wrong player? Enter \"0\" to go back..."); Console.WriteLine("Decided you like the colours as they are? Enter \"-1\" to go back"); try { sbyte colour = sbyte.Parse(Console.ReadLine()); Console.Clear(); // Sorting out non-colour changing related options. if (colour == -1) { return; } else if (colour == 0) { break; } // colour will always be +1 higher than the array because the array starts at '0'. // Dealing with errors that would otherwise cause the player reassign the same colour or assign the same colour to different players. if (chosenPlayer.Colour == colourChoices[colour - 1]) { Console.WriteLine($"{player1.Name} already has this colour selected.\n"); Console.ReadKey(); continue; } else if (otherPlayer.Colour == colourChoices[colour - 1]) { Console.WriteLine($"{player2.Name} already has this colour selected, please choose another colour.\nPress any key to try again..."); Console.ReadKey(); continue; } else { byte answer = 0; while (true) { // Assigning the colour to the chosen player. chosenPlayer.Colour = colourChoices[colour - 1]; // Offering the player the option to change the other players colour. Console.Write("Would you like to change the colour of "); CX.Print(otherPlayer.Name, otherPlayer.Colour); Console.WriteLine("?"); Console.Write("1 - Yes | 2 - No | 0 - Recolour "); CX.Print(chosenPlayer.Name, chosenPlayer.Colour, null, true); // Processing user input. answer = CX.GetKey(limit: 2); if (answer == 0 || answer == 1) { break; } else if (answer == 2) { return; } else if (answer != 0 && answer != 1) { // Error handling. Console.Clear(); Console.WriteLine("That wasn't a valid option.\nPress any key to try again..."); Console.ReadKey(); Console.Clear(); } } if (answer == 0) { continue; } if (answer == 1) { Player temporaryPlayer = chosenPlayer; chosenPlayer = otherPlayer; otherPlayer = temporaryPlayer; break; } } } catch { CX.Catch(null); } Console.Clear(); } } }
public static void ChangeNameChoice(Player player1, Player player2) { Player chosenPlayer = player1, otherPlayer = player2; while (true) { Console.Clear(); Console.WriteLine("What player would you like to change the name of?"); CX.Print($"1 - {player1.Name}\n", player1.Colour, null, true); CX.Print($"2 - {player2.Name}\n", player2.Colour, null, true); Console.WriteLine("Please enter your choice..."); Console.WriteLine("Enter \"0\" to exit without changing any player names..."); try { byte playerNameChangeChoice = CX.GetKey(limit: 2); if (playerNameChangeChoice == 2) { chosenPlayer = player2; otherPlayer = player1; } else if (playerNameChangeChoice == 0) { return; } else { CX.Print("That wasn't a valid option, please try again...\n", ConsoleColor.Red, null, true); } while (true) { byte answer = 0; chosenPlayer.ChangeName(otherPlayer); Console.WriteLine(); while (true) { Console.Write("Would you also like to rename "); CX.Print(otherPlayer.Name, otherPlayer.Colour); Console.Write("?\n1 - Yes | 2 - No | 0 - Rename "); CX.Print(chosenPlayer.Name, chosenPlayer.Colour); answer = CX.GetKey(limit: 2); if (answer == 0 || answer == 1) { break; } else if (answer == 2) { return; } else { Console.WriteLine("That wasn't a valid option.\nPress any key to try again..."); Console.ReadKey(); Console.Clear(); } } if (answer == 0) { continue; } else if (answer == 1) { Player temporaryPlayer = chosenPlayer; chosenPlayer = otherPlayer; otherPlayer = temporaryPlayer; break; } } } catch { CX.Catch(true); } finally { Console.Clear(); } } }
public void ChangeDifficulty() { Console.Clear(); Console.Write("What dificulty would you like to change "); CX.Print(Name, Colour); Console.WriteLine(" to?"); Console.Write("Your current difficulty is: "); if (PlayerDifficulty == Difficulty.Easy) { Console.WriteLine("EASY"); } if (PlayerDifficulty == Difficulty.Medium) { Console.WriteLine("NORMAL"); } if (PlayerDifficulty == Difficulty.Hard) { Console.WriteLine("HARD"); } if (PlayerDifficulty == Difficulty.Master) { Console.WriteLine("MASTER"); } Console.WriteLine("\nPlease select from the following options by entering the associated number."); Console.WriteLine("1 - Easy Difficulty"); Console.WriteLine("2 - Normal Difficulty"); Console.WriteLine("3 - Hard Difficulty"); Console.WriteLine("4 - Master Difficulty"); Console.WriteLine("0 - Return without changing difficulty."); byte answer = CX.GetKey(limit: 4); switch (answer) { case 0: return; case 1: PlayerDifficulty = Difficulty.Easy; break; case 2: PlayerDifficulty = Difficulty.Medium; break; case 3: PlayerDifficulty = Difficulty.Hard; break; case 4: PlayerDifficulty = Difficulty.Master; break; } Console.Write("Difficulty for "); CX.Print(Name, Colour); Console.WriteLine(" changed to "); if (PlayerDifficulty == Difficulty.Easy) { Console.WriteLine("EASY!"); } if (PlayerDifficulty == Difficulty.Medium) { Console.WriteLine("NORMAL!"); } if (PlayerDifficulty == Difficulty.Hard) { Console.WriteLine("HARD!"); } if (PlayerDifficulty == Difficulty.Master) { Console.WriteLine("MASTER!"); } Console.WriteLine("\nPress any key to continue"); Console.ReadKey(); }
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); }
public static byte PauseMenu(Player player1, Player player2) { while (true) { Console.Clear(); Console.WriteLine("* PAUSE MENU *"); Console.WriteLine("Select an option by entering the relevant number."); Console.WriteLine("1 - Continue Playing"); Console.Write("2 - "); CX.Print("C", ConsoleColor.Red); CX.Print("h", ConsoleColor.DarkYellow); CX.Print("a", ConsoleColor.Yellow); CX.Print("n", ConsoleColor.Green); CX.Print("g", ConsoleColor.Cyan); CX.Print("e", ConsoleColor.DarkCyan); CX.Print(" C", ConsoleColor.Blue); CX.Print("o", ConsoleColor.Magenta); CX.Print("l", ConsoleColor.DarkMagenta); CX.Print("o", ConsoleColor.DarkRed); CX.Print("u", ConsoleColor.Red); CX.Print("r", ConsoleColor.DarkYellow); Console.WriteLine("s", ConsoleColor.Yellow); Console.WriteLine("3 - Change Player Names\n4 - Instructions (How to Play)\n5 - Modify Game\n7 - Restart Game\n0 - Quit Game"); try { byte Option = CX.GetKey(); Console.Clear(); switch (Option) { case 1: // Resume the game. Console.WriteLine("Returning to the game\nPress any key..."); Console.ReadKey(); Console.Clear(); return(1); case 2: // Change the playerss colours. Console.Clear(); Player.ColourChanger(player1, player2); break; case 3: Player.ChangeNameChoice(player1, player2); break; case 4: // View the instructions. Instructions(); break; case 5: Console.WriteLine("You can change the size of the board, change the height of the board and the number of counters needed to win."); if (GameEngine.NumberOfPlayers < 2) { Console.WriteLine("You can also change the computer difficulty."); } if (GameEngine.NumberOfPlayers == 2) { Console.WriteLine("Modifying the board will end the match, are you sure you want to modify the board?"); } Console.WriteLine((GameEngine.NumberOfPlayers < 2 ? "1 - Modify Game\n" : "1 - Modfiy Board\n") + "2 - Return to Pause Menu"); byte answer = CX.GetKey(limit: 2); if (answer == 1) { GameEngine.ModifyGame(player1, player2); } break; case 6: // Restart the game. Console.WriteLine("Restarting the game\n"); Console.Clear(); return(6); case 0: // Quit the game. return(0); default: CX.Print("This wasn't an option.", ConsoleColor.Red); Console.WriteLine("Press any key to try again..."); Console.ReadKey(); Console.Clear(); break; } } catch { CX.Catch(false); } } }
// * THE GAME * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- private static int PlayGame(Player player1, Player player2) { // Used to decide who makes the first move. It is only used once in the next statement. Random random = new Random(); Player activePlayer = random.Next(2) == 0 ? player1 : player2; // These are used for declaring a winner and displaying information at the end of the game. Player winningPlayer = null; Player losingPlayer = null; // Setting up the game. Gameboard.Mode = GameEngine.ChooseGameMode(player1, player2); // Get game mode. GameEngine.ChooseNumberOfPlayers(player1, player2); // Declare and name players. Gameboard.SetUpBoard(); // Printing a last message to the user / users playing. Console.Clear(); CX.Print(player1.Name, player1.Colour); Console.Write(" vs "); CX.Print(player2.Name, player2.Colour); Console.WriteLine("!\nPress any key to play..."); Console.ReadKey(); Console.Clear(); // IN-GAME DateTime startingTime = DateTime.Now; // This will be used to show how long a game has been played for. while (true) { // Printing to the terminal who is playing and whose move it is. Console.Clear(); CX.Print(player1.Name, player1.Colour); Console.Write(" vs "); CX.Print(player2.Name, player2.Colour); Console.WriteLine($" | \u001b[4mConnect {Gameboard.VictoryNumber} to win!\u001b[0m"); Console.Write("\nIt is "); CX.Print(activePlayer.Name + "'s", activePlayer.Colour); Console.WriteLine(" turn...\n"); // Printing the game board with player instructions. Gameboard.PrintGameBoard(); // PLAYER INPUT if (!activePlayer.IsRobot) { byte playerChoice = CX.GetKey(limit: Gameboard.BoardWidth); // Access pause menu. if (playerChoice == 0) // Entering '0' accesses the pause menu. { byte pauseMenuValue = PauseMenu(player1, player2); if (pauseMenuValue == 0) { return(0); // Quit the game. } else if (pauseMenuValue == 6) { Gameboard.ResetBoard(true, player1, player2); } continue; } else if (playerChoice == 10 && Gameboard.History.Count > 0) { Gameboard.UndoMove(); // Undo Move... 'CX.GetKey()' Returns 10 when you enter [Z]. if (player2.IsRobot) { continue; } } else if (playerChoice < 10) { if (!activePlayer.MakeMove(playerChoice)) { continue; // Place a counter on the board. If invalid (false) try again. } } } else { activePlayer.ComputerMove(activePlayer == player1 ? player2 : player1); } // It makes no sense to check for victory if the player hasn't made enough moves to win. if (activePlayer.Moves >= Gameboard.VictoryNumber) { winningPlayer = Gameboard.CheckVictory(); losingPlayer = winningPlayer != null ? activePlayer == player1 ? player2 : player1 : null; } // This is the end game screen. This code only runs when a winner has been declared. if (winningPlayer != null) { Console.Clear(); while (true) { GameEngine.VictoryMessage(winningPlayer, losingPlayer, startingTime); byte answer = CX.GetKey(limit: 2); if (answer == 0) { Gameboard.MatchReplay(); } else if (answer == 1 || answer == 2) { Console.Clear(); Gameboard.ResetBoard(true, player1, player2); Gameboard.History.RemoveRange(0, Gameboard.History.Count); Console.WriteLine(answer == 1 ? "Press any key to continue..." : "Thank you for playing\nPress any key to continue..."); Console.ReadKey(); Console.Clear(); winningPlayer = null; losingPlayer = null; if (answer == 1) { break; } if (answer == 2) { return(0); } } Console.Clear(); } } activePlayer = activePlayer == player1 ? player2 : player1; if (player1.IsRobot && player2.IsRobot) { Thread.Sleep(500); } } }