Answered step by step
Verified Expert Solution
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 are shown in the pictures: public class Grid extends JPanel implements MouseListener
int height ;
int width ;
int rows ;
int columns ;
int circleRows rows ;
int circleColumns columns ;
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
addMouseListenerthis;
circles new ArrayListarea;
lines new ArrayList;
squares new ArrayList;
int xStart width circleColumns ;
int yStart height circleRows ;
int x xStart;
int y yStart;
for int i ; i circleRows; i
for int j ; j circleColumns; j
circles.addnew MCirclex y;
x ;
y ;
x xStart;
setPreferredSizenew Dimensionwidth height;
setBackgroundColorwhite;
public void mouseClickedMouseEvent mouse
int x mouse.getX;
int y mouse.getY;
for MCircle circle : circles
if circlecontainsx y
if firstCircle null
firstCircle circle;
firstCircle.setColorcurrentPlayerColor;
else if secondCircle null && circle firstCircle && firstCircle.isAdjacentcircle
secondCircle circle;
secondCirclesetColorcurrentPlayerColor;
MLine line new MLinefirstCircle secondCircle currentPlayerColor;
lines.addline;
boolean boxFormed checkForBoxline;
if boxFormed
switchPlayer;
firstCircle null;
secondCircle null;
repaint;
break;
private void switchPlayer
currentPlayerColor currentPlayerColor Color.blue Color.red : Color.blue;
private boolean checkForBoxMLine newLine
boolean boxFormed false;
for MCircle circle : circles
int x circle.xLoc;
int y circle.yLoc;
MLine possibleLines
new MLinecircle getCircleAtx y currentPlayerColor
new MLinecircle getCircleAtx y currentPlayerColor
new MLinegetCircleAtx y getCircleAtx y currentPlayerColor
new MLinegetCircleAtx y getCircleAtx y currentPlayerColor
;
if linescontainspossibleLines && lines.containspossibleLines &&
lines.containspossibleLines && lines.containspossibleLines
MSquare square new MSquarepossibleLines possibleLines possibleLines possibleLines;
square.setColorcurrentPlayerColor;
squares.addsquare;
boxFormed true;
if currentPlayerColor Color.blue
firstPlayerMatches;
else
secondPlayerMatches;
return boxFormed;
private MCircle getCircleAtint x int y
for MCircle circle : circles
if circlexLoc x && circle.yLoc y
return circle;
return null;
public void mousePressedMouseEvent mouse
public void mouseReleasedMouseEvent mouse
public void mouseEnteredMouseEvent mouse
public void mouseExitedMouseEvent mouse
public void paintComponentGraphics brush
super.paintComponentbrush;
for MCircle circle : circles
circle.drawGraphicsD brush;
for MLine line : lines
line.drawGraphicsD brush;
for MSquare square : squares
square.drawGraphicsD brush;
brush.setFontnew FontMonospaced Font.BOLD, ;
brush.setColorColorblue;
brush.drawStringPlayer Score: firstPlayerMatches, ;
brush.setColorColorred;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started