Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Error in program

  Asked By: Techguy    Date: Jan 27    Category: Java    Views: 484
  

I’m creating a game 4 in a row or connect four and I’m having serious
plroblems solving the game. Please help me. Here’s the code


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

public class Testing12 extends Applet implements ActionListener
{
Button cleanGame;
boolean CanPlay = true;
Button [] [] tabela = new Button [8] [8] ;

Label WhichPlayer = new Label("Please Play Player: X");
Label AvailableHouses = new Label("Plays Still Available:
63");
Label GiveSpace = new Label(" ");
Label WonGame = new Label(" ");

int playNumber = 0; //integer used to see what player is
playing
int playsLeft = 63; //integer to count remainding plays
int countsLines = 0;

public void init()
{

cleanGame = new Button ("Clean Game");

cleanGame.addActionListener(this);

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


Panel p1 = new Panel ();
p1.setLayout (new FlowLayout ());
p1.add (cleanGame);
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 ());
p3.add (WonGame);


for (int x = 0; x<tabela.length; x++) //creates
the array of buttons
{ //with
labels that show their position in the board
for (int y = 0; y<tabela[x].length;
y++)
{
tabela [x] [y] = new
Button ("["+x+"]"+"["+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) //in case there are still plays in
the game available
{
if (playNumber%2 == 0) //if the remainder is
0, player X's turn to play
{
if ((tabela[x] [y]).getLabel() !=
("X")) //verifies the label of the button
{
//forcing the player to be able to
if ((tabela[x] [y]).getLabel() !=
("0")) //click a button only once
{

tabela[x] [y].setLabel("X");

while (tabela[x+1] [y].getLabel() ==
("X"));
{
countsLines++;
}



AvailableHouses.setText("Plays Still
Available: "+playsLeft);
WhichPlayer.setText("Please Play
Player: 0");
playNumber++;
playsLeft--;
}
}


}

if (playNumber%2 != 0) //if the remainder is
not 0, player 0's turn to play
{
if ((tabela[x] [y]).getLabel() !=
("0")) //verifies the label of the button
{
//forcing the player to be able to
if ((tabela[x] [y]).getLabel() !=
("X")) //click a button only once
{

tabela[x] [y].setLabel("0");


AvailableHouses.setText("Plays Still
Available: "+playsLeft);
WhichPlayer.setText("Please Play
Player: X");
playNumber++;
playsLeft--;
}
}
}

}
}


if (playsLeft < 0) // In case there are no more plays
available the game is over
{
CanPlay = false;
WhichPlayer.setText("Game's Over!");
}

}

}



public void paint (Graphics g)
{
setSize(750,600);
}

protected boolean checkHorizontal(int x, int y)
{

//String WhichPlayer;
int contador = 1;



// checks left of current position
int posicaoOff = -1;

if(IsInBounds(x+posicaoOff,y))
{
while(tabela[x+posicaoOff][y].getLabel().equals(WhichPlayer))
{
contador++;
posicaoOff--; // goes further left
if(!IsInBounds(x+posicaoOff,y)) break;

}
}

// checks right of current position
posicaoOff = 1;
if(IsInBounds(x+posicaoOff,y))
{
while(tabela[x+posicaoOff][y].getLabel().equals(WhichPlayer))
{
contador++;
posicaoOff++; // goes further right
if(!IsInBounds(x+posicaoOff,y)) break;

}
}

// checks to see if there's 4 in a row horizontally
if(contador>=4) return true;
return false;

}


// method that checks if indexes of array are inside the board
private boolean IsInBounds(int x, int y)
{
if ((x >= 0) && (x<=7))
{
if ((y >= 0) && (y<=7))
return true;

}

return false;

}
}

Share: 

 

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

 
Didn't find what you were looking for? Find more on Error in program Or get search suggestion and latest updates.




Tagged: