Numbers (help needed)

Hello,

I have different numbers in my app that generate randomly. There’s one number on the top of the simulator, you then have three different numbers on the bottom of the simulator that you need to add, subtract, multiply and divide to get the answer on the top. You basically need to use the numbers on the bottom to get to the top number.

I have provided code on below that you can copy paste to further see what i mean.

I have generated the random numbers and have the all the numbers showing, when you click on the bottom numbers they becoming darker. But what i don’t know how to do is say, if you click on let’s say 2 numbers those too numbers will add together and be the new score on the bottom of the screen…etc. Same with multiplying,subtracting dividing. So for further clarification, i need to know how i would be able to click on the numbers, then the numbers that you have clicked on have added together. The two numbers that you have added together will be the final score on the furthest bottom of the screen.

Thanks! I really appreciate help! If you do not understand please contact me!

  • Michael

The code:

[code]

limit = math.random(50,800)
local scoreTxt = display.newText("",150,150,“Cartoon”,20)
limit2 = math.random(1,50)
local scoreTxt2 = display.newText("",150,300,“Cartoon”,20)
limit3= math.random(1,50)
local scoreTxt3 = display.newText("",200,300,“Cartoon”,20)

limit4 = math.random(1,50)
local scoreTxt4 = display.newText("",100,300,“Cartoon”,20)

randNum = true
local function moveHeavens(e)
scoreTxt.text = limit
if randNum == true then
if randNum == false then
end
end
end
Runtime:addEventListener(“enterFrame”, moveHeavens )

randNum2 = true
local function moveHeavens2(e)
scoreTxt2.text = limit2
if randNum2 == true then
if randNum2 == false then
end
end
end
Runtime:addEventListener(“enterFrame”, moveHeavens2 )
randNum3 = true
local function moveHeavens3(e)
scoreTxt3.text = limit3
if randNum3 == true then
if randNum3 == false then
end
end
end
Runtime:addEventListener(“enterFrame”, moveHeavens3 )
randNum4 = true
local function moveHeavens4(e)
scoreTxt4.text = limit4
if randNum4 == true then
if randNum4 == false then
end
end
end
Runtime:addEventListener(“enterFrame”, moveHeavens4 )

function PressRan()
limit = math.random(100,500)
end
scoreTxt:addEventListener(“tap”, PressRan)

function PressAlpah2()
scoreTxt2.alpha = 0.4
itDoes = true
end
scoreTxt2:addEventListener(“tap”, PressAlpah2)
function PressAlpah3()
scoreTxt3.alpha = 0.4
itDoes = true
end
scoreTxt3:addEventListener(“tap”, PressAlpah3)
function PressAlpah4()
scoreTxt4.alpha = 0.4
itDoes = true
end
scoreTxt4:addEventListener(“tap”, PressAlpah4)
fScore = 0
local scoreTxt5 = display.newText("",150,400,“Cartoon”,20)

local function moveHeavens5(e)
scoreTxt5.text = fScore
if itDoes == true then

if itDoes == false then
end
end
end
Runtime:addEventListener(“enterFrame”, moveHeavens5 )
[import]uid: 23689 topic_id: 21061 reply_id: 321061[/import]

Hi Michael, interesting app here.

I’m about confused as to what you mean by new score on the bottom of the screen.

Here is what I think you mean: A number at the top is randomly generated on the screen, say it is 6 and this is the target number, then there are 3 different numbers on the bottom say 3,2,1 so then if the player selects 3 X 2 their score becomes 1 and the 3 X 2 result is shown at bottom. If they get another one correct their score becomes 2.

So in picture form:

Target: 6

Operations: + - * /
3 2 1
Results: 6
Score: 1

Tell me if this is what you mean and I will then help explain further. [import]uid: 116225 topic_id: 21061 reply_id: 83245[/import]

Almost :slight_smile:

Thanks for replying!

So ya let’s say the top number is 8. And the randomly generated numbers on the bottom is 1,2,3. Their would be 4 options on the left of the screen, + - * /, if you click on one of those symbols you would use it to finish the equation. If i was playing i would click on the * symbol and do 3x2, then 6 would appear on the very bottom of the screen, showing what you have done so far. But what you are really trying to achieve is 8, so you would then click on the + symbol. And press on the number 2. Then The bottom number would be 8 and you have solved the equation. You would then go to the next level. Hope you understand.

So what i need help is making those symbols active. If you click on + - * / then click on two or however many numbers you want to press on, it would either add, subtract, multiply or divide all the numbers you have selected and then it would display that number on the bottom of the screen. I tried saying that, if you click on the number and its dark then randomly generated number would be the bottom number. But it did not work. Do you know how i can achieve this?

