Creating loops without loop commands. Possible?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Creating loops without loop commands. Possible?

Post by crystalfrost on Thu Oct 31, 2013 4:56 pm
([msg=77945]see Creating loops without loop commands. Possible?[/msg])

Hey everyone. I got this problem i need to find a solution for.
On a 2d map (lets say 10 x 10 though the size is irrelevant) i got to move my cursor from a bottom box to a top box. There might be obstacles in the way which u need to avoid in order to move forward and check for them on every step you move.
The restriction is that I cannot use loop commands (for/while etc) and pretty much there's only ifs i can use to check if i am able to move. I am able to mark the spots I've been before if needed.

My questions are :
a) Is there any way to create a loop in my code without the actual commands?

PS
I'm programming in java but i don't mind if u answer in psuedocode or whatever... Thanks in advance ;)

Moved. Please post in the correct subforum, next time.
crystalfrost
New User
New User
 
Posts: 4
Joined: Mon Apr 18, 2011 7:12 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by centip3de on Thu Oct 31, 2013 5:10 pm
([msg=77946]see Re: Creating loops without loop commands. Possible?[/msg])

crystalfrost wrote:On a 2d map (lets say 10 x 10 though the size is irrelevant) i got to move my cursor from a bottom box to a top box. There might be obstacles in the way which u need to avoid in order to move forward and check for them on every step you move.

I'm confused why this is relevant to the question. Are you making a game similar to that? Is this homework?

crystalfrost wrote:The restriction is that I cannot use loop commands (for/while etc) and pretty much there's only ifs i can use to check if i am able to move. I am able to mark the spots I've been before if needed.

Still confused why there's this restriction.

crystalfrost wrote:My questions are :
a) Is there any way to create a loop in my code without the actual commands?

Are you allowed to use recursion?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1443
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by crystalfrost on Thu Oct 31, 2013 5:19 pm
([msg=77947]see Re: Creating loops without loop commands. Possible?[/msg])

It's relevant cause in order to move i need to check if the position i'm planing to move at is taken by something. Normaly i would do this with a loop ie.
while (not on the top)
if the box infront of you is clear
move forward
else
avoid

But there's a restriction in which i can't use loops. And if i'm going to just use if statements there's gonna be infinite lines of code since i need to do an if/check for every step taken (which i don't know how many are there gonna be).

So i ask if there's another way of looping commands... something like a manual while or for of some kind.

edited ps.
yes it's homework and there's this stupid restriction about loop commands.
Also i don't know what's recursion :/
crystalfrost
New User
New User
 
Posts: 4
Joined: Mon Apr 18, 2011 7:12 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by Goatboy on Thu Oct 31, 2013 5:47 pm
([msg=77949]see Re: Creating loops without loop commands. Possible?[/msg])

This should get you pointed in the right direction. Obviously very abstract as you need to actually write the code to check obstacles, and not to mention fix the "always turn left first" logic, but you get the idea. No for or while loops, and you can still branch out and repeat.

Code: Select all
function check_ahead()
  if ahead_is_clear
    move_forward()
  else
    if left_is_clear
      turn_left()
      check_ahead() // This is a form of recursion
    if right_is_clear
      turn_right()
      check_ahead() // This is a form of recursion

move_forward()
  position++
  check_ahead()


As for a more concrete example of recursion, check out this pastebin: http://pastebin.com/a72hhN9n
Last edited by Goatboy on Thu Oct 31, 2013 6:05 pm, edited 2 times in total.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by crystalfrost on Thu Oct 31, 2013 5:56 pm
([msg=77950]see Re: Creating loops without loop commands. Possible?[/msg])

Ok I see... well that's not allowed either. I know what i ask is pretty stupid, but i need to reach the top of the map prety much using only movement commands and if statements. (without writing like a million lines of code). Since recursion is prety much some sort of a loop it's excluded.

Maybe the answer lies on marking a path so i wont step on the same spot again and gradually the only boxes that are left are the road to the top (obstacle free road) though that still will take an enormous amount considering i need an if statement for every step i take. That reduced the if a lot but still seems way too lame to be a good solution
crystalfrost
New User
New User
 
Posts: 4
Joined: Mon Apr 18, 2011 7:12 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by Goatboy on Thu Oct 31, 2013 6:08 pm
([msg=77951]see Re: Creating loops without loop commands. Possible?[/msg])

Recursion is not technically a loop. I would really clear up on whether or not you are allowed to use recursion. That's a pretty common test they throw you early on in programming classes.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by centip3de on Thu Oct 31, 2013 6:18 pm
([msg=77952]see Re: Creating loops without loop commands. Possible?[/msg])

Building off what GoatBoy said, there's no possible way to write code that loops, without allowing loops. That's the equivalent of telling someone to feed themselves without allowing them to eat or use an IV. If you're just supposed to avoid for/while, then the answer is pretty clearly recursion. If you have to avoid recursion as well, then you're going to have to use gotos. If you have to avoid gotos as well, then you're going to have to do some weird-ass shit (multiple programs calling each other and passing information, interesting OS/language hacks), that I guarantee you no programming class will ask of you. But if you have to avoid ALL forms of looping, then it's impossible.

TL;DR: Read the directions again.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1443
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by Goatboy on Thu Oct 31, 2013 6:21 pm
([msg=77953]see Re: Creating loops without loop commands. Possible?[/msg])

Well it wouldn't be impossible, just impractical. You really could hard-code rules for each of the 100 positions, each direction, each edge, but that would take a long time.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by centip3de on Thu Oct 31, 2013 6:31 pm
([msg=77954]see Re: Creating loops without loop commands. Possible?[/msg])

Goatboy wrote:Well it wouldn't be impossible, just impractical. You really could hard-code rules for each of the 100 positions, each direction, each edge, but that would take a long time.


Well s/he did say that '10 x 10' was just as an example and that the size is irrelevant, i.e. it's more like 'N x N' instead of '10 x 10'.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1443
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Creating loops without loop commands. Possible?

Post by Goatboy on Thu Oct 31, 2013 6:41 pm
([msg=77955]see Re: Creating loops without loop commands. Possible?[/msg])

Then for any value n it would take n * a shit ton of lines. Still doable, just not practical.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests