5 Replies - 3893 Views - Last Post: 11 March 2014 - 08:56 AM Rate Topic: -----

#1 Anarchius   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 14

How to create/move objects on a 2d array?

Posted 11 March 2014 - 07:10 AM

We are trying to create a shape and place it on a 2d console based board. The shapes will be able to move and grow/shrink. We have the shapes able to display their dimensions already, as well as the board itself made. But actually putting them on the board and moving them is proving to be difficult. Any suggestions? Here is our code so far...

Board code...

public class Board {
	
	private int size;
	
	public Board(int boardSize){
		this.size = boardSize;
	}

	public String toString() {

		Playable[][] grid = new Playable [getSize()][getSize()];

		int k = 1;
		while (k <= (grid.length+2)) {
			System.out.print('-');
			k++;
		}
		
		System.out.println();

		for (Playable[] row : grid) {
			System.out.print("|");
			for (Playable item : row) {
				System.out.print((item == null ? " " : item));
			} System.out.print("| \n");
		}

		k = 1;
		while (k <= (grid.length+2)) {
			System.out.print('-');
			k++;
		}
		return "";
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
}



Shape Code...

public class Rectangle extends Quads implements Playable {

	public Rectangle(int numberOfSides, int numberOfDimensions) {
		super(4, 2);
		// TODO Auto-generated constructor stub
	}

	public double calcPerimeter() {
		return ((this.getDimensions()[0] + this.getDimensions()[1]) * 2);
	}

	public double calcArea() {
		double area;
		area = this.getDimensions()[0] * this.getDimensions()[1];
		return area;
	}

	public String showDimensions() {
		String display = "";
		display += "For this " + this.getColor() + " "
				+ this.getClass().getSimpleName() + ": \n";

		display += this.getDIMENSION_LABELS()[0] + ": "
				+ this.getDimensions()[0] + "\n";
		display += this.getDIMENSION_LABELS()[1] + ": "
				+ this.getDimensions()[1] + "\n";
		display += this.getDIMENSION_LABELS()[2] + ": "
				+ this.getDimensions()[0] + "\n";
		display += this.getDIMENSION_LABELS()[3] + ": "
				+ this.getDimensions()[1] + "\n";
		display += "The perimeter is " + this.calcPerimeter() + ", \n";
		display += "The area is " + this.calcArea() + ", \n";
		display += "And the seniority is " + this.getSeniority() + "\n";
		return display;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: How to create/move objects on a 2d array?

#2 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 95
  • View blog
  • Posts: 523
  • Joined: 29-July 09

Re: How to create/move objects on a 2d array?

Posted 11 March 2014 - 07:47 AM

I dont see you actually filling the array anywhere.
To me it seems that the grid will just have null values. Is that the problem?

If I understand it correctly then what youre looking for is grid[i][j] = new Rectangle(2,3) Which by the looks of it will always be of size 4, 2 since thats what youre passing in your super() rather than the variables youre picking up in the constructor.

As I said, im not sure thats what youre looking for.
Was This Post Helpful? 0
  • +
  • -

#3 Anarchius   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 14

Re: How to create/move objects on a 2d array?

Posted 11 March 2014 - 07:56 AM

View PostAtspulgs, on 11 March 2014 - 07:47 AM, said:

I dont see you actually filling the array anywhere.
To me it seems that the grid will just have null values. Is that the problem?

If I understand it correctly then what youre looking for is grid[i][j] = new Rectangle(2,3) Which by the looks of it will always be of size 4, 2 since thats what youre passing in your super() rather than the variables youre picking up in the constructor.

As I said, im not sure thats what youre looking for.


Right now the board prints blank/null values because we have not put a shape into it yet. Users will be able to create a shape and we put it on the board based on its dimensions.
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 95
  • View blog
  • Posts: 523
  • Joined: 29-July 09

Re: How to create/move objects on a 2d array?

Posted 11 March 2014 - 08:09 AM

Which still leaves me wondering what would be the trouble.
Your title states that youre having trouble creating and moving objects within a 2d array,
To me it seems you are fairly handy with the arrays and familiar with objects.

Maybe youre looking for a way to swap the data in your array?
You can do the classic
Playable temp = grid[x1][y1];
grid[x1][y1] = grid[x2][y2];
grid[x2][y2] = temp;

or you can look into XOR swap.

Other than that I cant grasp your troubles :D
Was This Post Helpful? 0
  • +
  • -

#5 Anarchius   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 14

Re: How to create/move objects on a 2d array?

Posted 11 March 2014 - 08:45 AM

View PostAtspulgs, on 11 March 2014 - 08:09 AM, said:

Which still leaves me wondering what would be the trouble.
Your title states that youre having trouble creating and moving objects within a 2d array,
To me it seems you are fairly handy with the arrays and familiar with objects.

Maybe youre looking for a way to swap the data in your array?
You can do the classic
Playable temp = grid[x1][y1];
grid[x1][y1] = grid[x2][y2];
grid[x2][y2] = temp;

or you can look into XOR swap.

Other than that I cant grasp your troubles :D/>


we need to make a whole shape not just one point. so like a triangle would look like this...

1
1 1 1
1 1 1 1

and it should be created based on dimensions the user puts in. so the triangle above would be 4x3x3.

so i guess what we need help with is creating a method that builds a shape based on user input and places it on the board. single points are easy to make and move around, but what about a whole body of them at once?
Was This Post Helpful? 0
  • +
  • -

#6 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 95
  • View blog
  • Posts: 523
  • Joined: 29-July 09

Re: How to create/move objects on a 2d array?

Posted 11 March 2014 - 08:56 AM

And you want this in the console as far as i understand right?

Ok first of all lets put down some rules for them. (just to get the basic idea working, you can tweak it after)
They have to have certain limits, smallest - largest, make each square in the grid able to hold the largest size allowed.
Put the shape at the top left corner of the grid square.

Now each shape is an Object, simply make so each shape takes care of its own display in a form of holding an array of strings where each index represents a row. Each row is pre-generated within the object to its specifications.

When you display the grid (you have to print the whole thing out again anyways), ask each object (if not null) to give row 0 then row 1 and so on, when the object doesnt have that row, fill it with your filler character.

Think that would work in your code?
As far as I see thats something similar you tried, but instead of asking the objects printable rows you printed the whole thing.
Think about i ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1