static void Main(string[] args) { if (args.Contains("tournament")) { Tournament.RunTournament(args.Contains("--verbose")); } else if (args.Contains("playturn")) { PlayATurnNetwork.TestPlayATurn(); } else if (args.Contains("player")) { int port = args.Length < 3 ? 12345 : Int32.Parse(args[2]); string ip = args.Length < 4 ? null : args[3]; NPlayerProxy.RunNPlayerProxy(args[1], port, ip); } else if (args.Contains("host")) { NetworkTournament.RunNetworkTournament(Int32.Parse(args[1])); } else { Console.WriteLine("Available arguments:"); Console.WriteLine("<tournament>: run internal tournament simulation"); Console.WriteLine("<playturn>: test playATurn function on stdin and stdout"); Console.WriteLine("<player> name: launch a player to connect to network with name"); Console.WriteLine("<host> n: host a network tournament with n players in the game"); } Console.WriteLine("done with program"); }
public static void RunNPlayerProxy(string name, int port, string ip = null) // input host IP address and port number as 2 args { NPlayerProxy player = new NPlayerProxy(name); IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAddress localAddress = ipHostInfo.AddressList[0]; IPAddress ipAddress = ip == null ? localAddress : IPAddress.Parse(ip); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sender.Connect(remoteEP); NetworkStream networkStream = new NetworkStream(sender); StreamWriter writer = new StreamWriter(networkStream); StreamReader reader = new StreamReader(networkStream); int count = 0; while (true) { count++; string incoming = reader.ReadLine(); (string, Boolean)response = player.ParseInput(incoming); writer.WriteLine(response.Item1); writer.Flush(); if (response.Item2) { if (count == 2) { break; } } } }