public void CreateGameTest() { Connect4Service target = new Connect4Service(6001, 30, Connect4Service.WhoGoesFirst.random); SocketTestHelper client1 = new SocketTestHelper("localhost", 6001); SocketTestHelper client2 = new SocketTestHelper("localhost", 6001); client1.Send("@name\n#Mr.Test\n"); client2.Send("@name\n#Mrs.Test\n"); string message1 = client1.Recieve(3, 2000); string message2 = client2.Recieve(3, 2000); Assert.IsTrue("@play\r\n#Mrs.Test\r\n#30\r\n" == message1 || "@play\n#Mrs.Test\n#30\n" == message1); Assert.IsTrue("@play\r\n#Mr.Test\r\n#30\r\n" == message2 || "@play\n#Mr.Test\n#30\n" == message2); }
public void DisconnectTest1() { Connect4Service target = new Connect4Service(6015, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6015); client1.Close(); try { SocketTestHelper client2 = new SocketTestHelper("localhost", 6015); } catch (Exception e) { Assert.Fail("Unable to connect to the server:" + e.Message); } }
private ClientState state; //Current state of the player #endregion Fields #region Constructors /// <summary> /// Constructs a new Connect4ClientConnection object to manage the connection /// between the player connecting to the given server through the given socket. /// </summary> /// <param name="_socket">Socket the player is connecting through</param> /// <param name="_server">Server the player is connecting to</param> public Connect4ClientConnection(Socket _socket, Connect4Service _server) { socket = _socket; Name = null; state = ClientState.notPlaying; server = _server; outBuffer = new byte[1024]; inBuffer = new byte[1024]; pendingSends = ""; incomingMessage = ""; isSending = false; lastBadCommand = null; try { socket.BeginReceive(inBuffer, 0, inBuffer.Length, SocketFlags.None, MessageReceived, null); } catch (Exception) { DisconnectHelper(); } }
public static void Main(string[] args) { int port = 0; int timeLimit = 0; if (args.Length != 2 || !Int32.TryParse(args[0], out port) || !Int32.TryParse(args[1], out timeLimit)) { Console.WriteLine("usage: " + System.IO.Path.GetFileName(Application.ExecutablePath) + " <portNumber> <timelimit>"); return; } else if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) { Console.WriteLine("invalid port number"); return; } else if (timeLimit <= 0) { Console.WriteLine("time limit must be positive"); return; } try { Connect4Service serv = new Connect4Service(port, timeLimit, Connect4Service.WhoGoesFirst.random); serv.NotificationEvent += (msg) => { Console.WriteLine(msg); }; Console.ReadLine(); serv.Shutdown(); } catch (InvalidOperationException e) { Console.WriteLine("Server couldn't start: " + e.Message); Console.ReadLine(); } }
private long whiteTimeLeft; //Time remaining for White's turns #endregion Fields #region Constructors /// <summary> /// Constructs a new game between two players. Randomly selects positions (black or white), and sets /// up a timer to manage time left for a turn. /// </summary> /// <param name="client1">Player 1</param> /// <param name="client2">Player 2</param> /// <param name="timeLimit">Time limit for this game</param> public Connect4Game(Connect4ClientConnection client1, Connect4ClientConnection client2, int _timeLimit, Connect4Service.WhoGoesFirst choosingMethod) { switch (choosingMethod) { case Connect4Service.WhoGoesFirst.first: black = client1; white = client2; break; case Connect4Service.WhoGoesFirst.second: black = client2; white = client1; break; case Connect4Service.WhoGoesFirst.random: int tmp; lock (r) { tmp = r.Next() % 2; } if (tmp == 0) { black = client1; white = client2; } else { black = client2; white = client1; } break; default: break; } black.MoveRequest += MoveRequest; white.MoveRequest += MoveRequest; black.Resign += Resigned; white.Resign += Resigned; black.Disconnected += Disconnected; white.Disconnected += Disconnected; timeLimit = _timeLimit; whiteTimeLeft = _timeLimit * 1000; blackTimeLeft = _timeLimit * 1000; blacksMove = true; gameBoard = new BoardSquareState[6][]; for (int i = 0; i < 6; i++) { gameBoard[i] = new BoardSquareState[7]; for (int j = 0; j < 7; j++) { gameBoard[i][j] = BoardSquareState.empty; } } itsOver = false; black.SendPlay(white.Name, _timeLimit, "black"); white.SendPlay(black.Name, _timeLimit, "white"); gameTimer = new Timer(1000); // tick every second Tick(null, null); gameTimer.Elapsed += Tick; gameTimer.Start(); }
public void DisconnectTest2() { Connect4Service target = new Connect4Service(6016, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6016); SocketTestHelper client2 = new SocketTestHelper("localhost", 6016); //Give the server a bit of time to put the clients into a game System.Threading.Thread.Sleep(10); client1.Close(); try { SocketTestHelper client3 = new SocketTestHelper("localhost", 6016); } catch (Exception e) { Assert.Fail("Unable to connect to the server:" + e.Message); } }
public void WinGameTest9() { Connect4Service target = new Connect4Service(6010, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6010); SocketTestHelper client2 = new SocketTestHelper("localhost", 6010); client1.Send("@name\n#Mr.Test\n"); System.Threading.Thread.Sleep(20); client2.Send("@name\n#Mrs.Test\n"); //Confirm the game has started client1.Recieve(4, 10000); client1.Send("@move\n#4\n"); System.Threading.Thread.Sleep(20); client2.Send("@move\n#2\n"); System.Threading.Thread.Sleep(20); client1.Send("@move\n#2\n"); System.Threading.Thread.Sleep(20); client2.Send("@move\n#3\n"); System.Threading.Thread.Sleep(20); client1.Send("@move\n#3\n"); System.Threading.Thread.Sleep(20); client2.Send("@move\n#4\n"); System.Threading.Thread.Sleep(20); client1.Send("@move\n#3\n"); System.Threading.Thread.Sleep(20); client2.Send("@move\n#4\n"); System.Threading.Thread.Sleep(20); client1.Send("@move\n#1\n"); System.Threading.Thread.Sleep(20); client2.Send("@move\n#5\n"); System.Threading.Thread.Sleep(20); //at least mostly clear out the current mass of tick commands //client1.ReceiveCurrent(2000); //client2.ReceiveCurrent(2000); client1.Send("@move\n#4\n"); System.Threading.Thread.Sleep(20); //Receive the remainder string message1 = client1.ReceiveCurrent(2000); string message2 = client2.ReceiveCurrent(2000); Assert.IsTrue(message1.Contains("@win\n#black\n") || message1.Contains("@win\r\n#black\r\n")); Assert.IsTrue(message2.Contains("@win\n#black\n") || message2.Contains("@win\r\n#black\r\n")); }
public void SplitMessages() { Connect4Service target = new Connect4Service(6018, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6018); SocketTestHelper client2 = new SocketTestHelper("localhost", 6018); client1.Send("@na"); System.Threading.Thread.Sleep(5); client1.Send("me\n"); System.Threading.Thread.Sleep(5); client1.Send("#1"); System.Threading.Thread.Sleep(5); client1.Send("\n"); System.Threading.Thread.Sleep(5); client2.Send("@"); System.Threading.Thread.Sleep(5); client2.Send("n"); System.Threading.Thread.Sleep(5); client2.Send("a"); System.Threading.Thread.Sleep(5); client2.Send("m"); System.Threading.Thread.Sleep(5); client2.Send("e"); System.Threading.Thread.Sleep(5); client2.Send("\r"); System.Threading.Thread.Sleep(5); client2.Send("\n"); System.Threading.Thread.Sleep(5); client2.Send("#"); System.Threading.Thread.Sleep(5); client2.Send("2"); System.Threading.Thread.Sleep(5); client2.Send("\r"); System.Threading.Thread.Sleep(5); client2.Send("\n"); System.Threading.Thread.Sleep(5); string message1 = client1.ReceiveCurrent(2000); string message2 = client2.ReceiveCurrent(2000); Assert.IsTrue(message1.Contains("@play")); Assert.IsTrue(message2.Contains("@play")); }
public void MultipleConnections() { Connect4Service target = new Connect4Service(6017, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6017); SocketTestHelper client2 = new SocketTestHelper("localhost", 6017); SocketTestHelper client3 = new SocketTestHelper("localhost", 6017); SocketTestHelper client4 = new SocketTestHelper("localhost", 6017); SocketTestHelper client5 = new SocketTestHelper("localhost", 6017); SocketTestHelper client6 = new SocketTestHelper("localhost", 6017); SocketTestHelper client7 = new SocketTestHelper("localhost", 6017); client1.Send("@name\n#1\n"); System.Threading.Thread.Sleep(25); client2.Send("@name\n#2\n"); System.Threading.Thread.Sleep(25); client3.Send("@name\n#3\n"); System.Threading.Thread.Sleep(25); client4.Send("@name\n#4\n"); System.Threading.Thread.Sleep(25); client5.Send("@name\n#5\n"); System.Threading.Thread.Sleep(25); client6.Send("@name\n#6\n"); System.Threading.Thread.Sleep(25); client7.Send("@name\n#7\n"); System.Threading.Thread.Sleep(25); string message1 = client1.ReceiveCurrent(2000); string message2 = client2.ReceiveCurrent(2000); string message3 = client3.ReceiveCurrent(2000); string message4 = client4.ReceiveCurrent(2000); string message5 = client5.ReceiveCurrent(2000); string message6 = client6.ReceiveCurrent(2000); Assert.IsTrue(message1.Contains("@play") && message1.Contains("#2")); Assert.IsTrue(message2.Contains("@play") && message2.Contains("#1")); Assert.IsTrue(message3.Contains("@play") && message3.Contains("#4")); Assert.IsTrue(message4.Contains("@play") && message4.Contains("#3")); Assert.IsTrue(message5.Contains("@play") && message5.Contains("#6")); Assert.IsTrue(message6.Contains("@play") && message6.Contains("#5")); try { string message7 = client7.ReceiveCurrent(100); Assert.Fail("client7 should not have been paired into a game."); } catch (Exception) { //Exception expected - pass through } }
public void IgnoringMessages() { Connect4Service target = new Connect4Service(6019, 30, Connect4Service.WhoGoesFirst.first); SocketTestHelper client1 = new SocketTestHelper("localhost", 6019); client1.Send("#name\n@aric\n@name\n"); System.Threading.Thread.Sleep(50); string test = client1.ReceiveCurrent(2000); Assert.IsTrue("@ignoring\n#aric\n" == test || "@ignoring\r\n#aric\r\n" == test); }