lights out game

hi i'm new in java and i have a problem with a game called "lights out".

can someone help me?i don't now what to do. Thanks!

this is the code.the message i get is "Exception in thread "main" java.lang.NoSuchMethodError:main" and if change "public void init" to "public static void main(String[] args)" i get other errors.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class lightsout extends Applet

implements ActionListener, MouseListener {

// Variables. Note that the vector arrays are boolean,

// represented by trues and falses. So true is on.

TextField Difficulty = new TextField("0", 3);

Button Restart = new Button("Restart");

Button Generate = new Button("New");

Button Hint = new Button("Hint");

TextField Moves = new TextField("0", 3);

boolean finished = false;

int MinNumber, movesleft, movecount = 0;

boolean[] InitialVectorArray = new boolean[36];

boolean[] CurrentVectorArray = new boolean[36];

boolean[] ZeroArray = new boolean[36];

boolean[] WinningArray = new boolean[36];

int[] InitialButtonArray = new int[36];

int[] CurrentButtonArray = new int[150];

// The program starts here.

public void init () {

// Creates prompts at top of applet.

add(new Label("Difficulty (0-36):"));

add(Difficulty);

Difficulty.addActionListener(this);

add(Restart);

Restart.addActionListener(this);

add(Generate);

Generate.addActionListener(this);

add(new Label("Number of Moves Made:"));

Moves.setEditable(false);

add(Moves);

add(Hint);

Hint.addActionListener(this);

addMouseListener(this);

}

// Redraws buttons (called by "repaint();").

public void paint (Graphics g) {

finished = true;

for(int j=0; j<36; j++) {

if(CurrentVectorArray[j]!=ZeroArray[j])

finished = false;

}

if(!finished || movecount>MinNumber)

for(int i=0; i<36; i++)

if(CurrentVectorArray) {

g.setColor(new Color(255, 96, 0));

g.fillRect(5 + 45*(i%6), 70 + 25*(i/6), 40, 20);

}

else {

g.setColor(new Color(0, 0, 170));

g.fillRect(5 + 45*(i%6), 70 + 25*(i/6), 40, 20);

}

else

for(int i=0; i<36; i++)

if(WinningArray) {

g.setColor(new Color(255, 96, 0));

g.fillRect(5 + 45*(i%6), 70 + 25*(i/6), 40, 20);

}

else {

g.setColor(new Color(0, 0, 170));

g.fillRect(5 + 45*(i%6), 70 + 25*(i/6), 40, 20);

}

}

// Activated when the user enters a number in the prompt

// or pushes a button at the top.

public void actionPerformed (ActionEvent e) {

// Gets difficulty level if changed.

if(e.getSource()==Difficulty)

MinNumber = Integer.parseInt(Difficulty.getText());

// This long section, continuing at this indentation,

// generates a new board.

if(e.getSource()==Difficulty || e.getSource()==Generate) {

// Initializing arrays.

for(int i = 0; i<36; i++) {

ZeroArray = false;

InitialVectorArray = false;

if(i!=7 && i!=10 && i!=18 && i!=23 && i!=25 && i!=26 && i!=27 && i!=28)

WinningArray = false;

else

WinningArray = true;

}

if(MinNumber>=1 && MinNumber<=36) {

// "Filling" the vector array.

for(int i = 0; i<MinNumber; i++) {

// Generating a random list of buttons for initial configuration.

boolean done = false;

while(!done) {

done = true;

InitialButtonArray = (int)(36.0*Math.random());

for(int j = 0; j><i; j++)

if(InitialButtonArray==InitialButtonArray[j])

done = false;

}

// From the list made above, each of these buttons is "pushed."

// The if statements make sure that the surrounding buttons

// which will be changed are on the board, and not over the edge.

int k = InitialButtonArray;

InitialVectorArray[k] = !InitialVectorArray[k];

if(k%6>0)

InitialVectorArray[k-1] = !InitialVectorArray[k-1];

if(k%6<5)

InitialVectorArray[k+1] = !InitialVectorArray[k+1];

if(k/6>0)

InitialVectorArray[k-6] = !InitialVectorArray[k-6];

if(k/6<5)

InitialVectorArray[k+6] = !InitialVectorArray[k+6];

}

}

}

// Gives a hint, then redraws.

if(e.getSource()==Hint) {

movesleft--;

movecount++;

Moves.setText(Integer.toString(movecount));

while(movesleft>=0 && CurrentButtonArray[movesleft]==-1)

movesleft--;

if(movesleft<0)

movesleft = 0;

else {

// Changes the states around hinted button.

int k = CurrentButtonArray[movesleft];

CurrentVectorArray[k] = !CurrentVectorArray[k];

if(k%6>0)

CurrentVectorArray[k-1] = !CurrentVectorArray[k-1];

if(k%6<5)

CurrentVectorArray[k+1] = !CurrentVectorArray[k+1];

if(k/6>0)

CurrentVectorArray[k-6] = !CurrentVectorArray[k-6];

if(k/6<5)

CurrentVectorArray[k+6] = !CurrentVectorArray[k+6];

CurrentButtonArray[movesleft] = -1;

}

repaint();

}

else {

// Now we set the move counter back to zero, set the current light

// configuration to the inital one, and redraw the buttons. Note that if

// the restart button was pressed, a new initial configuration was not

// generated, thus restoring the inital state from memory.

finished = false;

movesleft = MinNumber;

movecount = 0;

Moves.setText("0");

for(int i=0; i<MinNumber; i++)

CurrentButtonArray = InitialButtonArray;

for(int i=0; i><36; i++)

CurrentVectorArray = InitialVectorArray;

repaint();

}

}

// Surprise, surprise--this is activated by mouse clicks.

public void mouseClicked (MouseEvent e) {

// Find and record position of "button" pressed, where k=-1 when a place

// other than a "button" was clicked.

if(!finished) {

int k = -1;

for(int i=0; i<36; i++)

if(5 + 45*(i%6) <= e.getPoint().x

&& e.getPoint().x <= 45 + 45*(i%6)

&& 70 + 25*(i/6) <= e.getPoint().y

&& e.getPoint().y <= 90 + 25*(i/6))

k = i;

if(k!=-1) {

// Change five states, again checking for existence along edges.

CurrentVectorArray[k] = !CurrentVectorArray[k];

if(k%6>0)

CurrentVectorArray[k-1] = !CurrentVectorArray[k-1];

if(k%6<5)

CurrentVectorArray[k+1] = !CurrentVectorArray[k+1];

if(k/6>0)

CurrentVectorArray[k-6] = !CurrentVectorArray[k-6];

if(k/6<5)

CurrentVectorArray[k+6] = !CurrentVectorArray[k+6];

// We check to see, for hint purposes, whether the button was in its

// "correct state". If it wasn't, the button should be removed from

// the ButtonArray, and the player is closer to his goal. Otherwise,

// the user has moved farther away than necessary, and it needs to be

// added to the ButtonArray list.

boolean inlist=false;

for(int j=0; j<movesleft; j++) {

if(CurrentButtonArray[j]==k) {

CurrentButtonArray[j]=-1;

inlist=true;

}

}

if(!inlist) {

CurrentButtonArray[movesleft]=k;

movesleft++;

}

// Increment move counter and redraw puzzle.

movecount++;

Moves.setText(Integer.toString(movecount));

repaint();

}

}

}

public void mousePressed (MouseEvent e) {}

public void mouseReleased (MouseEvent e) {}

public void mouseEntered (MouseEvent e) {}

public void mouseExited (MouseEvent e) {}

}>

[8033 byte] By [daredevilgra] at [2007-11-27 7:28:55]
# 1

What you have there is an applet. If you want to turn it into a standalone app, you have to convert it. Adding a main() method is a good first step, but it's just the first step. You're going to have to do more stuff, such as fixing the errors if you rename init().

However, rather than renaming init() to main(), it might make more sense to create a new main() method, that would call init(), among other things.

There are a variety of ways you can turn an applet into a standalone app, or vice-versa. Generally, I suggest abstracting the functionality into a class that doesn't know what kind of environment it's in, and then provide applet or app wrappers. However, that sounds like it would be beyond your skills at the moment, so let's try something easier.

First, add this main() method:

public static void main(String[] args) {

Frame f = new Frame();

lightsout lo = new lightsout();

lo.init();

f.add(lo, BorderLayout.CENTER);

f.pack();

f.setVisible(true);

}

I'm not a GUI expert but I think that will do it.

There will be errors. You will have to fix those errors. That's the programming game.

When you post code, you should wrap it in [code][/code] tags. What you have now is impossible to read.

Your code has other problems, but we'll worry about those later.

paulcwa at 2007-7-12 19:09:04 > top of Java-index,Java Essentials,Java Programming...