Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Number match logic problem HELP PLZ!!!! :)

Author
Message
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 31st Jul 2014 03:41
Hi guys,

It's 1:28 am, and I've got an issue with a program that's going to bug me all night if I don't solve it.

The goal is simple, randomly chose 1 number, then 10 more. take the first and detect which of the 10 is a match or the closest match to the first number. I'm trying to do it as simply as possible without any arrays, and only basic logic. Been going over this with another guy and neither of us can work out why the program isn't working. We're both likely doing something really stupid so if anyone else can shine a light on it I'd be very greatful.

The code is:

fog
20
Years of Service
User Offline
Joined: 5th Oct 2003
Location: Newcastle, England
Posted: 31st Jul 2014 04:06
Simplify these bits:


becomes


And your later logic checks are all wrong. You need to store the smallest gap and compare against that. (I've also changed the logic a bit as checking for a lower value makes more sense in this case)



easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 31st Jul 2014 04:54 Edited at: 31st Jul 2014 05:02
Quote: " I'm trying to do it as simply as possible without any arrays, and only basic logic"

Doing it with arrays is the simplest way of doing it.... You could easily do it recursively if you use arrays, cut it down to about 20 lines then.


Here is my untested way of doing it with arrays:



The code is horribly messy and spaghetti-ish, but all you really need to do it rename the variables, comment it and modularise it a bit more


Audacia Games - Latest WIP - AUTOMAYTE 2.1, AppGameKit one click deploy to Android
"When you've finished 90% of your game, you only have 90% left"
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 31st Jul 2014 05:43 Edited at: 31st Jul 2014 05:53
@easter

This is part of an early education example, we're trying to write this as a logic exercise before students learn anything about arrays, functions, subroutines. Basic identifiers only, basic logic only. Thanks though your example is great, we just can't use it this early.
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 31st Jul 2014 05:51
@fog

Your logic is exactly the same as ours, I'm afraid you're just adding an unnecessary identifier into the mix. Also you can't use colons after an if statement. Tried your code, it performed in exactly the same way as our original code did.

We hit upon the abs originally, it was removed as part of the troubleshooting process because it outputs to float, and we're using pure integer. It shouldn't make a difference, but at this point we're trying anything!!

Thanks for the suggestions though
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 31st Jul 2014 06:55
Could you just do it the same way as mine, but expand each for/next loop out into 10 separate statements, and instead of the diff function, use the same logic each time?


Audacia Games - Latest WIP - AUTOMAYTE 2.1, AppGameKit one click deploy to Android
"When you've finished 90% of your game, you only have 90% left"
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 31st Jul 2014 08:10
I get the feeling this post isn't of any help to you, but if it's this difficult for you perhaps it's not the best example for true beginners Do you have to use this specific example?

Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 31st Jul 2014 08:30 Edited at: 31st Jul 2014 08:31
Replace this section:



with this:



The issue is this:

if test1 > test2 then result = 2
if test2 > test3 then result = 3

Your automatically assuming that if test1 is greater than test2 then test3 must also be greater than test1. This isn't always true since your values are completely random.

The solution I posted above is a bit messy, there are probably some other solutions you could use that don't utilize arrays (possibly ones that use Repeat Loops), however since this is a beginners tutorial I figured you would want the most basic approach.



Hope this helps.


Sean

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Jul 2014 10:50
To be honest, I wouldn't use that example for teaching unless you wanted to show how ghastly life can be without loops, list and arrays. If I did use it I wouldn't have eleven numbers - four should be adequate. As it is, looking at that horrible code would put me off programming for ever!

According to the help files, ABS uses integers, not floats.

-- Jim - When is there going to be a release?
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 31st Jul 2014 11:17
Amen, to JimHawkins.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Jul 2014 13:35
There's a fundamental error in the code, anyway, so not a brilliant example.

To find the closest, as others have said, you have to keep the value of the difference. Let's call it Gap.

Gap MUST be instantiated to an impossibly big number:

Gap = 500

Then, for each value, where varxyz is one of your variables:

if ABS(lucky - varxyz) < gap
gap = ABS(lucky - varxyz)
best = varxyz
endif

If you don't give Gap an out-of-range starting point it may well be zero, in which case you'll never get a hit. Because the maximum RANDOM(0,100) will return, at least one of the values will trigger.

Obviously, this code does not cater for more than one value matching the criterion. If you wanted to handle that, then it would be a good introduction to lists - but that would take two passes!

-- Jim - When is there going to be a release?
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 31st Jul 2014 14:06 Edited at: 31st Jul 2014 14:06
Thanks JimHawkins, that solved the approach. You're right I should have been working with the distance value rather than the raw values. E-Mail me at daniel@dcforeman.co.uk and I'll send you a $10 TGC voucher as a thanks.

fog
20
Years of Service
User Offline
Joined: 5th Oct 2003
Location: Newcastle, England
Posted: 31st Jul 2014 14:09 Edited at: 31st Jul 2014 14:12
@Daniel
I use native AppGameKit so wasn't aware of the colon on a THEN line thing sorry. Changed it to a IF/ENDIF block and it works fine here so don't know why it doesn't for you.

Quote: "If you don't give Gap an out-of-range starting point it may well be zero, in which case you'll never get a hit. Because the maximum RANDOM(0,100) will return, at least one of the values will trigger."

The code I posted does give the gap an initial value and Daniel said it doesn't work (he also said the logic was the same as his which it clearly isn't lol!)

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Jul 2014 16:21
@fog - Sorry, but you don't explicitly set the value to something impossibly large (smallestGap should be at least 101 to start).

