Responding to Player Clicks

The game’s action comes in two places. The passage of time will move the various player images on the field, but this has no real bearing on the game. The most important actions come when the user clicks one of the player images. Each of these images was registered with the clickPlayer() method as its event handler. This method takes action based on the algorithm described in the Shot Demo program earlier in this chapter:

private void clickPlayer(Object sender, EventArgs e){
  //happens whenever you click a player
  Random roller = new Random(); 
   double toSucceed;
   double myRoll;

   //figure out which player was clicked
   PictureBox thePic = (PictureBox)sender;
   int playerNumber = Convert.ToInt32(thePic.Tag);
   nextPlayer = playerNumber;

   //figure out how likely success is
   toSucceed = shotChance[currentPlayer, nextPlayer];

   //roll a random double
   myRoll = roller.NextDouble();

  //Announce what's going on
  string message = "From: " + playerName[currentPlayer];
  message += " to: " + playerName[nextPlayer] + " ";
  message += toSucceed.ToString();
  lblAnnounce.Text = message;

  //look for success
  if (myRoll < toSucceed){
    goodShot();
  } else {
    badShot();
  } // end 'pass succceeds' if

} // end clickPlayer

This method uses a number of variables to get started. It will need to know which player currently has the ball and to which player the user intends to pass. I’ll need to do a little technical gymnastics to squeeze out this information, but it is accessible. The currentPlayer variable was declared at the class level, and its value is preset to whichever player currently has the ball. The trickier part is to determine nextPlayer, because the user doesn’t directly enter this information. Instead, he clicks a picture box. The problem is to determine which picture box the user clicked.

Fortunately, C# provides a couple good tricks for figuring out exactly this type of information. First, recall the sender object that is automatically a parameter of all event handlers. This parameter contains an object that represents whatever object triggered the event. In this case, the picture box that the user clicked will be stored in the sender variable. I cast the sender object as a picture box and stored it in the local variable thePic. Back when I created each picture box, I stored its ID in the Tag property. I can extract that property now and convert it to an integer to determine which player was clicked. That value is stored in nextPlayer.

When I know what currentPlayer and nextPlayer are, I can use them to look up the likelihood of success in the shotChance lookup table. The mechanics of this activity are the same as in the Shot Demo program. The lookup table returns a double value representing the likelihood that the given shot will succeed.

I then send a message to the announcer label of the shot being attempted and the likelihood this shot will succeed. Finally, I evaluate the shot with a random number and call a method based on the results. If the shot succeeds, control flows to the goodShot() method. If the shot fails, the badShot() method takes control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.62