Guess the image game, Random Image generator without repetition [duplicate] - java

This question already has answers here:
How to generate random numbers without repetition
(5 answers)
Closed 1 year ago.
I wanna make image guessing android game, there are 150 images, I want the image to be generated randomly, and the user should type the name of character if it is correct the next button will display another image till the 150 are finished
This the code for avoiding image repition
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.myImage);
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
list.remove(position);
but how should I compare the user input with character name, should I use image path, or make array for answers and compare it to user input?

There are many ways to do it, depending on your project. An effective one would be storing them in an object (let's call it Card), and card will contain as properties (imageURL as String / characterName as String/ ifCorrect as Boolean).
Store these objects inside an array and if the user guesses the right answer, change the object's ifCorrect field to true, so you know the image has been played.

Related

Saving a String into a file, then loading it into a String variable will give a "different" string that seems to have the exact same name [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
So I am having an issue, I put my self to the test of creating a 2D sandbox game kind of for fun, but has turned out evolving into something a bit more after about a week, but, to the point, I am saving chunk data into a few files, these are just .file or, just the most basic file type I assume, and the problem persists even using .txt files.
So what's happening is after saving a file containing the object name, x, and y positions, I am creating an object in a linked list using that information, when I enter a new "chunk", but things are not exactly right, somehow, my images load up for the tiles correctly, which is handled in the constructor for that tile, being called after it is added to the list, however, what made me notice is I have a check in the tiles update method, to see if it wants to change to a different grass texture, and it doesn't happen, so I did some checking and printing, and well, I can tell that the name variable certainly contains the desired name, eg. "dirt", however it does not equal "dirt", so I thought it was catching some spaces and I even trimmed the String, and when I run a test like
if(name == "dirt")System.out.print("dirt is actually "+name);
else System.out.print("dirt is apparently not "+name);
and the result is always, "dirt is apparently not dirt", with no spaces, and even when looking in the actual file I save it to, it saves the name fine, I have them saving in an order like so;
name x y
eg.
dirt 0 560
dirt 28 560
and so on, these all load in position perfectly fine and what stumps me is that the initial image loads in correctly, which is quite explicit, a switch statement to determine what the image, material type and such should be..
I was wondering if anyone has come across something like this and what it could possibly be.
Also any code desired I will add to the desc.
(I didn't know so many people would be so quick to help. Thank you all, I know it was a simple problem, but it had me super confused, now thanks to you all I understand something about comparing variables and objects I should have really known before, and which is really important
.)
Chances are the result is "dirt" but you're not testing it correctly.
Test should be if name.equals("dirt") ... not the equality operator ==.
See: How do I compare strings in Java?
Equality in Java:
String dirt1 = "dirt";
String dirt2 = new String("dirt");
System.out.println(dirt1 == "dirt"); // TRUE
System.out.println(dirt1 == dirt2); // FALSE
System.out.println(dirt1 == dirt2.intern()); // TRUE
System.out.println(dirt1.equals(dirt2)); // TRUE

Processing save 3D array to file and load it [duplicate]

This question already has answers here:
how to write an array to a file Java
(6 answers)
Closed 7 years ago.
I have a problem. I want to store a 3D array to a text file. And how could I set the array to the value of a text file?
My array:
int mosaics[][][] = new int[100][100][5];
How could I do that?
It absolutely doesn't matters how the text file looks like.
Thanks
If you just want to work with text data and not any binary then the simplest format which comes to my mind is as below
x,y,z:123
In the above format specs,
x = index of the first
y = index of the second
z = index of the third
To read this you would first
-- initialize the value of the full array to nulls or Zero
-- read the text file line by line
--- split the line on ":" , split the left side with "," comma
--- read the individual indexes and set the value for that.
To Write it will be simple as you will be writing the values through the loop.
-- to keep the file smaller, you can skip the null.

How can I repeat a task in java/android? [duplicate]

This question already has answers here:
is there any way to find resource Id of drawable
(7 answers)
Closed 7 years ago.
I want to complete a task, which assigns the variable randomButton a value. At the end, I want to check the value of randomButton If it is something I don't like, I want to re do the task where I assign randomButton a value.
I want to do this task:
int i = rng.nextInt(4); //Find random button from between 1-4
randomButton = buttons.get(i);
buttonBackground = randomButton.getBackground();
Log.v(TAG, "In the do");
and if
buttonBackground.equals(R.drawable.redcircle)
I want to try and do the task again. How do I achieve this...?
I have tried:
do {
int i = rng.nextInt(4); //Find random button from between 1-4
randomButton = buttons.get(i);
buttonBackground = randomButton.getBackground();
Log.v(TAG, "In the do");
}while(!buttonBackground.equals(R.drawable.redcircle));
But that is an infinite loop. So how do I repeat a task if the value is something I don't like?
By the way, buttons is an arraylist with different image button objects. I want to see if the background of that imagebutton (randomButton) is redcircle. If it is, I want to go through the code again. If it isn't, thats great, and I can continue.
A do-while loop would be the correct way to to this. I do not know what R.drawable.redcircle means, is that a color?
You did something wrong in the while clause:
}while(!buttonBackground.equals(R.drawable.redcircle));
I should have posted this in the comment but i couldn't since i dont have 50 reputation yet.

Dividing by four in combobox

I'm a new programmer doing a date of birth selector for part of my project. I have got everything set up apart from a few things and I am unsure of how to do these things.
ArrayList<String> years_tmp = new ArrayList<String>();
years_tmp.add("Year");
for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--)
{
years_tmp.add(years+"");
}
Y = new JComboBox(years_tmp.toArray());
Above is my part of my code for a JComboBox which lists the previous 90 years and has the word "Years" as the first object.
For my code above how would I list the years like it currently does, but to only display years which divide by four exactly (leap years)?
Also how do I make it so once the JComboBox list has been opened the selection years can not be selected so when the value is saved in my save file it does not allow the save of the word "Years"?
To get a value which can be devided be 4 without a rest you can use the modulo operator '%':
if(year % 4 == 0) {
...
}
To disallow the Selection of "year" itself, you have several ways. One could be to append an ItemListener to your ComboBox and check whether the user selected the "year" value. If the user selected this value you can print an error message or just select another value - maybe the next possible one. You also can do more fancy stuff like disabling the save Button if the user selected an invalid value..
I think this should help you to get to the right direction.

Having problems with storing variable in classes

Please see ifmy logic is correct i do not know how to implement what i am trying to do. I am a beginner in java.
Game is a class which stores the Name and the Steps of that user after the game is complete.
[ First Method ]
private game[] gameArray = new game[10];
for(int i = 0; i <gameArray.length;i++){
gameArray[i].setName(nameTxt.getText());
gameArray[i].setpSteps(stepsCount);
}
By Clicking History Button they will get the previous 10 people's name and steps.
JOptionPane.showMessageDialog(null,
"Player's Name No. of Steps"+
"\n"+gameArray[i].toString());
-The Problem:
1) This code limits to only previous 10 results is there anyway i can get all the previous results
2) This code is not working!
[ Second Method - This is also not working! ]
private game[] gameArray = new game[10];
// Storing the name of player[i]
gameArray[i].setName(nameTxt.getName());
// Storing the number of steps player[i] took.
gameArray[i].setpSteps(stepsCount);
//Displaying previous 10 players name and steps
JOptionPane.showMessageDialog(null,
"Player's Name No. of Steps"+
"\n"+gameArray[i].toString());
Some points:
you can't directly convert an array to a String through toString() because it isn't supported in Java, you should use Arrays.toString(..)
Java arrays have static size, if you want to store the whole history you will need a dynamic data structure as an ArrayList<Game> or a LinkedList<Game>
name of classes should be capitalized (Game, not game)
without knowing the internals of your Game class it's impossible to tell what is not working and why it is not working, we don't even know what it is supposed to do exactly

Categories

Resources