Recent

Author Topic: Help with recursivity.  (Read 6394 times)

roklobster315

  • New member
  • *
  • Posts: 7
Help with recursivity.
« on: January 20, 2017, 01:47:21 am »
So this recursivity problem i'm having is part of a project that I've been coding. The summary of the project is a game I must make called tom&jerry:

1. it asks the user the size of a grid (x,y) (max size is 20x20).
2. it asks how many walls (tom can't jump over the walls, only go around) the user wants on the grid, represented by a P.
3. it asks how many obstacles (tom can jump over the obstacles, if there are two obstacles back to back it jumps over both) the user wants on the grid, represented by a O.
4. once it has this information, it generates randomly these coordinates of each element (tom,Jerry,obstacles, and walls) and shows them to the user ex Tom : (4,2).
5. it uses this generated information to then represent it graphically using a grid or matrix array ex: matrix:array(1..20)(1..20) of string;

Now my program already does all of the previous things mentioned, Just providing context. My issue is that after it is represented it must generate 3 different paths from T to J (if one path is generated it is valid, if it is not possible it must print an error message), obviously it must also follow the other conditions where tom can't jump over walls etc. Tom can move in all directions: Up,down,left,right, and diagonally too. I've come up with a way to do it randomly:

for i:=1 to x do
for j:=1 to y do

I:=8;
randomize;

if matrix[i,j]='T' then
U:=1+random(eight);
if u:=1 then
begin
(does first movement)
matrix[i-1,j]:=T
matrix[i,j]:=C
end;

this will do the following graphically

(original)
||||
||J|           
||||
||T|

(moved once)
||||
||J|
||T|
||C|

What it does is that it puts a C in its original position to indicate it was there or leave a path and then the new position becomes T which once it goes into the for again it will detect and move again. Now my problem is if I put this on a repeat loop until T=J's position it will loop infinitely. I've heard that with recursivity it is possible to make this, if I need to change my code or rewrite it i would appreciate the help I've tried a couple things without success.  :-\ :'(

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Help with recursivity.
« Reply #1 on: January 20, 2017, 04:11:03 am »
Hello roklobster315,
Welcome to this forum.

Do you know, you can use code-block when posting you code in this forum? By doing so your code will be more readable. Here I've done it for you:

Code: Pascal  [Select][+][-]
  1. for i:=1 to x do
  2.   for j:=1 to y do
  3.     I:=8;
  4.  
  5. randomize;
  6.  
  7. if matrix[i,j]='T' then
  8.   U := 1+random(eight);
  9. if u:=1 then
  10. begin
  11.   // (does first movement)
  12.   matrix[i-1,j]:=T
  13.   matrix[i,j]:=C
  14. end;

Now, it is clear the code won't work because:
- Lines: 1, 2, 3 are not very useful
- Lines: 7, 8 are not useful

You mentioned it is a game, but where is part in the code for reading user input?

Also, you said you want to make it using recursive solution. I will suggest don't do it. If there still has bugs or logic problems, making it recursive will make thing worse. As far as I know, usually recursive solutions can be rewritten into non-recursive code using iteration. In my opinion, it is harder to debug logic problem in recursive solutions and it may introduce stack issue.

We really want to help you, but the information you provided (especially the code) is very limited.

Here is my recommendation for you if you want to learn game programming using Lazarus/FPC:
http://wiki.freepascal.org/Category:Games
« Last Edit: January 20, 2017, 04:13:56 am by Handoko »

derek.john.evans

  • Guest
Re: Help with recursivity.
« Reply #2 on: January 20, 2017, 08:11:44 am »
Sounds like a maze solving algorithm to me

https://en.m.wikipedia.org/wiki/Maze_solving_algorithm

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #3 on: January 21, 2017, 12:06:04 am »
Hello roklobster315,
Welcome to this forum.

Do you know, you can use code-block when posting you code in this forum? By doing so your code will be more readable. Here I've done it for you:

Code: Pascal  [Select][+][-]
  1. for i:=1 to x do
  2.   for j:=1 to y do
  3.     I:=8;
  4.  
  5. randomize;
  6.  
  7. if matrix[i,j]='T' then
  8.   U := 1+random(eight);
  9. if u:=1 then
  10. begin
  11.   // (does first movement)
  12.   matrix[i-1,j]:=T
  13.   matrix[i,j]:=C
  14. end;

Now, it is clear the code won't work because:
- Lines: 1, 2, 3 are not very useful
- Lines: 7, 8 are not useful

You mentioned it is a game, but where is part in the code for reading user input?

Also, you said you want to make it using recursive solution. I will suggest don't do it. If there still has bugs or logic problems, making it recursive will make thing worse. As far as I know, usually recursive solutions can be rewritten into non-recursive code using iteration. In my opinion, it is harder to debug logic problem in recursive solutions and it may introduce stack issue.

We really want to help you, but the information you provided (especially the code) is very limited.

Here is my recommendation for you if you want to learn game programming using Lazarus/FPC:
http://wiki.freepascal.org/Category:Games

I can provide my current code, it reads user input (x and y, the amount of walls the user wants, the amount of obstacles the user wants).  Is paste-bin allowed? as it is rather long.

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Help with recursivity.
« Reply #4 on: January 21, 2017, 09:23:03 am »
Paste-bin is allowed, or you can zip the files and attached it to this forum.

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #5 on: January 21, 2017, 02:36:37 pm »
Paste-bin is allowed, or you can zip the files and attached it to this forum.

heres the paste, it runs so if you don't understand something let me know! (some code is in spanish).

http://pastebin.com/g5e1HzEw

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Help with recursivity.
« Reply #6 on: January 21, 2017, 04:47:57 pm »
Believe it or not using a random moving 'sometimes' works  :D

Attached a test project. It works when for example Tom is close to Jerry.

You've not said too much on what was your testing case, you know that things can work in a case and sometimes in another case not. So what's your testing case? My testing case was 20x20, 30 walls, 30 obstacles. And it works only when Tom is close to Jerry, 'sometimes'. So it works and at the same time doesn't works, so in fact doesn't works  :P Apart from the joke this is a way faster to test your code, with a visual representation of things in a form, the part that is wrong is the part you need to do.

PD: Hay un sub foro en español en este mismo foro de lazarus.

PD2: https://es.wikipedia.org/wiki/B%C3%BAsqueda_de_ruta

en la wiki en inglés hay mas información https://en.wikipedia.org/wiki/Pathfinding
« Last Edit: January 21, 2017, 05:02:30 pm by lainz »

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #7 on: January 22, 2017, 02:28:14 pm »
Believe it or not using a random moving 'sometimes' works  :D

Attached a test project. It works when for example Tom is close to Jerry.

You've not said too much on what was your testing case, you know that things can work in a case and sometimes in another case not. So what's your testing case? My testing case was 20x20, 30 walls, 30 obstacles. And it works only when Tom is close to Jerry, 'sometimes'. So it works and at the same time doesn't works, so in fact doesn't works  :P Apart from the joke this is a way faster to test your code, with a visual representation of things in a form, the part that is wrong is the part you need to do.

PD: Hay un sub foro en español en este mismo foro de lazarus.

PD2: https://es.wikipedia.org/wiki/B%C3%BAsqueda_de_ruta

en la wiki en inglés hay mas información https://en.wikipedia.org/wiki/Pathfinding

the max matrix you can use is 20x20, 20 walls, and 20 obstacles.

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #8 on: January 22, 2017, 02:42:59 pm »
Believe it or not using a random moving 'sometimes' works  :D

Attached a test project. It works when for example Tom is close to Jerry.

You've not said too much on what was your testing case, you know that things can work in a case and sometimes in another case not. So what's your testing case? My testing case was 20x20, 30 walls, 30 obstacles. And it works only when Tom is close to Jerry, 'sometimes'. So it works and at the same time doesn't works, so in fact doesn't works  :P Apart from the joke this is a way faster to test your code, with a visual representation of things in a form, the part that is wrong is the part you need to do.

PD: Hay un sub foro en español en este mismo foro de lazarus.

PD2: https://es.wikipedia.org/wiki/B%C3%BAsqueda_de_ruta

en la wiki en inglés hay mas información https://en.wikipedia.org/wiki/Pathfinding

how do I use your code?  :D It has some declarations that I don't understand.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Help with recursivity.
« Reply #9 on: January 22, 2017, 02:56:12 pm »
Quote
the max matrix you can use is 20x20, 20 walls, and 20 obstacles.

OK that I was wanting.

Quote
how do I use your code?  :D It has some declarations that I don't understand.

First you need to open it with Lazarus. Is object oriented pascal, not just functions. Also it uses enumeration and custom array types.

I think the main method you need to change is
Code: Pascal  [Select][+][-]
  1. procedure TTomAndJerry.GeneratePath;
, the other code works almost fine.

If you don't know all the pascal syntax yet you can read a book or the wiki.
« Last Edit: January 22, 2017, 02:59:37 pm by lainz »

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #10 on: January 22, 2017, 05:14:43 pm »
Quote
the max matrix you can use is 20x20, 20 walls, and 20 obstacles.

OK that I was wanting.

Quote
how do I use your code?  :D It has some declarations that I don't understand.

First you need to open it with Lazarus. Is object oriented pascal, not just functions. Also it uses enumeration and custom array types.

I think the main method you need to change is
Code: Pascal  [Select][+][-]
  1. procedure TTomAndJerry.GeneratePath;
, the other code works almost fine.

If you don't know all the pascal syntax yet you can read a book or the wiki.

the thing is im only allowed to use functions,arrays,loops,records we don't cover object oriented pascal :C. i was looking at path finding methods you provided, how could i implemente a Dijkstra's Algorithm using basic functions arrays and loops that sort of thing?

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: Help with recursivity.
« Reply #11 on: January 22, 2017, 06:04:50 pm »
heres the paste, it runs so if you don't understand something let me know! (some code is in spanish).

http://pastebin.com/g5e1HzEw

That does not even compile:
tomandjerry.lpr(262,5) Error: Illegal assignment to for-loop variable "i"
(Replace that line with "Break;" without the quotes obviously)

Labels, Goto's, that's horrible.
And the indentation is rather strange as well.

Press a wrong key and the program crashes.

Bart
« Last Edit: January 22, 2017, 06:23:31 pm by Bart »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Help with recursivity.
« Reply #12 on: January 22, 2017, 06:08:57 pm »

roklobster315

  • New member
  • *
  • Posts: 7
Re: Help with recursivity.
« Reply #13 on: January 23, 2017, 12:40:36 am »
a google search first always helps

https://www.google.com/#q=Dijkstra's+Algorithm+pascal
it moves up,down,left,and right, but i want it to move diagonally. can someone help me with that? It runs perfectly if you follow the instructions.
http://pastebin.com/LQhYtrmv

 

TinyPortal © 2005-2018