0

I want to create an Array representation of an Element (shape) (such as in Tetris)

For simplicity let's assume this Element will always be a length of 4.

so in an Array there might be

[0][1][1][1]
[0][0][0][1]
[0][0][0][0]
[0][0][0][0]

How would I go about calculating all possible rotations of this array? I wanted to create something where I can "draw" these shapes, and determine if it was valid (thus needing to check against the predefined element in the array, and each possible rotation of it)

Edit

Thank you TheLethalCoder for linking How do you rotate a two dimensional array?

I need not only the rotation (though that is already some great help) but also each possible position within the array.

A bit more of what my planned approach was :

The background will consist of a larger Array (Also 2D)

When I click on the Background, This is set as the initial point. At this point all shapes will match the input. (as all points have one point)

As the user goes to the next Point (Left, right, Up Down from the orig. point), I want to recalculate which Shapes still match the input (thus any positioning and rotation)

Community
  • 1
  • 1
Johan
  • 262
  • 1
  • 15
  • Possible duplicate of [How do you rotate a two dimensional array?](http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array) – TheLethalCoder Nov 16 '15 at 16:01
  • FYI It took less than 30 seconds to google search it, please try and do some research first – TheLethalCoder Nov 16 '15 at 16:04
  • It comparing it to each rotation enough though? I thought that I also needed to move it about to each possible co-ordinate – Johan Nov 16 '15 at 16:11
  • All rotations of the shape and all rotations AND all locations of the shape are two different questions. The latter adding extra complexity, If you only want all rotations I would suggest just making a smaller array, i.e. for the one in question 3x2, and scaling as appropriate – TheLethalCoder Nov 16 '15 at 16:13
  • To do that just simply copy elements from the array to another (plenty of questions on this), obviously moving them, if they overflow put them at the other side, you will need to do checking to make sure the shape isn't broken though – TheLethalCoder Nov 16 '15 at 16:28
  • Do you place the shapes onto the 2d array? If you do you know where they are what shape they are, therefore just keep a list of a `shape` class. This will hold the sub aray of the shape i.e. 3x2 and its positional info within the grid. Then all you need to do is grab the user input and match it against these and their 4 rotations, no need for positional movements. If this has helped you I'll write up an answer explaining what I mean in more detail. – TheLethalCoder Nov 16 '15 at 16:39
  • That sounds like the answer I am looking for :) – Johan Nov 16 '15 at 16:44
  • Added my answer, hopefully its clear enough – TheLethalCoder Nov 16 '15 at 16:52

1 Answers1

1

Essentially what I would suggest doing is whilst placing the shapes on the array also setting them in a list for later use. This list will be of a class like Shape, where it has the following declaration:

public class Shape
{
    public int[,] ShapeArray { get; set; }

    public int[,] Position { get; set; }
}

An example of the ShapeArray, taking the one from your question is as follows:

[1][1][1]
[0][0][1]

Also if it makes it simpler Position can be simplified to UpperLeftXIndex and UpperLeftYIndex, with a Width and Height or essentially a System.Drawing.Rectangle.

Then when it comes to checking against the user input to the shapes just compare against each shape within the actual list. If any match you have their positions to do with, what you like.

This approach also simplifies the matching algorithm by only needing to match against rotations not rotations and positions. Information on rotating a 2D array can be found at How do you rotate a two dimensional array?

Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69