18 noviembre 2013

pieza

// Piece.java
package tetris;

import java.util.*;

/**
 An immutable representation of a tetris piece in a particular rotation.
 Each piece is defined by the blocks that make up its body.

 Typical client code looks like...

 Piece pyra = new Piece(PYRAMID_STR); // Create piece from string
 int width = pyra.getWidth(); // 3
 Piece pyra2 = pyramid.computeNextRotation(); // get rotation, slow way

 Piece[] pieces = Piece.getPieces(); // the array of root pieces
 Piece stick = pieces[STICK];
 int width = stick.getWidth(); // get its width
 Piece stick2 = stick.fastRotation(); // get the next rotation, fast way
*/
public class Piece {
// Starter code specs out a few basic things, leaving
// the algorithms to be done.
private TPoint[] body;
private int[] skirt;
private int width;
private int height;
private Piece next; // "next" rotation

static private Piece[] pieces; // singleton static array of first rotations

/**
Defines a new piece given a TPoint[] array of its body.
Makes its own copy of the array and the TPoints inside it.
*/
    public Piece(TPoint[] points) {
       
        body = new TPoint[points.length];
        System.arraycopy(points, 0, body, 0, points.length);
        for (TPoint tPoint : points) {
            width = Math.max(width, tPoint.x);
            height = Math.max(height, tPoint.y);
        }
        width++;
        height++;
        skirt = new int[width];
        for(int i = 0; i < width; i ++)
            skirt[i] = 100;
        for (TPoint tPoint : points)
            skirt[tPoint.x] = Math.min(skirt[tPoint.x],tPoint.y);
    }




/**
* Alternate constructor, takes a String with the x,y body points
* all separated by spaces, such as "0 0  1 0  2 0 1 1".
* (provided)
*/
public Piece(String points) {
this(parsePoints(points));
}

/**
Returns the width of the piece measured in blocks.
*/
public int getWidth() {
return width;
}

/**
Returns the height of the piece measured in blocks.
*/
public int getHeight() {
return height;
}

/**
Returns a pointer to the piece's body. The caller
should not modify this array.
*/
public TPoint[] getBody() {
return body;
}

/**
Returns a pointer to the piece's skirt. For each x value
across the piece, the skirt gives the lowest y value in the body.
This is useful for computing where the piece will land.
The caller should not modify this array.
*/
public int[] getSkirt() {
return skirt;
}


/**
Returns a new piece that is 90 degrees counter-clockwise
rotated from the receiver.
*/
public Piece computeNextRotation() {

//body.length=4 cuatro coordenadas de los 4 cuadrados
TPoint[] brotacion = new TPoint[body.length];
        int minX = 0;//menor x de lspuntos para acomodar en el sistem coordenada
       
//lo gira pero no en el mismo sistema de coordenada
for (int i=0; i < body.length; i++){

brotacion[i] = rotarPunto(body[i]);

}


for (int i=0; i < brotacion.length; i++){

if (brotacion[i].x < minX)
minX = brotacion[i].x;

}

//determinar el ancho en i CUdrante

for (int i=0; i < brotacion.length; i++)
brotacion[i]=new TPoint(new TPoint(minX*-1 + brotacion[i].x,brotacion[i].y+0));

//newWidth - 1,0 agrgada para porner coordenada correcta en el primer cuadrante
return new Piece(brotacion);
}

public TPoint rotarPunto(TPoint xx)
{
//devuele las coordenadas rotadas en sentido antihorario  angleInRadians el angulodeseadoa rotar

return new TPoint(- 1*xx.y,1 * xx.x );

}

/**
Returns a pre-computed piece that is 90 degrees counter-clockwise
rotated from the receiver. Fast because the piece is pre-computed.
This only works on pieces set up by makeFastRotations(), and otherwise
just returns null.
*/
public Piece fastRotation() {
return next;
}



/**
Returns true if two pieces are the same --
their bodies contain the same points.
Interestingly, this is not the same as having exactly the
same body arrays, since the points may not be
in the same order in the bodies. Used internally to detect
if two rotations are effectively the same.
*/
public boolean equals(Object obj) {
// standard equals() technique 1
if (obj == this) return true;

// standard equals() technique 2
// (null will be false)
if (!(obj instanceof Piece)) return false;
Piece other = (Piece)obj;

// YOUR CODE HERE
return true;
}


// String constants for the standard 7 tetris pieces
public static final String STICK_STR = "0 0 0 1 0 2  0 3";
public static final String L1_STR = "0 0 0 1 0 2  1 0";
public static final String L2_STR = "0 0 1 0 1 1 1 2";
public static final String S1_STR = "0 0 1 0 1 1  2 1";
public static final String S2_STR = "0 1 1 1  1 0  2 0";
public static final String SQUARE_STR = "0 0  0 1  1 0  1 1";
public static final String PYRAMID_STR = "0 0  1 0  1 1  2 0";

// Indexes for the standard 7 pieces in the pieces array
public static final int STICK = 0;
public static final int L1  = 1;
public static final int L2  = 2;
public static final int S1  = 3;
public static final int S2  = 4;
public static final int SQUARE = 5;
public static final int PYRAMID = 6;

/**
Returns an array containing the first rotation of
each of the 7 standard tetris pieces in the order
STICK, L1, L2, S1, S2, SQUARE, PYRAMID.
The next (counterclockwise) rotation can be obtained
from each piece with the {@link #fastRotation()} message.
In this way, the client can iterate through all the rotations
until eventually getting back to the first rotation.
(provided code)
*/
public static Piece[] getPieces() {
// lazy evaluation -- create static array if needed
if (Piece.pieces==null) {
// use makeFastRotations() to compute all the rotations for each piece
Piece.pieces = new Piece[] {
makeFastRotations(new Piece(STICK_STR)),
makeFastRotations(new Piece(L1_STR)),
makeFastRotations(new Piece(L2_STR)),
makeFastRotations(new Piece(S1_STR)),
makeFastRotations(new Piece(S2_STR)),
makeFastRotations(new Piece(SQUARE_STR)),
makeFastRotations(new Piece(PYRAMID_STR)),
};
}


return Piece.pieces;
}



/**
Given the "first" root rotation of a piece, computes all
the other rotations and links them all together
in a circular list. The list loops back to the root as soon
as possible. Returns the root piece. fastRotation() relies on the
pointer structure setup here.
*/
/*
Implementation: uses computeNextRotation()
and Piece.equals() to detect when the rotations have gotten us back
to the first piece.
*/
private static Piece makeFastRotations(Piece root) {
return null; // YOUR CODE HERE
}



/**
Given a string of x,y pairs ("0 0 0 1 0 2 1 0"), parses
the points into a TPoint[] array.
(Provided code)
*/
private static TPoint[] parsePoints(String string) {
List points = new ArrayList();
StringTokenizer tok = new StringTokenizer(string);
try {
while(tok.hasMoreTokens()) {
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());

points.add(new TPoint(x, y));
}
}
catch (NumberFormatException e) {
throw new RuntimeException("Could not parse x,y string:" + string);
}

// Make an array out of the collection
TPoint[] array = points.toArray(new TPoint[0]);
return array;
}




}




