Saturday 15 August 2015

java - Trouble with comparing integers -



java - Trouble with comparing integers -

i working on project cs class involves making gui game allows user play card game concentration (or memory). of code works fine, until start comparing values of each card. tell cards flip on when not have same value, remain up. have looked through code, , whenever have code:

if (card1.returnvalue() == card2.returnvalue())

it states values equal, when can see not.

here rest of code problem, appreciate help given:

//format taken handout in class import java.awt.*; import javax.swing.*; import javax.swing.timer; import java.util.*; import java.awt.event.*; public class cards extends jpanel { private static final long serialversionuid = 1l; imageicon back; actionlistener timerperformer; timer buttontimer; private int count = 0; private int turncounter = 0; private int matchcounter = 0; //randomization public static void randomize(button[] x) //http://en.wikipedia.org/wiki/fisher%e2%80%93yates_shuffle algorithm { random randombutton = new random(); (int = x.length - 1; > 0; i--) { int temp = randombutton.nextint(i + 1); button button = x[temp]; x[temp] = x[i]; x[i] = button; } } public cards() { setbackground(color.gray); //card = new imageicon(getclass().getresource("b2fv.png")); image i1 = back.getimage(); image i2 = i1.getscaledinstance(75, 100, image.scale_default); back.setimage(i2); //card faces imageicon[] faces = new imageicon[55]; //creates array of imageicons (int = 1; <= 54; i++) { faces[i] = new imageicon(getclass().getresource(i + ".png")); i1 = faces[i].getimage(); i2 = i1.getscaledinstance(75, 100, image.scale_default); faces[i].setimage(i2); } //create buttons button[] buttons = new button[54]; //creates array of buttons for(int = 0; < 54; i++) { if(i / 4 == 0) buttons[i] = new button(faces[i + 1], back, 14); if(i / 4 == 1) buttons[i] = new button(faces[i + 1], back, 13); if(i / 4 == 2) buttons[i] = new button(faces[i + 1], back, 12); if(i / 4 == 3) buttons[i] = new button(faces[i + 1], back, 11); if(i / 4 == 4) buttons[i] = new button(faces[i + 1], back, 10); if(i / 4 == 5) buttons[i] = new button(faces[i + 1], back, 9); if(i / 4 == 6) buttons[i] = new button(faces[i + 1], back, 8); if(i / 4 == 7) buttons[i] = new button(faces[i + 1], back, 7); if(i / 4 == 8) buttons[i] = new button(faces[i + 1], back, 6); if(i / 4 == 9) buttons[i] = new button(faces[i + 1], back, 5); if(i / 4 == 10) buttons[i] = new button(faces[i + 1], back, 4); if(i / 4 == 11) buttons[i] = new button(faces[i + 1], back, 3); if(i / 4 == 12) buttons[i] = new button(faces[i + 1], back, 2); if(i / 4 == 13) buttons[i] = new button(faces[i + 1], back, 1); } //listener (int = 0; < 54; i++) { buttons[i].addactionlistener(new gamelogic()); } //add randomize(buttons); (int = 0; < 54; i++) { add(buttons[i]); } //timer - handout in class timerperformer = new actionlistener() { public void actionperformed(actionevent e) { for(int = 0; < 54; i++) { buttons[i].turn = buttons[i].turn = true; buttons[i].turn(); } } }; buttontimer = new timer(500, timerperformer); buttontimer.setrepeats(false); } // end cards() public class gamelogic implements actionlistener { public void actionperformed(actionevent e) { button card1 = (button) e.getsource(); button card2 = (button) e.getsource(); if(!card1.turn && !buttontimer.isrunning()) { card1.turn(); count++; } if (card1.returnvalue() == card2.returnvalue()) { if (count > 1) { matchcounter += 1; turncounter += 1; count = 0; } } if (card1.returnvalue() != card2.returnvalue()) { if (count > 1) { buttontimer.start(); turncounter += 0; count = 0; } } } } } //format taken handout in class import javax.swing.*; public class button extends jbutton { private static final long serialversionuid = 1l; imageicon face, back; int value; boolean turn; boolean faceup; boolean facedown; public button(imageicon face, imageicon back, int value) { this.face = face; this.back = back; this.value = value; turn = true; turn(); } public void turn() { turn = !turn; if (turn) this.seticon(face); else this.seticon(back); } public boolean cardup() { homecoming faceup; } public boolean carddown() { homecoming !faceup; } public int returnvalue() { homecoming value; } }

correct. same.

button card1 = (button) e.getsource(); button card2 = (button) e.getsource();

since e.getsource() same card1 == card2.

java integer equals

No comments:

Post a Comment