public void WinTest2() { AutoResetEvent ar1 = new AutoResetEvent(false); AutoResetEvent ar2 = new AutoResetEvent(false); AutoResetEvent turn1 = new AutoResetEvent(false); AutoResetEvent turn2 = new AutoResetEvent(false); bool won1 = false; bool won2 = false; Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5014, 30, Connect4Server.Connect4Service.WhoGoesFirst.second); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.Disconnected += (r) => { if (r == DisconnectReason.youWon) { won1 = true; ar1.Set(); } }; target1.MoveMade += (row, pos, you) => { if (!you) { turn1.Set(); } }; target2.Disconnected += (r) => { if (r == DisconnectReason.opponentWon) { won2 = true; ar2.Set(); } }; target2.MoveMade += (row, pos, you) => { if (!you) { turn2.Set(); } }; target1.StartConnection("localhost", 5014); Thread.Sleep(500); target2.StartConnection("localhost", 5014); Thread.Sleep(1000); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(2); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(2); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(2); turn2.WaitOne(); target2.MakeMove(3); turn1.WaitOne(); target1.MakeMove(2); ar1.WaitOne(); ar2.WaitOne(); Assert.IsTrue(won1); Assert.IsTrue(won2); }
public void TimeOutTest1() { AutoResetEvent ar1 = new AutoResetEvent(false); AutoResetEvent ar2 = new AutoResetEvent(false); bool disconnected1 = false; bool disconnected2 = false; Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5010, 3, Connect4Server.Connect4Service.WhoGoesFirst.first); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.Disconnected += (r) => { if (r == DisconnectReason.opponentOutOfTime) { disconnected1 = true; ar1.Set(); } }; target2.Disconnected += (r) => { if (r == DisconnectReason.youOutOfTime) { disconnected2 = true; ar2.Set(); } }; target1.StartConnection("localhost", 5010); Thread.Sleep(500); target2.StartConnection("localhost", 5010); Thread.Sleep(1000); target1.MakeMove(1); Thread.Sleep(6000); ar1.WaitOne(); ar2.WaitOne(); Assert.IsTrue(disconnected1); Assert.IsTrue(disconnected2); }
public void MakeMoveTest() { AutoResetEvent ar = new AutoResetEvent(false); bool moveMade = false; Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5003, 30, Connect4Server.Connect4Service.WhoGoesFirst.random); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.MoveMade += (row, pos, you) => { moveMade = true; ar.Set(); }; target2.MoveMade += (row, pos, you) => { moveMade = true; ar.Set(); }; target1.StartConnection("localhost", 5003); target2.StartConnection("localhost", 5003); Thread.Sleep(1000); target1.MakeMove(4); target2.MakeMove(3); ar.WaitOne(); Assert.IsTrue(moveMade); }
public void GameBoardTest() { AutoResetEvent ar = new AutoResetEvent(false); Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5005, 30, Connect4Server.Connect4Service.WhoGoesFirst.random); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.MoveMade += (row, pos, you) => { ar.Set(); }; target2.MoveMade += (row, pos, you) => { ar.Set(); }; target1.StartConnection("localhost", 5005); target2.StartConnection("localhost", 5005); Thread.Sleep(1000); target1.MakeMove(4); target2.MakeMove(4); ar.WaitOne(); BoardSquareState state1 = target1.GameBoard.ElementAt(0).ElementAt(3); BoardSquareState state2 = target2.GameBoard.ElementAt(0).ElementAt(3); bool works = (state1 == state2) && (state1 != BoardSquareState.empty); Assert.IsTrue(works); }
public void FullTest() { AutoResetEvent ar1 = new AutoResetEvent(false); AutoResetEvent turn1 = new AutoResetEvent(false); AutoResetEvent turn2 = new AutoResetEvent(false); bool badMove = false; Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5012, 30, Connect4Server.Connect4Service.WhoGoesFirst.first); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.IllegalMove += () => { badMove = true; ar1.Set(); }; target1.MoveMade += (row, pos, you) => { if (!you) { turn1.Set(); } }; target2.MoveMade += (row, pos, you) => { if (!you) { turn2.Set(); } }; target1.StartConnection("localhost", 5012); Thread.Sleep(500); target2.StartConnection("localhost", 5012); Thread.Sleep(1000); target1.MakeMove(1); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(1); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(1); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(1); ar1.WaitOne(); Assert.IsTrue(badMove); }
public void DrawTest() { AutoResetEvent ar1 = new AutoResetEvent(false); AutoResetEvent ar2 = new AutoResetEvent(false); AutoResetEvent turn1 = new AutoResetEvent(false); AutoResetEvent turn2 = new AutoResetEvent(false); bool draw1 = false; bool draw2 = false; Connect4Server.Connect4Service server = new Connect4Server.Connect4Service(5013, 30, Connect4Server.Connect4Service.WhoGoesFirst.first); Connect4ClientGame target1 = new Connect4ClientGame("Mr. test"); Connect4ClientGame target2 = new Connect4ClientGame("Mrs. test"); target1.Disconnected += (r) => { if (r == DisconnectReason.draw) { draw1 = true; ar1.Set(); } }; target1.MoveMade += (row, pos, you) => { if (!you) { turn1.Set(); } }; target2.Disconnected += (r) => { if (r == DisconnectReason.draw) { draw2 = true; ar2.Set(); } }; target2.MoveMade += (row, pos, you) => { if (!you) { turn2.Set(); } }; target1.StartConnection("localhost", 5013); Thread.Sleep(500); target2.StartConnection("localhost", 5013); Thread.Sleep(1000); for (int i = 0; i < 3; i++) { target1.MakeMove(1); turn2.WaitOne(); target2.MakeMove(3); turn1.WaitOne(); target1.MakeMove(2); turn2.WaitOne(); target2.MakeMove(4); turn1.WaitOne(); target1.MakeMove(5); turn2.WaitOne(); target2.MakeMove(7); turn1.WaitOne(); target1.MakeMove(6); turn2.WaitOne(); target2.MakeMove(1); turn1.WaitOne(); target1.MakeMove(3); turn2.WaitOne(); target2.MakeMove(2); turn1.WaitOne(); target1.MakeMove(4); turn2.WaitOne(); target2.MakeMove(5); turn1.WaitOne(); target1.MakeMove(7); turn2.WaitOne(); target2.MakeMove(6); turn1.WaitOne(); } ar1.WaitOne(); ar2.WaitOne(); Assert.IsTrue(draw1); Assert.IsTrue(draw2); }