开发者

Having a Program wait for a click event

开发者 https://www.devze.com 2023-01-26 20:26 出处:网络
I am looking to have a program wait for a click event开发者_StackOverflow社区 to occur. This is my code for the click event. The program runs right through it. To give you context, this is an othello

I am looking to have a program wait for a click event开发者_StackOverflow社区 to occur. This is my code for the click event. The program runs right through it. To give you context, this is an othello program, and this is for when it is a humans turn.

Sadly the program runs through it and continues and the AI player goes.

for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {

                textboxes[i, j].Click += new EventHandler(textboxes_Click);
                textboxes[i, j].Tag = new GridIndex() { ipos = i, jpos = j };

            }
        }

Then I handle textboxes_Click.

Thanks for the help! -Lewis


That's just adding handlers to be executed when something is clicked (although I'm not convinced that textboxes are really the best controls to be using here).

You need to think of GUI programming in an event-based way: you don't direct the flow of control, the user does. You set everything up so that when an event occurs, you react appropriately.

So instead of making the AI player go immediately after you've set up the event handlers, you should react to the human player taking a turn by making the AI player go afterwards - assuming the turn was valid, of course. In other words, your event handler for reacting to the human player's turn should include logic to work out whose turn it is next, and to take the turn if it's the AI. (This general logic should be encapsulated in another method, and executed after the AI's turn too - that way you could have two computers playing against each other, or two humans, etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号