If you still don’t quite understand, please ask!

Thanks alot!

  • Michael [import]uid: 23689 topic_id: 21061 reply_id: 83251[/import]

Would it be a variable ?

The randomly generated numbers would = (x), then (x) + - * / another number would be the bottom number?

I get the logic, just don’t know how to do it. [import]uid: 23689 topic_id: 21061 reply_id: 83252[/import]

Hi Michael,

Yes I understand what you are trying to achieve now. So in each top number, you must use all 3 numbers below, not just 2. Once one operation is done, show that result, then do another operation on the result and check it matches the top number.

So yeah, as you said, you will have to update a variable after each operation is applied.

Ok, so you need several variables. One variable for each of the numbers, another for their corresponding text, one to keep track of the score, another to keep track of the numbers being selected, and maybe another single variable which will be used to select which operation to select.

So you will have for example:

local topNumber;  
local number1;  
local number2;  
local number3;  
local bottomNumber;  
local topNumber\_txt;  
local number1\_txt;  
local number2\_txt;  
local number3\_txt;  
local bttomNumber\_txt;  
local score;  
local numbers;  
local operationSelect;  

The topNumber, number1, number2, number3 are really the limit variables in your code.

So what you do is with a timer.performWithDelay() call a function to say

topNumber = math.random(10,99);  
-- use on the other numbers, but there must be some logic here because they have to somehow be able to equal the number up the top.  

So give the player maybe 5 seconds to try and get the correct answer, if they don’t within the time, all numbers are randomly generated again and their score is not incremented.

You need to also make it so each number, a text object, can be touched. I’ll provide an example below:

function number1\_txt:touch(event)  
 if(event.phase == "began") -- when the user touches the number  
 if(numbers == 0) then  
 bottomNumber = number1;  
 bottomNumber.text = bottomNumber; -- update display  
 end  
 end  
end  
number1\_txt:addEventListener("touch", number1\_txt)  

So every time the person touches the number1_txt object, the above code is executed.

the numbers variable is another variable which is used so we know how many numbers have been used up so far. Because this is out first number pressed, the current value will be equal to bottomNumber.

So now you will need touch events for the other number objects, and also for the operators. For the operations for each touch function say

operation\_select = 1 -- or 2 or 3 or 4 depending on which one pressed .
Then when you select your second number you can do the following:

if(numbers == 1) then  
 if(operation\_select = 1)  
 bottomNumber = bottomNumber + number2;  
 bottomNumber.text = bottomNumber;  
 elseif(operation\_select = 2)  
 bottomNumber = bottomNumber - number2;  
 bottomNumber.text = bottomNumber;  
 end  
end  

etc. This would also be added to the number1_txt touch function, it must apply to any because they can pick any number they want in any order.
I think what would also make the app better would be to remove the numbers you select when you have pressed them. You can do that by:

 object:remove\_self();  

Where object would be one of your numbers or the operation used.
Hopefully this puts you on the right track, best of luck with it.
If you need any more help, feel free to ask.
Regards,
Glenn. [import]uid: 116225 topic_id: 21061 reply_id: 83261[/import]

Thanks so much!

You’ve helped so much Glenn!

I just have another question, how would i then do it when dividing and multiplying two numbers?
I tried doing, bottomNumber = bottomNumber * number2; but it stays 0 because what it’s doing is the randomly generated number * 0, which is still 0.

Thanks again! Your great!

-Michael [import]uid: 23689 topic_id: 21061 reply_id: 83460[/import]

Hey Michael, no worries at all glad I’ve been of help. I’m quite new to Corona myself, so trying to help others out to further familiarize myself with Corona is beneficial to me too.

Ok as for the issue you have mentioned, I think it has to do with variable scope.

So when you click the first number, the bottom number is equal to that. So you need some conditional logic so that the first thing selected is a number and not an operation.

So say the numbers are 1, 2, 3 to choose from

I pick 1 then in memory we have:

bottomNumber = 1.

Then say I choose multiply then 2
bottomNumber = bottomNumber*2
= 2.

This should work fine.

I thought of something else last night, you may already have a way of doing it, but I thought this could be useful. I thought of a way to generate random numbers that will always work in your game, it involves you setting them up your self beforehand though.

Basically what I am suggesting is that you have a variable called choices. You use math.random on this and say it’s a number between 0 and 100 (this number is however many different number combinations you want). Then all you would simply do, is call a function and have for example:

function numbers\_Select(choices)  
if(choices == 0) then  
topNumber = 6;  
number1 = 2;  
number2 = 3  
number3 = 1;  
elseif(choices == 1) then  
topNumber = 10;  
number1 = 5;  
number2 = 3;  
number3 = 12;  
-- keep going  

