user4833678
user4833678

Reputation:

Getting the wrong coordinates

The placedShapeInfo() gives me almost the correct result except it gives me wrong coordinates for Shapes at (?,?) like in the following test case: enter image description here

It seems Im doing smth wrong while reading this line in placedShapeInfo():

"("+coords.get(i)+"," + coords.get(i+1)+")"

Each time a shape passed to placeShapeAt(int row, int col, Shape shape) I add its row and col into the Arraylist coords. Could smb help me out with this?

import java.util.*;

public class CreateSpace implements Space{

    private int height;
    private int width;
    //private int placedAtRow = 0;
    //private int placedAtCol = 0;
    private String layout;
    private char[][] space;
    private Shape originalShape;
    private ArrayList<CreateShape> shapes = new ArrayList<>();
    private ArrayList<Integer> coords = new ArrayList<>();


    Set<Character> shapesCount;

    public CreateSpace(int height, int width, char[][] space, String layout)
    {
        this.height = height;
        this.width = width;
        this.space = space;
        this.layout = layout;
    }
    public void placeShapeAt(int row, int col, Shape shape)
    {
        int row1 = row;
        int col1 = col;

        int sHeight = shape.getHeight();
        int sWidth = shape.getWidth();

        char [][] spaceWithShapes = space;
        if(shapeFitsAt(row, col, shape))
        {
            for(int r = 0; r < sHeight; r++)
            {
                for(int c = 0; c < sWidth; c++)
                {

                    if(spaceWithShapes[r+row][c+col] == '.' && shape.isFilledAt(r, c))// && shape.isFilledAt(r,c) == true) //|| (((CreateShape)shape).getShape()[r][c] == '.'))
                    {
                        spaceWithShapes[r+row][c+col] = shape.getDisplayChar();
                    }

                }

            }

            shapes.add((CreateShape)shape);
            coords.add((Integer)row1);
            coords.add((Integer)col1);
            //shapes.add((Integer)row1);
            Collections.sort(shapes);
            setSpace(spaceWithShapes);

        }


        else                                                           
            throw new FitItException("The shape does not fit!");
    }
public String placedShapeInfo() 
    {
        Collections.sort(shapes);
        int shapesTotal = placedShapeCount();
        String desc = shapesTotal + " shapes placed";
        String getShapeInfo = "";


        for(int i = 0; i < shapes.size(); i++)
            //for(int j = 0; j < coords.size(); j++)
        {

            getShapeInfo +="Shape at " + "("+coords.get(i)+"," + coords.get(i+1)+")" + "\n" + shapes.get(i).toString() + "\n";
            System.out.println();
        }

        return desc + "\n" +
             getShapeInfo;

Upvotes: 1

Views: 79

Answers (1)

dcsohl
dcsohl

Reputation: 7396

Your coords is a list of integers. So you add two shapes at (1,2) and (3,4). So coords is now (1,2,3,4).

Then you iterate through your list of shapes and for each shape get coords.get(i) and coords.get(i+1). Are you seeing the problem yet?

For your first shape, i=0 and you get coords 0 and 1, which gives you (1,2).

For your second shape, i=1 and you get coords 1 and 2, which gives you (2,3).

You're not using i consistently. Maybe each shape should know its own coordinates and you should ask it for its x and y instead of storing them in an external data structure. That's one way of dealing with it, and that's the sort of thing OO programming is good at.

Upvotes: 2

Related Questions