public void allButtonChoice(){
Button[] multiChoice = new Button[3];
multiChoice = new Button[]{choiceButton1,choiceButton2,choiceButton3,choiceButton4};
//to output shuffled array in an randomized order.
//Random number generator.
Random generateMultiChoice = new Random();
for(int i = 0; i < multiChoice.length; i++){
int randomPosition = generateMultiChoice.nextInt(multiChoice.length);
Button temp = multiChoice[i];
multiChoice[i] = multiChoice[randomPosition];
multiChoice[randomPosition] = temp;
}
//set the positions to the buttons in order
choiceButton1.setX(10f);
choiceButton1.setY(10f);
choiceButton2.setX(10f);
choiceButton2.setY(10f);
choiceButton3.setX(10f);
choiceButton3.setY(12f);
choiceButton4.setX(10f);
choiceButton4.setY(12f);
}
How can I assign a variable to choiceButton1, choiceButton2, choiceButton3, and choiceButton4? Is it possible to have one variable that holds the four buttons with setX and setY data?
choiceButton1.setTag(value);
will be useful for you
you can retrieve data by calling getTag
Related
Lets say I have a public class called GameBoard that will be a two dimensional array with 4 rows and 5 columns. The spaces in the array are filed with String values from 1 to 20. A card will be drawn that has a name (King of Spades for example) . If the user inputs 15 I will store it in a String variable called userLocation. What would be the most efficient way to create a method that takes the input location and updates the array with the name of the Card? Would a for loop be most efficient?
public GameBoard() {
square = new String[4][5];
square[0][0] = new String("1");
square[0][1] = new String("2");
square[0][2] = new String("3");
square[0][3] = new String("4");
square[0][4] = new String("5");
square[1][0] = new String("6");
square[1][1] = new String("7");
square[1][2] = new String("8");
square[1][3] = new String("9");
square[1][4] = new String("10");
square[2][1] = new String("11");
square[2][2] = new String("12");
square[2][3] = new String("13");
square[3][1] = new String("14");
square[3][2] = new String("15");
square[3][3] = new String("16");
square[2][0] = new String(17);
square[3][0] = new String(18);
square[2][4] = new String(19);
square[3][4] = new String(20);
}
My preferred method as of now would look something like this but it gives me the error code "type mismatch:cannot convert string to boolean" under userLocation = board[i][j]
public String[][] updateBoard(String userLocation, Card card, String[][] board) {
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 5; j++)
{
if(userLocation = board[i][j]) {
board[i][j] = card.name;
}
}
}
return board;
}
So the reason it will not compile is your = does not return a boolean expression. == would, but it's still not what you want, since you want to check if the String contents are the same, not if they're the same object, so use .equals.
But, no, I think you don't want to depend on strings to identify locations. What if you want to replace a card? And why look through everything when you need not?
Rather if i is some number between 1 and 20, identify the corresponding spot in the array by square[(i-1)/5][(i-1)%5]
That should bypass the issue you are having with matching strings.
So for example, your constructor becomes:
public GameBoard() {
square = new String[4][5];
for (int i=1; i<=20;i++){
square[(i-1)/5][(i-1)%5]=""+i;//initialize with 1 to 20 if you like
}
and userLocation is an int.
I have the array of images with a length of 6 (we call array first).I want to save this 6 values in another array with a length of 12 so that each value of the first array repeated twice in the second array and the values placed in random places of the second array .
For example : first array = {3,4,20,33,1,25}
The second array can be = {4,20,33,1,20,25,25,3,4,1,3,33}
public class PlayActivity extends AppCompatActivity {
public static boolean isTheSameOk = true;
final Random rnd = new Random();
int saverandom;
int dontsame = 0 ;
int [] allimages = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11,
R.drawable.img12, R.drawable.img13, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17,
R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23,
R.drawable.img24, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29,
R.drawable.img30, R.drawable.img31, R.drawable.img32, R.drawable.img33, R.drawable.img34, R.drawable.img35,
R.drawable.img36, R.drawable.img37, R.drawable.img38, R.drawable.img39, R.drawable.img40};
int [] chooseimages = new int [6];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
//choose 6 image from 40 image we have for putting into buttons
for(int counter = 0 ; counter<6 ; counter++){
saverandom = rnd.nextInt(39);
if(DontSameChoose(allimages[saverandom])){
chooseimages[counter] = saverandom;
}else {
counter--;
}
}
//put the select images in buttons
}
protected boolean DontSameChoose (int imageid){
for(int c = 0 ; c<6 ; c++){
if(imageid == chooseimages[c]){
isTheSameOk = false;
}
}
return isTheSameOk;
}
}
the first array is choose images that have the 6 id of the images and i want to save the values in the way i said above in the another array with the lenght of 12 .
If you transform your arrays into ArrayList, here is a short solution:
ArrayList<Integer> secondList = new ArrayList<>(firstList);
secondList.addAll(firstList);
Collections.shuffle(secondList);
Use this code
HashMap <Integer,Integer> mapa;
mapa = new HashMap<>();
int cont=0;
int arr[] = new int[12];
int arr2[] = new int[]{3,4,20,33,1,25};
for(int i=0;i<12;i++){
arr[i] = (int)Math.random()*100;
}
while(true){
int random = (int)Math.random()*12;
if(mapa.containsKey(random)){
}else{
mapa.put(random, 1);
arr[random] = arr2[cont++];
}
if(cont >= 6){
break;
}
}
I want to automatically disable a few buttons.
I wrote the code:
import javax.swing.*;
public int[] zamrozone = new int[4];
a1 = new JButton("A1");
a2 = new JButton("A2");
a3 = new JButton("A2");
a4 = new JButton("A2");
a5 = new JButton("A2");
private void zamroz()
{
zamrozone[0]=1;
zamrozone[1]=1;
zamrozone[2]=1;
zamrozone[3]=0;
zamrozone[4]=0;
for(int i=0; i<8; i++) //losuje 8 statkow
{
if(zamrozone[i]==1)
"a"+i.setEnabled(false); // here is an error
}
}
Unfortunately this is not working. Anyone know how to do this?
You can put the JButtons in an array and then use their index:
import javax.swing.*;
final int SIZE = 5;
JButton[] buttons = new JButton[SIZE]
for (int i=0; i<SIZE;i++) {
buttons[i] = new JButton("A" + i)
}
public int[] zamrozone = new int[SIZE];
private void zamroz()
{
zamrozone[0]=1;
zamrozone[1]=1;
zamrozone[2]=1;
zamrozone[3]=0;
zamrozone[4]=0;
for (int i=0; i<SIZE; i++) //losuje SIZE statkow
{
if (zamrozone[i]==1) {
buttons[i].setEnabled(false); // here is an error
}
}
:
}
Use defined SIZE rather then constant values all over your code to avoid OutOfBounds exception and make the code easier to change/maintain.
"a"+i.setEnabled(false); cannot work as variables don't work that way. What you are doing right there is trying to call setEnabled on the integer i and then adding the return value (which does not exist as setEnabled returns void) to the String literal "a".
I would suggest storing your buttons in an array as well and then simply calling buttonArray[i].setEnabled(false) inside the loop.
I have a for loop within my main activity that is used to randomly shuffle/reassign the values within an array at the beginning of every time the application is opened.
I was told that for loops in Android Studio could only exist within a method (after getting errors when it was placed without one), but by doing so the random shuffling of the array is not carried over to the rest of the click events that are outside of the method and it just continues to output the same originally assigned values of in the array.
int[] money = {1,10,5,2,20,500,50,100,1000000,5000,1000,50000,100000,500000,250000,10000};
int swap1;
int swap2;
int temp;
Random random = new Random();
void what(){
for (int j=0;j<16;j+=1)
{
swap1 = random.nextInt(16);
swap2 = random.nextInt(16);
temp = money[swap1];
money[swap1] = money[swap2];
money[swap2] = temp;
}
}
The click events that are using the values from this array are like this one:
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View b) {
one.setVisibility(View.INVISIBLE);
counter++;
money[0] = 0;
Context one = getApplicationContext();
CharSequence message1 = "$1";
int duration = Toast.LENGTH_LONG; //this could also be a number
final Toast one1 = Toast.makeText(one, message1, duration);
one1.show();
for (int x = 0; x < 16; x++) {
sum += money[x];
}
banker = sum / 16;
Context oney = getApplicationContext();
CharSequence message1y = "The Banker offers you $" + banker + " for your case.";
int durationy = Toast.LENGTH_LONG; //this could also be a number
final Toast one1y = Toast.makeText(oney, message1y, durationy);
one1y.show();
}
});
The aim was that if I were to click this button this time the money output was something like $500, and the next time I opened the application it would randomly shuffle and I might get a new value like $1.
You might want to check out Collections.shuffle() instead of implementing it (don't reinvent the wheel etc).
Sounds like what you want to do is call the shuffle method from inside onCreate() and onResume()
You should do the shuffle inside of the "onCreate" method.
public void onCreate(Bundle savedInstanceState){
...
int[] money = {1,10,5,2,20,500,50,100,1000000,5000,1000,50000,100000,500000,250000,10000};
int swap1;
int swap2;
int temp;
Random random = new Random();
void what(){
for (int j=0;j<16;j+=1)
{
swap1 = random.nextInt(16);
swap2 = random.nextInt(16);
temp = money[swap1];
money[swap1] = money[swap2];
money[swap2] = temp;
}
}
}
I have an array of arrays with color variable
Color [] [] bin = new Color [64] [];
afterwards I want to insert colors into this array
Im looping through a list of colors and tmpColor is the particular color in the loop. I need to insert it into the specific loop it belongs.
int idx = 16* (tmpColor.getRed()/64) + 4*(tmpColor.getGreen()/64) + (tmpColor.getBlue()/64);
bin[idx].push(tmpColor);
However this doesn't seem to work. How do I add a color into the array in the specific index?
How I solve it is the following: instead of creating an array of arrays I created and array of ArrayLists
int size = 64;
ArrayList<Color>[] lists = new ArrayList[size];
for( int i = 0; i < size; i++) {
lists[i] = new ArrayList<Color>();
}
After that I pushed the elements to their particular bin ( in this case in lists)
lists[idx].add(tmpColor);
Afterwards getting the lenght and the first color in the array is s follows:
for (ArrayList<Color> p : lists){
System.out.println("size of bin" + p.size());
if (p.isEmpty())
continue;
else {
System.out.println("list" + p.get(0));
}
}
If you need a List of colors for your index you can use a Map:
private Map<Integer, List<Color>> bin = new HashMap<Integer, List<Color>>();
int idx = 16* (tmpColor.getRed()/64) + 4*(tmpColor.getGreen()/64) + (tmpColor.getBlue()/64);
if(bin.get(idx)==null) bin.put(idx, new ArrayList<Color>());
bin.get(idx).add(tmpColor); //This should be exactly what you need
You can use also different structures like List<List<Color>> or List<Color>[]. Every structure relays on a List since List can be updated and created without knowing the initial length.
What I would do is create a separate class for color bin and some useful methods within:
public class Bin{
private List<Color> colors = new ArrayList<Color>();
public void addColor(Color col){
this.colors.add(col);
}
public List<Color> getColors(){
return this.colors;
}
public boolean hasColor(Color col){
return colors.contains(col);
}
//and so on...
}
And the best structure for your goal now is a map with lazy initialization:
private Map<Integer, Bin> myBinsOfColors = new HashMap<Integer, Bin>();
int idx = 16* (tmpColor.getRed()/64) + 4*(tmpColor.getGreen()/64) + (tmpColor.getBlue()/64);
if(myBinsOfColors.get(idx)==null) myBinsOfColors.put(idx, new Bin()); //Lazy
myBinsOfColors.get(idx).addColor(tmpColor); //This should be exactly what you need
To get the avarage and the number of colors you can implement two methods in the Bin class:
public class Bin{
private List<Color> colors = new ArrayList<Color>();
//As above.
public Color calculateAverage() {
Integer red = 0;
Integer blue = 0;
Integer green = 0;
if(!colors.isEmpty()) {
for (Color col : colors) {
red+= col.getRed();
green+= col.getGreen();
blue+= col.getBlue();
}
return new Color(red/colors.size(), green/colors.size(), blue/colors.size());
}
return null;
}
public int getColorCount(){
return this.colors.size();
}
//and so on...
}