# Need Homework guidance, not Solution... 8 queen problem

Page 1 of 1

## 11 Replies - 2212 Views - Last Post: 12 December 2011 - 01:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258895&amp;s=b13701e1eb878036e3165e93145fa51e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

# Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 10:23 AM

Hello Everyone!!!

Ok, I just want some guidance on this. I am thinking that I should create columns with their very own queen; arrays with only one queen. And, the queens should be smart enough to mark their spots down the other columns, their rows, and diagonally.

Now here is where I run into problems; for me to be able to get the queens to mark their spots diagonally, I would have to use math to mark the correct spots. For example, if queen in column zero is in space 0 (the upper left corner on the board) then, to mark the space diagonally, I would add one to column so i am in next column then, I would add 1 to row so that I am down one space in the new column. This marks the first diagonal space as claimed property.

I would have to do this for all 8 queens, for all diagonals of the queen. This would make my program very long. Normally this wouldn't bother me, but I have seen other programs that do it in under 200 lines. I tried to decipher the other programs, but they are much too complicated for me to understand with my level of knowledge. If anyone could please give me some pointers on solving this more efficiently I would greatly appreciate it.

Is This A Good Question/Topic? 0

## Replies To: Need Homework guidance, not Solution... 8 queen problem

### #2 Robin19

Reputation: 272
• Posts: 552
• Joined: 07-July 10

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 10:51 AM

Loops.

I'm not sure how much more help I can provide for homework without feeling like I did the homework.

One thing you could try is to make the program long. You aren't paying per character for the code size. You may see ways to reduce code once it is complete. I do that myself, right verbose code and come back when it works to "clean it up" and "make it pretty (ie shorter)".

### #3 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 11:11 AM

Copy that. Guess its back to work..

Thanks

### #4 RexGrammer

• Coding Dynamo

Reputation: 183
• Posts: 785
• Joined: 27-October 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 12:09 PM

Just a little hint...

One solution to avoid repetitive code is to (at least in this kind of situation):
Use a m_th_d... (Decipher it)

Spoiler

### #5 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 12:32 PM

thanks, I was just going to use numbers to represent queens. This way the prof can see all 8 queens and their claimed spaces. i was working on doing the one column per queen but I can figure out how to loop to the next column without specifically calling it. example;
```   int i = 1;
q1[0] = 1;
int x = q1[0];

while (q1[0] == i)
{
foreach (int k in q1[i])
{
k = 1;
}
while (i <= 7)
{ // here is where i want the loop to make space 0 in the other
columns be 1 as well so that the space is denoted as being occupied by queen number 1

```

### #6 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 12:41 PM

Sorry, my one year old is sick, so he decided to take advantage of it and threw a ball at me. it hit the keyboard and submitted my reply before I could finish.

Anyways, as u can see the columns are named q1[] q2[], etc.
I can't figure out how to loop through them without being specific in calling the different columns. So I decided to just create an 8x8 array. This should let me use variables so that I may point to any space depending on what number the variable is at a certain point in the process.

### #7 RexGrammer

• Coding Dynamo

Reputation: 183
• Posts: 785
• Joined: 27-October 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 07 December 2011 - 01:02 PM

Nice logic...

But could you post your full source... (Just curious, maybe there is a way to loop through them but I'm not sure that your project is the one in my mind...)

### #8 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 09 December 2011 - 11:28 AM

I dont have much code as i am still trying to put thinking into code. so far i have 8x8 object array. I will use strng qn1, qn2 to denote where piece is at. And u will use 1,2,3 to denote spots that are taken by respective queen. Problem i havr is how to take spaces when a queen is in middle of row/column. For example qn1 is at board[3,4] i know to add to the 4 to fill in the column with 1's. But how can i go up? How do tell program that there are 4 more spaces in that column that need to be filled by 1's? Can i just 4++ and 4-- 7 times? Will this give me exception?

### #9 RexGrammer

• Coding Dynamo

Reputation: 183
• Posts: 785
• Joined: 27-October 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 09 December 2011 - 12:32 PM

Please format your forum posts a little neater so that it is more readable (use several paragraphs)

And for your problem why don't you actually try it (I think it won't)

P.S. I still didn't understand you completly (I must be an idiot)

### #10 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 10 December 2011 - 12:34 PM

Robin19, on 07 December 2011 - 10:51 AM, said:

Loops.