/////////////////////////////////////////////////////////////

package tetris;

import static org.junit.Assert.*;

import java.util.*;

import junit.framework.TestCase;

import org.junit.*;

/*
  Unit test for Piece class -- starter shell.
 */
public class PieceTest extends TestCase {
// You can create data to be used in the your
// test cases like this. For each run of a test method,
// a new PieceTest object is created and setUp() is called
// automatically by JUnit.
// For example, the code below sets up some
// pyramid and s pieces in instance variables
// that can be used in tests.
//private Piece pyr1, pyr2, pyr3, pyr4;
private Piece I1,I2,I3,I4;
private Piece L1,L2,L3,L4;
private Piece _L1,_L2,_L3,_L4;
private Piece S1,S2,S3,S4;
private Piece _S1,_S2,_S3,_S4;
private Piece C1,C2,C3,C4;
private Piece pyr1, pyr2, pyr3, pyr4;
private Piece s, sRotated;

@Before
public void setUp() throws Exception {
I1 = new Piece(Piece.STICK_STR);
I2 = I1.computeNextRotation();
I3 = I2.computeNextRotation();
I4 = I3.computeNextRotation();
L1 = new Piece(Piece.L1_STR);
L2 = L1.computeNextRotation();
L3 = L2.computeNextRotation();
L4 = L3.computeNextRotation();
_L1 = new Piece(Piece.L2_STR);
_L2 = _L1.computeNextRotation();
_L3 = _L2.computeNextRotation();
_L4 = _L3.computeNextRotation();
S1 = new Piece(Piece.S1_STR);
S2 = S1.computeNextRotation();
S3 = S2.computeNextRotation();
S4 = S3.computeNextRotation();
_S1 = new Piece(Piece.S2_STR);
_S2 = _S1.computeNextRotation();
_S3 = _S2.computeNextRotation();
_S4 = _S3.computeNextRotation();
C1 = new Piece(Piece.SQUARE_STR);
C2 = C1.computeNextRotation();
C3 = C2.computeNextRotation();
C4 = C3.computeNextRotation();
pyr1 = new Piece(Piece.PYRAMID_STR);
pyr2 = pyr1.computeNextRotation();
pyr3 = pyr2.computeNextRotation();
pyr4 = pyr3.computeNextRotation();
s = new Piece(Piece.S1_STR);
sRotated = s.computeNextRotation();
}
// Here are some sample tests to get you started
@Test
public void testSampleSize() {
////La I  (4) ancho y altura
assertEquals(1, I1.getWidth());
assertEquals(4, I1.getHeight());
assertEquals(4, I2.getWidth());
assertEquals(1, I2.getHeight());
assertEquals(1, I3.getWidth());
assertEquals(4, I3.getHeight());
assertEquals(4, I4.getWidth());
assertEquals(1, I4.getHeight());
/////la L
assertEquals(2, L1.getWidth());
assertEquals(3, L1.getHeight());
assertEquals(3, L2.getWidth());
assertEquals(2, L2.getHeight());
assertEquals(2, L3.getWidth());
assertEquals(3, L3.getHeight());
assertEquals(3, L4.getWidth());
assertEquals(2, L4.getHeight());
/////espejo de la L
assertEquals(2, _L1.getWidth());
assertEquals(3, _L1.getHeight());
assertEquals(3, _L2.getWidth());
assertEquals(2, _L2.getHeight());
assertEquals(2, _L3.getWidth());
assertEquals(3, _L3.getHeight());
assertEquals(3, _L4.getWidth());
assertEquals(2, _L4.getHeight());
/////la S
assertEquals(3, S1.getWidth());
assertEquals(2, S1.getHeight());
assertEquals(2, S2.getWidth());
assertEquals(3, S2.getHeight());
assertEquals(3, S3.getWidth());
assertEquals(2, S3.getHeight());
assertEquals(2, S4.getWidth());
assertEquals(3, S4.getHeight());
/////espejo de la S
assertEquals(3, _S1.getWidth());
assertEquals(2, _S1.getHeight());
assertEquals(2, _S2.getWidth());
assertEquals(3, _S2.getHeight());
assertEquals(3, _S3.getWidth());
assertEquals(2, _S3.getHeight());
assertEquals(2, _S4.getWidth());
assertEquals(3, _S4.getHeight());
//
/////EL CUADRADO
assertEquals(2, C1.getWidth());
assertEquals(2, C1.getHeight());
assertEquals(2, C2.getWidth());
assertEquals(2, C2.getHeight());
assertEquals(2, C3.getWidth());
assertEquals(2, C3.getHeight());
assertEquals(2, C4.getWidth());
assertEquals(2, C4.getHeight());
// Check size of pyr piece
assertEquals(3, pyr1.getWidth());
assertEquals(2, pyr1.getHeight());
assertEquals(2, pyr2.getWidth());
assertEquals(3, pyr2.getHeight());
assertEquals(3, pyr3.getWidth());
assertEquals(2, pyr3.getHeight());
assertEquals(2, pyr4.getWidth());
assertEquals(3, pyr4.getHeight());
// Now try with some other piece, made a different way
Piece l = new Piece(Piece.STICK_STR);
assertEquals(1, l.getWidth());
assertEquals(4, l.getHeight());
}
// Test the skirt returned by a few pieces
@Test
public void testSampleSkirt() {
// Note must use assertTrue(Arrays.equals(... as plain .equals does not work
// right for arrays.
//la I
assertTrue(Arrays.equals(new int[] {0},          I1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0, 0}, I2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0},          I3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0, 0}, I4.getSkirt()));
//la L
assertTrue(Arrays.equals(new int[] {0, 0},    L1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0}, L2.getSkirt()));
assertTrue(Arrays.equals(new int[] {2, 0},    L3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1, 1}, L4.getSkirt()));
//espejo de L
assertTrue(Arrays.equals(new int[] {0, 0},    _L1.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 1, 0}, _L2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 2},    _L3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0}, _L4.getSkirt()));
//la S
assertTrue(Arrays.equals(new int[] {0, 0, 1}, S1.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0},    S2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 1}, S3.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0},    S4.getSkirt()));
//espejode S
assertTrue(Arrays.equals(new int[] {1, 0, 0}, _S1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1},    _S2.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0, 0}, _S3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1},    _S4.getSkirt()));
//cuadrado C
assertTrue(Arrays.equals(new int[] {0, 0}, C1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0}, C2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0}, C3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0}, C4.getSkirt()));
//yla piramide
assertTrue(Arrays.equals(new int[] {0, 0, 0}, pyr1.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0}, pyr2.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0, 1}, pyr3.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1}, pyr4.getSkirt()));

assertTrue(Arrays.equals(new int[] {0, 0, 1}, s.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0}, sRotated.getSkirt()));
}
}