using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace BlackjackClient { class Program { // hostname of the server public const string HOST_NAME = "server.allcatsaregrey.co.uk"; static void Main(string[] args) { //ask the user their name for use further in the program Console.WriteLine("Please enter your name:"); //declare a username string from what the user has entered string userName = Console.ReadLine(); //clear the console to start the gameplay Console.Clear(); //connect to the server try { //define the remote IP address and port number IPAddress ipAddress = Dns.GetHostAddresses(HOST_NAME)[0]; IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, 26260); //create a new TCP/IP socket and connect to the remote endpoint Socket server = new Socket(SocketType.Stream, ProtocolType.Tcp); server.Connect(remoteEndPoint); //when the client has connected to the server, tell them Console.WriteLine("Connected to server."); //receive the player's total from the server byte[] playerTotalData = new byte[1024]; int bytesReceived = server.Receive(playerTotalData); int playerTotal = int.Parse(Encoding.ASCII.GetString(playerTotalData, 0, bytesReceived)); //let the user know the total of their cards Console.WriteLine("\n" + userName + " , your cards add up to {0}.", playerTotal); //close client if server disconnects if (!server.Connected) { Console.WriteLine("Connection lost."); //wait for 3 seconds before closing the console Thread.Sleep(3000); //close the console application Environment.Exit(0); } //loop to understand the user's decision while (true) { //code for if the player has scored more than 21 if (playerTotal > 21) { Console.WriteLine("\nUnlucky" + userName + ", your score is over 21. Dealer wins!"); Console.WriteLine("Please press any key to stop."); Console.ReadKey(); Environment.Exit(0); } //ask the user if they want to hit or stand Console.Write("Hit or Stand? "); //convert what they have said to uppercase string hitStand = Console.ReadLine().ToUpper(); //if the user wants to hit, send this decision to the server if (hitStand == "HIT") { //send HIT to the server byte[] hitStandData = Encoding.ASCII.GetBytes(hitStand); server.Send(hitStandData); //check to see if the server has closed the connection if (server.Poll(1000, SelectMode.SelectRead) && server.Available == 0) { //alert the user that the connection has been lost Console.WriteLine("Connection lost."); //wait for 3 seconds before closing the console Thread.Sleep(3000); //close the console application Environment.Exit(0); } //receive the player's new total from the server playerTotalData = new byte[1024]; bytesReceived = server.Receive(playerTotalData); playerTotal = int.Parse(Encoding.ASCII.GetString(playerTotalData, 0, bytesReceived)); //alert the player of their new total Console.WriteLine("\n" + userName + ", Your cards add up to {0}.", playerTotal); } else if (hitStand == "STAND") { //if the user has decided to stand, send STAND to the server byte[] hitStandData = Encoding.ASCII.GetBytes(hitStand); server.Send(hitStandData); //check if the server has closed the connection if (server.Poll(1000, SelectMode.SelectRead) && server.Available == 0) { //alert the user that the connection has been lost Console.WriteLine("Connection lost."); //wait for 3 seconds before closing the console Thread.Sleep(3000); //close the console application Environment.Exit(0); } //receive the dealer's total from the server byte[] DealerTotalData = new byte[1024]; bytesReceived = server.Receive(DealerTotalData); string totalOfDealer = Encoding.ASCII.GetString(DealerTotalData, 0, bytesReceived); //tell the user what the dealer has got Console.WriteLine("\nThe dealer has got {0}!", totalOfDealer); //receive the winner from the server byte[] winnerData = new byte[1024]; bytesReceived = server.Receive(winnerData); string winner = Encoding.ASCII.GetString(winnerData, 0, bytesReceived); //tell the user who has won Console.WriteLine("\nThe winner is the {0}!", winner); //messages the user a congratulatory message if they have won if (winner == "PLAYER") { Console.WriteLine("\nWell Done " + userName + "!"); } //messages the user unlucky, if they have lost or tied with the dealer else { Console.WriteLine("\n Unlucky " + userName); } //when the game has stopped, tell the user that they have to press any key for the game to stop Console.WriteLine("\nPlease press any key to stop."); Console.ReadKey(); Environment.Exit(0); } } //close the socket server.Shutdown(SocketShutdown.Both); server.Close(); } catch (Exception e) { //if the server is not active, alert the user and close the client Console.WriteLine("Server is not active"); Console.WriteLine("The client will now close"); Thread.Sleep(4000); Environment.Exit(0); } } } }