e4ch

Program.cs

Jan 2nd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Net.Sockets;
  4. using System.IO;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             const string strFind1 = "Press enter to start the game";
  13.             const string strFind2 = "Make your move. Type the number of the field you want to set! (1-9)\n\nField: ";
  14.             const string strFind3 = "Press enter to start again";
  15.             const string strFind4 = "Congratulations you won!";
  16.             const string strFind5 = "Congratulations you won! 100/100";
  17.             const int gridSize = 144;
  18.             TcpClient tcpClient = new TcpClient();
  19.             tcpClient.Connect("challenges.hackvent.hacking-lab.com", 1037);
  20.             NetworkStream networkStream = tcpClient.GetStream();
  21.             StreamReader clientStreamReader = new StreamReader(networkStream);
  22.             string readstring = "";
  23.             while (true) {
  24.                 int readChar = clientStreamReader.Read();
  25.                 if (readChar != -1) {
  26.                     readstring += Convert.ToChar(readChar);
  27.                     if (readstring.Length >= strFind1.Length) {
  28.                         if (readstring.Substring(readstring.Length - strFind1.Length) == strFind1) {
  29.                             readstring = "";
  30.                             byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("\n");
  31.                             networkStream.Write(bytesToSend, 0, bytesToSend.Length);
  32.                         }
  33.                     }
  34.                     if (readstring.Length >= strFind2.Length) {
  35.                         if (readstring.Substring(readstring.Length - strFind2.Length) == strFind2) {
  36.                             string grid = readstring.Substring(1, readstring.Length - strFind2.Length - 1).Substring(readstring.Substring(1, readstring.Length - strFind2.Length - 1).Length - gridSize);
  37.                             grid = grid.Replace(" ", "");
  38.                             grid = grid.Replace("|", "");
  39.                             grid = grid.Replace("-", "");
  40.                             grid = grid.Replace("\n", "");
  41.                             readstring = "";
  42.                             string send = "";
  43.                             if (grid == "*********") send = "1";
  44.                             if (grid == "X***O****") send = "9";
  45.                             if (grid == "X*O*O***X") send = "7";
  46.                             if (grid == "X*O*O*XOX") send = "4";
  47.                             if (grid == "XOO*O*X*X") send = "8";
  48.                             if (grid == "XO*******") send = "8";
  49.                             if (grid == "XOO****X*") send = "7";
  50.                             if (grid == "XO**O***X") send = "8";
  51.                             if (grid == "XO**O*OXX") send = "3";
  52.                             if (grid == "XO**O**X*") send = "7";
  53.                             if (grid == "XO**O*XXO") send = "4";
  54.                             if (grid == "XOX*OOOXX") send = "4";
  55.                             if (grid == "XOO*O**XX") send = "7";
  56.                             if (grid == "XOO*O*XX*") send = "9";
  57.                             if (grid == "XOXOO*OXX") send = "6";
  58.                             if (grid == "XOO***XXO") send = "4";
  59.                             if (send == "")
  60.                                 throw new Exception("not handled combination!");
  61.                             byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(send + "\n");
  62.                             networkStream.Write(bytesToSend, 0, bytesToSend.Length);
  63.                         }
  64.                     }
  65.                     if (readstring.Length >= strFind3.Length) {
  66.                         if (readstring.Substring(readstring.Length - strFind3.Length) == strFind3) {
  67.                             int pos1 = readstring.IndexOf(strFind4);
  68.                             if (pos1 >= 0) {
  69.                                 int pos2 = readstring.IndexOf("\n", pos1);
  70.                                 string found = readstring.Substring(pos1, pos2 - pos1);
  71.                                 if (found != strFind5)
  72.                                     Console.WriteLine(found);
  73.                                 else {
  74.                                     Console.WriteLine(readstring.Substring(pos1));
  75.                                     break;
  76.                                 }
  77.                             }
  78.                             byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("\n");
  79.                             networkStream.Write(bytesToSend, 0, bytesToSend.Length);
  80.                             readstring = "";
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment