Issue
I have made an app in Android Studio.
Quick summary: Its an app for kids that presents a random shape when the user starts the game. The user has 4 options to choose from where one shape is the correct shape. The user then needs to drag and drop the shape into the outline. A pic of an example is shown below.
The problem is, I need 1 of the 4 shapes below to match the shape to be guessed. I have 2 sets of 18 shapes, the first set is the shape outline with the ? inside.
int[] outlines = new int[] {R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
R.drawable.outline_15, R.drawable.outline_16,R.drawable.outline_17};
The second set is the actual coloured shapes with the faces.
int[] images = new int[] {R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
R.drawable.img_17};
I need some sort of function or statement where the 4 shapes in the bottom cannot be the same as well as 1 of the shapes corresponding to the shape that needs to be guessed.
NOTE: outline_0 shape corresponds to img_0, outline_1 corresponds to img_1 etc.
This is the whole code for this activity.
public class SecondActivity extends AppCompatActivity {
int n;
ImageView shape1, shape2, shape3, shape4, guessShape;
ImageButton exit;
Random rand = new Random();
ImageView[] shapes = new ImageView[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
shape1 = (ImageView) findViewById(R.id.shape1);
shape2 = (ImageView) findViewById(R.id.shape2);
shape3 = (ImageView) findViewById(R.id.shape3);
shape4 = (ImageView) findViewById(R.id.shape4);
guessShape = (ImageView) findViewById(R.id.guessShape);
shapes[0] = shape1;
shapes[1] = shape2;
shapes[2] = shape3;
shapes[3] = shape4;
//store all the shapes in an array
int[] images = new int[] {R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
R.drawable.img_17};
int[] outlines = new int[] {R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
R.drawable.outline_15, R.drawable.outline_16,R.drawable.outline_17};
//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int outlineID = (int) Math.round((Math.random() * outlines.length));
//set the image
guessShape.setBackgroundResource(outlines[outlineID]);
shape1.setBackgroundResource(images[img1]);
shape2.setBackgroundResource(images[img2]);
shape3.setBackgroundResource(images[img3]);
shape4.setBackgroundResource(images[img4]);
//set tags for the imageViews
guessShape.setTag("RandomImage");
shape1.setTag("Shape1");
shape2.setTag("Shape2");
shape3.setTag("Shape3");
shape4.setTag("Shape4");
//1 of the 4 image views needs to match outline of the shape that needs to be guessed
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_0)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_0);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_1)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_1);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_2)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_2);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_3)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_3);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_4)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_4);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_5)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_5);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_6)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_6);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_7)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_7);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_8)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_8);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_9)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_9);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_10)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_10);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_11)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_11);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_12)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_12);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_13)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_13);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_14)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_14);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_15)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_15);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_16)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_16);
}
if(guessShape.getBackground().getConstantState().equals(R.drawable.outline_17)){
int random = new Random().nextInt(shapes.length);
shapes[random].setBackgroundResource(R.drawable.img_17);
}
}
}
- What am I doing wrong here?
- Why doesn't my if statements work for making sure one of the random shapes below corresponds to the shape to be guessed?
- How do I make sure that the 4 shapes in the bottom are different?
Any tips to solve my problem would be great. Thank you! :)
Solution
Without a log, stacktrace, or output its hard to know exactly where you went wrong. There are several things you can do that will either fix this error or may prevent another down the road.
Setting Variables
Let's save the guessShape.getBackground().getConstantState() as a variable. Now I don't know what type this is so I will just call it an Object of now. Please update with the correct Type.
Object currentBackground = guessShape.getBackground().getConstantState();
If-Else If
Currently you are using if statements followed by more if statements. This has the effect of testing the first if than the second than the third every time. Instead, we only want to match each of these once. Here is a tutorial.
Let's change this:
if(currentBackground.equals(R.drawable.outline_0){
...
}
if(currentBackgorund.equals(R.drawable.outline_1){
...
} ....
To this:
if(currentBackground.equals(R.drawable.outline_0){
...
} else if(currentBackgorund.equals(R.drawable.outline_1){
...
} ....
Else
Now that we know it will only match it once, we want to make sure it does get found. We will want to catch an else
at the end of the if-else if
statements we just finished.
.... } else if (currentBackground.equals(R.drawable.outline_17){
...
} else {
// How do you want to handle if the background did not equal any of your images?
}
Debugging
Now that we have some of the major improvements. You need to track down where its going wrong. One helpful way to do this is to print things to the console. There are cooler more powerful ways to debug in IDEs such as NetBeans or Eclipse, but System.out.println
is great for now!
The trick is knowing where you might be going wrong. I see a couple "points of failure". These are areas you have a higher risk of something going wrong. It will help to check that the value is what you think it is at that point.
At your random numbers. Figure out what the numbers are. Are they in the correct range? This will also help you debug which shape it should have been.
//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int outlineID = (int) Math.round((Math.random() * outlines.length));
// Print these to know what they are, especially outlineID.
You can put prints in each of the if statements to see which one (including the else) is being caught and why. You can make sure it was the one you wanted.
Conclusion
Try those out. It may not catch your bug, but at least you will have a concrete idea of what your setting it to and which if its catching. This will help you debug thoroughly. If the outline is getting into the correct if-statement every time, then we know there is a problem inside the if statement! Debugging may not find the problem the first time, but it does narrow it down.
Non-Matching Images Answer
I am editing my answer to include this. I'm pretty sure I spotted your problem.
In your code here:
//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int outlineID = (int) Math.round((Math.random() * outlines.length));
You are creating a 4 random images for the images, but then you are also creating a random image for the outline. This means you could get images 1,2,3,4 but then get the outline for image 17 on the outline! This would match in your if statements, but it would give you for the image of 17 instead of the one you wanted.
In this case, you want to get the 4 random numbers, and then pick from those for your outline. Here is one way to do this (note: there are better/easier ways, but I want to make sure its at your current level. You'll get there soon enough!)
//generate random number between 0 and image.length
int img1 = (int) Math.round((Math.random() * images.length));
int img2 = (int) Math.round((Math.random() * images.length));
int img3 = (int) Math.round((Math.random() * images.length));
int img4 = (int) Math.round((Math.random() * images.length));
int whichImg = (int) Math.round((Math.random() * 4));
if(whichImg == 1){
whichImg = img1;
} else if(whichImg == 2){
whichImg = img2;
} else if(whichImg == 3){
whichImg = img3;
} else {
whichImg = img4;
}
int outlineID = outlines[whichImg];
This gives you a random number 1-4. You can then use to save the the correct image to whichImg. Then you use that to get the same number in your outlines array.
Side Note
You may not have learned them yet, but a for loop
would be an excellent way to get your code down from all of these if statements to only one. :) If your not familiar with them, fix your bug first before trying it out. Here is a link to a tutorial in case you want to check it out.
Answered By - Melly Beechwood
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.