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()));
}
}



28 octubre 2013

rectngulo

import java.awt.geom.Point2D;
import java.util.Scanner;

/**
 * @author Nestor Fico Cuchuyrume Mamani
 * @version 1.0 Creada 23 de Octubre del 2013.
 */
public class DemoRect {

public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);

double x,y,x1,y1;

System.out.print("Ingrese primera esquina del 1er rectangulo: ");
x=sc.nextDouble();
y=sc.nextDouble();
System.out.print("Ingrese segunda esquina del 1er rectangulo: ");
x1=sc.nextDouble();
y1=sc.nextDouble();

Point2D.Double p1 = new Point2D.Double(min(x,x1),min(y,y1));
Point2D.Double p2 = new Point2D.Double(max(x,x1),max(y,y1));




System.out.print("Ingrese primera esquina del 2er rectangulo: ");
x=sc.nextDouble();
y=sc.nextDouble();
System.out.print("Ingrese segunda esquina del 2er rectangulo: ");
x1=sc.nextDouble();
y1=sc.nextDouble();

Point2D.Double p3 = new Point2D.Double(min(x,x1),min(y,y1));
Point2D.Double p4 = new Point2D.Double(max(x,x1),max(y,y1));


MiRectangulo rectangulo1=new MiRectangulo(p1,p2);
MiRectangulo rectangulo2=new MiRectangulo(p3,p4);

System.out.println("Rectanulo A = "+rectangulo1);
System.out.println("Rectanulo B = "+rectangulo2);

rectangulo2.CasoRectangulo(rectangulo1);

}

