Welcome to a New Series: Code Review

This series was inspired by a comment left on my blog post, "Why MiniScript?". By one of the readers sharing their personal experience with MiniScript, saying:

I discovered the MiniScript language a week ago and I'm in love with it. I'm studying this language.
I'm a beginner in learning programming, and I've already managed to create two text-based games with MiniScript. One is a dice betting game, and the other is a robot fighting game.

They also shared the code for their dice game.

So, in this post, we’ll review that dice game — looking at what’s good, what can be improved, and how we can take it one step further together.

Special thanks to @fernando_noise for the comment and their code contribution for mini-script community.

DICE GAME

// The goal of this game is to reach $1000000.
// You bet on a dice number and the amount of money on that number.
// If you guess the correct dice number, your bet amount will be doubled.
// If you guess wrong, you lose the amount of money you bet.

money = 100
print "DICE GAME"
print ""

while true
    print("YOU HAVE $" + str(money))

    while true
        bet = val(input("Bet on a number from 1 to 6 and press Enter!"))
        if bet >= 1 and bet <= 6 then
            break
        else
            print("Invalid number! Choose between 1 and 6.")
        end if
    end while

    while true
        amount = val(input("Enter how much money you want to bet and press Enter!"))
        if amount > money then
            print("You don't have that much money.")
        else 
            break
        end if    
    end while

    dice = floor(rnd * 6) + 1 

    print("The dice rolled " + (dice) + "!")

    if dice == bet then
        winnings = amount * 2  // Player wins double the bet
        money = money + winnings
        print("You won $" + (winnings) + "!")
    else 
        money = money - amount
        print("You lost $" + (amount) + "!")
    end if

    wait 2
    print ""

    if money <= 0 then
        print("YOU LOST EVERYTHING!!!")
        break
    else if money >= 1000000 then
        print("CONGRATULATIONS! YOU ARE A MILLIONAIRE!!!")
        break
    end if

end while

How the Game Works

This game is a simple yet exciting example of how Random Number Generation (RNG), Loops, and Control Flow can work together to create a fun, interactive experience.

If you're new to these concepts, I recommend checking out the following articles to get a better understanding:

Understanding the Code

It's clear that @fernando_noise has done a very well job of making this code. One of the best practices he used is adding comments at top of his code. This is a one of the best practice, especially when someone else (like me) is reading the code. It makes it easy to understand the context of the code.

The goal of the game is simple: you bet some money on a number (between 1 and 6), and if the dice lands on your number, you double your bet. If you lose, you lose the money you bet.

In a way, it's like a "Double it and give it to the next person!" type of vibe 😂. But seriously, it's a great beginner project to get a feel for programming.

Understanding The Control Flow

The Control flow of this code goes like

  1. Initialize money to $100

  2. Start infinite game loop

  3. Show current money

  4. Ask player to bet on a number (1–6)

  • 1. Repeat until valid input
  1. Ask player how much money to bet
  • 1. Repeat until it's ≤ current money
  1. Roll a dice (1–6)

  2. Compare bet with dice roll

    1. If correct: double the money bet and add to total
    1. If wrong: subtract bet from money
  1. Pause briefly (wait 2)

  2. Check for game-ending conditions:

    1. If money ≤ 0 → Game over (lost everything)
    1. If money ≥ 1,000,000 → Player wins (becomes millionaire)
  1. Repeat from step 3 if no win/lose condition is met

Basic Functions and Commands

Before we move on to improving the code, understanding some commands will help us to familiarize yourself with some of the basic functions used here. If you're not sure what they do, I'll break them down simply.

  • val() - Converts a data type into an integer.
  • str() - Converts a data type into a string.
  • floor() - Rounds a number down to the nearest whole number.
  • break - Terminates the current loop.
  • rnd - Generates a random number.

If you’d like to get a deeper understanding of how rndworks, I recommend checking out this post:

-Learn By Code 1.2 - Understanding Random Number Generation(RNG)

Improving the code

After reviewing the game, I realized the original code was already well-written, leaving little room for improvement.
Still, for the sake of learning and practicing my own style, I decided to give it a personal touch.

So, I rewrote the code the way I would write it—if I were building this game.

And here’s the result 👇

// The goal of this game is to reach $1000000.
// You bet on a dice number and the amount of money on that number.
// If you guess the correct dice number, your bet amount will be doubled.
// If you guess wrong, you lose the amount of money you bet.

money = 100
print "DICE GAME"
print ""

while true

    print "YOU HAVE $" + money

    bet = val(input("Bet on a number from 1 to 6 and press Enter!"))
    while bet < 1 or bet > 6
        print "Invalid number! Choose between 1 and 6."
        bet = val(input("Bet on a number from 1 to 6 and press Enter!"))
    end while

    amount = val(input("Enter how much money you want to bet and press Enter!"))
    while amount <= 0 or amount > money
        if amount <= 0 then
            print "Bet must be a positive amount!"
        else 
            print "You dont have much money."
        end if  
        amount = val(input("Enter how much money you want to bet and press Enter!"))
    end while

    dice = ceil(rnd * 6)

    print "The dice rolled " + dice + "!"

    if dice == bet then
        winnings = amount * 2  // Player wins double the bet
        money = money + winnings
        print "You won $" + winnings + "!"
        print "You are $" + (1000000 - money) + " away from becoming a millionaire!"
    else 
        money = money - amount
        print "You lost $" + amount + "!"
        print "You are $" + (1000000 - money) + " away from becoming a millionaire!"
    end if

    wait 2
    print ""

    if money <= 0 then
        print "YOU LOST EVERYTHING!!!"
        break
    else if money >= 1000000 then
        print "CONGRATULATIONS! YOU ARE A MILLIONAIRE!!!"
        break
    end if

end while

Before jumping into the changes I made, here are a few small things that @fernando_noise missed while writing the original code:

  • In most languages, print is often used with parentheses, but in MiniScript, it works just fine without them. So, there's no need to write print()—just use print "".

  • For random dice rolls, the code uses dice = floor(rnd * 6) + 1. While this works, using ceil(rnd * 6) is a cleaner alternative, since it naturally gives values from 1 to 6 without needing to add 1.

  • The line print("YOU HAVE $" + str(money)) uses str() to convert the number to a string, which is common in other programming languages. But in MiniScript, you can simply write print "YOU HAVE $" + money—it handles different data types automatically when printing.

Now, let’s talk about the tweaks I made—not because the original code needed them, but because this is how I would’ve written it:
All while loops had if else statements nested in them which looked a bit messy instead i used the condition on if else statement in while loop itself and made code shorter and easy to read

  • The original version used while loops with nested if-else blocks, which made things look a bit bulky. I cleaned that up by shifting conditions directly into the while loops, making the code shorter and more readable.

  • I added a new error message for when the player enters a negative number as their bet. It’s a small touch, but helps improve the user experience.

  • Lastly, I noticed the game hides the goal of becoming a MILLIONAIRE until someone either checks the code or reaches it. I added a fun little message that shows how close you are to reaching that million-dollar milestone—makes the game feel more engaging and gives players something to look forward to.

Outro

And that wraps up Part 1 of this code review!
If you want to be featured in an upcoming episode—just like @fernando_noise—drop your code in the comments!

Who knows? Your project might be the star of the next one!

Bonus: After all 3 parts of this mini-series are out, I’ll be putting together a long-form video with all the reviewed projects and posting it on the main channel. So stay tuned!

In the meantime, feel free to check out my other posts or connect with me through the links below

Social Links

New Video

Miniscript Site

Miniscript Wiki for rnd

Minimicro Site

My Youtube

My Itch Page