1

I want to move false elements in empty space (just like in Schelling's Model of Segregation). This is the array ..(the elements in the array are placed randomly)

'X*''O'' ''X''O*'
'O''O'' '' ''X'
'O'' ''X''X''O*'
'X'' '' ''X''X'
'X''X''O*''X''O*'

the element at (0,0),(0,4),(2,4),(4,2),(4,4) are false because it does no have similar elements around it.(i have * next to it is easier for you to see). I want to move those false elements to blank location ' ' in the array. It can be moved to any empty location in the array.

' ''O''X''X'' '
'O''O''O'' ''X'
'O''O''X''X'' '
'X''O''O''X''X'
'X''X'' ''X'' '

This is my code.

//isArrGood is a method that checks if the element is false or true
//arr is 2D array that is being passed in the method

//store the false elements
char[] falseEle = new char[arr.length * arr[0].length];
for(int i=0; i<arr.length; i++){
   for(int j=0; j<arr[i].length; j++){
       if(!isArrGood(arr,i,j)){
           falseEle[i] = arr[i][j];
        }
      }
     }

//finds the blank space and replaces it with a false cell and finds a false cell and
//replace it with a blank space
for(int x=0; x<arr.length; x++){
   for(int y=0; y<arr[x].length; y++) {
      if (arr[x][y] == ' ') {
          arr[x][y] = falseEle[x];
       }
      if(!isArrGood(arr,x,y){
         arr[x][y] = ' ';
       }
      }
     }

This is the what i get.
the current array(arr) being send in the method is. The false elements in this array are at (1,0),(2,2),(3,2)

' ' 'X' 'X' 'X' 'X' 
'O' ' ' 'X' 'X' ' ' 
' ' 'X' 'O' ' ' 'X' 
'O' 'O' 'X' ' ' 'O' 
'O' 'O' ' ' 'O' ' '

And this is what I get

''  'X' 'X' 'X' 'X' 
'O' 'O' 'X' 'X' 'O' 
'O' 'X' 'O' 'O' 'X' 
'O' 'O' 'X' 'X' 'O' 
'O' 'O' ''  'O' ''  

The array on the very top is just a example of what I am trying to do.

6
  • So what is your problem/question? Commented Nov 21, 2014 at 5:41
  • move those false elements to empty elements in the array Commented Nov 21, 2014 at 5:43
  • Yes, but what is wrong with your current code? Can you provide an error message? I sure as hell am not going to copy, paste, compile, then run your code to figure out what's wrong. Commented Nov 21, 2014 at 5:44
  • i edited the question, the last part is what i get Commented Nov 21, 2014 at 5:48
  • What do you mean by empty location? Empty location such that they have similar elements besides them? Please also define meaning of "beside" Top, down, left right? or All 8 directions surrounding it? Commented Nov 21, 2014 at 6:07

1 Answer 1

1

Could you post more code i.e. a Minimal, Complete, and Verifiable example of your problem?

For the problem "Schelling's Model of Segregation" you could use the code I wrote to learn from:

import java.util.Random;

public class ShellingSegregationModel
{
    public static final int EMPTY = 0;
    public static final int BLUE  = 1;
    public static final int RED   = 2;

    // number of elements for random
    public static final int ELEMENTS = 3;

    public static final double THRESHOLD = 0.15;        

    //size of the field
    public int size;

    int[][] field;
    // temporary field for the iteration
    int[][] temporary;

    public int iteration;

    public ShellingSegregationModel(int size)
    {    
        Random random = new Random();
        this.size = size;
        field     = new int[size][size];
        for (int y = 0; y < size; y++)
        {
            for (int x = 0; x < size; x++)
            {
                field[y][x] = random.nextInt(ELEMENTS);
            }
        }
    }

    public int getSize()
    {
        return size;
    }

    public void setField(int[][] field)
    {
        this.field = field;
    }

    public int[][] getField()
    {
        return field;
    }

    public void setTemporary(int[][] temporary)
    {
        this.temporary = temporary;
    }

    public int[][] getTemporary()
    {
        return temporary;
    }

    public int getIteration()
    {
        return iteration;
    }

    public void setIteration(int iteration)
    {
        this.iteration = iteration;
    }

    public double getThreshold()
    {
        return THRESHOLD;   
    }

    //how many neighbors needed for threshold
    public double getCalculatedThreshold()
    {
        return getThreshold()*8;//8 is the neighbors count total possible
    }

    public static String getSymbolFor(int x)
    {
        String s = "";
        switch (x)
        {            
            case BLUE:
                s = "x";
                break;
            case RED :
                s = "o";
                break;
            case EMPTY:
            default:
                s = " ";                
        }
        return s;
    }

    public boolean isEmpty(int x, int y)
    {
        return get(x,y) == EMPTY;
    }

    /**
     * Prints field
     */
    public void print(String message)
    {
        System.out.println(message);
        for (int y = 0; y < getSize(); y++)
        {          
            StringBuilder row = new StringBuilder();
            for (int x = 0; x < getSize(); x++)
            {                
                row.append("'").append(getSymbolFor(get(x,y))).append("' ");
            }
            System.out.println(row.toString());
        }
    }

    public void printSameNeighorsCount(String message)
    {
        System.out.println(message);
        for (int y = 0; y < getSize(); y++)
        {          
            StringBuilder row = new StringBuilder();
            for (int x = 0; x < getSize(); x++)
            {                
                row.append("'").append(sameNeighbors(x, y)).append("' ");
            }
            System.out.println(row.toString());
        }
    }

    public int get(int x, int y)
    {
        return getField()[y][x];
    }

    private int add(boolean c)
    {
        return c ? 1 : 0;
    }

    public int sameNeighbors(int x, int y)
    {
        return isEmpty(x,y) ? 0 : 
                  add(isSame(get(x,y),x  ,y-1)) 
                + add(isSame(get(x,y),x-1,y-1))
                + add(isSame(get(x,y),x-1,y  ))
                + add(isSame(get(x,y),x-1,y+1))
                + add(isSame(get(x,y),x  ,y+1))
                + add(isSame(get(x,y),x+1,y+1))
                + add(isSame(get(x,y),x+1,y  ))
                + add(isSame(get(x,y),x+1,y-1));
    }    

    private static void copyArray(int[][] src, int[][] dest)
    {        
        for (int i = 0; i < src.length; i++)
        {
            dest[i] = new int[src[i].length];
            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);            
        }                
    }
    private void duplicateToTemporary()
    {
        setTemporary(new int[getField().length][]);
        copyArray(getField(),getTemporary());
    }

    //
    private void assignFromTemporary()
    {
        setField(new int[getField().length][]);
        copyArray(getTemporary(), getField());
    }

    public void iterate(int iterations)
    {        
        for (int i = 0; i < iterations; i++)          
        {
            duplicateToTemporary();
            for (int y = 0; y < getSize(); y++)
            {
                for (int x = 0; x < getSize(); x++)
                {
                    if (!isHappy(x,y))
                    {
                        swap(x,y);
                    }
                }
            }
            assignFromTemporary();
        }
        setIteration(getIteration()+iterations);
    }

    //Swaps with empty random from temporary
    public void swap(int i, int j)
    {
        Random random    = new Random();
        boolean swapped  = false;
        //skip a random number of empty
        int     skip     = random.nextInt(100);
        while (!swapped)
        {
            for (int y = 0; !swapped && y < getSize(); y++)
            {
                for (int x = 0; !swapped && x < getSize(); x++)
                {
                    if (getTemporary()[y][x] == EMPTY && 0 >= --skip)
                    {                        
                        getTemporary()[y][x] = getTemporary()[j][i];
                        getTemporary()[j][i] = EMPTY   ;
                        swapped = true;
                    }
                }
            }
        }
    }

    public boolean isHappy(int x, int y)
    {
        return getCalculatedThreshold() < sameNeighbors(x, y);        
    }

    public boolean isSame(int me, int x, int y)
    {
        return 
                //check bounds
                x >= 0 && y >= 0 && x < getSize() && y < getSize()
                //check element
                && get(x,y) == me;
    }

    public static void main(String[] args)
    {
        ShellingSegregationModel ssm = new ShellingSegregationModel(10);
        ssm.print("Randomly generated field");       
        ssm.printSameNeighorsCount("Same neighbors count");
        ssm.iterate(5);
        ssm.print("Field after 5 iterations");       
        ssm.printSameNeighorsCount("Same neighbors count");
        ssm.iterate(5);
        ssm.print("Field after 10 iterations");       
        ssm.printSameNeighorsCount("Same neighbors count");
        ssm.iterate(5);
        ssm.print("Field after 15 iterations");       
        ssm.printSameNeighorsCount("Same neighbors count");
        ssm.iterate(50);
        ssm.print("Field after 65 iterations");       
        ssm.printSameNeighorsCount("Same neighbors count");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.