Showing posts with label coding exercise. Show all posts
Showing posts with label coding exercise. Show all posts

Tuesday, August 20, 2024

Player vs. Robot Number Guessing Game

1. **Initialization of Game Statistics**:
   - The game starts by setting up a way to track the outcomes: how many times the player wins, how many times the robot wins, and how many times there is a draw.

2. **Gameplay Loop**:
   - The game runs in a loop that allows continuous play until the player decides to exit.
   - Each round begins with the player being prompted to input a number. 
   - If the player inputs "exit game," the loop ends, and the game displays the final statistics: how many times each side won and how many draws occurred.

3. **Input Validation**:
   - The game checks whether the player's input is a valid number:
     - If the input is not a number, the game informs the player that a string is not valid and asks for a new input.
     - If the input is a negative number, the game informs the player that negative numbers are not allowed.
     - If the input is a number greater than 1,000,000, the game flags it as invalid and prompts for a valid input.

4. **Robot's Guess and Goal Number**:
   - Once a valid number is provided, the game randomly generates two numbers:
     - One for the robot's guess.
     - One as the goal number that both the player's and the robot's guesses will be compared against.

5. **Determine the Winner**:
   - The game compares how close the player's guess and the robot's guess are to the goal number:
     - If the player's guess is closer, the player wins that round.
     - If the robot's guess is closer, the robot wins.
     - If both guesses are equally close, it's a draw.

6. **Update Statistics**:
   - The game updates the win/draw counts based on the result of each round.

7. **Game Exit**:
   - When the player types "exit game," the game ends and displays the final tally of wins, losses, and draws.


Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts