WebNovels

Chapter 1 - code 1

import random import time import json import os

leaderboard_file = "rps_leaderboard.json"

Load or initialize leaderboard

def load_leaderboard(): if os.path.exists(leaderboard_file): with open(leaderboard_file, "r") as file: return json.load(file) return {}

def save_leaderboard(lb): with open(leaderboard_file, "w") as file: json.dump(lb, file, indent=2)

def animate_text(text): for char in text: print(char, end="", flush=True) time.sleep(0.03) print()

def get_player_choice(): while True: choice = input("Choose Rock, Paper, or Scissors: ").strip().lower() if choice in ["rock", "paper", "scissors"]: return choice print("Invalid choice. Try again!")

def get_computer_choice(): return random.choice(["rock", "paper", "scissors"])

def determine_winner(player, computer): if player == computer: return "tie" wins = { "rock": "scissors", "paper": "rock", "scissors": "paper" } return "player" if wins[player] == computer else "computer"

def update_leaderboard(lb, name): if name in lb: lb[name] += 1 else: lb[name] = 1 save_leaderboard(lb)

def display_leaderboard(lb): sorted_lb = sorted(lb.items(), key=lambda x: x[1], reverse=True) animate_text("\n--- Leaderboard ---") for i, (name, wins) in enumerate(sorted_lb[:10], 1): animate_text(f"{i}. {name} - {wins} wins")

def play_game(): name = input("Enter your name: ").strip() rounds = 3 player_score = 0 computer_score = 0

animate_text("Let's play Rock, Paper, Scissors!")

for round_num in range(1, rounds + 1):

animate_text(f"\n--- Round {round_num} ---")

player = get_player_choice()

computer = get_computer_choice()

animate_text(f"You chose {player}. Computer chose {computer}.")

result = determine_winner(player, computer)

if result == "player":

animate_text("You win this round!")

player_score += 1

elif result == "computer":

animate_text("Computer wins this round!")

computer_score += 1

else:

animate_text("It's a tie!")

animate_text(f"\nFinal Score - You: {player_score}, Computer:

More Chapters