Question: Can someone help me fix my dots and boxes program. I need the counter to actually work, the quit button acts as a forefit button,

Can someone help me fix my dots and boxes program. I need the counter to actually work, the quit button acts as a forefit button, when square should be filled, the inside isn't by the player who did it. Find any bugs in my code and please fix to make it proper: Here is the big class that contians most of the game logic, the other 4 are shown in the pictures: public class Grid extends JPanel implements MouseListener {
int height =480;
int width =640;
int rows =3;
int columns =3;
int circleRows = rows +1;
int circleColumns = columns +1;
int area = circleRows * circleColumns;
int firstPlayerMatches;
int secondPlayerMatches;
ArrayList circles;
ArrayList lines;
ArrayList squares;
MCircle firstCircle = null;
MCircle secondCircle = null;
Color currentPlayerColor = Color.blue;
public Grid(){
addMouseListener(this);
circles = new ArrayList>(area);
lines = new ArrayList>();
squares = new ArrayList>();
int xStart =(width -(circleColumns -1)*80)/2;
int yStart =(height -(circleRows -1)*80)/2;
int x = xStart;
int y = yStart;
for (int i =0; i circleRows; i++){
for (int j =0; j circleColumns; j++){
circles.add(new MCircle(x, y));
x +=80;
}
y +=80;
x = xStart;
}
setPreferredSize(new Dimension(width, height));
setBackground(Color.white);
}
public void mouseClicked(MouseEvent mouse){
int x = mouse.getX();
int y = mouse.getY();
for (MCircle circle : circles){
if (circle.contains(x, y)){
if (firstCircle == null){
firstCircle = circle;
firstCircle.setColor(currentPlayerColor);
} else if (secondCircle == null && circle != firstCircle && firstCircle.isAdjacent(circle)){
secondCircle = circle;
secondCircle.setColor(currentPlayerColor);
MLine line = new MLine(firstCircle, secondCircle, currentPlayerColor);
lines.add(line);
boolean boxFormed = checkForBox(line);
if (!boxFormed){
switchPlayer();
}
firstCircle = null;
secondCircle = null;
repaint();
}
break;
}
}
}
private void switchPlayer(){
currentPlayerColor =(currentPlayerColor == Color.blue)? Color.red : Color.blue;
}
private boolean checkForBox(MLine newLine){
boolean boxFormed = false;
for (MCircle circle : circles){
int x = circle.xLoc;
int y = circle.yLoc;
MLine[] possibleLines ={
new MLine(circle, getCircleAt(x +80, y), currentPlayerColor),
new MLine(circle, getCircleAt(x, y +80), currentPlayerColor),
new MLine(getCircleAt(x +80, y), getCircleAt(x +80, y +80), currentPlayerColor),
new MLine(getCircleAt(x, y +80), getCircleAt(x +80, y +80), currentPlayerColor)
};
if (lines.contains(possibleLines[0]) && lines.contains(possibleLines[1]) &&
lines.contains(possibleLines[2]) && lines.contains(possibleLines[3])){
MSquare square = new MSquare(possibleLines[0], possibleLines[1], possibleLines[2], possibleLines[3]);
square.setColor(currentPlayerColor);
squares.add(square);
boxFormed = true;
if (currentPlayerColor == Color.blue){
firstPlayerMatches++;
} else {
secondPlayerMatches++;
}
}
}
return boxFormed;
}
private MCircle getCircleAt(int x, int y){
for (MCircle circle : circles){
if (circle.xLoc == x && circle.yLoc == y){
return circle;
}
}
return null;
}
public void mousePressed(MouseEvent mouse){}
public void mouseReleased(MouseEvent mouse){}
public void mouseEntered(MouseEvent mouse){}
public void mouseExited(MouseEvent mouse){}
public void paintComponent(Graphics brush){
super.paintComponent(brush);
for (MCircle circle : circles){
circle.draw((Graphics2D) brush);
}
for (MLine line : lines){
line.draw((Graphics2D) brush);
}
for (MSquare square : squares){
square.draw((Graphics2D) brush);
}
brush.setFont(new Font("Monospaced", Font.BOLD, 25));
brush.setColor(Color.blue);
brush.drawString("Player 1 Score: "+ firstPlayerMatches, 10,40);
brush.setColor(Color.red);
 Can someone help me fix my dots and boxes program. I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!