CyT3
CyT3

Reputation: 19

Ant on a Chess board

currently my program is only always giving me 4, how can I determine how many steps the ant took to cover the whole board? The ant can walk up down left right, but can't walk off the board, and then do this simulation 4 times.

public static void main(String args[]) {
    int[][] grid = new int[8][8];

    int count = 0;
    int x = 0;
    int y = 0; // arrays are 0 based
    while(true)
    {
        int random =  (int)Math.random()*4+1;
        if (random == 1)
        {
            x--; // move left
        }
        else if (random == 2)
        {
            x++; // move right
        }
        else if (random == 3)
        {
            y--; // move down
        }
        else if (random == 4)
        {
            y++; // move up
        }
        if(x < 0 || y < 0 || x >= grid.length || y >= grid[x].length) break;
        count++;
        grid[x][y]++;
    }
    System.out.println("Number of Steps in this simulation: " + count); // number of moves before it fell
    }
}

Upvotes: 0

Views: 1225

Answers (2)

user1234SI.
user1234SI.

Reputation: 1869

The program will exit the while(true) loop once one of the 4 conditions is true. My suggestion is to move these conditions in your if(random == value) checks like this:

if( random == 1 )
 {
   x--;
   if (x < 0 )
    {
      x++;
    }
 }

Now to exit your while(true) loop you need to have an extra condition. I would suggest to think about your board in terms of 0's and 1's. Everytime the ant cross a cell, you set the grid[x][y] = 1.

int stepsTaken = 0;
int cellsToCover = grid.length * grid[0].length ;
int coveredCells = 0;
while(true)
{
 //your code here
 if( random == 1 )
  {
    stepsTaken++;
    x--;
    if (x < 0 )
    {
      x++;
    }
  }
  // the other if's with "stepsTaken" incremented too.
  if ( grid[x][y] == 0 )
  {
    grid[x][y] = 1;
    coveredCells++;
  }
  if (coveredCells == cellsToCover )
    break;
 }

But please notice the many ifs statements inside a while(true) loop. If you have to fill a board of 10 rows x 10 columns it would take too much until the board is filled. Instead I would suggest you to use some more efficient algorithms like backtracking, dynamic programming etc.

Edit : Added step counter.

Upvotes: 0

Turing85
Turing85

Reputation: 20205

The problem is this expression:

int random =  (int)Math.random()*4+1;

Through the explicit cast, only Math.random() ist casted to int. But since Math.random() returns a dobule < 1, it is casted to 0 and thus random is always 1 and the method always returns 0.

The problem can be fixed by casting Math.random() * 4:

int random = (int) (Math.random() * 4) + 1;

The parenthesis enforce that the value of Math.random() * 4 (which will be a value in the interval [0, 3)) will be casted to int.


Two remarks on your code:

  • I would recommend introducing an enum Direction with four values (one for each direction) and choose a random Direction by calling Direction.values()[(int) (Math.random() * 4)];
  • I would recommend to use a switch instead of the if-else-if cascade.

Ideone demo

Upvotes: 1

Related Questions