I have read a lot of posts about this and nothing I've tried seems to work. Any help would be greatly appreciated.
I have 2 Classes, ColouredShape
and ShapeMatchingGame
.
I'm trying to create ColouredShape
objects using 2 arguments and add them to an ArrayList in the ShapeMatchingGame
Class.
I need 4 shapes and 3 colours of each of the 4 shapes * 3, so 36 items in the array.
I can see 36 objects are adding to the array in the loop but whenever i try to access the values of the objects in the array im not seeing the expected values for the object at its index. I guess what I'm trying to ask is am I accessing the values incorrectly or have I done something wrong with creating the objects and adding them to the array?
public class ColouredShape {
static int shapeValue;
static int colourValue;
static String colour, shape;
public ColouredShape() // Default constructor
{
this.shapeValue = 1;
this.colourValue =1;
this.colour ="";
this.shape ="";
}
public ColouredShape (int shapeValue,int colourValue) // Constructor with 2 arguments
{
this.shapeValue = shapeValue;
this.colourValue =colourValue;
this.colour = colour;
this.shape =shape;
}
public int getColour()
{
return colourValue;
}
public int getShape()
{
return shapeValue;
}
public class ShapeMatchingGame {
static int noShapes;
static ArrayList<ColouredShape> ColouredShapes = new ArrayList<ColouredShape>();
static int index;
static int shapeValue=1;
static int colourValue;
public static void main(String[] args)
{
ObjectCreation();
}
public static void ObjectCreation()
{
do// runs loops 3 times to create 3 of every shape/colour combo
{
do // loop to continue onto the next shape until 4 are created
{
do // loop to create 3 of colours of same shape
{
colourValue++;
System.out.println("Shape value " +shapeValue + " colour value " +colourValue);
ColouredShape gameShapes = new ColouredShape(shapeValue,colourValue);
ColouredShapes.add(gameShapes);//creates an object of colourshapes and passes the current shapevalue + colourvalue as arguments then adds it to the arraylist
System.out.println ("Value of object at array index "+ index + " shape value " + ColouredShapes.get(index).getShape()+" colour value " +ColouredShapes.get(index).getColour()+ "colour variable value = " + colourValue);
index++;
for (ColouredShape colouredShape : ColouredShapes)
{
System.out.println(colouredShape.getClass().getName() + "/" +
colouredShape.shape + "/" +
colouredShape.colour);
}
}while(colourValue < 3 );
System.out.println ("Value of object at array index "+ "0" + " shape value " + ColouredShapes.get(0).getShape()+" colour value " +ColouredShapes.get(0).getColour()+ "colour variable value = " + colourValue);
colourValue=0;//reset colourValue to allow next iteration of the loop
shapeValue++;//incrementing shapeValue to add colours to next shape
System.out.println ("Value of object at array index "+ "0" + " shape value " + ColouredShapes.get(0).getShape()+" colour value " +ColouredShapes.get(0).getColour()+ "colour variable value = " + colourValue);
}while(shapeValue < 5 );
shapeValue=1; // resetting shapeValue to allow next iteration of the loop
noShapes++;
}while (noShapes<3);
}
}