Solved

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

Posted on 1998-10-03
879 Views
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..!!

0
Question by:missq
• 6
• 5
• 2
• +6

LVL 1

Expert Comment

ID: 1253258
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

LVL 84

Expert Comment

ID: 1253259
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 Comment

ID: 1253260
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

LVL 5

Expert Comment

ID: 1253261
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

LVL 5

Expert Comment

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

0

LVL 8

Expert Comment

ID: 1253263
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

LVL 84

Expert Comment

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

LVL 5

Expert Comment

ID: 1253265
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 Comment

ID: 1253266
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

LVL 3

Expert Comment

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

LVL 3

Expert Comment

ID: 1253268
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

LVL 8

Expert Comment

ID: 1253269
An array is declared like this

char Chess[] = "Q*-" ;

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

Expert Comment

ID: 1253270
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

Accepted Solution

Bayo earned 250 total points
ID: 1253271
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

Author Comment

ID: 1253272
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

LVL 5

Expert Comment

ID: 1253273
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 Comment

ID: 1253274
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

LVL 5

Expert Comment

ID: 1253275
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

LVL 5

Expert Comment

ID: 1253276
#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

LVL 3

Expert Comment

ID: 1253277
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

LVL 1

Expert Comment

ID: 1253278
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 Comment

ID: 1253279
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.