Solved

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

Posted on 1998-10-03
22
867 Views
Last Modified: 2010-05-18
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
Comment
Question by:missq
  • 6
  • 5
  • 2
  • +6
22 Comments
 
LVL 1

Expert Comment

by:Grailman
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

by:ozo
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

by:missq
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

by:scrapdog
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

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

0
 
LVL 8

Expert Comment

by:Answers2000
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

by:ozo
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

by:scrapdog
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

by:missq
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

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

Expert Comment

by:elfie
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 8

Expert Comment

by:Answers2000
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

by:Bayo
ID: 1253270
I Agree braveheart.
it's a very easy problem, I'm sure U can do it only try
if you have one problem on your try ask us.

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

Accepted Solution

by:
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

by:missq
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

by:scrapdog
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

by:missq
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

by:scrapdog
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

by:scrapdog
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

by:elfie
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

by:NakedTimeMan
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

by:missq
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now