/**
* Para determinar los puntos inferiores
* @param x double
* @param x1 double
* @return regresa menor valor
*/
public static  double min(double x,double x1){
if(x return x;
else
return x1;
}
/**
* Para determinar los puntos superiores
* @param x double
* @param x1 double
* @return regresa mayor valor
*/
public static  double max(double x,double x1){
if(x return x1;
else
return x;
}
}



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


import java.awt.geom.Point2D;


public class MiRectangulo {
/**
* puntoInferiorIzq representara las coordenadas del
* punto inferior izquierdo del rectangulo.
* puntoSuperiorDer representara las coordenadas del
* punto superior derecho del rectangulo.
*/
private Point2D.Double puntoInferiorIzq;
private Point2D.Double puntoSuperiorDer;

/**
* constructor de la clase para inicializar directamente
* con dos puntos con sus respectivas coordenadas
* @param pt1 double
* @param pt2 double
*/
MiRectangulo(Point2D.Double pt1, Point2D.Double pt2) {
setPuntoInferiorIzq(pt1);
setPuntoSuperiorDer(pt2);
}

/**
* Asigna valores deseados a punto inferior izquierda
* @param pt con cordenaas x e y
*/
public void setPuntoInferiorIzq(Point2D.Double pt) {

puntoInferiorIzq=new Point2D.Double(pt.x,pt.y);

}
/**
* Asigna valores deseados a punto superior derecha
* @param pt con cordenaas x e y
*/
public void setPuntoSuperiorDer(Point2D.Double pt) {

puntoSuperiorDer=new Point2D.Double(pt.x,pt.y);
}
/**
* @return punto inferior izquierda del rectangulo
*/
public Point2D.Double getPuntoInferiorIzq() {

return puntoInferiorIzq;
}
/**
* @return punto superior derecha del rectangulo
*/
public Point2D.Double getPuntoSuperiorDer() {

return puntoSuperiorDer;
}
/**arr toma valores de los x e y de los puntos
* para hallar el area y verificar casos
* @param rx, rectangulo x para comparar con el rectangulo 1
* El for que se muestra ordena los valores del array en cada fila
* xx es la suma de las longitudes horizontales de los rectangulos
* yy es la suma de las longitudes verticales de los rectangulos
*/
public void CasoRectangulo(MiRectangulo rx)
{
int i,j,k;
double x;
double [][] arr = {
{rx.puntoInferiorIzq.x,rx.puntoSuperiorDer.x,puntoInferiorIzq.x,puntoSuperiorDer.x},
{rx.puntoInferiorIzq.y,rx.puntoSuperiorDer.y,puntoInferiorIzq.y,puntoSuperiorDer.y},
                  };

for(k=0; k      for (i=1; i            j=i;        
           while (j>0 && arr[k][j-1]>arr[k][j]){
           
                 x=arr[k][j];
                 arr[k][j]=arr[k][j-1];
                 arr[k][j-1]=x;
                 j--;
           }
     }
}

double area=Math.abs((arr[0][2]-arr[0][1])*(arr[1][2]-arr[1][1]));

double xx=Math.abs(puntoSuperiorDer.x-puntoInferiorIzq.x)+Math.abs(rx.puntoSuperiorDer.x-rx.puntoInferiorIzq.x);
double yy=Math.abs(puntoSuperiorDer.y-puntoInferiorIzq.y)+Math.abs(rx.puntoSuperiorDer.y-rx.puntoInferiorIzq.y);

if(xx
System.out.println("Rectanguloa A y B son disjuntos.");
}
else{
if(area==0){

System.out.println("Rectanguloa A y B se tocan.");
        }
else{
System.out.println("Rectanguloa A y B se sobreponen.");
System.out.println("Area sobrepuesta = "+Math.abs(area));
}

}
}


//Muestra las coordenada como: ([1.0, 2.0], [23.0, 4.0])
public String toString(){

return "(["+puntoInferiorIzq.x+", "+puntoInferiorIzq.y+"], ["+puntoSuperiorDer.x+", "+puntoSuperiorDer.y+"])";
}

}

14 octubre 2013

master mind

/**
 *
 * Fundamentos de Programacion 2: CuatroEsferas.java
 *
 */



class CuatroEsferas {
static  int MENOS =0;
   // Datos miembro
   private Esfera esfera1;
   private Esfera esfera2;
   private Esfera esfera3;
   private Esfera esfera4;

   // Constructores
 
   public CuatroEsferas() {
     esfera1 = new Esfera('R');
esfera2 = new Esfera('R');
esfera3 = new Esfera('R');
esfera4 = new Esfera('R');
   }

   public CuatroEsferas(CuatroEsferas cuatroEsferas) {
      esfera1 =  new Esfera(cuatroEsferas.esfera1);
      esfera2 =  new Esfera(cuatroEsferas.esfera2);
      esfera3 =  new Esfera(cuatroEsferas.esfera3);
      esfera4 =  new Esfera(cuatroEsferas.esfera4);
   }

   public void CuatroPegs(Esfera e1, Esfera e2, Esfera e3, Esfera e4){
      esfera1 = e1;
      esfera2 = e2;
      esfera3 = e3;
      esfera4 = e4;
   }

//-------------------------------------------------
//      Metodos publicos
//          void setColor ( int, char );
//          char getColor ( int );
//          String toString ( );
//          int contandotPseudoHits ( CuastroEsferas );
//          int contandotHits ( CuatroEsferas );
//------------------------------------------------
 
   // Cambia el color del peg seleccionado
   public void setColor (int queColor, char elColor) {
 
       switch (queColor) {
         case 1: esfera1.setColor(elColor);
          break;
         case 2: esfera2.setColor(elColor);
        break;
         case 3: esfera3.setColor(elColor);
        break;
         case 4: esfera4.setColor(elColor);
        break;
         default: System.err.println("No hay tal color!");
                  System.exit(1);
       }
   }




   // Retorna el color de la esfera seleccionada
   public char getColor (int queColor) {
       char color = ' ';
       switch (queColor) {
         case 1: color = esfera1.getColor();
break;
         case 2: color = esfera2.getColor();
break;
         case 3: color = esfera3.getColor();
break;
         case 4: color = esfera4.getColor();
break;
         default: System.err.println("No hay tal color!g");
                  System.exit(2);
       }
       return color;
   }

   // Retorna la representacion  string del objeto CuatroEsferas

   public String toString (){
       // este es un stub (recuerde que un stub es un metodo incompleto)
       return esfera1.getColor()+" "+esfera2.getColor()+" "+esfera3.getColor()+" "+esfera4.getColor();
   }



   // Retorna el numero de pseudoHits entre el objeto y la suposición

   public int contandoPseudoHits (CuatroEsferas esferaAdivina) {
       // este es un stub (recuerde que un stub es un metodo incompleto)
 
 
  int phit=0,i=0,j=0,C=4;
 // int i=0,j=0;
  //int C=4;
  char f[]={getColor(1),getColor(2),getColor(3),getColor(4)};
 
  for(i=0;i   {
  if(f[i]==esferaAdivina.getColor(i+1))
  {
  f[i]='*';
  esferaAdivina.setColor(i+1, '*');
  }
  }
 
  for(i=1;i   {
  for(j=1;j   {
  if(f[i-1]==esferaAdivina.getColor(j)&&f[i-1]!='*')
  {
  phit++;
  esferaAdivina.setColor(j, '*');
 break;
  }
  }
  }


       return phit;
     
     
   }


   // Retorna el numero de hits entre el objeto y la suposicion

   public int contandoHits (CuatroEsferas esferaAdivina) {
       // este es un stub (recuerde que un stub es un metodo incompleto)
 
  int hit=0,i=0,C=4;
 
  for(i=1; i   {
  if(esferaAdivina.getColor(i)==getColor(i))
  {
  hit++;
      }
  }
 
       return hit;
   }

}



     
       //////////////////////////////////////////////////////////////////7

/**
 * Definicion de los colores
 * Esta clase esta completa y NO DEBE SER MODIFICADA
 */

class Esfera {
 
   // Dato miembro
   private char color;

   // Constructores
   public Esfera() {
      color = 'R';
   }

   public Esfera(Esfera e) {
      color = e.getColor();
   }

   public Esfera(char c) {
      color = c;
   }

//-------------------------------------------------
//      Metodos Publicos:
//          void setColor ( char );
//          char getColor ( );
//          String toString ( );
//------------------------------------------------
 
   // Cambia el color de la esfera
   public void setColor (char c) {
       color = c;
   }

   // Retorna el color de la esfera
   public char getColor () {
       return color;
   }

   // Retorna la representacion  string del ojbeto Esfera
   public String toString () {
       return "" + getColor();
   }

}


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

import java.text.DecimalFormat;
import java.util.Scanner;


public class MenteMestra {

public static void main(String[] arg)
{
/** df 01,02...10
* hit guarda hit
* cdx=cadenax=cadena ingresada
* cd=cadena clave
* cdg=cadena guardada (ingresada)
* OkSize para verificar correcto numero de char
* EsferasIni para inicialiar cdx con valores ingresados (cdg)
*/
DecimalFormat df = new DecimalFormat("00");

int i=0,hit;
String cdg="";
CuatroEsferas cdx = new CuatroEsferas();
CuatroEsferas cd = new CuatroEsferas();

cd.setColor(1,'M');
cd.setColor(2,'L');
cd.setColor(3,'A');
cd.setColor(4,'R');


System.out.println("\t\tCodgo secreto: "+cd);
do{

   cdg=OkSize(cdg);
   EsferasIni(cdg,cdx);
   hit=cd.contandoHits(cdx);

   System.out.println("\t\tSuposicion #"+df.format((i+1))+": "+cdx+"   Hits: "+hit+"; PseudoHits = "+cd.contandoPseudoHits(cdx));

        if(hit==4){

       System.out.print("Felicitaciones! \nEspero que hayas disfrutado el juego. Adios!");
       break;
        }

i++;

}while(i<10 p="">
if(hit!=4)
{
System.out.println();
System.out.println("Lo siento, no lo logró. El código secreto es "+cd);
System.out.println("Espero que hayas disfrutado el juego. Adios!");
}


}


public static String OkSize(String s)
{
Scanner sc=new Scanner(System.in);

do{
System.out.print("Ingrese su suposcion: ");
s=sc.nextLine();

}while(s.length()!=4);

return s;
}


public static CuatroEsferas EsferasIni(String s, CuatroEsferas e4)
{
int i=0;
for(i=0;i {
e4.setColor(i+1, s.charAt(i));
}
return e4;
}

}