Svar till Syndafloden [
Gå till post]:
här har ni hela koden :)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import extra.*;
public class hej extends Applet implements actionListener
{
int player=0;
Panel p = new Panel();
Button grid[] = new Button[13];
public void main(){
int ruta=0;
p.setLayout(new GridLayout(4,3)); //
setBackground(Color.green);
grid[0] = new Button("Nytt Spel");
grid[1] = new Button(" ? ");
grid[1].setVisible(false);
grid[2] = new Button(" ");
grid[2].setVisible(false);
grid[0].addActionListener(this);
grid[0].setFont(new Font("Sans Serif", Font.BOLD, 14));
p.add(grid[0]);
p.add(grid[1]);
p.add(grid[2]);
for(ruta=3;ruta<=11;ruta++)
{
grid[ruta] = new Button();
p.add(grid[ruta]);
grid[ruta].addActionListener(this);
grid[ruta].setFont(new Font("Sans Serif", Font.BOLD, 14));
grid[ruta].setLabel(" ");
add("Center", p);
}
}
public void actionpreformed(ActionEvent e)
{
grid[12] = (Button) e.getSource();
if(player == 0)
{
for(int ruta=3;ruta<=11;ruta++)
{
if(grid[12] == grid[ruta])
{
if(grid[ruta].getLabel() == " ")
{
grid[ruta].setLabel("O");
player=1;
}
}
}
}
else if(player == 1)
{
for(int ruta=3; ruta<=11; ruta )
{
if(grid[12] == grid[ruta])
{
if(grid[ruta].getLabel() = " ")
{
grid[ruta].setLabel("X");
player=0;
}
if(grid[12] == grid[0])
{
player = 0;
}
for (int ruta=3; ruta<=11; ruta++)
{
grid[ruta].setLabel(" ");
}
}
}
}
}
}