Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Connect 4 or Four in a row help

  Asked By: Anita    Date: Mar 09    Category: Java    Views: 1434
  

I'm creating a school project, the game connect 4, or four in a row.
I have everything done, except for where the program checks wether
the

lines are 4 in a row or not. Can anyone help? Here's the code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Projecto2 extends Applet implements ActionListener
{
Button botao;
boolean CanPlay = true;
Button [] [] tabela = new Button [8] [8] ;
//Button [] [] tabelaJogada = new Button [8] [8] ;

Label WhichPlayer = new Label("Please play player: X");
Label AvailableHouses = new Label("Available Houses: 63");
Label GiveSpace = new Label
(" ");

int numPlay = 0;
int PlaysLeft = 63;

public void init()
{

botao = new Button ("Restart Game");

botao.addActionListener(this);

this.setLayout(new GridLayout(2,1));


Panel p1 = new Panel ();
p1.setLayout (new FlowLayout ());
p1.add (botao);
p1.add (GiveSpace);
p1.add (WhichPlayer);
p1.add (AvailableHouses);

Panel p2 = new Panel ();
p2.setLayout (new FlowLayout ());

Panel p3 = new Panel ();
p3.setLayout (new FlowLayout ());


for (int x = 0; x<tabela.length; x++)
{
for (int y = 0; y<tabela[x].length; y++)
{
tabela [x] [y] = new Button (x+"
e "+y);
p2.add (tabela[x] [y]);
tabela [x] [y].addActionListener
(this);
}

}


this.add (p1);
this.add (p2);
this.add (p3);

}

public void actionPerformed (ActionEvent ae)
{
for (int x = 0; x < tabela.length; x++)
{
for (int y = 0; y < tabela[x].length; y++)
{
if (ae.getSource() == tabela[x] [y] )
if (CanPlay)
{
if (numPlay%2 == 0)
{
tabela[x] [y].setLabel("X");
WhichPlayer.setText("Please play player: 0");
AvailableHouses.setText("Available
Houses: "+PlaysLeft);
}

if (numPlay%2 != 0)
{
tabela[x] [y].setLabel("0");
WhichPlayer.setText("Please play player: X");
AvailableHouses.setText("Available
Houses: "+PlaysLeft);
}
numPlay++;
PlaysLeft--;
}
}


if (PlaysLeft < 0)
{
CanPlay = false;
WhichPlayer.setText("Game's Over!");
}

}

}















public void paint (Graphics g)
{
//Dimension tamanho = this.getSize();
setSize(750,600);
//g.drawLine(10, 10, tamanho.width-1, tamanho.height-
1);
}
}



Sorry if I wasn't supposed to post this here, just needed some
help....

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Connect 4 or Four in a row help Or get search suggestion and latest updates.




Tagged: