I am trying to create a puzzle game where I have a grid of buttons that reduce their number when clicked. These numbers are held within the below array.
public int[] a = {4,4,4,4,4};
public int[] b = {4,4,4,4,4};
public int[] c = {4,4,4,4,4};
public int[] d = {4,4,4,4,4};
public int[] e = {4,4,4,4,4};
public int[] Puzzle[] = {a,b,c,d,e};
When the activity is first created I call the info from the puzzle array and set the objects in the view accordingly to the numbers in the this data array. When the buttons are pushed on the view. The numbers in the array reduce.
What I am trying to do is set up a reset button to revert the view and the numbers within this array back to the original data. When the puzzle is created I try to copy the data from Puzzle[] into savedPuzzle[] (the other array i created to store the data). When the puzzle is first started savedPuzzle[] is copying from Puzzle[] and when the puzzle is reset Puzzle[] is taking back the data from savedPuzzle[].
I have researched this problem a bit and found the most common anwsers to this were use the .clone() function so it would look like:
savedPuzzle = Puzzle.clone()
The .clone() command has not worked for me however. The system.arrarycopy() command and array.copyOf() haven't worked either.
Here is the code i am working with:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBoard();
savedPuzzle = Puzzle.clone();
}
// CHANGES VALUES DURING PUZZLE
public int[] a = {4,4,4,4,4};
public int[] b = {4,4,4,4,4};
public int[] c = {4,4,4,4,4};
public int[] d = {4,4,4,4,4};
public int[] e = {4,4,4,4,4};
public int[] Puzzle[] = {a,b,c,d,e};
// STORES THE SQUARE INFO FOR THE PUZZLE BEING PLAYED
public int[] V = {0,0,0,0,0};
public int[] W = {0,0,0,0,0};
public int[] X = {0,0,0,0,0};
public int[] Y = {0,0,0,0,0};
public int[] Z = {0,0,0,0,0};
public int[] savedPuzzle[] = {V,W,X,Y,Z};
// ASSIGNING BUTTON VIEWS TO ARRAY COORDINATES
final int[] R1 = {R.id.A1,R.id.B1,R.id.C1,R.id.D1,R.id.E1};
final int[] R2 = {R.id.A2,R.id.B2,R.id.C2,R.id.D2,R.id.E2};
final int[] R3 = {R.id.A3,R.id.B3,R.id.C3,R.id.D3,R.id.E3};
final int[] R4 = {R.id.A4,R.id.B4,R.id.C4,R.id.D4,R.id.E4};
final int[] R5 = {R.id.A5,R.id.B5,R.id.C5,R.id.D5,R.id.E5};
final int[] rAll[] = {R1,R2,R3,R4,R5};
public void setSquareColor(int x, int y){
Button theView = (Button) findViewById(rAll[x][y]);
if (Puzzle[x][y]==0){
theView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
theView.setText("");}
else if(Puzzle[x][y]==1){
theView.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
theView.setText("1");}
else if(Puzzle[x][y]==2){
theView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
theView.setText("2");}
else if(Puzzle[x][y]==3){
theView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
theView.setText("3");}
else if(Puzzle[x][y]==4){
theView.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
theView.setText("4");}
else if(Puzzle[x][y]==5){
theView.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
theView.setText("5");}
}
public void resetSquareColor(int x, int y){
Button theView = (Button) findViewById(rAll[x][y]);
if (savedPuzzle[x][y]==0){
theView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
theView.setText("s");}
else if(savedPuzzle[x][y]==1){
theView.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
theView.setText("s1");}
else if(savedPuzzle[x][y]==2){
theView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
theView.setText("s2");}
else if(savedPuzzle[x][y]==3){
theView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
theView.setText("s3");}
else if(savedPuzzle[x][y]==4){
theView.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
theView.setText("s4");}
else if(savedPuzzle[x][y]==5){
theView.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
theView.setText("s5");}}
public void resetCurrentPuzzle(View view){
Puzzle = savedPuzzle.clone();
for (int i=0; i<5; i++) {
for (int j = 0; j < 5; j++) {
resetSquareColor(i, j);
}
}}
public void setBoard() {
for (int i=0; i<5; i++) {
for (int j = 0; j < 5; j++) {
setSquareColor(i, j);
}}}
public void DoCheck(View view){
switch(view.getId()){
case R.id.A1:
ReduceOnPress(0, 0);
setSquareColor(0, 0);
break;
case R.id.A2:
ReduceOnPress(1, 0);
setSquareColor(1, 0);
break;
case R.id.A3:
ReduceOnPress(2, 0);
setSquareColor(2, 0);
break;
case R.id.A4:
ReduceOnPress(3, 0);
setSquareColor(3, 0);
break;
case R.id.A5:
ReduceOnPress(4, 0);
setSquareColor(4, 0);
break;
case R.id.B1:
ReduceOnPress(0, 1);
setSquareColor(0, 1);
break;
case R.id.B2:
ReduceOnPress(1, 1);
setSquareColor(1, 1);
break;
case R.id.B3:
ReduceOnPress(2, 1);
setSquareColor(2, 1);
break;
case R.id.B4:
ReduceOnPress(3, 1);
setSquareColor(3, 1);
break;
case R.id.B5:
ReduceOnPress(4, 1);
setSquareColor(4, 1);
break;
case R.id.C1:
ReduceOnPress(0, 2);
setSquareColor(0, 2);
break;
case R.id.C2:
ReduceOnPress(1, 2);
setSquareColor(1, 2);
break;
case R.id.C3:
ReduceOnPress(2, 2);
setSquareColor(2, 2);
break;
case R.id.C4:
ReduceOnPress(3, 2);
setSquareColor(3, 2);
break;
case R.id.C5:
ReduceOnPress(4, 2);
setSquareColor(4, 2);
break;
case R.id.D1:
ReduceOnPress(0, 3);
setSquareColor(0, 3);
break;
case R.id.D2:
ReduceOnPress(1, 3);
setSquareColor(1, 3);
break;
case R.id.D3:
ReduceOnPress(2, 3);
setSquareColor(2, 3);
break;
case R.id.D4:
ReduceOnPress(3, 3);
setSquareColor(3, 3);
break;
case R.id.D5:
ReduceOnPress(4, 3);
setSquareColor(4, 3);
break;
case R.id.E1:
ReduceOnPress(0, 4);
setSquareColor(0, 4);
break;
case R.id.E2:
ReduceOnPress(1, 4);
setSquareColor(1, 4);
break;
case R.id.E3:
ReduceOnPress(2, 4);
setSquareColor(2, 4);
break;
case R.id.E4:
ReduceOnPress(3, 4);
setSquareColor(3, 4);
break;
case R.id.E5:
ReduceOnPress(4, 4);
setSquareColor(4, 4);
break;
}
}
public void ReduceOnPress(int x, int y){
if (Puzzle[x][y] > 0){
Puzzle[x][y] -= 1;
}
}