public void UpdateGui(CallBackInfo info, int id) { if (System.Threading.Thread.CurrentThread == this.Dispatcher.Thread) { try { //this statment changes the group box colour of the current player switch (id) { case 1: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Red); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Gray); break; case 2: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Red); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Gray); break; case 3: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Red); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Gray); break; case 4: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Red); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Gray); break; case 5: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Red); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Gray); break; case 6: groupBoxPlayer1.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer2.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer3.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer4.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer5.BorderBrush = new SolidColorBrush(Colors.Gray); groupBoxPlayer6.BorderBrush = new SolidColorBrush(Colors.Red); break; } //} if (info.DieRoll > 1) { // change the die picture based on whatever the player rolled changeDiePicture(info.DieRoll); switch (id) { case 1: textBoxPlayerPoints1.Text = Convert.ToString(info.BankedPoints); break; case 2: textBoxPlayerPoints2.Text = Convert.ToString(info.BankedPoints); break; case 3: textBoxPlayerPoints3.Text = Convert.ToString(info.BankedPoints); break; case 4: textBoxPlayerPoints4.Text = Convert.ToString(info.BankedPoints); break; case 5: textBoxPlayerPoints5.Text = Convert.ToString(info.BankedPoints); break; case 6: textBoxPlayerPoints6.Text = Convert.ToString(info.BankedPoints); break; } // update the log based on the roll textBoxLog.AppendText("Player " + id + ": rolled a " + info.DieRoll + "\n"); scrollViewer.ScrollToBottom(); } else if (info.DieRoll == 1) { // change the die picture based on whatever the player rolled changeDiePicture(info.DieRoll); switch (id) { case 1: textBoxPlayerPoints1.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal1.Text = Convert.ToString(info.TotalPoints); break; case 2: textBoxPlayerPoints2.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal2.Text = Convert.ToString(info.TotalPoints); break; case 3: textBoxPlayerPoints3.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal3.Text = Convert.ToString(info.TotalPoints); break; case 4: textBoxPlayerPoints4.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal4.Text = Convert.ToString(info.TotalPoints); break; case 5: textBoxPlayerPoints5.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal5.Text = Convert.ToString(info.TotalPoints); break; case 6: textBoxPlayerPoints6.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal6.Text = Convert.ToString(info.TotalPoints); break; } // update the log based on the roll textBoxLog.AppendText("Player " + id + ": rolled a " + info.DieRoll + " Oh No!\n"); if (id + 1 > totalPlayers) { id = 1; textBoxLog.AppendText("Player " + id + "'s turn!\n"); } else textBoxLog.AppendText("Player " + (id + 1) + "'s turn!\n"); scrollViewer.ScrollToBottom(); } else//if die roll is 0 then that means the user is staying { switch (id) { case 1: textBoxPlayerPoints1.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal1.Text = Convert.ToString(info.TotalPoints); break; case 2: textBoxPlayerPoints2.Text = Convert.ToString(info.BankedPoints); textBoxPlayerTotal2.Text = Convert.ToString(info.TotalPoints); break; } textBoxLog.AppendText("Player " + id + ": is staying.\n"); if (id + 1 > totalPlayers) { id = 1; textBoxLog.AppendText("Player " + id + "'s turn!\n"); } else textBoxLog.AppendText("Player " + (id + 1) + "'s turn!\n"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { // Only the main (dispatcher) thread can change the GUI this.Dispatcher.BeginInvoke(new ClientUpdateDelegate(UpdateGui), info, id); } }
//This method is for when a player chooses to Roll public void Roll(int clientId) { //for making random numbers var r = new Random(); //put a random number between 1 and 6 into this int. (range is exclusive, so we'll never actually roll a 7) int roll = r.Next(1, 7); //check to see if the roll is a 1 if (roll == 1) { //put data in clientData to be sent to client clientData[clientId].DieRoll = roll; //sets banked point to 0 because game rules =( clientData[clientId].BankedPoints = 0; CallBackInfo temp = new CallBackInfo(); temp.DieRoll = roll; temp.BankedPoints = 0; temp.TotalPoints = clientData[clientId].TotalPoints; //update the players foreach (ICallback cb in clientCallbacks.Values) { cb.UpdateGui(temp, clientId); } //change player turns Game(); } else//if its not a one, add the die roll to the banked points for that player { clientData[clientId].DieRoll = roll; clientData[clientId].BankedPoints += roll; CallBackInfo temp = new CallBackInfo(); temp.DieRoll = roll; temp.BankedPoints = clientData[clientId].BankedPoints; temp.TotalPoints = clientData[clientId].TotalPoints; //update the players foreach (ICallback cb in clientCallbacks.Values) { cb.UpdateGui(temp, clientId); } } }
//this method is for when players want to stay. public void Stay(int clientId) { //Takes the points they have accrued and adds them to that players total points clientData[clientId].TotalPoints += clientData[clientId].BankedPoints; clientData[clientId].BankedPoints = 0; CallBackInfo temp = new CallBackInfo(); temp.DieRoll = 0; temp.BankedPoints = 0; temp.TotalPoints = clientData[clientId].TotalPoints; //If a player has gotten over t100 points then they win the game if (clientData[clientId].TotalPoints >= numWinningPoints) GameEnd(clientId); else { //Then updates all the GUI's foreach( ICallback cb in clientCallbacks.Values ) cb.UpdateGui(temp, clientId); Game(); } }
//Registers new players for callbacks public int RegisterForCallbacks() { if (!startGame) { // Store the ICallback interface (client object) reference for // the client which is currently calling RegisterForCallbacks() ICallback cb = OperationContext.Current.GetCallbackChannel<ICallback>(); clientCallbacks.Add(nextCallbackId, cb); CallBackInfo info = new CallBackInfo(); clientData.Add(nextCallbackId, info); return nextCallbackId++; } else return 0; }