I'm trying to make a 2-player Connect 4 game, but I'm having trouble wrapping my head around some of the concepts related to incorporating event handlers while progressing with the game.
On the one hand, I have my windows:
One is a control window used for entering player information, and has an exit button.
One is the actual game window used for drawing on and interaction with the player.
One is a popup that asks the player a question that they must answer correctly in order to take that turn.
On the other hand, I have my actual game logic. A player's turn could progress as follows:
//create question window with question and await an answer (game state != WAITING_FOR_ANSWER)
//question window is destroyed after the question
//if not correct, move onto the next player's turn
//if correct, await their move (by clicking on the board, state != WAITING_FOR_MOVE)
//then check for win/tie
//then perform end-of-game function / change state if needed
//move onto next player's turn (same function)
//all this time, game window redraws when game has started and needs to change
Meanwhile, my game window draws and responds to clicks depending on the game state. My general method is:
//change state to something like WAITING_FOR_MOVE
//window sees state is now this, and responds to clicks
//window changes state back when move is made
//function continues
The problems arise when trying to get all of this running concurrently. If I try as is, the question window for the first player's turn comes up after clicking the "Start Game: button, but then all windows freeze up. I would imagine this is due to the logic not actually ending, as it requires the window to give it a reason to stop.
I tried wrapping the very first nextTurn()
call (in the "Start Game" button handler) in a new thread, but it causes the same thing to happen, except the control window responds when clicking the exit button.
How should I be dealing with program logic and window event handlers at the same time?
I'm also pretty sure I should be accessing the state differently than just checking it until it's the desired result. Would I need a mutex for this?
nextTurn
function calls itself to move on. Must I replace that function with a state-based switch executing from the main loop? I already have one for the windows, of course. I do find that article pretty good though. I might take a look through some other things on that site. \$\endgroup\$