Guess The Number (tutorial)

From ComputerCraft Wiki
Jump to: navigation, search

Creating The Program

The main new things with this program are loops, and math.random.

Let's delve right in.

term.clear()
textutils.slowPrint("Guess the Number")
print("You have 6 guesses. Guess the number before you run out of guesses.")
numb = math.random(1, 100)

The only new thing here other than that covered in the Hello World Tutorial and math.random is term.clear(). This clears the screen, making it blank.

Meanwhile, math.random sets a variable. Basically, it picks a number between one and one-hundred, and makes "numb" be a short name for it. It's like this because we don't know what numb really is, because it's a number between one and one-hundred, but we don't know which.

The first number in math.random's parenthesis is as small as the number can go, while the second one is as big as it can go. If we wanted to make it possible to do 1 to 400, for instance, we would type math.random(1, 400)

term.clear()
textutils.slowPrint("Guess the Number")
print("You have 6 guesses. Guess the number before you run out of guesses.")
numb = math.random(1, 100)
while true do

end

This is called a while loop. While the condition is true, it does the code from the start of the while, to the part that says "end". Once it hits end, it goes back to the start of the while.

Now let's start using it, by putting code in between. (We put spaces before the lines to make it easier to read, you do not need to do this.)

term.clear()
textutils.slowPrint("Guess the Number")
print("You have 6 guesses. Guess the number before you run out of guesses.")
numb = math.random(1, 100)
while true do
    guess = io.read()
    guess = tonumber(guess)
end

Now we can get what the player says. Variable = io.read() means to get whatever the player types in. Since we don't know what the player types in, we just give what the player types in a name that we use instead. In this case, the name is "guess", because it is the player's guess.

BUT, the player only says strings, not actual numbers, due to how io.read() works. A string is like, "cat", or "3mg", though it can also be "3305" or "2poodles1food bowl". This is easy to fix, by using tonumber(), which turns it into a -real- number. You may be confused by putting guess in parentheses after; this tells it -what- to turn into a number, and then it sets it.

Now we can start messing with the check:

term.clear()
textutils.slowPrint("Guess the Number")
print("You have 6 guesses. Guess the number before you run out of guesses.")
numb = math.random(1, 100)
while true do
    guess = io.read()
    guess = tonumber(guess)
    if guess == numb then
        win = true
        break
    else
        print("Incorrect!")
        if guess > numb then
            print("Your guess was too high.")
        elseif guess < numb then
            print("Your guess was too low.")
        else
            print("I have no idea what happened.")
        end
        tries = tries + 1
    end
end

Now, this is a lot to take in, but let's take it slowly. == means to check, while = means to set. If means to only do the part in-between the if and the elseif if it exists. If not, then it only does the part in between the if and the else. If not, then the if and the end.

So if it is right, it BREAKS it. This is used to stop the loop, which makes while true do stop.

If it's wrong, it first checks if it is bigger. If it is bigger, then it says the guess is too big.

Contrariwise, if it is too small, it says it is too small.

If it's neither too big OR too small, due to some magic, it says that it has no idea what happened.

What We Learned

We learned about breaks, loops, ifs, basic strings, and math.random.

Final Code

This is a polished and Creative Commons licensed version.

-- Guess the Number, coded in Lua, for ComputerCraft by Kaleb702 is licensed under a
-- Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
 
-- Initialization --

do
    numb = math.random(1,100)
    tries = 1
    triesmax = 7
    version = 1.1
    win = false
end
 
-- Intro --
 
do
    term.clear()
    textutils.slowPrint("-------------------------------")
    textutils.slowPrint("Guess the Number V " .. version)
    textutils.slowPrint("-------------------------------")
    print("Enter to continue...")
    io.read()
    print("You have 6 guesses. Guess the number before you run out of guesses.")
end
 
-- Main loop --
 
while true do
    if tries > triesmax then
        break
    else
        print("Guess. [1 - 100, whole number]")
        guess = io.read()
        guess = tonumber(guess)
        if guess == numb then
            win = true
            break
        else
            print("Incorrect!")
            if guess > numb then
                print("Your guess was too high.")
            elseif guess < numb then
                print("Your guess was too low.")
            else
                print("I have no idea what happened.")
            end
            tries = tries + 1
        end
    end
end

-- Outro --
 
do
    term.clear()
    textutils.slowPrint("GAME OVER")
    if win == true then
        print("Winner: PLAYER")
    else
        print("Winner: COMPUTER")
    end
        textutils.slowPrint("Thank you for choosing Kapow Creations.")
end