• C

# queen's position on a chess board...!!!

I need help writing a program in C language  that will display where a queen can move on a chess board.  The user will decide where the queen will start.

Q = queen's position
M = row of queen
N = column of queen
U = character to show where queen can move = '*'
V = character to show where queen cannot move = '-'

The print out would be in 8 rows and 8 columns showing the symbols for the position of the queen, places she can move, and places she cannot move...
Any suggestions or snippets of code would be greatly appreciated..!!  Thanks..!!

###### Who is Participating?

x

Commented:
Ok, here they are one of the posibles codes, you, try to do other one

char Chess[8][8];
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
Chess[i][j]=((i==M)||(j==U)||(abs(j-U)==abs(i-M)))?'*':'-';

then you need display the array.
0

Commented:
Can you give a little more info? Do you mean you want a print out of where the queen can/can't move given a her current position on an empty board, one w/ other peices?
0

Commented:
for( i=1; i<=8; i+= 1 ){
for( j=1; j<=8; j+= 1 ){
printf("%c","-*M*NUVQ"[(j==N)<<2|(i==M)<<1|(i-M==j-N)|(M-i==j-N)]);
}
printf("\n");
}

0

Author Commented:
the printout is in relation to an empty board...the queen can move vertically, horizontally or diagonally...the user inputs the starting position of the queen.
same row, same column, diagonal = legal move
(i = m) or (j = n) or (|i - m|)= (|j - n|)
0

Commented:
Ozo's method is very ingenious (using shifts)!!!

Here is a method similar to it, but probably equally as cryptic!! :)

for( i=1; i<=8; i+= 1 ){
for( j=1; j<=8; j+= 1 ){
printf("%c","-*"[((i-M) * (j-N) * (abs(i-M) * abs(j-N))&1)];
}
printf("\n");
}

}
printf("\n");
}

0

Commented:
D'oh!!  Disregard that last } printf("\n") }

0

Commented:
ozo - that answer's too long - sure'll you can pack your code into less lines and statements :-)

Seriously just in case missq didn't get it

[(j==N)<<2|(i==M)<<1|(i-M==j-N)|(M-i==j-N)]

calculates the offset into the "-*M*NUVQ" string dependant on the row,col position being processed.
0

Commented:
scrapdog, did you mean to type
printf("%c","*-"[(i-M) * (j-N) * (abs(i-M) - abs(j-N))&&1]);
?
0

Commented:
Yes.

I usually don't use C, so I get the operators mixed up.

It is supposed to be the logical and and not the bitwise one.

(&& = logical, right?)
0

Author Commented:
I want to set this program up using "Chess"  as a character array but I don't know how...
char Chess []  //is this close...for an 8 position character     array???

This is the start of my program...as you can see, I'm no C programming Genius...!!!

/*queen.c */

#include <stdio.h>
#include <stdlib.>

char 'Q';             //queen's position
char '*';             //legal move for the queen
char '-';            //illegal move for the queen
int i;
int j;
int M;
int N;

0

Commented:
C'mon guys, don't do his homework for him.
0

Commented:
The start of the program is -almost- right.

Normally header files end with .h
So change stdlib into stdlib.h

If you want more 'hints' try to finish/complete the program and we will do 'suggestions'

0

Commented:
An array is declared like this

char Chess[] = "Q*-" ;

Like braveheart says, I don't want to do your homework for you
0

Commented:
I Agree braveheart.
it's a very easy problem, I'm sure U can do it only try

The recommended form for beginners declare a bidementional array is
char Chess[8][8]; and then put your '*' and '-' on this.
0

Author Commented:
this is what I have for my program...I don't know how to incorporate the array:  chess[]

/*queen.c */

#include <stdio.h>
#include <stdlib.h>

char 'Q'             //queen's position
char '*'             //legal move for the queen
char '-'            //illegal move for the queen
int i;
int j;
int M;
int N;

main()
{
int i;

for( i=1; i<=8; i+= 1 ){
for( j=1; j<=8; j+= 1 ){
printf("%c","-*"[((i-M) * (j-N) * (abs(i-M) * abs(j-N))&&1)]);
}

return 0;
}
}

/*Compiling QUEEN.C:
Error QUEEN.C 9: Declaration terminated incorrectly
Warning QUEEN.C 29: Function should return a value in function main() */

0

Commented:
Wowie zowie!!!

What is

char 'Q' //queen's position
char '*' //legal move for the queen
char '-' //illegal move for the queen

supposed to do?

0

Author Commented:
I thought this is how you initialize characters...!!!

char 'Q' //queen's position
char '*' //legal move for the queen
char '-' //illegal move for the queen

like integers:  int i;

I take it I'm wrong...!!!
0

Commented:
You have to give the variable a name.  Like this:

char queen = 'Q'
char legalmove = '*'
char illegalmove = '-'

Since these will be constants, you will be better off to use defines

#define QUEEN 'Q'
#define LEGAL '*'
#define ILLEGAL '-'

I'll try to get back to you with a program.
0

Commented:
#include <stdio.h>
#include <stdlib.h>

char chess[8][8];
int i;
int j;
int M=3; //enter any value for M here
int N=4; //enter any value for N here

void main()
{
int i;

for( i=1; i<=8; i++){
for( j=1; j<=8; j+= 1 ){
chess[i-1][j-1] = "-*M*NUVQ"[(j==N)<<2|(i==M)<<1|(i-M==j-N)|(M-i==j-N)];
}
}

for(i=0; i<=7; i++) {
for(j=0; j<=7; j++) printf("%c",chess[i][j]);
printf("\n");
}

}

0

Commented:
missq,

Now you have a program that more or less works, but if i were your teacher i
1st: would give you all the points, because the user cannot enter the queens starting position. With the above solution you will to recompile your program,

2nd: the proposed solution is so genious that it's hard to believe that a student who doesn't know how to initialize a character can come up with a solution using indexing in a constant array using an algarithm that combines shifting and ORs.
At least you will have to figure out HOW it works.
0

Commented:
Hi Missq,

Here is a less elaborate scheeme, that WILL work and it wont seem so advanced that your teacher will know you are cheating. (unless he/she is a member of the expert exchange.... hehehe)

Why dont you declare a bidimensional aray of integers of 17x17 and hardcode into it the posible moves of the queen assuming that the queen is on position (9,9).

ie.

int template[17][17] = {
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,
0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,
0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,
0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,
0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,
0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,
0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
}
Once this is done, all you need to do is print the overlap of the original array with the sencond array assuming that the center of the second array is on the position where the queen is suposed to be.

for (int row=1; row<=8;row++)
{
for (int col=1; col<=8; col++)
{
if (template[row + m][col + n])
{
printf("*");
}
else
{
printf("-");
}
}
printf("\n");
}

I know I'll probably get flamed for this answer, but I always look for the easiest and safest way to do this things....   and this algorithm will NEVER fail, there is no exceptions to the rule, there is no hidden problems that come and bite you in the sitter, etc.

Hope this helps,
NakedTimeMan

P.S.
I havent actually coded and run this, and you might have to "fine tune" it but the basic idea is there.  (after all you are the one that'll get the grade, right, *lol*)
0

Author Commented:
Thanks to all of you....with a variety of answers, my brain is actually working , and I'm very close to creating my own program...!!!  I'd be lost without all of you...!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.