Tommon
Tommon

Reputation: 29

Making something move in an array

I have to make this program that will make a 12 by 12 array filled with different letters, then make something like a disease that spreads to the nearby cells of the same letter (it'll be randomly placed in the array). I've managed so far to make a class for the array and for the disease (to select a random char to be diseased) but I don't understand how I can make it see the nearby cells. In addition in the Disease the user can choose how many different letters(2-4). So far i've done:

import java.util.Random;
public class Disease {
public char D ;
Random r = new Random();
public void forTwo() {
    Random r = new Random();
    D = (char)(r.nextInt(2) + 'E');
}
public void forThree() {
    Random r = new Random();
    D = (char)(r.nextInt(3) + 'E');
}
public void forFour() {
    Random r = new Random();
    D = (char)(r.nextInt(4) + 'E'); 
}
}

AND THE ARRAY:

import java.util.Arrays;
import java.util.Random;
public class PlayingArea {
private String letters;
public char[][] grid;
public PlayingArea(String letters) {
    this.letters = letters;
}
public void populate() {
    int n = letters.length();
    grid = new char[12][12];

    Random r = new Random();
    for (int j = 0; j < grid.length; j++) {
        for (int i = 0; i < grid.length; i++) {
            grid[i][j] = letters.charAt(r.nextInt(n));
        }
    }
}
public String gridAsString() {
    StringBuilder sb = new StringBuilder();
    for (char[] letterRow : grid) {
        sb.append(Arrays.toString(letterRow)).append('\n');
    }
    return sb.toString();
}
}

Upvotes: 0

Views: 208

Answers (4)

Robin S
Robin S

Reputation: 199

If you work with arrays the neighbors of an example cell a[1][1] can be a[0][1], a[2][1], a[1][0] and a[1][2] if you only work in 4 directions.

You can also create a class for the cells, which knows its direct neighbor cells, so you can get the left neighbor like cell.getLeftNeighbor().

Edit:

Ok, so now that you have your code so far we can help a bit more specific. :)

The way you implemented it, you would have to access the disease logic in your class PlayingArea. I do not quite understand what your methods in the class Disease do, forTwo() will assign 'E' or 'F' to the char D for example.

You need to determine on which coordinates the disease is, you can do it on different ways:

  • Specify the coordinates in PlayingArea, like 5 and 4 = grid[5][4].The direct neighbors are now grid[x-1][x], grid[x+1][x], grid[x][x-1] and grid[x][x+1].

  • Replace the char[][] with a Cell[][]. You would have to create a class Cell, which only needs to know with a boolean, which letter is in it and if it is diseased. Additionally you can give it other Cells as attributes, this way you can access a cells neighbors directly.

    public class Cell{
        public char letter;
        public boolean diseased;
        public Cell leftCell, rightCell, topCell, bottomCell;
    
        public Cell(char newLetter){
            letter = newLetter;
        }
     }
    

    This example is without setter/getter methods, you can create cells in the PlayingArea, set the state of disease of each cell and assign/access neighbor cells.

Upvotes: 1

Gander7
Gander7

Reputation: 581

Store the current position of the disease object as variables in your disease object so each disease has a current location/coordinates ie x,y

Using x and y as the current location of your disease(which you don't know):

  • To look at the cells adjacent you can use

array[x+1][y] // Right
array[x-1][y] // Left
array[x][y+1] // Down
array[x][y-1] // Up

  • if you wanted to look at diagonals as well, use the same method. (array[x-1][y-1] = top left)

You wouldn't be able to do it from the disease unless you passed your disease object the array or a subset of the array.

Upvotes: 0

Roman
Roman

Reputation: 1857

Given the limited information, I think recursive way would be more natural, but you can as easily implement it with iteration(it would require you to store deceased cells and where to go next).

Since it is a 2D array, once you have the infected cell, you can check if the character on the left and right are "infectable" (same letter). To check up and down, you would need to go adjacent subarrays.

For example: Your infected cell is [3,4]. To check left and right, you need to go to [3,3] and [3,5] entries. To check up and down , you need to go to [2,4] and [4,4]. If you need to check cells diagonally, then check [2,3],[2,5], [4,3] and [4,5].

Then if you are able to "infect" another cell - call your function on that cell again.

This should give you an idea where to start. If you get stuck with some errors - post your code and specify where you are stuck ( if you are allowed to do so).

Upvotes: 0

Slihp
Slihp

Reputation: 793

Start by drawing out your array on paper as a grid...like a checkers board. Select any space in that grid and figure out the indexes of the adjacent spaces relative to the one you've selected.

Write a method to do what you just did mentally.

It sounds like this is an iterative question. "How many iterations will it take for the array to be completely infected?"

Upvotes: 0

Related Questions