How to write the 7 Boom game. If a number is divided by 7 or includes the digit 7, the user writes Boom in C#

1 Answer

0 votes
using System;

// 7 Boom game. The user enters a number he wants to start the game, 
// (-1 to end the game, or lost), then the user enters numbers, from the number he entered
// If the current number is divisible by 7, or one of its digits is 7, 
// And the user did not write the word 'Boom', the user loses. 

class Program
{
    static void Main()
    {
        RunGame();
    }

    // -----------------------------
    // Game Controller
    // -----------------------------
    static void RunGame() {
        Console.Write("Enter a number from which you want to start the game (-1 to exit): ");
        string startInput = Console.ReadLine();

        if (!int.TryParse(startInput, out int currentNum) || currentNum == -1)
            return;

        while (true) {
            string userInput = GetUserInput();

            if (userInput == "-1") {
                Console.WriteLine("Game Over!");
                break;
            }

            if (!ProcessTurn(currentNum, userInput))
                break;

            currentNum++;
        }
    }

    // -----------------------------
    // Input Handling
    // -----------------------------
    static string GetUserInput() {
        Console.Write("Enter a number (or Boom): ");
        return Console.ReadLine();
    }

    // -----------------------------
    // Turn Logic
    // -----------------------------
    static bool ProcessTurn(int expectedNumber, string userInput) {
        bool shouldBoom = IsBoom(expectedNumber);
        string lower = userInput.ToLower();

        if (shouldBoom) {
            if (lower != "boom") {
                Console.WriteLine($"{userInput} (should be Boom)");
                Console.WriteLine("YOU LOSE!");
                return false;
            }
        }
        else {
            if (lower == "boom" || !int.TryParse(userInput, out int value) || value != expectedNumber)
            {
                Console.WriteLine($"Wrong move! It was {expectedNumber}");
                Console.WriteLine("YOU LOSE!");
                return false;
            }
        }

        return true;
    }

    // -----------------------------
    // Boom Logic
    // -----------------------------
    static bool ContainsSeven(int num) {
        return num.ToString().Contains('7');
    }

    static bool IsBoom(int num) {
        return num % 7 == 0 || ContainsSeven(num);
    }
}


    
    
/*
run:
    
Enter a number from which you want to start the game (-1 to exit): 6
Enter a number (or Boom): 6
Enter a number (or Boom): Boom
Enter a number (or Boom): 8
Enter a number (or Boom): 9
Enter a number (or Boom): 10
Enter a number (or Boom): 11
Enter a number (or Boom): 12
Enter a number (or Boom): 13
Enter a number (or Boom): Boom
Enter a number (or Boom): 15
Enter a number (or Boom): 16
Enter a number (or Boom): Boom
Enter a number (or Boom): 18
Enter a number (or Boom): 19
Enter a number (or Boom): 20
Enter a number (or Boom): 21
21 (should be Boom)
YOU LOSE!
    
*/


 



answered Mar 14 by avibootz
edited Mar 14 by avibootz
...