This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Boss Nomad's profile picture

Need help with number variables.

Started by Boss Nomad, 30 December 2012 - 11:26 PM
Boss Nomad #1
Posted 31 December 2012 - 12:26 AM
Im going to make some private dice game. I will not share code. Numbers are 1-6.

Question 1: 3 Random numbers, How can i detect how many of those are example 1 and make result as variable? 2 1 1 (variable is: 2) in this condition.

Question 2: 3 Random numbers, How can i detect that the numbers are 1, 2 and 3 in whatever order, result as variable again. like 1 2 3, 2 3 1, 2 1 3

Question 3: 10 Random numbers, How can i detect that 5 of them are all the same number from 1-6, result as variable. like 1 1 1 1 1 2 3 4 5 6

Question 4: 6 Random numbers, How can i detect that there is 2 same numbers and 2 different same numbers like (2 2 4 4) 2 5

I hope i get good answers. I hope you understand me.
remiX #2
Posted 31 December 2012 - 12:33 AM
I'd think you would have to store the variables in a table and then count them while looping through the table
Boss Nomad #3
Posted 31 December 2012 - 12:34 AM
Any example code to help me out?
remiX #4
Posted 31 December 2012 - 12:53 AM
I might be wrong in what you're trying to look for but just run this simple code and check it..


term.clear() term.setCursorPos(1,1)
tCount = {
    [1] = 0,
    [2] = 0,
    [3] = 0,
    [4] = 0,
    [5] = 0,
    [6] = 0
}

for i, v in pairs(tCount) do
    print(i .. " has " .. v .. " counts")
end

for i = 1, 3 do
    tempNumber = math.random(1, 6)
    tCount[tempNumber] = tCount[tempNumber] + 1
end

print() -- skip line

for i, v in pairs(tCount) do
    if v > 0 then -- Only print if it has gone up
        print(i .. " has " .. v .. (v == 1 and " count" or " counts"))
    end
end
Boss Nomad #5
Posted 31 December 2012 - 01:25 AM
This is what i got now. Only problem that it only shows if there is number one in those numbers, not how many of them there is =(

I think it breaks the if condition if some of those is true. How can i continue with if condition to next "elseif"?


ones=0
if num1 == 1 then
ones=ones + 1
elseif num2 == 1 then
ones=ones + 1
elseif num3 == 1 then
ones=ones + 1
elseif num4 == 1 then
ones=ones + 1
elseif num5== 1 then
ones=ones + 1
end
remiX #6
Posted 31 December 2012 - 01:49 AM
It will only go once because you're using elseifs.
You will have to have an if for each one like

if num1 == 1 then
ones=ones+1
end
if num2 == 1 then
ones=ones+1
end

But did you check my code? Mine does count them, and then prints it out, just for a test… Check it out