I'm trying to code a board game in c#, and right now the way the game is supposed to work is that the game mechanics are handled in a separate class (called GameBoard) from the GUI, and when a button in the GUI is pressed, it pulses, which is supposed to be detected by the GameBoard, which handles the action.
I've written a test to simulate this by running the "takeTurn" method in a thread, setting the static variable that indicates which card was clicked on, and sending the pulse, than waiting for a reply from the "takeTurn" to indicate that is is finished playing that card so that I can execute an Assert to verify that card I want played has been played. As far as I can tell, the thread is executing normally, instead of as a thread. I placed a print statement directly after thread.Start(), and it has not executed, the code is just hanging up on a wait statement (I think). this is the test code:
Thread t = new Thread(new ThreadStart(p1.actionPhase));
t.Start();
Console.WriteLine("TEST: thread launched successfully");
GameBoard.lastCardPlayed = new Witch();
Console.Write("about to enter sync block.");
lock (GameBoard.syncObject){
Console.WriteLine("Entering sync block");
Thread.Sleep(5000);
Monitor.PulseAll(GameBoard.syncObject);
Console.WriteLine("Button pressed!");
Monitor.Wait(GameBoard.syncObject);
Console.WriteLine("finished waiting.");
}
Assert.IsTrue(p2.getDiscard().Contains(new Witch()));
and here is the code I'm testing:
public override void actionPhase()
{
lock (GameBoard.syncObject)
{
Console.WriteLine("PLAYER: Action Phase called on player " + getNumber());
Monitor.Wait(GameBoard.syncObject);
Console.WriteLine("PLAYER: Button pulse recieved.");
Card cardPlayed = GameBoard.lastCardPlayed;
playCard(cardPlayed);
Monitor.PulseAll(GameBoard.syncObject);
Console.WriteLine("PLAYER: finished playing card, pulse sent.");
Console.WriteLine("Playing a card with ID " + cardPlayed.getID());
}
}
The only thing output before the test hangs up is "Action Phase called on player 1"