Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- namespace Arena
- {
- public class UTTT
- {
- public Process player1Process;
- public Process player2Process;
- public StreamWriter player1Input;
- public StreamWriter player2Input;
- public StreamReader player1Output;
- public StreamReader player2Output;
- public int lastX = -1;
- public int lastY = -1;
- public bool lineReceived = false;
- public UTTT(string player1FileName, string player2FileName)
- {
- ProcessStartInfo info1 = new ProcessStartInfo();
- info1.FileName = player1FileName;
- info1.RedirectStandardInput = true;
- info1.RedirectStandardOutput = true;
- info1.CreateNoWindow = true;
- info1.UseShellExecute = false;
- player1Process = Process.Start(info1);
- ProcessStartInfo info2 = new ProcessStartInfo();
- info2.FileName = player2FileName;
- info2.RedirectStandardInput = true;
- info2.RedirectStandardOutput = true;
- info2.CreateNoWindow = true;
- info2.UseShellExecute = false;
- player2Process = Process.Start(info2);
- player1Input = player1Process.StandardInput;
- player2Input = player2Process.StandardInput;
- player1Output = player1Process.StandardOutput;
- player2Output = player2Process.StandardOutput;
- }
- public int HandlePlayerOutput(int playerIndex, string output)
- {
- string[] split = output.Split(' ');
- int x = int.Parse(split[0]);
- int y = int.Parse(split[1]);
- lastX = x; lastY = y;
- if (split.Length > 2)
- {
- if (split[2] == "WIN")
- return playerIndex + 1;
- else if (split[2] == "LOSS")
- return 2 - playerIndex;
- else if (split[2] == "DRAW")
- return 3;
- else return 0;
- }
- else return 0;
- }
- public int Play()
- {
- lastX = -1;
- lastY = -1;
- int winner = 0;
- int playerId = 0;
- while (winner == 0)
- {
- string updateString = lastX + " " + lastY + "\n" + "0" + "\n";
- if (playerId == 0)
- {
- player1Input.Write(updateString);
- player1Input.Flush();
- string output1 = null;
- while (output1 == null)
- output1 = player1Output.ReadLine();
- winner = HandlePlayerOutput(0, output1);
- }
- else
- {
- player2Input.Write(updateString);
- player2Input.Flush();
- string output2 = null;
- while (output2 == null)
- output2 = player2Output.ReadLine();
- winner = HandlePlayerOutput(1, output2);
- }
- playerId = -playerId + 1;
- }
- player1Input.Close();
- player2Input.Close();
- player1Output.Close();
- player2Output.Close();
- player1Process.Kill();
- player2Process.Kill();
- return winner;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment