/// <summary> /// Method used to update the controls from the UI after a player's move. /// </summary> public void UpdateUI() { /* update current player and score. */ currentPlayerLabel.Text = "Current player: " + game.GetCurrentPlayer().GetColor().ToString(); currentPlayerLabel.Refresh(); playerOneScoreLabel.Text = game.GetPlayer(1).GetColor().ToString() + " score: " + game.GetScore(1); playerOneScoreLabel.Refresh(); playerTwoScoreLabel.Text = game.GetPlayer(2).GetColor().ToString() + " score: " + game.GetScore(2); playerTwoScoreLabel.Refresh(); /* check if the game is finished. */ if (game.IsFinished() == true) { currentPlayerLabel.Text = game.GetGameResult(); game.SetGameState(GameState.stopped); } pictureBox.Refresh(); }
/// <summary> /// "Start" button press event handler. This method starts the game. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void startButton_Click(object sender, EventArgs e) { /* set game type. */ int gameIdx = gameTypeComboBox.SelectedIndex; game = GameFactory <int> .Create(gameIdx, botOne, botTwo); game.Start(); pictureBox.Size = boardImage.Size; pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint); Type a = game.GetType(), b = typeof(HHGame <int>), c = typeof(HCGame <int>); if (a.Equals(b) == true || a.Equals(c) == true) { pictureBox.MouseUp += new MouseEventHandler(this.pictureBox_MouseUp); } else { /* CCGame: launch a thread that will update the ui. */ // launch a worker thread that will get the bot's next move and update the UI. // this way the UI thread is not blocked and the operation is permitted (does not throw any exceptions). uiUpdaterWorker = new Thread( new ParameterizedThreadStart( (parameter) => { Form1 form = (Form1)parameter; while (game.IsFinished() == false && cancelToken.IsCancellationRequested == false) { form.Invoke((MethodInvoker) delegate { form.UpdateUI(); // runs on UI thread. }); Thread.Sleep(UI_REFRESH_RATE); } Console.WriteLine("[FORM] UI Updater thread exit."); }) ); uiUpdaterWorker.Start(this); } pictureBox.Refresh(); }