-2

I wanted to make program for solving 8 queens problem. I made "Table" class and started going through 8 iterations and making all possible combination of table so that none of the queens can attack one another. But somehow list at the end of the iteration is filled with same elements and the table from last iteration(Table table) which I am using as starting point for next one is changed as well. I have already seen posts about problems with ArrayList, but all of them were solved by making new instance of object in loop, but here new instance is alredy made inside loop.

Class Table

package mixony;

public class Table 
{

    private char[][] table;
    private final int WIDTH = 8;
    private final int HEIGHT = 8;
    private final char COVERED = '+';

    public Table() 
    {
        table = new char[HEIGHT][WIDTH];
    }

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

    public void setQueen(int x, int y)
    {
        //SET THAT TILE AS QUEEN TILE
        table[y][x]='Q';

        //GO TROUGH THAT COLUMN AND FILL ALL THE COVERED TILES
        for(int i=0; i<HEIGHT; i++)
            if(table[i][x]!='Q')
                table[i][x]=COVERED;

        //GO TROUGH THAT ROW AND FILL ALL THE COVERED TILES
        for(int i=0; i<WIDTH; i++)
            if(table[y][i]!='Q')
                table[y][i]=COVERED;

        //GO TROUGH THAT DOWN VERTICAL AND FILL ALL THE COVERED TILES
        int factor = (x<y)?x:y;
        int x1=x-factor;
        int y1=y-factor;
        for (int i = 0; (x1 + i) < WIDTH && (y1 + i) < HEIGHT; i++)
            if(table[y1+i][x1+i]!='Q')
                table[y1+i][x1+i]=COVERED;

        //GO TROUGH THAT UP VERTICAL AND FILL ALL THE COVERED TILES
        x1=x;
        y1=y;
        while(true)
        {
            if(x1==0 || y1==WIDTH-1)
                break;
            x1--;
            y1++;
        }
        for (int i = 0; (x1 + i) < WIDTH && (y1 - i) > 0; i++)
            if(table[y1-i][x1+i]!='Q')
                table[y1-i][x1+i]=COVERED;


    }

    public char[][] getTable() {
        return table;
    }

    public void setTable(char[][] table) {
        this.table = table;
    }

    public void log(int index)
    {
        System.out.println("\nTABLE "+index+"\n\t---------------------------------");
        for (int y=0; y<HEIGHT; y++)
        {
            System.out.print("ROW "+y+"\t|");
            for (int x=0; x<WIDTH; x++)
            {   

                System.out.print(" "+getField(x, y)+" |");
            }
            System.out.print("\n\t---------------------------------\n");
        }
    }

}

Class with main method

package mixony;

import java.util.LinkedList;

public class PlayTest 
{
    public static void main(String[] args)
    {
        LinkedList<Table> tables = new LinkedList<Table>();
        LinkedList<Table> improvedTables = new LinkedList<Table>();
        tables.add(new Table());
        final Table table = tables.get(0);
        for(int y=0; y<8; y++)
        {
            for(int x=0; x<8; x++)
            {
                if(table.getField(x, y)!='Q'
                && table.getField(x, y)!='+')
                {
                    Table imp = new Table();
                    imp.setTable(table.getTable());
                    imp.setQueen(x, y);
                    improvedTables.add(imp);
                    table.log(0);

                }
            }
        }
    }
}
Mixony
  • 63
  • 1
  • 7
  • Can you produce a [mcve]? – Zabuzard Jan 23 '18 at 17:48
  • `imp.setTable(table.getTable());` **copies** the reference so `imp` and `table` share the same `char[][]`, so changes are seen by both `imp` and `table`. There should probably be a defensive copy. – Andrew S Jan 23 '18 at 18:00

1 Answers1

0

One issue i've seen in your code is that you dont copy the LinkedList but reference it:

LinkedList<String> firstList = new LinkedList<>();
LinkedList<String> secondList;

secondList = firstList;//Dont do that because Call by Reference!

firstList.add("Test");//Everything i do to firstList affects secondList
System.out.println(secondList.size());//prints 1

Instead you should copy the LinkedList via the copy constructor:

LinkedList<String> firstList = new LinkedList<>();
LinkedList<String> secondList;

secondList = new LinkedList<>(firstList);//Copy the List instead of referencing it

firstList.add("Test");//Everything i do to firstList affects only firstList
System.out.println(secondList.size());//prints 0
Jérôme
  • 1,254
  • 2
  • 20
  • 25
  • It was "passing the reference of table" that Andrew S sugested. But this would probably end up as an error in the future. – Mixony Jan 23 '18 at 18:28