java - Lights out game using 2d arrays -


i'm trying make lights out game, simplified change 1 tile @ time i'm still encountering 1 bug. can click change yellow black can't click , have opposite happen. here game if unfamiliar: http://www.logicgamesonline.com/lightsout/

my code:

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*;  public class lightsoutpanel extends jpanel implements mouselistener {     private boolean[][] lights;      public static void main(string[] args) throws exception {         jframe frame = new jframe();         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.settitle("lights out!");         frame.setresizable(false);         frame.setvisible(true);          lightsoutpanel panel = new lightsoutpanel();         if (panel.lights == null) {             system.out.println("you did not initialize light array!" +                                "it's still null...");             system.exit(-1);         }         panel.addmouselistener(panel);         panel.setpreferredsize(new dimension(601, 501));         panel.setminimumsize(new dimension(601, 501));          container c = frame.getcontentpane();         c.setlayout(new borderlayout());         c.add(panel, borderlayout.center);          frame.pack();     }      public lightsoutpanel() {         lights = new boolean[5][6];         (int = 0; < 5; i++) {             (int j = 0; j < 6; j++) {                 lights[i][j] = true;             }         }      }      // unused methods     public void mouseclicked(mouseevent e) {}      public void mousereleased(mouseevent e) {}      public void mouseentered(mouseevent e) {}      public void mouseexited(mouseevent e) {}      public void paint(graphics g) {         int boxwidth = 600 / 6;         int boxheight = 500 / 5;          int y = 0;         (int row = 0; row < 5; row++) {             int x = 0;             (int col = 0; col < 6; col++) {                 if (lights[row][col]==true) {                     g.setcolor(color.yellow);                 } else {                     g.setcolor(color.black);                 }                 g.fillrect(x, y, boxwidth, boxheight);                  g.setcolor(color.blue);                 g.drawrect(x, y, boxwidth, boxheight);                 x += boxwidth;             }             y += boxheight;         }     }      // called when mouse pressed - determines row/column user     // has clicked     public void mousepressed(mouseevent e) {         int mousex = e.getx();         int mousey = e.gety();          int panelwidth = getwidth();         int panelheight = getheight();          int boxwidth = panelwidth / lights[0].length;         int boxheight = panelheight / lights.length;          int col = mousex / boxwidth;         int row = mousey / boxheight;          toggle(row, col);         repaint();     }      // called "toggle" selected row , column, 4     // adjacent lights     public void toggle(int row, int col) {          if (row >= 0 && col >= 0 && row < lights.length &&              col < lights[0].length)         {             if (lights[row][col] = true) {                 lights[row][col] = false;             } else {                 lights[row][col] = true;             }         }      } } 

your "core" problem in toggle method...

if (lights[row][col] = true) { 

= assignment, saying assign true lights[row][col] , if lights[row][col] == true following...so it's true.

a simpler method do....

lights[row][col] = !lights[row][col]; 

no, if's or else's, nice simple.

you should have a @ painting in awt , swing , performing custom painting. you've violate paint method chain contract (by not calling super.paint)

instead of overriding paint, should using paintcomponent , should calling super.paintcomponent before custom painting, ensure graphics context nicely prepared you


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -