diff --git a/game.py b/game.py new file mode 100644 index 0000000..47d6438 --- /dev/null +++ b/game.py @@ -0,0 +1,59 @@ +import requests +import sys +import getpass +import random + +def start_game(): + print("\n" + "="*30) + print("WELCOME TO THE GUESSING GAME") + print("="*30) + + secret_number = random.randint(1, 100) + attempts = 0 + win = False + + print("I'm thinking of a number between 1 and 100.") + + while not win: + try: + guess = int(input("Enter your guess: ")) + attempts += 1 + + if guess < secret_number: + print("Too low! Try again.") + elif guess > secret_number: + print("Too high! Try again.") + else: + print(f"🎉 Correct! You found it in {attempts} tries.") + win = True + except ValueError: + print("Please enter a valid number.") + +def check_server(): + # Replace 'localhost' with your Tailscale IP if running on a different Pi + url = "http://localhost:6000/status" + + print("--- Remote Access Required ---") + password = getpass.getpass("Enter Server Password: ") + + try: + headers = {'x-password': password} + response = requests.get(url, headers=headers) + + if response.status_code == 200: + data = response.json() + if data.get("run") == "yes": + print("Access Granted. Launching game...") + start_game() + else: + print("Access Denied: The server administrator has disabled this game.") + elif response.status_code == 401: + print("Authentication Failed: Incorrect Password.") + else: + print(f"Error: Server returned {response.status_code}") + + except requests.exceptions.ConnectionError: + print("Network Error: Cannot reach the Node.js server.") + +if __name__ == "__main__": + check_server()