Running my java code works in debugger but not in run. In the eclipse debugger it shows my jframes.
However when I run it, my frame doesn't appear whatsoever.
I believe I messed up my EventListener and all that.
private class ButtonHandler implements ActionListener{
JButton button;
public void actionPerformed(ActionEvent event) {
find(event.getSource());
button = buttonHolder[indexK[kCount-1]][indexJ[jCount-1]];
if((button.getIcon() == red && choice1 == 1) || (button.getIcon() == green && choice1 == 0)){
JOptionPane.showMessageDialog(null, "You chose the wrong color!");
return;
}
if(turn ==1){
if(clicks != 1){
if(button.getIcon() == red){
clicks++;
temp = button;
}else if (button.getIcon() == green){
clicks++;
temp = button;
}else{
kCount=0;
jCount=0;
}
}else{
if((button.getIcon() == green) || (button.getIcon() == red)){
//do nothing
}else if(temp.getIcon() == green && (grid[indexK[1]][indexJ[1]] == 2 || grid[indexK[1]][indexJ[1]] == 3 || grid[indexK[1]][indexJ[1]] == 4) && button.getIcon() != green && ((indexK[0]+1 ==indexK[1] && indexJ[0] == indexJ[1]) || (indexK[0] ==indexK[1] && indexJ[0]+1 == indexJ[1]) || (indexK[0] ==indexK[1] && indexJ[0]-1 == indexJ[1])) ){
temp.setIcon(null);
button.setIcon(green);
grid[indexK[1]][indexJ[1]] = 2;
if(indexK[0] == 0 || indexK[0]== 9){
grid[indexK[0]][indexJ[0]] = 4;
}else{
grid[indexK[0]][indexJ[0]] =3;
}
found = 2;
}else if(temp.getIcon() == red && (grid[indexK[1]][indexJ[1]] == 1 || grid[indexK[1]][indexJ[1]] == 3 || grid[indexK[1]][indexJ[1]] == 5) && button.getIcon() != red && ((indexK[0]+1 ==indexK[1] && indexJ[0] == indexJ[1]) || (indexK[0] ==indexK[1] && indexJ[0]+1 == indexJ[1]) || (indexK[0]-1 ==indexK[1] && indexJ[0] == indexJ[1]))){
temp.setIcon(null);
button.setIcon(red);
grid[indexK[1]][indexJ[1]] = 1;
if(indexJ[0] == 0 || indexJ[0]== 9){
grid[indexK[0]][indexJ[0]] = 5;
}else{
grid[indexK[0]][indexJ[0]] =3;
}
found = 2;
}
clicks=0;
kCount=0;
jCount=0;
}
}
}
}
}
if you don't see any problems, can you think of a reason why it might be messing up?
First, double check that you're running it in the same mode. A program made for Applet mode is not the same as Application.
If that doesn't work:
If running as an Applet:
Check that you have an init function.
If running as an Application:
Check that you define a main class in your run configuration. If you did, make sure you are running a main() method within it.
Related
I've got loop:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
I need it because I work on TicTacToe Java game.
Here is my code for check win:
static boolean checkWin(char dot) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[0][i] == dot && i == 2) {
return true;
}
if (map[1][i] == dot && i == 2) {
return true;
}
if (map[2][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
if (map[i][1] == dot && i == 2) {
return true;
}
if (map[i][2] == dot && i == 2) {
return true;
}
if (map[i][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
}
}
return false;
}
last thing that I need to refactor this method:
static boolean checkWin(char dot) {
if (map[0][0] == dot && map[0][1] == dot && map[0][2] == dot) {
return true;
}
if (map[1][0] == dot && map[1][1] == dot && map[1][2] == dot) {
return true;
}
if (map[2][0] == dot && map[2][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][0] == dot && map[2][0] == dot) {
return true;
}
if (map[0][1] == dot && map[1][1] == dot && map[2][1] == dot) {
return true;
}
if (map[0][2] == dot && map[1][2] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
return false;
}
}
I almost done it.
Last thing that i need refactor this part of code:
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
to something like
if (map[i][0] == dot && i == 2) {
return true;
}
Main question is here:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
to feel this last part:
if (map[i][0] == dot && i == 2) {
return true;
}
i need to feel this last condition with values 2 1 0
if (map[i][insert here] == dot && i == 2) {
return true;
}
Please Help.
If all you want to do is reverse the loop output, you can just invert what you're doing in the for loop
for (int i = 2; i>=0; i--){
System.out.println(i);
}
output:
2
1
0
As a quick example.
I'm making a checkwinner class that checks 4 arrays for a bingo win, and it is wayyyy too long. Any way i can shorten it? I know you can use a for loop, but I have no idea how.thank you in advance!
public static boolean checkWinner(String[][] card, String[][] card2, String[][] card3, String[][]card4) {
if ((card[0][0] == card[0][1] && card[0][1] == card[0][2] && card[0][2] == card[0][3] && card[0][3] == card[0][4]) || (card[1][0] == card[1][1] && card[1][1] == card[1][2] && card[1][2] == card[1][3] && card[1][3] == card[1][4])|| (card[2][0] == card[2][1] && card[2][1] == card[2][3] && card[2][3] == card[2][4]) || (card[3][0] == card[3][1] && card[3][1] == card[3][2] && card[3][2] == card[3][3] && card[3][3] == card[3][4])
|| (card[4][0] == card[4][1] && card[4][1] == card[4][2] && card[4][2] == card[4][3] && card[4][3] == card[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card[0][0] == card[1][0] && card[1][0] == card[2][0] && card[2][0] == card[3][0] && card[3][0] == card[4][0]) || (card[0][1] == card[1][1] && card[1][1] == card[2][1] && card[2][1] == card[3][1] && card[3][1] == card[4][1])
|| (card[0][2] == card[1][2] && card[1][2] == card[3][2] && card[3][2] == card[4][2]) || (card[0][3] == card[1][3] && card[1][3] == card[2][3] && card[2][3] == card[3][3] && card[3][3] == card[4][3])
|| (card[0][4] == card[1][4] && card[1][4] == card[2][4] && card[2][4] == card[3][4] && card[3][4] == card[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card[0][0] == card[1][1] && card[1][1] == card[3][3] && card[3][3]== card[4][4]) || (card[4][0] == card[3][1] && card[3][1] == card[1][3] && card[1][3]== card[0][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[0][1] && card2[0][1] == card2[0][2] && card2[0][2] == card2[0][3] && card2[0][3] == card2[0][4]) || (card2[1][0] == card2[1][1] && card2[1][1] == card2[1][2] && card2[1][2] == card2[1][3] && card2[1][3] == card2[1][4])
|| (card2[2][0] == card2[2][1] && card2[2][1] == card2[2][3] && card2[2][3] == card2[2][4]) || (card2[3][0] == card2[3][1] && card2[3][1] == card2[3][2] && card2[3][2] == card2[3][3] && card2[3][3] == card2[3][4])
|| (card2[4][0] == card2[4][1] && card2[4][1] == card2[4][2] && card2[4][2] == card2[4][3] && card2[4][3] == card2[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[1][0] && card2[1][0] == card2[2][0] && card2[2][0] == card2[3][0] && card2[3][0] == card2[4][0]) || (card2[0][1] == card2[1][1] && card2[1][1] == card2[2][1] && card2[2][1] == card2[3][1] && card2[3][1] == card2[4][1])
|| (card2[0][2] == card2[1][2] && card2[1][2] == card2[3][2] && card2[3][2] == card2[4][2]) || (card2[0][3] == card2[1][3] && card2[1][3] == card2[2][3] && card2[2][3] == card2[3][3] && card2[3][3] == card2[4][3])
|| (card2[0][4] == card2[1][4] && card2[1][4] == card2[2][4] && card2[2][4] == card2[3][4] && card2[3][4] == card2[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[1][1] && card2[1][1] == card2[3][3] && card2[3][3]== card2[4][4]) || (card2[4][0] == card2[3][1] && card2[3][1] == card2[1][3] && card2[1][3]== card2[0][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card3[0][0] == card3[0][1] && card3[0][1] == card3[0][2] && card3[0][2] == card3[0][3] && card3[0][3] == card3[0][4]) || (card3[1][0] == card3[1][1] && card3[1][1] == card3[1][2] && card3[1][2] == card3[1][3] && card3[1][3] == card3[1][4])|| (card3[2][0] == card3[2][1] && card3[2][1] == card3[2][3] && card3[2][3] == card3[2][4])
|| (card3[3][0] == card3[3][1] && card3[3][1] == card3[3][2] && card3[3][2] == card3[3][3] && card3[3][3] == card3[3][4])
|| (card3[4][0] == card3[4][1] && card3[4][1] == card3[4][2] && card3[4][2] == card3[4][3] && card3[4][3] == card3[4][4])) {
System.out.println("CPU CALLS BINGO! YOU LOSE!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
}
}
return true;
}
I think what you have written in the checkWinner method is quite short, because using loops will give make your code more tedious to work with and it will be a lot longer. But, I have written down the CheckWinner method using loops. I haven't ran this code in IDE. So, there maybe some bugs. I hope is helped you.
NOTE:below code is only implementing for the card method only
public static boolean checkWinner(String[][] card, String[][] card2, String[][] card3, String[][]card4) {
boolean flag = true;
//repeat this nested-loop for card2 and card3 similarly, which I haven't showed in my code.
//The below nested loop are for the condition for the 'if' condition in your code.
for(int j=0; j<5; j++) {
boolean flag1 = true;
for (int i=0; i<4; i++) {
if (card[i][j] == card[i][j+1]) {
}
else {
flag1 = false;
break;
}
}
if (flag1) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
break;
}
}
if (flag) {
int i=0, j=0;
boolean flag2 = true;
while (i<5 && j<5) {
if (i!=2 && j!=2) {
if (card[i][j]==card[i+1][j+1]) {
` }
else flag2 = false;
break;
}
i++;
j++;
}
if (flag2) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
}
else {
i=4;
j=0;
flag2 = true;
while (i<5 && j<5) {
if (i!=2 && j!=2) {
if (card[i][j]==card[i+1][j+1]) {
` }
else {
flag2 = false;
break;
}
}
i--;
j++;
}
if (flag2) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
}
}
}
return(flag);
}
I never used nested switch statement. Just wanted to know using nested switch statement is good in my code or not.
I got 3 integer variables. type and position values 0 or 1 on both and item with 0 to 6.
My code is too lengthy of this, anyway to shorten this:
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
type = spinner_type.getSelectedItemPosition();
position = spinner_pos.getSelectedItemPosition();
item = spinner.getSelectedItemPosition();
switch (adapterView.getId()) {
case R.id.cck_spinner_type:
switch (pos) {
case 0:
if(position == 0 && item == 0) {
sp_name = "cc_left";
} else if(position == 0 && item == 1){
sp_name = "cc_left1";
} else if(position == 0 && item == 2){
sp_name = "cc_left2";
}else if(position == 0 && item == 3){
sp_name = "cc_left3";
}else if(position == 0 && item == 4){
sp_name = "cc_left4";
}else if(position == 0 && item == 5){
sp_name = "cc_left5";
}else if(position == 0 && item == 6){
sp_name = "cc_left6";
}else if(position == 1 && item == 0) {
sp_name = "cc_right";
} else if(position == 1 && item == 1){
sp_name = "cc_right1";
} else if(position == 1 && item == 2){
sp_name = "cc_right2";
}else if(position == 1 && item == 3){
sp_name = "cc_right3";
}else if(position == 1 && item == 4){
sp_name = "cc_right4";
}else if(position == 1 && item == 5){
sp_name = "cc_right5";
}else if(position == 1 && item == 6){
sp_name = "cc_right6";
}
break;
case 1:
if(position == 0 && item == 0) {
sp_name = "ccL_left";
} else if(position == 0 && item == 1){
sp_name = "ccL_left1";
} else if(position == 0 && item == 2){
sp_name = "ccL_left2";
}else if(position == 0 && item == 3){
sp_name = "ccL_left3";
}else if(position == 0 && item == 4){
sp_name = "ccL_left4";
}else if(position == 0 && item == 5){
sp_name = "ccL_left5";
}else if(position == 0 && item == 6){
sp_name = "ccL_left6";
}else if(position == 1 && item == 0) {
sp_name = "ccL_right";
} else if(position == 1 && item == 1){
sp_name = "ccL_right1";
} else if(position == 1 && item == 2){
sp_name = "ccL_right2";
}else if(position == 1 && item == 3){
sp_name = "ccL_right3";
}else if(position == 1 && item == 4){
sp_name = "ccL_right4";
}else if(position == 1 && item == 5){
sp_name = "ccL_right5";
}else if(position == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
}
break;
case R.id.cck_spinner_pos:
switch (pos) {
case 0:
if(type == 0 && item == 0) {
sp_name = "cc_left";
} else if(type == 0 && item == 1){
sp_name = "cc_left1";
} else if(type == 0 && item == 2){
sp_name = "cc_left2";
}else if(type == 0 && item == 3){
sp_name = "cc_left3";
}else if(type == 0 && item == 4){
sp_name = "cc_left4";
}else if(type == 0 && item == 5){
sp_name = "cc_left5";
}else if(type == 0 && item == 6){
sp_name = "cc_left6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_left";
} else if(type == 1 && item == 1){
sp_name = "ccL_left1";
} else if(type == 1 && item == 2){
sp_name = "ccL_left2";
}else if(type == 1 && item == 3){
sp_name = "ccL_left3";
}else if(type == 1 && item == 4){
sp_name = "ccL_left4";
}else if(type == 1 && item == 5){
sp_name = "ccL_left5";
}else if(type == 1 && item == 6){
sp_name = "ccL_left6";
}
break;
case 1:
if(type == 0 && item == 0) {
sp_name = "cc_right";
} else if(type == 0 && item == 1){
sp_name = "cc_right1";
} else if(type == 0 && item == 2){
sp_name = "cc_right2";
}else if(type == 0 && item == 3){
sp_name = "cc_right3";
}else if(type == 0 && item == 4){
sp_name = "cc_right4";
}else if(type == 0 && item == 5){
sp_name = "cc_right5";
}else if(type == 0 && item == 6){
sp_name = "cc_right6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_right";
} else if(type == 1 && item == 1){
sp_name = "ccL_right1";
} else if(type == 1 && item == 2){
sp_name = "ccL_right2";
}else if(type == 1 && item == 3){
sp_name = "ccL_right3";
}else if(type == 1 && item == 4){
sp_name = "ccL_right4";
}else if(type == 1 && item == 5){
sp_name = "ccL_right5";
}else if(type == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
default:
Toast.makeText(getContext(),"ERR 208",Toast.LENGTH_SHORT).show();
}
break;
case R.id.cck_spinner:
switch (pos) {
case 0:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
break;
case 1:
if (type == 0 && position == 0) {
sp_name = "cc_left1";
} else if (type == 0 && position == 1) {
sp_name = "cc_right1";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left1";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right1";
}
break;
case 2:
if (type == 0 && position == 0) {
sp_name = "cc_left2";
} else if (type == 0 && position == 1) {
sp_name = "cc_right2";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left2";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right2";
}
break;
case 3:
if (type == 0 && position == 0) {
sp_name = "cc_left3";
} else if (type == 0 && position == 1) {
sp_name = "cc_right3";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left3";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right3";
}
break;
case 4:
if (type == 0 && position == 0) {
sp_name = "cc_left4";
} else if (type == 0 && position == 1) {
sp_name = "cc_right4";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left4";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right4";
}
break;
case 5:
if (type == 0 && position == 0) {
sp_name = "cc_left5";
} else if (type == 0 && position == 1) {
sp_name = "cc_right5";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left5";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right5";
}
break;
case 6:
if (type == 0 && position == 0) {
sp_name = "cc_left6";
} else if (type == 0 && position == 1) {
sp_name = "cc_right6";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left6";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right6";
}
break;
default:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
}
break;
}
pfKey4.setHint(sp.getString(sp_name, "not_found"));
}
So it seems sp_name simply changes based on those 3 variables and adapterView.getId() which you don't go into. From your code I believe if position is 0 then the name has left, or if 1 then right. The number at the end is item if greater than 0. And type of 0 means it starts with cc_ and type 1 means ccL_. I would make a short function to create sp_name.
String sp_name="";
// type
if(type==0)
sp_name="cc_";
else
sp_name="ccL_";
//position
if(pos==0)
sp_name+="left";
else
sp_name+="right";
//item
if(item>0)
sp_name+=(item+"");
return sp_name;
Like #deperm's answer but shorter.
return (type == 0 ? "cc_" : "ccL_") +
(pos == 0 ? "left" : "right") +
(item > 0 ? "" + item : "");
I'm knew with multi-dimensional arrays and I kind of know the basics but I'm stuck on combining them into a boolean method. In this case, I'm trying to find out how I can make this method simplified which will function as:
Comparing one array with a location (row:column) to another array (with the same location).
If all locations from both arrays match, it should return true.
The method below works with what I have for my code but I want to know the proper way on how to not "hard code" the numbers single handedly. Would I need to use a nested for loop for comparing both arrays?
Thanks for the assistance~
public static boolean gameIsWon(int[][] workingPuzzle, int[][] solvedPuzzle)
{
if (workingPuzzle[0][0] == solvedPuzzle[0][0] &&
workingPuzzle[0][1] == solvedPuzzle[0][1] &&
workingPuzzle[0][2] == solvedPuzzle[0][2] &&
workingPuzzle[0][3] == solvedPuzzle[0][3] &&
workingPuzzle[0][4] == solvedPuzzle[0][4] &&
workingPuzzle[0][5] == solvedPuzzle[0][5] &&
workingPuzzle[0][6] == solvedPuzzle[0][6] &&
workingPuzzle[0][7] == solvedPuzzle[0][7] &&
workingPuzzle[0][8] == solvedPuzzle[0][8] &&
workingPuzzle[1][0] == solvedPuzzle[1][0] &&
workingPuzzle[1][1] == solvedPuzzle[1][1] &&
workingPuzzle[1][2] == solvedPuzzle[1][2] &&
workingPuzzle[1][3] == solvedPuzzle[1][3] &&
workingPuzzle[1][4] == solvedPuzzle[1][4] &&
workingPuzzle[1][5] == solvedPuzzle[1][5] &&
workingPuzzle[1][6] == solvedPuzzle[1][6] &&
workingPuzzle[1][7] == solvedPuzzle[1][7] &&
workingPuzzle[1][8] == solvedPuzzle[1][8] &&
workingPuzzle[2][0] == solvedPuzzle[2][0] &&
workingPuzzle[2][1] == solvedPuzzle[2][1] &&
workingPuzzle[2][2] == solvedPuzzle[2][2] &&
workingPuzzle[2][3] == solvedPuzzle[2][3] &&
workingPuzzle[2][4] == solvedPuzzle[2][4] &&
workingPuzzle[2][5] == solvedPuzzle[2][5] &&
workingPuzzle[2][6] == solvedPuzzle[2][6] &&
workingPuzzle[2][7] == solvedPuzzle[2][7] &&
workingPuzzle[2][8] == solvedPuzzle[2][8] &&
workingPuzzle[3][0] == solvedPuzzle[3][0] &&
workingPuzzle[3][1] == solvedPuzzle[3][1] &&
workingPuzzle[3][2] == solvedPuzzle[3][2] &&
workingPuzzle[3][3] == solvedPuzzle[3][3] &&
workingPuzzle[3][4] == solvedPuzzle[3][4] &&
workingPuzzle[3][5] == solvedPuzzle[3][5] &&
workingPuzzle[3][6] == solvedPuzzle[3][6] &&
workingPuzzle[3][7] == solvedPuzzle[3][7] &&
workingPuzzle[3][8] == solvedPuzzle[3][8] &&
workingPuzzle[4][0] == solvedPuzzle[4][0] &&
workingPuzzle[4][1] == solvedPuzzle[4][1] &&
workingPuzzle[4][2] == solvedPuzzle[4][2] &&
workingPuzzle[4][3] == solvedPuzzle[4][3] &&
workingPuzzle[4][4] == solvedPuzzle[4][4] &&
workingPuzzle[4][5] == solvedPuzzle[4][5] &&
workingPuzzle[4][6] == solvedPuzzle[4][6] &&
workingPuzzle[4][7] == solvedPuzzle[4][7] &&
workingPuzzle[4][8] == solvedPuzzle[4][8] &&
workingPuzzle[5][0] == solvedPuzzle[5][0] &&
workingPuzzle[5][1] == solvedPuzzle[5][1] &&
workingPuzzle[5][2] == solvedPuzzle[5][2] &&
workingPuzzle[5][3] == solvedPuzzle[5][3] &&
workingPuzzle[5][4] == solvedPuzzle[5][4] &&
workingPuzzle[5][5] == solvedPuzzle[5][5] &&
workingPuzzle[5][6] == solvedPuzzle[5][6] &&
workingPuzzle[5][7] == solvedPuzzle[5][7] &&
workingPuzzle[5][8] == solvedPuzzle[5][8] &&
workingPuzzle[6][0] == solvedPuzzle[6][0] &&
workingPuzzle[6][1] == solvedPuzzle[6][1] &&
workingPuzzle[6][2] == solvedPuzzle[6][2] &&
workingPuzzle[6][3] == solvedPuzzle[6][3] &&
workingPuzzle[6][4] == solvedPuzzle[6][4] &&
workingPuzzle[6][5] == solvedPuzzle[6][5] &&
workingPuzzle[6][6] == solvedPuzzle[6][6] &&
workingPuzzle[6][7] == solvedPuzzle[6][7] &&
workingPuzzle[6][8] == solvedPuzzle[6][8] &&
workingPuzzle[7][0] == solvedPuzzle[7][0] &&
workingPuzzle[7][1] == solvedPuzzle[7][1] &&
workingPuzzle[7][2] == solvedPuzzle[7][2] &&
workingPuzzle[7][3] == solvedPuzzle[7][3] &&
workingPuzzle[7][4] == solvedPuzzle[7][4] &&
workingPuzzle[7][5] == solvedPuzzle[7][5] &&
workingPuzzle[7][6] == solvedPuzzle[7][6] &&
workingPuzzle[7][7] == solvedPuzzle[7][7] &&
workingPuzzle[7][8] == solvedPuzzle[7][8] &&
workingPuzzle[8][0] == solvedPuzzle[8][0] &&
workingPuzzle[8][1] == solvedPuzzle[8][1] &&
workingPuzzle[8][2] == solvedPuzzle[8][2] &&
workingPuzzle[8][3] == solvedPuzzle[8][3] &&
workingPuzzle[8][4] == solvedPuzzle[8][4] &&
workingPuzzle[8][5] == solvedPuzzle[8][5] &&
workingPuzzle[8][6] == solvedPuzzle[8][6] &&
workingPuzzle[8][7] == solvedPuzzle[8][7] &&
workingPuzzle[8][8] == solvedPuzzle[8][8]
)
return true;
else
return false;
}
You need to learn about loops.
And about storing a two-dimensional matrix in a one-dimensional array using index mapping (which is easier to handle than a two-dimensional array).
But in this particular case:
return java.util.Arrays.deepEquals(workingPuzzle, solvedPuzzle);
Use nested for loops based on the array lengths.
public static boolean gameIsWon(int[][] workingPuzzle, int[][] solvedPuzzle) {
if(workingPuzzle.length != solvedPuzzle.length) {
return false;
}
for(int i = 0; i < workingPuzzle.length; i++) {
if(workingPuzzle[i].length != solvedPuzzle[i].length) {
return false;
}
for(int j = 0; j < workingPuzzle[i].length; j++) {
if(workingPuzzle[i][j] != solvedPuzzle[i][j]) {
return false;
}
}
}
return true;
}
I have been trying to solve the Wildcard pattern matching and I have found a working solution in the below link:
http://www.geeksforgeeks.org/wildcard-character-matching/
I have written an Equivalent java code for the C code given in the above link, which is:
public class wildcard
{
public static void main(String[] args)
{
test("g*ks", "geeks");
//test("g*k", "gee");
//test("c*d*", "cad");
}
static boolean matches(String format, String data)
{
if (format.length() == 0 && data.length() == 0)
{
return true;
}
if ((format.charAt(0) == '*' && format.charAt(1) != 0 && data.length() == 0) && (format.length() != 0 && data.length() != 0))
{
return false;
}
if ((format.charAt(0) == '?' || format.charAt(0) == data.charAt(0)) && (format.length() != 0 && data.length() != 0))
{
return matches(format.substring(1), data.substring(1));
}
if ((format.charAt(0) == '*')&& (format.length() != 0 && data.length() != 0))
{
return matches(format.substring(1), data) || matches(format, data.substring(1));
}
return false;
}
static void test(String first, String second)
{
System.out.println(matches(first, second));
}
}
On Execution, a String out of bounds Exception is thrown, which is:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at wildcard.matches(wildcard.java:29)
at wildcard.matches(wildcard.java:36)
at wildcard.matches(wildcard.java:42)
at wildcard.matches(wildcard.java:42)
at wildcard.matches(wildcard.java:36)
at wildcard.test(wildcard.java:52)
at wildcard.main(wildcard.java:16)
I can see that this exception occurs at the CharAt() method, is there any other way to make this work??
:)
First I noticed that the code && (format.length() != 0 && data.length() != 0) is not on the original code from the page you provide and are causing conflicts with your algorithm so I remove it and it looks like this:
static boolean matches(String format, String data) {
if (format.length() == 0 && data.length() == 0) {
return true;
}
if (format.charAt(0) == '*' && format.charAt(1) != 0 && data.length() == 0) {
return false;
}
if (format.charAt(0) == '?' || format.charAt(0) == data.charAt(0)) {
return matches(format.substring(1), data.substring(1));
}
if (format.charAt(0) == '*') {
return matches(format.substring(1), data) || matches(format, data.substring(1));
}
return false;
}
The problems I talked about are in the second condition you have data.length() == 0 and in the code I remove was data.length() != 0, so that condition was always false because the length cannot be 0 and not 0 at the same time.
I discovered two problems. The first one was that some cases like matches("ge?ks*", "geeksforgeeks") end up with having a single wildcard at the format and crashed on if (format.charAt(0) == '*' && format.charAt(1) != 0 && data.length() == 0) because of format.charAt(1). To solve this I add the next code to the algorithm:
if (format.length() == 1 && format.charAt(0) == '*')
return true;
The other problem was when the format was empty but still have data and solve it with:
if (format.length() == 0 && data.length() > 0)
return false;
The final algorithm looks like this:
static boolean matches(String format, String data) {
if (format.length() == 0 && data.length() == 0)
return true;
if (format.length() == 1 && format.charAt(0) == '*')
return true;
if (format.length() == 0 && data.length() > 0)
return false;
if (format.charAt(0) == '*' && format.charAt(1) != 0 && data.length() == 0)
return false;
if (format.charAt(0) == '?' || format.charAt(0) == data.charAt(0))
return matches(format.substring(1), data.substring(1));
if (format.charAt(0) == '*')
return matches(format.substring(1), data) || matches(format, data.substring(1));
return false;
}