Handling Bad Shots

If the shot did not succeed, the opposing team has the ball, and I’ll give them the chance to score. This is the easiest way to control the difficulty of the game. As the code stands now, the opponent will score about 5 percent of the time the player misses a pass or shot. To make the opposing team better, change the rate to a larger number. To make the opponent weaker, use a smaller number.

public void badShot(){
  Random roller = new Random();
  double toSucceed;
  int playerNumber;

  lblAnnounce.Text = "Opponent gets ball…";
  //check for opponent goal
  toSucceed = roller.NextDouble();
  //change the following value to alter game difficulty
  if (toSucceed < .05){
    oppScore++;
    updateScore();
    MessageBox.Show("Opponent Scores!!");
    setPlayer(HALFBACK);
  } else {
    playerNumber = roller.Next(5);
    lblAnnounce.Text += "recovered by " + playerName[playerNumber];
    setPlayer(playerNumber);
  } // end opponent scores if

} // end badShot

When the opponent gets the ball, I create a random double and compare it to .05. Five percent of the time, the random value will be smaller than .05, and the opponent will score. If this happens, I increase the opponent’s score, update the scoreboard, and send a message box to the user. I then give the ball to the halfback to restart play.

If the opponent’s shot did not succeed, I randomly determine which player begins with the ball and set that player as the current player.

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

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