/// <summary> /// The local play menu. /// </summary> private void LocalPlayMenu() { Console.Clear(); ChessTable chess = new ChessTable(); chess.LocalPlay(); }
public static void Main(string[] args) { ChessTable chessTable = new ChessTable(8, 8); Game game = new Game(chessTable); IChessman chessman = new King(); IPosition position = new Position(0, 0); game.PutChessmanOnChessTable(chessman, position); game.ShowMoves(chessman); Console.WriteLine(position); }
/// <summary> /// Menu that allows a player to stay on playing, ask for a draw or surrender /// </summary> private void PlayerMenu() { //should allow the player to surrender, ask for a draw or return to play. //remember, you can call the board paint and chess paint functions to recreate the visual board and visual pieces. //Should people be allowed to use the menu while it is not their turn? If they are, have to deal with a menu appearing while they e.g. moving a piece and such. Console.Title = Settings.GetTitle + ": Player Menu"; string[] playerOptions = { "Stay Playing", "Call for Draw", "Game Stats", "Surrender" }; Console.Clear(); string title = "Options"; isActive = false; string answer = Menu.MenuAccess(playerOptions, title); isActive = true; switch (answer) { case "Stay Playing": Console.Clear(); ChessTable.GameRunTitle(); ChessTable.RepaintBoardAndPieces(); break; case "Call for Draw": if (GameStates.IsOnline) { Network.Transmit.GeneralValueTransmission(3, Network.Transmit.OtherPlayerIpAddress); GameStates.Pause = true; Console.Clear(); Console.SetCursorPosition(Settings.MenuTitleOffset[0], Settings.MenuTitleOffset[1]); Console.WriteLine("Waiting on answer"); while (GameStates.Pause && !GameStates.LostConnection) { ; } ChessTable.GameRunTitle(); } else { Console.Clear(); string drawTitle = "Other player wants to draw"; string[] drawOptions = { "Accept Draw", "Decline Draw" }; string drawAnswer = Menu.MenuAccess(drawOptions, drawTitle); switch (drawAnswer) { case "Accept Draw": GameStates.GameEnded = true; GameStates.VictoryType = null; break; case "Decline Draw": Console.Clear(); ChessTable.RepaintBoardAndPieces(); ChessTable.GameRunTitle(); break; } } break; case "Surrender": GameStates.GameEnded = true; GameStates.VictoryType = false; GameStates.WhiteWin = !white; if (GameStates.IsOnline /*&& !GameStates.IsTurn*/) { if (GameStates.PlayerTeam == false) { GameStates.TurnCounter++; } Network.Transmit.GeneralValueTransmission(41, Network.Transmit.OtherPlayerIpAddress); } break; case "Game Stats": GameStatsDisplay(); ChessTable.GameRunTitle(); ChessTable.RepaintBoardAndPieces(); break; } }
/// <summary> /// The net menu. Allows for people to host, join and return the main menu. /// </summary> private void NetMenu() { Console.Title = Settings.GetTitle + ": Net Menu"; string title = "Net Menu"; string[] options = { "Host", "Join", "Back" }; string option; do { Console.Clear(); option = Interact(options, title); switch (option) { case "Host": Host(); break; case "Join": Join(); break; case "Back": break; case "Test": Console.Clear(); Abort(); //testing break; } } while (option != options[2]); void Host() //ensure good comments in all of this net code and code related to the net { //gets and displays the IP address the joiner needs string ipAddress = Network.NetworkSupport.LocalAddress; Console.Clear(); Console.CursorTop = Settings.MenuOffset[1]; Console.WriteLine($"{"".PadLeft(Settings.MenuOffset[0])}{Settings.CVTS.BrightWhiteForeground}IP Address: {Settings.CVTS.BrightCyanForeground}{ipAddress}{Settings.CVTS.Reset}"); Console.WriteLine($"{"".PadLeft(Settings.MenuOffset[0])}{Settings.CVTS.BrightWhiteForeground}Waiting on player{Settings.CVTS.Reset}"); //starts up the reciver. Network.Receive.ReceiveSetup(ipAddress); //need to catch if it fails Network.Receive.Start(); GameStates.NetSearch.Searching = true; //gives the player the options to abort searching for a player. Thread abortThread = new Thread(Abort); abortThread.Start(); //waits on the joiner to connect to the receiver so their IP-address is arquired to ensure data can be transmitted to their receiver. //Network.Receive.GetConnectionIpAddress() loops until Network.Transmit.TransmitSetup(ipAddress) in Join() has ran or GameStates.NetSeach.Abort is true. string ipAddressJoiner = Network.Receive.GetConnectionIpAddress(true); //if not aborted, run the rest of the set-up. if (!GameStates.NetSearch.Abort) { Console.Clear(); Network.Transmit.OtherPlayerIpAddress = ipAddressJoiner; //transmitter now got the ip address needed to contact the receiver of the joiner //select colour string[] colourOptions = { "White", "Black" }; string colourTitle = "Select your colour"; string colour = null; colour = Interact(colourOptions, colourTitle); Console.CursorTop += 1; Console.WriteLine($"{Settings.CVTS.BrightWhiteForeground}Conneting{Settings.CVTS.Reset}"); //transmit the colour bool couldSend = Network.Transmit.GeneralDataTransmission(colour, ipAddressJoiner); if (couldSend) { //wait on response from the player joined to ensure they are ready. string response = Network.Receive.GeneralDataReception(true, 5000); //starts game, string colour parameters that decide whoes get the first turn. if (response == "ready") { bool firstMove = colour == "White" ? true : false; GameStates.PlayerTeam = firstMove; Console.WriteLine($"{Settings.CVTS.BrightWhiteForeground}Game Starting{Settings.CVTS.Reset}"); Start(firstMove); } else { ErrorHandling(); } } }//if aborted, skip the rest else { Network.Receive.Stop(); GameStates.Reset(); } } void Join() { string ipAddress; //ensure it is a proper address. string ownIpAddress = Network.NetworkSupport.LocalAddress; //sets up and starts the receiver Network.Receive.ReceiveSetup(ownIpAddress); Network.Receive.Start(); GameStates.NetSearch.Searching = true; Debug.WriteLine("Join Function"); do { Console.Clear(); Console.CursorTop = Settings.MenuOffset[1]; Console.Write($"" + $"{"".PadLeft(Settings.MenuOffset[0])}{Settings.CVTS.BrightWhiteForeground}Enter {Settings.CVTS.BrightCyanForeground}host IP address{Settings.CVTS.BrightWhiteForeground}.{Environment.NewLine}" + $"{"".PadLeft(Settings.MenuOffset[0])}Press {Settings.CVTS.BrightRedForeground}Enter{Settings.CVTS.BrightWhiteForeground} to comfirm.{Environment.NewLine}" + $"{"".PadLeft(Settings.MenuOffset[0])}If {Settings.CVTS.BrightCyanForeground}empty{Settings.CVTS.BrightWhiteForeground}, return to menu.{Environment.NewLine}" + $"{"".PadLeft(Settings.MenuOffset[0])}Address:{Settings.CVTS.Reset} "); Console.CursorVisible = true; ipAddress = Console.ReadLine(); if (ipAddress == "") { GameStates.NetSearch.Abort = true; } Network.Transmit.OtherPlayerIpAddress = ipAddress; Console.CursorVisible = false; } while (!GameStates.NetSearch.Abort && !Network.Transmit.TransmitSetup(ipAddress, out _, true)); //starts up the transmitter to ensure the host' receiver can get the joiner' IP address and give it to host' transmitter. Debug.WriteLine("TransmitSetup has run or abort"); if (!GameStates.NetSearch.Abort) { Debug.WriteLine("Search not aborted"); //function is run to allows the host' transmitter to get the ip address of the client' receiver. Port is known in the code, just the IP-address that is missing... can be better explained... Console.WriteLine($"{Settings.CVTS.BrightWhiteForeground}Connecting{Settings.CVTS.Reset}"); //gets the colour the host is using and selects the other string colourHost = Network.Receive.GeneralDataReception(); if (colourHost == "White" || colourHost == "Black") { string colour = colourHost == "White" ? "Black" : "White"; //send ready data. bool couldSend = Network.Transmit.GeneralDataTransmission("ready", ipAddress, true); if (couldSend) { //starts game, the string colour parameter decides who get the first turn. bool firstMove = colour == "White" ? true : false; GameStates.PlayerTeam = firstMove; Console.WriteLine($"{Settings.CVTS.BrightWhiteForeground}Game Starting{Settings.CVTS.Reset}"); Start(firstMove); } else { ErrorHandling(); } } else { ErrorHandling(); } } else { Network.Receive.Stop(); GameStates.Reset(); } } void ErrorHandling() { Network.Receive.Stop(); GameStates.Reset(); Console.Clear(); Console.WriteLine("An error occured. Press {0} to go back.", Settings.SelectKey); isActive = true; while (key.Key != Settings.SelectKey) { ; } isActive = false; key = new ConsoleKeyInfo(); } void Start(bool playerStarter) { Console.Clear(); ChessTable chess = new ChessTable(); chess.NetPlay(playerStarter); } void Abort() { //threated. Maybe not needed to be threaten, Network.Receive.GetConnectionIpAddress() should be threaten then GameStates.NetSearch.Searching = true; //just here for testning int[] cursorPosistion = new int[] { Console.CursorLeft, Console.CursorTop }; Console.WriteLine($"{"".PadLeft(Settings.MenuOffset[0])}{Settings.CVTS.BrightWhiteForeground}Abort?{Settings.CVTS.Reset}"); Console.WriteLine($"{"".PadLeft(Settings.MenuOffset[0])}{Settings.CVTS.BrightRedForeground}{Settings.CVTS.Underscore}Y{Settings.CVTS.BrightWhiteForeground}{Settings.CVTS.Underscore_Off}es{Settings.CVTS.Reset}"); isActive = true; while (GameStates.NetSearch.Searching) //bool, true if searching for a player. False if a player is found. Store it in GameStates? { if ((int)key.Key != 0) { //ConsoleKeyInfo key = Console.ReadKey(true); if (key.Key == ConsoleKey.Y) { GameStates.NetSearch.Searching = false; GameStates.NetSearch.Abort = true; } } } isActive = false; } }