#2D Arrays AntWalk

1 messages · Page 1 of 1 (latest)

chrome heath
#
  1. AntWalk Write a program to simulate the random movement of an ant walking on a
    coordinate grid with dimensions 9x9. Position the ant in cell (4,4) to begin. Then allow the ant to
    move randomly up, down, left or right. The ant stops when it falls off of the grid (goes to 0 or 10
    in either direction). In the example below, the ant falls off the grid to the South, noted by the X.
    As the ant is moving, you need to keep track of the following information:
     The number of moves before falling off
     The number of times the ant visited each cell (displayed in the matrix)
     Which direction the ant fell off the grid (North, South, East or West)
    Note: You must update and redisplay the matrix after each movement

Here is my code, for some reason its not displaying the proper direction the ant fell..
https://pastebin.com/1z69JgbQ

lapis grottoBOT
#

This post has been reserved for your question.

Hey @chrome heath! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

prime owl
#

What exactly do you expect? And what exactly happens?

chrome heath
#

u can run my code if u wish

prime owl
#

Like what's the exact problem with that?

chrome heath
# prime owl Like what's the exact problem with that?

Create a program that simulates an ant moving randomly on a 9x9 grid. Start the ant at position (5,5). Allow it to move up, down, left, or right randomly. The ant stops when it reaches the edge of the grid (0 or 10 in any direction). Keep track of the ant's movements

prime owl
#

yeah

#

So what's the problem with that?

chrome heath
#

thats the error

#

im trying to figure out why

prime owl
#

Which direction is displayed and which do you want?

#

I think you confused x and y

oblique furnace
#

is 0 and 8 part of the array or not?

chrome heath
#

In my code, I used a Math.random to basically determine how the ant moves, in one of my example outputs I got this:

000000000
000011000
000001000
000001000
000032000
000044100
000011000
000000000
000000000
Moves: 20
The ant fell off to the NORTH

#

the correct answer is the ant should have fallen off to the South

prime owl
oblique furnace
#

in fact, in the SOUTH where u say it fell, it didnt even even get withing 2 rows of the edge

prime owl
prime owl
oblique furnace
prime owl
#

from what I have seen

oblique furnace
#

and, yeah, his code is not consistent (not even with itself)
but according to the description he has given off the edge means outside of the grid

prime owl
#

and does so twice

chrome heath
#

i figured out some stuff

#

if its north

#

it needs to be smaller than 0

#

so instead of

#

x == 0

#

its x < 0

oblique furnace
#

@chrome heath take ur code,
instead of random do 1 move north
print out the grid
and see which direction the ant moved

repeat the same for all 4 of the directions u have specified

as @prime owl says, u are not understanding ur directions correctly

AND
u have to decide if ur code is correct (there r only 7x7 squares it can walk on)

or if ur instructions are correct (all 9x9 is available to walk)

lapis grottoBOT
prime owl
#

btw

oblique furnace
#

if u know how to write methods, and u split ur code into methods, it would be much easier for u to debug/test/modify