Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am having an issue with my loops in a game that I created. I have a game board that is a 10 x 10 grid. It is a 3 part challenge. The first part asked for 20 random treasures, which I worked out just fine. Then the second part was adding 20 random trolls. The loop(s) I used gave me 20 treasures and 30 trolls. I AM TERRIBLE AT LOOPS. I have to stay along these lines of code and cannot use break (I am new to Java, but these are my profs rules).
private int width = 10;
private int height = 10;
//Create an array to hold buttons in a 10 by 10 grid
private EmptyButton[] buttons = new EmptyButton[width * height];
Random random = new Random();
int counter = 0;
//Using a while loop when counter is less than number of treasures still hidden
while (counter < game.getNumberStillHidden()) {
//Declare insertion variable and set to random to place buttons randomly
int insertionIndex = random.nextInt(buttons.length);
//Using conditional if button at index is null
if (buttons[insertionIndex] == null) {
//insert a treasure button
buttons[insertionIndex] = new TreasureButton(game, this);
//Use an acumulator for the counter to move on
counter++;
}
}
while (counter < game.getNumberOfTriesLeft()) {
Random randomOne = new Random();
//Declare insertion variable and set to random to place buttons randomly
int insertionIndex = randomOne.nextInt(buttons.length);
if (buttons[insertionIndex] == null) {
//insert a treasure button
buttons[insertionIndex] = new TrollButton(game, this);
//Use an acumulator for the counter to move on
counter++;
}
}
// Loop to add EmptyButton()'s into remaining null indexes within the button array
for (int index = 0; index < buttons.length; index++) {
// if the current index is null
if (buttons[index] == null) {
//add an EmptyButton()
buttons[index] = new EmptyButton(newGame, this);
}
}
// Loop to add all of the contents of the buttonGrid into the gamePanel
for (int index = 0; index < buttons.length; index++) {
//add buttons to panel
gridPanel.add(buttons[index]);
}
When you use
while (counter < game.getNumberOfTriesLeft())
the value of counter is 20 if you have 20 treasures.
You need to reset counter to 0 or use a different one.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was trying to solve a problem in a coding trainer. But, I just could not figure this problem for the life of me.
Here is the problem:
You are given an m x n 2D image matrix where each integer represents a pixel. Flip it in-place along its horizontal axis.
Example:
Input image :
1 1
0 0
Modified to :
0 0
1 1
I tried swapping rows as I traversed 2d array down the row for test case:
1,2,3
4,5,6
7,8,9
But, I end up getting
4,5,6
7,8,9
1,2,3
instead of
{{7,8,9},
{4,5,6},
{1,2,3}}
Here is the answer code.
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length - 1, c = matrix[0].length - 1;
int temp = 0;
for(int i = 0; i <= r/2; i++){
for(int j = 0; j <= c; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[r-i][j];
matrix[r-i][j] = temp;
}
}
}
I still do not understand the answer code. Specifically, why the outer loop has "i <= r/2" and the swap has "matrix[r-i]" in the index. Why r/2 and r-i? I really do not understand why and I am totally stuck.
Can someone explain those lines so I can understand the code?
Here is the expected output for test cases:
1
{{1}}
1,0,0
0,0,1
{{0,0,1},{1,0,0}}
1,0
{{1,0}}
1,2,3
4,5,6
7,8,9
{{7,8,9},{4,5,6},{1,2,3}}
1,0,1
1,0,1
{{1,0,1},{1,0,1}}
Focusing on only the double loop:
for (int i=0; i <= r/2; i++){
for (int j=0; j <= c; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[r-i][j];
matrix[r-i][j] = temp;
}
}
The outer loop in i only ranges up to (and including) one half the height of the matrix because we want to swap each array with its "mirror" image on the other side of the median row. That is, for a 3x3 matrix we want to do the following:
1,2,3 i=0
4,5,6
7,8,9 r-i=matrix.length-1 = 3-1 = 2
(swap these rows, i=i+1)
7,8,9
4,5,6 i=1, r-i=1
1,2,3
(swap the median row with itself, nothing changes)
If we were to allow the outer loop to run the full height of the input matrix, then after the median row we would actually undo the swap already done, and would just end up the original input matrix.
The number of row swaps you need to do is matrix.length/2 - 1. You could have written:
for (int i = 0; i < matrix.length/2; i++)
instead of:
for(int i = 0; i <= r/2; i++)
for matrices with an odd number of rows, matrix.length/2 and r/2 are equal, which means that in the second form, because of the <=, you will swap the middle row with itself, which is useless, so I prefer the first form.
Now the index r-i will go downwards from the index of the last row (r = matrix.length-1). It's the index of the row that must be swapped with the one at index i.
Note that the the rows themselves are array, and it would be more efficient to swap the whole rows instead of each individual element, so here is a better solution:
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length-1;
for (int i = 0; i < matrix.length/2; i++) {
int[] temp = matrix[i];
matrix[i] = matrix[r - i];
matrix[r - i] = temp;
}
}
Or:
public static void flipHorizontalAxis(int[][] matrix) {
int r = matrix.length;
for (int i = 0; i < matrix.length/2; i++) {
--r;
int[] temp = matrix[i];
matrix[i] = matrix[r];
matrix[r] = temp;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner in java and right now I'm trying to "solve" this puzzle:
http://zetcode.com/tutorials/javagamestutorial/puzzle/
Everything is clear to me, but I want to add something. As you can see, when you run it, the puzzle is already solved, so I want to add an algorithm that puts the parts of the image randomly. Any hints?
Thanks!
As you initialize the images you should put it into a List and then use Collections::shuffle to shuffle the List:
List<Image> croppedImages = new ArrayList<Image>(4 * 3);
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
croppedImages.add(createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j*width/3, i*height/4,
(width/3)+1, height/4))));
}
}
Collections.shuffle(croppedImages);
Now in the loop where you initialize the pieces:
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
if ( j == 2 && i == 3) {
label = new JLabel("");
centerPanel.add(label);
} else {
button = new JButton();
button.addActionListener(this);
centerPanel.add(button);
button.setIcon(new ImageIcon(croppedImages.get(0)));
croppedImages.remove(0);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
else if occur the case of
ArrayList of Integer ArrayList and I want remove only one index inside the ArrayList?
For Example:
1 -> [3,1,8,9]
ArrayList 2 -> [0,2,4,5]
3 -> [9,3,4,5,5,9,0]
I wish remove the first index of ArrayList 2 that is the zero (0).
If I use
ArrayList.remove(2), it will delete all first position. That is [0,2,4,5]. But I just want to remove the first position. The number 0.
How to proceed?
In this case, my block code is:
for (int i = 0; i < conta.size(); i++)
{
listaRepetida2[conta[i]].remove(conta[i+1]);
listaRepetida2[conta[i]].remove(conta[i+1]);
/*
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
*/
i+=2;
}
conta, get the index of listaRepetida[i][j] that will be remove from it.
You get the inner list which is at index 1:
List<Integer> innerList = outerList.get(1);
And then you remove its first element:
innerList.remove(0);
Or, in a single instruction:
outerList.get(1).remove(0);
First you should fetch the array you want to remove elements from. You an simply use onstruct like this:
arr.get(2).remove(0);
See it;
My code (don't remove):
import java.util.ArrayList;
public class RemoveRepetidas
{
ArrayList<ArrayList<Integer>> listaRepetida = new ArrayList<ArrayList<Integer>>();
RemoveRepetidas (ArrayList<ArrayList<Integer>> _listaRepetida)
{
this.listaRepetida = _listaRepetida;
}
String removeRepetida()
{
System.out.println(this.listaRepetida);
ArrayList<Integer> conta = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> listaRepetida2 = listaRepetida;
for (int i = 0; i < listaRepetida2.size(); i++)
{
for (int j = i+1; j < listaRepetida2.size(); j++)
{
for (int k = 0; k < listaRepetida2.get(i).size(); k++)
{
for (int l = 0; l < listaRepetida2.get(j).size(); l++)
{
if (
( listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l+1) ) ||
( listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l+1) )
)
{
conta.add(j);
conta.add(l);
}
l++;
}
k++;
}
}
}
for (int i = 0; i < conta.size(); i++)
{
//This does not remove
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
i+=2;
}
String texto = "{";
texto += "}";
System.out.println(listaRepetida2);
return texto;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to code positions in a two-dimensional array in Java with dimension of [n][n]
we have n rows and n columns, so what i want to achieve from this perspective is
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
I have to get this result ( code row and col to get the result in decimal):
(0) (1) (2)
(3) (4) (5)
(6) (7) (8)
For two-dimensional indices in matrix[i][j], and a linear index in vector[index] the following relation holds.
final int n = 3; // dimension of the columns
int i = ...
int j = ...
int index = i * n + j;
i = index / n;
j = index % n;
Integer division and modulo is used.
(n * row + column) would give you those indices. Is that what you're asking?
Here is the code for it:
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
matrix[i][j] = i*n + j;