I’ve just written this quite quickly, I will investigate a bit further through the code I suggested and edit if I see something. [import]uid: 116225 topic_id: 21061 reply_id: 83472[/import]

Thanks again Glenn. Wow, i never thought of doing it like that. I think i’m going to incorporate that to my game! I sometimes lay down on my bed and ideas just come :smiley:

I really appreciate your help.
So getting back to the multiplying and dividing part, the adding and subtracting totally makes sense for me. But instead of multiplying/dividing the bottomNumber, having a separate equation. As you said before, you would pick 1 then multiply it by 2. That is great and i want to also add that feature but i was wondering how i could make a separate equation, something likes this:

12,20,5,9,4 are randomly generated, i forgot to mention that their is not only three numbers. You add 9 + 4 together which makes 13, 13 is now the bottom number. But instead of doing the bottom number * 4 which will be 52. You would click on both 20 and 4 then press on the * button. 20 * 4 = 80, then the bottom number would equal to 13 + 80 which = 93. Do you see what I’m trying to get at? You would have the option to divide/ multiply the bottom number by a randomly generated number, and also multiply randomly generated numbers together that would then add to the bottom number.

Again, please ask if you have any questions. You have been a great help, I hope I’m being clear and not confusing you.

  • Michael [import]uid: 23689 topic_id: 21061 reply_id: 83475[/import]

Are you always going to use 4 numbers down the bottom? If so I know of a way to achieve that.

You now use another variable, say bottomNumber2 (Use this to store the value of your 20*4 operation). Then after you have performed that you would say bottomNumber = bottomNumber+bottomNumber2 so the final result is then updated. You could also make the game more complicated, by allowing the user to choose whether to add these two results together as you did but allow them to choose any operation on the two. You can do this using conditional logic again.

What you could even do is display bottomNumber2 maybe in a seperate spot so the player can see what is going to be added before showing the total result. [import]uid: 116225 topic_id: 21061 reply_id: 83481[/import]

How would i store the value in a variable ?

Is it possible to say, if the alpha of the text is 0.4 then the it would * the other texts that have an alpha of 0.4?

  • Michael [import]uid: 23689 topic_id: 21061 reply_id: 83601[/import]

What you can do is use a counter variable, to count how many numbers have been chosen. And then say you have chosen two numbers, (counter=1) you start working with the new bottomNumber variable I mentioned. For testing purposes and maybe in the final release, up to you, I think you should use 3 result numbers, bottomNumber1 (on the bottom left), bottomNumber2 (on the bottom right) and bottomNumber appearing in the middle

So for example in your touch events implement logic like this somewhere:

if(countNumbers \< 2) then   
 -- bottomNumber1 = ... (Work with bottomNumber1.  
elseif(countNumbers \> 1 and countNumbers \< 4) then  
 -- bottomNumber2 = .. (Work with bottomNumber2.  
elseif(countNumbers == 4) then  
 bottomNumber = (add/subtract/times/divide bottomNumber1 and bottomNumber2  
countNumbers = countNumber+1; -- don't forget to increment.  

Yes I’m pretty sure that code would work, something like this:

if(number1.alpha == 0.4 and number2.alpha == 0.4) then -- execute some code. [import]uid: 116225 topic_id: 21061 reply_id: 83665[/import]

Thanks again.

This is going to be my last question :slight_smile: I don’t want to bother you.

So when i’m dividing two numbers they usually turn into some decimal that goes on forever. Do you know if theirs away to round the decimal to the nearest whole number?

If i divide 19 by 8, it equals 2.375. Do you know how i can make it the number always round to the nearest whole number, meaning 2 in this case?

Thanks for your help! [import]uid: 23689 topic_id: 21061 reply_id: 83680[/import]

Corona’s API list is your friend – it’s a listing of all the available functions.

http://developer.anscamobile.com/resources/apis

You’re looking for math.round

:wink:

Best,
~~Kenn [import]uid: 13859 topic_id: 21061 reply_id: 83697[/import]

Thanks Kenn that looks like what I need!

Do you mind telling me how I could use this?

If I’m adding 0.8 + .9 which would equal 1.7, both .8 and .9 have different names. 1.7 is called “TheRealScore”, would I then do something like this?

local value = math.round( TheRealScore ) ?

I tried that but it didn’t work.

Thanks!

  • Michael
    [import]uid: 23689 topic_id: 21061 reply_id: 83700[/import]

From the API page, you can click on any of them and it’ll take you to the detail page:

IE:
http://developer.anscamobile.com/reference/index/mathround

You usage is correct, so you’ve got some other issue going on which you may have to debug based on the particular way that it didn’t work. :wink: [import]uid: 13859 topic_id: 21061 reply_id: 83703[/import]