I'm not sure how much more help I can provide for homework without feeling like I did the homework.

One thing you could try is to make the program long. You aren't paying per character for the code size. You may see ways to reduce code once it is complete. I do that myself, right verbose code and come back when it works to "clean it up" and "make it pretty (ie shorter)".

I did it your way; I wrote out the code until it started working to a point. Then I started to notice that I can put a method here a method there.

Now, I am starting to see that all steps that the queens are taking are repetitive. So I have one method for each specific step taken. For example, I have a method for taking negative diagonals, positive diagonals, +-horizontal and +- verticals. I am starting to see how this can work.

Thanks for help. I am sure I'll be back if I cant get the array to print in a 8x8 grid in console. I have ideas, but still not ready to test them.

### #11 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 12 December 2011 - 01:23 AM

OK here is my present problem.

I created a class called Queen that has properties. I want to create a method that will loop a certain number of times.

This method will create a Queen with unique properties. Is there a way for the method to create a new object with a unique name each time it runs through?

For example, during first loop, it names the first queen Q1, then second time around it names the next object Q2, and so on. It doesnt have to be letters, I would prefer numbers. And I am not talking about the property called name.

Here is what present method looks like:

``` if (x <= 7)
{    //Here is where I want the name of the Queen object to be different every time.
Queen queen = new Queen();

queen.Name = "Q"+i;
queen.XCoordinate = x;
queen.YCoordinate = y;
queen.ClaimedSPotNumber = i;

int sameXCoord = queen.XCoordinate;
int yCoordLookFor = queen.YCoordinate;
int qnClaimNumber = queen.ClaimedSPotNumber;
object qnName = queen.Name;

FindFirstNullSpot(board, sameXCoord, yCoordLookFor, qnClaimNumber, qnName);

i++;
x++;
}

```

With this loop, the same object will be updated with new values every time the method loops. This means that I will lose the position of the last queen when a new queen is created.

The reason I want the object to have a different name is so that every time this loops I want every object to retain the last values it was given.

Also if the queen that is running presently cannot find a spot to settle on in her row, then I need to go back to the last queen and move last queen to another open spot in her respective row.

Here is code that will make last queen find another spot. I plan on using goto to return to the top of the loop when the method FindFirstNullSpot is done.

``` else
{    //Tail end of loop that looks for a null spot on board
yCoordLookFor++;
if (board[sameXCoord, yCoordLookFor] == null)
{
board[sameXCoord, yCoordLookFor] = qnName;
}
//if no spot is found in the row, then I want to execute the code below to make                              //                                        last queen look for another spot
else
{
//Here is where I would pull last queens info and plug into method below
sameXCoord = queen.XCoordinate;
yCoordLookFor = queen.YCoordinate;
qnClaimNumber = queen.ClaimedSPotNumber ;
qnName = queen.Name;

FindFirstNullSpot(board, sameXCoord, yCoordLookFor, qnClaimNumber, qnName);
// once method above is complete, then goto below will redirect loop back to top of this loop so present queen can find a spot

goto BreakTillSpotIsFound;
}

```

Will what I want be possible? Or will I have to write out the method above 8 times for the 8 queens? I did a search but found nothing that was close to what I need. Perhaps its not possible?

### #12 odiepus

Reputation: 0
• Posts: 14
• Joined: 01-November 11

## Re: Need Homework guidance, not Solution... 8 queen problem

Posted 12 December 2011 - 01:32 AM

Repost of second set of code. Not sure what happened on last post, it looked fine in preview.

```  {    //Tail end of loop that looks for a null spot on board

yCoordLookFor++;
if (board[sameXCoord, yCoordLookFor] == null)
{
board[sameXCoord, yCoordLookFor] = qnName;
}

//if no spot is found in the row, then I want to execute the code below to make last queen look for another spot

else
{
//Here is where I would pull last queens info and plug into method below
sameXCoord = queen.XCoordinate;
yCoordLookFor = queen.YCoordinate;
qnClaimNumber = queen.ClaimedSPotNumber ;
qnName = queen.Name;

FindFirstNullSpot(board, sameXCoord, yCoordLookFor, qnClaimNumber, qnName);
// once method above is complete, then goto below will redirect loop back to top of this loop so present queen can find a spot
goto BreakTillSpotIsFound;
}

```

This code will not work if can only refer to the queen object that has the latest coordinates, instead of coordinates from last queen.