Daniel's code is still slightly (but not necessarily) wrong, because it sets the initial distance value to 100. This should be at least 101, since AppGameKit Random() returns from-to values, whereas most Random functions return the ceiling - 1.

You don't actually need an array to do this more elegantly, but you do need a loop. See how tight you can make it, girls! Of course, the loop could simply be the AppGameKit "do - loop" itself, in which case a FOR loop would not be needed.

For teaching I think that you need to focus on only one issue at a time. I'm not quite sure what this lesson is about. Strangely enough, it may turn out to be the usefulness of ABS() since ABS(x-y) is the same as ABS(y-x).

-- Jim - When is there going to be a release?
fog
20
Years of Service
User Offline
Joined: 5th Oct 2003
Location: Newcastle, England
Posted: 31st Jul 2014 16:56
Quote: "@fog - Sorry, but you don't explicitly set the value to something impossibly large (smallestGap should be at least 101 to start)."
It doesn't need to be set to something "impossibly large". I've set it to be equal to test1 which is the amount rnd1 is out by. It works fine

Quote: "For teaching I think that you need to focus on only one issue at a time."
For teaching I think it's a pretty horrible example, but then again, I'm not a teacher

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Jul 2014 17:01
Fair enough, but it would be better to set it to (say) maxint because then it's not dependent on the later code?

-- Jim - When is there going to be a release?
fog
20
Years of Service
User Offline
Joined: 5th Oct 2003
Location: Newcastle, England
Posted: 31st Jul 2014 18:11
Quote: "Fair enough, but it would be better to set it to (say) maxint because then it's not dependent on the later code?"

Based on the new code yes. My reply was to the original code and initialising the value to be test1 in that means you have to do one less logic check later.

In the real world we'd both use arrays, but even if you did it this way without arrays, it's quicker to use a simple function to replace all the 10 logic checks.

It really depends on how simple, readable, optimised, extendable etc you want the code

easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 1st Aug 2014 01:06
Quote: "(smallestGap should be at least 101 to start)."

exactly what my code had


Audacia Games - Latest WIP - AUTOMAYTE 2.1, AppGameKit one click deploy to Android
"When you've finished 90% of your game, you only have 90% left"
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Aug 2014 01:38
Indeed you did, bunny, but you used a loop, which was cheating!

-- Jim - When is there going to be a release?
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 1st Aug 2014 09:36
Quote: "Indeed you did, bunny, but you used a loop, which was cheating!
"


Ahh, but then I suggested expanding my loops out, even if I was too lazy to do it
Quote: "Could you just do it the same way as mine, but expand each for/next loop out into 10 separate statements, and instead of the diff function, use the diff logic each time?"

Even if I did have no idea what ABS was


Audacia Games - Latest WIP - AUTOMAYTE 2.1, AppGameKit one click deploy to Android
"When you've finished 90% of your game, you only have 90% left"

Login to post a reply

Server time is: 2024-04-26 03:45:23
Your offset time is: 2024-04-26 03:45:23