[Java] Eight queens help?
Hi. I'm a little new to Java and am in need of some help with my program, the goal is to make a program which will show all the "eight queens" solutions.
This is an example of one:
http://spaz.ca/aaron/SCS/queens/
I'm currently stuck however, and need help in getting mine working.
I've attached the source code to two variations of my program, both of which do not work at the moment, as i'm unsure of what i'm doing wrong.
If anyone can fix either of these programs, or tell me how to, then i'd appreciate it deeply.
All and any help is appreciated.
Thanks,
- Noxel
Attached Files
Originally Posted by
noxel
Hi. I'm a little new to Java and am in need of some help with my program, the goal is to make a program which will show all the "eight queens" solutions.
This is an example of one:
http://spaz.ca/aaron/SCS/queens/
I'm currently stuck however, and need help in getting mine working.
I've attached the source code to two variations of my program, both of which do not work at the moment, as i'm unsure of what i'm doing wrong.
If anyone can fix either of these programs, or tell me how to, then i'd appreciate it deeply.
All and any help is appreciated.
Thanks,
- Noxel
Maybe somone else is going to download and compile those files, but I'm not. If am willing to give you a hand though, but you'll need to post the code that's not working here on the forum, and tell what's wrong with it.
I do hope you saw the source code of the applet you posted? Did you peek under it's hood to see how it can be done?
Sorry, it's just a lot of code to paste and i just felt that it would be easier to look over in the compiler.
Here's the main program i was working on:
Code:
import java.awt.*;
import hsa.Console;
public class QueenChallenge
{
static Console c;
static int[] [] board;
static int[] [] [] solution;
static int n = 0;
static int i, j;
static int x, y;
static int placed = 0;
static int ans = 0;
public static void main (String[] args)
{
c = new Console ();
c.setColor (Color.black);
// row 1
c.drawRect (5, 5, 50, 49);
c.fillRect (50, 5, 50, 50);
c.drawRect (100, 5, 50, 49);
c.fillRect (150, 5, 50, 50);
c.drawRect (200, 5, 50, 49);
c.fillRect (250, 5, 50, 50);
c.drawRect (300, 5, 50, 49);
c.fillRect (350, 5, 50, 50);
// row 2
c.fillRect (5, 55, 45, 50);
c.drawRect (50, 55, 50, 49);
c.fillRect (101, 55, 50, 50);
c.drawRect (150, 55, 49, 49);
c.fillRect (200, 55, 50, 50);
c.drawRect (250, 55, 50, 49);
c.fillRect (300, 55, 50, 50);
c.drawRect (350, 55, 49, 49);
// row 3
c.drawRect (5, 105, 50, 49);
c.fillRect (50, 105, 50, 50);
c.drawRect (100, 105, 50, 49);
c.fillRect (150, 105, 50, 50);
c.drawRect (200, 105, 50, 50);
c.fillRect (250, 105, 50, 50);
c.drawRect (300, 105, 50, 50);
c.fillRect (350, 105, 50, 50);
// row 4
c.fillRect (5, 155, 45, 50);
c.drawRect (50, 155, 50, 49);
c.fillRect (101, 155, 50, 50);
c.drawRect (150, 155, 49, 49);
c.fillRect (200, 155, 50, 50);
c.drawRect (250, 155, 49, 49);
c.fillRect (300, 155, 50, 50);
c.drawRect (350, 155, 49, 49);
// row 5
c.drawRect (5, 205, 50, 49);
c.fillRect (50, 205, 50, 50);
c.drawRect (100, 205, 50, 49);
c.fillRect (150, 205, 50, 50);
c.drawRect (200, 205, 50, 49);
c.fillRect (250, 205, 50, 50);
c.drawRect (300, 205, 50, 49);
c.fillRect (350, 205, 50, 50);
// row 6
c.fillRect (5, 255, 45, 50);
c.drawRect (50, 255, 50, 49);
c.fillRect (101, 255, 50, 50);
c.drawRect (150, 255, 49, 49);
c.fillRect (200, 255, 50, 50);
c.drawRect (250, 255, 50, 49);
c.fillRect (300, 255, 50, 50);
c.drawRect (350, 255, 49, 49);
// row 7
c.drawRect (5, 305, 50, 49);
c.fillRect (50, 305, 50, 50);
c.drawRect (100, 305, 50, 49);
c.fillRect (150, 305, 50, 50);
c.drawRect (200, 305, 50, 49);
c.fillRect (250, 305, 50, 50);
c.drawRect (300, 305, 50, 49);
c.fillRect (350, 305, 50, 50);
// row 8
c.fillRect (5, 355, 45, 50);
c.drawRect (50, 355, 50, 49);
c.fillRect (101, 355, 50, 50);
c.drawRect (150, 355, 49, 49);
c.fillRect (200, 355, 50, 50);
c.drawRect (250, 355, 50, 49);
c.fillRect (300, 355, 50, 50);
c.drawRect (350, 355, 49, 49);
// text
Font f;
f = new Font ("Tahoma", Font.BOLD, 12);
c.setFont (f);
c.drawString ("Solution will change every 5 seconds.", 7, 425);
n = 8;
board = new int [n + 1] [n + 1];
solution = new int [5000] [n + 1] [n + 1];
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
board [i] [j] = 0;
}
}
x = 1;
Solution (1, 1);
while (x != 100)
{
int asd = c.readInt ();
int fo = 15;
int g = 20;
int h = 23;
int t = 43;
int u = 38;
int v = 28;
int w = 20;
int x = 7;
int y = 23;
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
solution [asd] [i] [j];
System.out.println (i + " " + j);
fo = fo + ((j-1) * 50);
g = g + ((j-1) * 50);
h = h + ((j-1) * 50);
t = t + ((i-1) * 50);
u = u + ((i-1) * 50);
v = v + ((i-1) * 50);
w = w + ((i-1) * 50);
x = x + ((i-1) * 50);
y = y + ((i-1) * 50);
c.setColor (Color.RED);
c.fillRect (fo, t, 25, 10);
c.fillRect (g, u, 15, 5);
c.fillRect (fo, v, 25, 10);
c.fillOval (fo, y, 25, 10);
c.fillRect (h, w, 9, 5);
c.fillOval (g, x, 15, 15);
}
}
}
}
public static int Solution (int x, int y)
{
while (y <= n && x > 0)
{
if (Check (x, y) == true)
{
board [x] [y] = 1;
placed += 1;
if (placed == n)
{
ans += 1;
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
solution [ans] [i] [j] = board [i] [j];
}
}
board [x] [y] = 0;
placed -= 1;
}
else
{
x += 1;
Solution (x, 1);
x -= 1;
board [x] [y] = 0;
placed -= 1;
}
}
y++;
}
return ans;
}
public static boolean Check (int x, int y)
{
boolean flag1 = false;
boolean flag2 = false;
for (i = 1 ; i <= n && flag1 == false ; i++)
{
if (i != x && board [i] [y] == 1)
{
flag1 = true;
}
}
if (flag1 == false)
{
for (i = 1 ; i <= n && flag2 == false ; i++)
{
for (j = 1 ; j <= n && flag2 == false ; j++)
{
if (i != x && j != y && Math.abs (i - x) == Math.abs (j - y) && board [i] [j] == 1)
{
flag2 = true;
}
}
}
}
if (flag1 == false && flag2 == false)
{
return true;
}
else
{
return false;
}
}
}
And yes, i did look over a few other source codes. But it didn't help me solve my issue.
Originally Posted by
noxel
Sorry, it's just a lot of code to paste and i just felt that it would be easier to look over in the compiler.
That's why I said to post the code that's not working, not all the code. I also suggested posting what went wrong with your current code. I (and I think a lot of people) will not be able to compile your code due to the fact you use classes from a hsa package, which are not a part of the standard Java classes.
I'm sorry, but I cannot help you this way.
Good luck.
Similar Threads
By Shaitan00 in forum Java
Replies: 1
Last Post: 02-28-2005, 09:58 AM
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
Bookmarks