Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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);
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

How do you think the payments for votes began?. LO58

Answered: 1 week ago

Question

Did you print a proof to view color and image consistency?

Answered: 1 week ago