// Update all the clients // parameter: bool of whether the game is over private void updateAllClients(bool endGame) { try { // if the turn needs to be passed to the next player if (advanceTurn) { playerTurn++; if (playerTurn > clientCallbacks.Count()) { playerTurn = 1; } // set the new current player turn playerTurnIdx = clientCallbacks.ToList()[playerTurn-1].Key; } // loop through all clients turning the tile count, playerhand, player turn, word scores foreach (int i in clientCallbacks.Keys) { CallbackInfo info = new CallbackInfo(tiles.Count - tileIdx, endGame, playerHands[i], i, i == playerTurnIdx, playerTurnIdx, plotTilesList, totalPlayerScoreDict, updateBoard, totalTurnScore, wordsPlayed); clientCallbacks[i].UpdateGui(info); } } catch (Exception ex) { Console.WriteLine("Error Updating clients! " + ex.Message); } }
// function to update the user interface with data from the callback public void UpdateGui(CallbackInfo info) { try { if (System.Threading.Thread.CurrentThread == this.Dispatcher.Thread) { lblBagCount.Content = bag.NumTiles.ToString(); ScoreListBox.Items.Clear(); lblPlayerTurn.Content = info.PlayerEndTurn; lblLastWord.Content = ""; foreach (KeyValuePair<int, int> match in info.TotalPlayerScore) { ScoreListBox.Items.Add("Player " + match.Key + "\t\t Score: " + match.Value.ToString()); } //Set the players number label PlayerNumber.Content = "Player " + info.PlayerID; //Set the player scores label lblPlayerScore.Content = info.TotalPlayerScore[info.PlayerID]; //Set the player turn! if (info.MyTurn) { // if the game is over, disable the player tiles if (info.EndGame) { btnEndTurn.IsEnabled = false; btnOne.IsEnabled = false; btnTwo.IsEnabled = false; btnThree.IsEnabled = false; btnFour.IsEnabled = false; btnFive.IsEnabled = false; btnSix.IsEnabled = false; btnSeven.IsEnabled = false; } else { btnEndTurn.IsEnabled = true; btnOne.IsEnabled = true; btnTwo.IsEnabled = true; btnThree.IsEnabled = true; btnFour.IsEnabled = true; btnFive.IsEnabled = true; btnSix.IsEnabled = true; btnSeven.IsEnabled = true; } } else { btnEndTurn.IsEnabled = false; btnOne.IsEnabled = false; btnTwo.IsEnabled = false; btnThree.IsEnabled = false; btnFour.IsEnabled = false; btnFive.IsEnabled = false; btnSix.IsEnabled = false; btnSeven.IsEnabled = false; } // update the tiles to have the image of their tile imageTiles(info.PlayerHand.Hand); //Update the player board - forloop to go through boardInformation if (info.UpdateBoard) { for (int i = 0; i < info.BoardInformation.Count(); i++) { Image gridBG = new Image { Width = 35, Height = 35 }; var bitmapImage = new BitmapImage(new Uri(@"..\..\images\tiles\" + info.BoardInformation[i].tileLetter + ".png", UriKind.Relative)); gridBG.Source = bitmapImage; gridBG.SetValue(Grid.ColumnProperty, info.BoardInformation[i].colNum); gridBG.SetValue(Grid.RowProperty, info.BoardInformation[i].rowNum); ScrabbleGrid.Children.Add(gridBG); // update board locations so tiles can't be placed on eachother allGridInfo.Add(new plotTileStruct(info.BoardInformation[i].rowNum, info.BoardInformation[i].colNum, info.BoardInformation[i].tileLetter)); } // list all the words scored from the last players turn foreach (string word in info.WordsPlayed) { lblLastWord.Content += word + " "; } lblLastWordScore.Content = info.LastTurnScore; playedGridInfo.Clear(); } else { //else remove the tiles off the board! for (int i = 0; i < info.BoardInformation.Count(); i++) { foreach (BoardData match in playedGridInfo) { if (match.rowNum == info.BoardInformation[i].rowNum && match.colNum == info.BoardInformation[i].colNum) { //Remove the image from the grid ScrabbleGrid.Children.Remove(match.gridBackground); playedGridInfo.Remove(match); break; } } } } // if the callback calls for the end of the game if (info.EndGame) { string endGame = ""; if(playerLeaveGWin) { endGame = "Player left the game!"; } else { endGame = "Tilebag Empty! Game Over!"; } EndGame eWin = new EndGame(info.TotalPlayerScore, endGame); eWin.Show(); } } else { // Only the main (dispatcher) thread can change the GUI so.. this.Dispatcher.BeginInvoke(new ClientUpdateDelegate(UpdateGui), info); } } catch (Exception ex) { MessageBox.Show("Error Updating Clients: " + ex.Message); } }