Welcome to the 6th PHP Coding puzzle !

Objective: The goal of the game is to guess a randomly generated number within a certain number of attempts.

Gameplay:

  1. The game begins by generating a random number between a specified range, let’s say between 1 and 100.
  2. The player is then given a certain number of attempts to guess the number. Let’s say the player has 10 attempts.
  3. On each turn, the player enters a guess.
  4. If the guess is correct, the game ends and the player wins.
  5. If the guess is incorrect, the game tells the player whether the guess was too high or too low, and the player can continue guessing until they run out of attempts.
  6. If the player runs out of attempts without guessing the number correctly, the game ends and the player loses.

Coding Challenge:

Write a PHP function that implements the “Guess the Number” game. The function should take two arguments: the minimum and maximum values of the range for the random number, and the maximum number of attempts. The function should return either the string “You win!” if the player correctly guesses the number within the specified number of attempts, or the string “You lose!” if the player runs out of attempts without guessing the number correctly.

Here’s a function signature to get you started:

function guessTheNumber($min, $max, $maxAttempts) {
  // Your code here
}

The solution is already available in the next page 😉 Feel free to share with us your solution here in comments or Github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here