Sheerpower Logo
Q.6  Simple Word Game
This is a simple word game:

How to Play

  1. The game scrambles a word and presents it to the player.
  2. The player types in what they believe the original word is.
  3. If the guess is correct, points are added to the score.
  4. The game continues with new scrambled words until all words are guessed.
  5. The player's final score is displayed at the end.

This game is simple, fun, and can be expanded with more words or more complex rules to keep it challenging and engaging!

// A Simple Word Scramble Game dim word_list$(0) word_list$(0) = "apple" word_list$(0) = "banana" word_list$(0) = "grape" word_list$(0) = "orange" word_list$(0) = "strawberry" // Initialize score score = 0 // Start the game print 'Welcome to the Word Scramble Game!' print 'Unscramble the word to score points.' print 'You can ask for a hint, but it will cost you points!' print for i = 1 to size(word_list$) word$ = word_list$(i) scramble_word with word$, returning scrambled$ hint_used = 0 print 'Scrambled word: '; scrambled$ input 'Your guess: ': guess$ if lcase$(guess$) = lcase$(word$) then print 'Correct!' score = score + 10 - hint_used * 2 else print 'Incorrect. The correct word was: '; word$ end if print 'Current score: '; score print next i print 'Game over! Your final score is: '; score stop // Function to scramble a word private routine scramble_word with word$, returning scrambled$ len = len(word$) scrambled$ = word$ for idx = 1 to len ch$ = scrambled$[idx:idx] random_idx = rnd(len) temp$ = scrambled$[random_idx:random_idx] scrambled$[idx:idx] = temp$ scrambled$[random_idx:random_idx] = ch$ next idx end routine end

Code Explanation:

1. Comment:

This comment briefly describes the program as a simple word scramble game.

2. Array Declaration:

Declares an array word_list$ with 5 elements to store the words that will be scrambled.

3. Populate Word List:

Each element of the word_list$ array is assigned a word that will be used in the game. The words are apple, banana, grape, orange, and strawberry.

4. Score Initialization:

Initializes the score variable to 0. This variable will track the player's score throughout the game.

5. Start of Game:

Indicates the start of the main game logic.

6. Welcome Message:

Prints a welcome message to the player, introducing the game.

7. Game Instructions:

Prints instructions explaining that the player needs to unscramble words to score points. A hint option is mentioned, though it is not implemented in this version of the code.

8. Loop Through Words:

Starts a loop that iterates through the words in the word_list$ array. The loop runs 5 times, once for each word.

9. Select Word:

In each iteration of the loop, the word$ variable is assigned the current word from the word_list$ array.

10. Scramble Word:

Calls the scramble_word routine, passing the current word (word$) to it. The scrambled version of the word is returned and stored in the scrambled$ variable.

11. Initialize Hint Counter:

Initializes hint_used to 0 for the current round. This variable tracks whether a hint is used (not implemented in this version).

12. Display Scrambled Word:

Prints the scrambled version of the word for the player to see.

13. Player Input:

Prompts the player to enter their guess for the unscrambled word. The player's guess is stored in the guess$ variable.

14. Check Guess:

Converts both the player's guess (guess$) and the original word (word$) to lowercase using lcase$ to ensure the comparison is case-insensitive. If they match, the player guessed correctly.

15. Correct Guess Message:

If the guess is correct, prints a congratulatory message.

16. Update Score:

Updates the player's score by adding 10 points. If hints were used (not implemented here), the score would be reduced by 2 points per hint.

17. Incorrect Guess Condition:

If the player's guess is incorrect, this block is executed.

18. Incorrect Guess Message:

Prints a message informing the player that their guess was incorrect and reveals the correct word.

19. End of Guess Check Block:

Ends the conditional block that checks whether the player's guess was correct or incorrect.

20. Display Score:

Prints the player's current score after each round.

21. Next Word:

Ends the loop iteration and moves to the next word in the word_list$ array.

22. End of Game:

After all 5 words have been played, the game prints a message saying "Game over!" and displays the player's final score. The stop statement ends the program.

23. Scramble Word Routine:

Defines a private routine named scramble_word. It takes a word (word$) as input and returns the scrambled version of the word (scrambled$).

24. Get Word Length:

Calculates the length of the word using len(word$) and stores it in the len variable.

25. Initialize Scrambled Word:

Initializes the scrambled$ variable with the original word (word$). The scrambling process will modify this variable.

26. Scrambling Loop:

Starts a loop that iterates over each character in the word.

27. Get Character:

Extracts the character at position idx in scrambled$ and stores it in ch$.

28. Generate Random Index:

Generates a random index within the length of the word using rnd(len) and stores it in random_idx.

29. Get Random Character:

Extracts the character at the random_idx position in scrambled$ and stores it in temp$.

30. Swap Characters:

Swaps the character at position idx with the character at random_idx.

31. Complete Swap:

Completes the swap by placing the original idx character (stored in ch$) at the random_idx position.

32. Next Character:

Ends the loop iteration and moves to the next character in the word.

33. End of Scramble Routine:

Ends the scramble_word routine.

34. End Program:

Ends the program execution.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen