Guest User

Untitled

a guest
Dec 20th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. namespace Arena
  7. {
  8.     public class UTTT
  9.     {
  10.         public Process player1Process;
  11.         public Process player2Process;
  12.         public StreamWriter player1Input;
  13.         public StreamWriter player2Input;
  14.         public StreamReader player1Output;
  15.         public StreamReader player2Output;
  16.         public int lastX = -1;
  17.         public int lastY = -1;
  18.  
  19.         public bool lineReceived = false;
  20.  
  21.         public UTTT(string player1FileName, string player2FileName)
  22.         {
  23.             ProcessStartInfo info1 = new ProcessStartInfo();
  24.             info1.FileName = player1FileName;
  25.             info1.RedirectStandardInput = true;
  26.             info1.RedirectStandardOutput = true;
  27.             info1.CreateNoWindow = true;
  28.             info1.UseShellExecute = false;
  29.             player1Process = Process.Start(info1);
  30.  
  31.             ProcessStartInfo info2 = new ProcessStartInfo();
  32.             info2.FileName = player2FileName;
  33.             info2.RedirectStandardInput = true;
  34.             info2.RedirectStandardOutput = true;
  35.             info2.CreateNoWindow = true;
  36.             info2.UseShellExecute = false;
  37.             player2Process = Process.Start(info2);
  38.  
  39.             player1Input = player1Process.StandardInput;
  40.             player2Input = player2Process.StandardInput;
  41.  
  42.             player1Output = player1Process.StandardOutput;
  43.             player2Output = player2Process.StandardOutput;
  44.         }
  45.  
  46.         public int HandlePlayerOutput(int playerIndex, string output)
  47.         {
  48.             string[] split = output.Split(' ');
  49.             int x = int.Parse(split[0]);
  50.             int y = int.Parse(split[1]);
  51.  
  52.             lastX = x; lastY = y;
  53.  
  54.             if (split.Length > 2)
  55.             {
  56.                 if (split[2] == "WIN")
  57.                     return playerIndex + 1;
  58.                 else if (split[2] == "LOSS")
  59.                     return 2 - playerIndex;
  60.                 else if (split[2] == "DRAW")
  61.                     return 3;
  62.                 else return 0;
  63.             }
  64.             else return 0;
  65.         }
  66.        
  67.         public int Play()
  68.         {
  69.             lastX = -1;
  70.             lastY = -1;
  71.  
  72.             int winner = 0;
  73.             int playerId = 0;
  74.  
  75.             while (winner == 0)
  76.             {
  77.                 string updateString = lastX + " " + lastY + "\n" + "0" + "\n";
  78.  
  79.                 if (playerId == 0)
  80.                 {
  81.                     player1Input.Write(updateString);
  82.                     player1Input.Flush();
  83.                     string output1 = null;
  84.                     while (output1 == null)
  85.                         output1 = player1Output.ReadLine();
  86.                     winner = HandlePlayerOutput(0, output1);
  87.                 }
  88.                 else
  89.                 {
  90.                     player2Input.Write(updateString);
  91.                     player2Input.Flush();
  92.                     string output2 = null;
  93.                     while (output2 == null)
  94.                         output2 = player2Output.ReadLine();
  95.                     winner = HandlePlayerOutput(1, output2);
  96.                 }
  97.  
  98.                 playerId = -playerId + 1;
  99.             }
  100.  
  101.             player1Input.Close();
  102.             player2Input.Close();
  103.             player1Output.Close();
  104.             player2Output.Close();
  105.  
  106.             player1Process.Kill();
  107.             player2Process.Kill();
  108.  
  109.             return winner;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment