Java Applet Help: Coding a Dots and Boxes Game Variant

  • Comp Sci
  • Thread starter Firestrider
  • Start date
  • Tags
    Java
In summary, the code is trying to paint out 64 dots, but only paints out one. It is also trying to have a MouseListener on each dot so when you enter/exit the dot the color will change, and when you click on the dot the color will stay and the MouseListener will be removed. However, it is not clear how to write code to check for adjacentity.
  • #1
Firestrider
104
0

Homework Statement



To code a dots and boxes game variant. Basically I need to code the same game except instead of drawing lines you choose two dots to form a line. I would need MouseListener for clicking and entering/exiting dots, a graphics update for changing colors of the dots and forming lines, a score tracker for "boxes" completed, and I guess an adjacent dot checker for creating boxes.

Homework Equations



N/A

The Attempt at a Solution



Can someone tell me what is wrong with this code? I want it to paint out 64 dots, but for some reason it will only paint out one.

Also I'm trying to figure out a way to have a MouseListener on each Dot so when you enter/exit the dot the color will change, and when you click on the dot the color will stay and the MouseListener will be removed. The color would be based on the current player. I'm not sure how to do this because you can't have a mouseClicked(MouseEvent e) inside the paint(Graphics g).

Here is the relevant code:
Code:
	public void paint(Graphics g)
	{
		Dot[] dots = new Dot[63];
		for(int i = 0; i < dots.length; i++)
		{
			for(int j = 25; j < 400; j += 50)
			{
				for(int k = 25; k < 400; k += 50)
				{
					dots[i] = new Dot(j, k);
				}
			}
		}		
		for(Dot d : dots)
		{
			g.setColor(Color.black);
			g.fillOval(d.getX(), d.getY(), 10, 10);
		}

Code:
public class Dot
{
	private int x;
	private int y;
	
	public Dot(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
}

Also any alternative pathways to get this problem done would be helpful. I know exactly what I want to be done but I've lost on how to implement it all in code :\
 
Last edited:
Physics news on Phys.org
  • #2
Ok I figured out most of that, but I still don't know how I can have a check to see if a Dot is adjacent to each other.

I have a for iterator that loops through all the dots: "For (Dot d : Dot)" and I have the methods getX(), getY(), and getColor() for the Dots but I'm not sure exactly how to write it in code to check for adjacentcy.

A line should be drawn if the dots have the same X or Y coordinate and are 50 pixels away from each other in the other coordinate.

I was thinking this condition:

If(d.getX() == d.getX() && Math.abs(d.getY() - d.getY())== 0)

Any help please?
 
  • #3
Try:

for(int i=0;i<63;i++) for(int j=i+1;j<63;j++)
{
if((d.getX()==d[j].getX() && java.lang.Math.abs(d.getY()-d[j].getY())==50) ||
(d.getY()==d[j].getY() && java.lang.Math.abs(d.getX()-d[j].getX())==50) )
{ ... whatever you'd like to do to join the two adjacent dots ...}
}
 
  • #4
OK Thanks a bunch man

This is what I ended up using

Code:
		for(int i = 0; i < 63; i++)
		{
			for(int j = i + 1; j < 63; j++)
			{
				boolean notBlack = (Dot[i].getColor() != Color.black) && (Dot[j].getColor() != Color.black);
				boolean sameX = Dot[i].getX() == Dot[j].getX();
				boolean sameY = Dot[i].getY() == Dot[j].getY();
				boolean adjacentX = (Dot[i].getX() - Dot[j].getX() == 50) || (Dot[i].getX() - Dot[j].getX() == -50);
				boolean adjacentY = (Dot[i].getY() - Dot[j].getY() == 50) || (Dot[i].getY() - Dot[j].getY() == -50);
				
				if((sameX && adjacentY && notBlack) || (sameY && adjacentX && notBlack))
				{
					g.drawLine(Dot[i].getX() + 5, Dot[i].getY() + 5, Dot[j].getX() + 5, Dot[j].getY() + 5);
				}
			}
		}

Now I got to figure out how to check for "boxes"
 
  • #5
How do you define a box, four points forming a square 50 pixels apart?
 

What is a Java applet?

A Java applet is a small program written in the Java programming language that is embedded into a web page. Applets run within a web browser and can provide interactive features and animations.

How do I code a Dots and Boxes game variant using Java applets?

To code a Dots and Boxes game variant using Java applets, you will need to have a basic understanding of Java programming language and the applet framework. You can find tutorials and resources online to help you get started. It is also helpful to have a clear understanding of the game rules and logic before beginning the coding process.

What are some key features to include in a Dots and Boxes game applet?

Some key features to include in a Dots and Boxes game applet are a user-friendly interface, the ability to track and display scores, and the option for multiplayer or single-player mode. It is also important to include error handling and validation to ensure the game runs smoothly.

How can I ensure my Dots and Boxes game applet is compatible with different web browsers?

To ensure compatibility with different web browsers, it is important to test your applet on various browsers and make any necessary adjustments. It is also helpful to follow coding best practices and use standard Java libraries to ensure maximum compatibility.

Are there any security concerns with using Java applets?

Yes, there have been security concerns with using Java applets in the past. It is important to keep your Java applet updated and use trusted sources for your code. You may also consider using alternative technologies for web development, such as HTML5 and JavaScript.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top