By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

searching an array

P: 9
I have to write a program that simulates chess such that a chess piece X is located in a 2D array and other pieces are tested if they are capable of moving to X's position.

Expand|Select|Wrap|Line Numbers
  1.    int board[8][8];
  2.    char command[200], code, charwidth[2], charheight[2];
  3.  
  4.    scanf("%s", command);
  5.  
  6.    char *ptr = command;
  7.  
  8.    for(int a = 0; a < 8; a++)
  9.    {
  10.  
  11.     for(int b = 0; b < 8; b++)
  12.     {
  13.         board[a][b] = '.';
  14.     }  
  15.  
  16.    }
  17.  
  18.    while(*ptr != '\0')
  19.    {
  20.         code = *ptr;
  21.         ptr++;
  22.         charwidth[0] = *ptr - '0';
  23.         ptr++;
  24.         charheight[0] = *ptr/* - '0'*/;
  25.         ptr++;
  26.         charwidth[0]--;
  27.         charheight[0]--;
  28.         int intwidth = atoi(charwidth);
  29.         int intheight = atoi(charheight);
  30.         board[8 - intheight][intwidth - 8] = code;
  31.  
  32.  
  33.    }
  34.  
  35.  
with what I'm written so far I am able to position the pieces when cordinates are inputed, but I am now stuck. How can I search the array for piece X and compare it with the other pieces in the array to see if they can move to X's position through their chess restrictions(only moving diagonally etc.) and print the location of the piece if it is capable of moving the X's position.
Mar 16 '07 #1
Share this Question
Share on Google+
13 Replies


Ganon11
Expert 2.5K+
P: 3,652
Do you have to use the specific piece to search for - in other words, do you have to search for piece X, or could you search for a piece in a Location?

I recently wrote a chess game in Java, and the function I used checked if a location was threatened rather than a piece - it looped through every possible location and checked if the piece there could capture a piece at the first location.
Mar 16 '07 #2

P: 9
well for example. in a 8x8 array i have pieces such as

king (3,4)
queen(1,2)
rook(2,3)
x(4,4)

from the code i have above, I am able to input these pieces into the array. But now I have to see if those piece will be able to move to (4,4) which is X's position based on each piece's limitations.
Mar 16 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
You could use a class to represent each chess piece - in the class, have two instance variables holding the piece's x and y position in the board - something like:

Expand|Select|Wrap|Line Numbers
  1. class ChessPiece {
  2.    private:
  3.       int x, y;
  4.  
  5.    public:
  6.       int getX() const;
  7.       int getY() const;
  8. };
If you know you have to find out if x can be captured, first get x's location by the getX and getY functions, and then iterate through the locations to see if any piece could capture at x's location.
Mar 16 '07 #4

P: 9
I am not able to use classes though:S is the an easier way to implement this??
Mar 16 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
I am not able to use classes though:S is the an easier way to implement this??
Through a struct?
Mar 16 '07 #6

P: 9
haven't learn struct neither :(

can you do this with an array?
Mar 16 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
haven't learn struct neither :(

can you do this with an array?
I suppose you could declare a 2-d array of chars or strings and then keep track of a bunch of p's for pawns, or something...

If it's just that you haven't learned classes or structs, as opposed to not being allowed by the project or class, I would recommend looking into them - it will make things a lot easier.
Mar 16 '07 #8

P: 9
I actually know how to use classes and structs but this project doesn't allow classes:(
Mar 16 '07 #9

sicarie
Expert Mod 2.5K+
P: 4,677
I actually know how to use classes and structs but this project doesn't allow classes:(
That's too bad, but is prevalent throughout programming (even in the corporate world - though it's usually not "you can't use classes" and more "you have to use this 'custom' (read: overly-complex) interface/library/backend/program"...

So yeah, my best guess would be the 2d array.
Mar 16 '07 #10

P: 9
corrsponding to my code, where would I place this 2d array?
Mar 16 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
corrsponding to my code, where would I place this 2d array?
Dangit. There are already at least 3 threads on what happens when I try to quit coffee. I guess there are 4 now.

You already have the 2d array (board). Now it's a matter of using representations of pieces. With a 2d array, you know where everything is, so you can correspond a number to stand for each individual piece, and have that sitting somewhere. Then you can call a function do figure out where the piece can move, given its current location. If you pass that back and check the array at those places, you will see if they are occupied, and then you can print out those that are not.

(For instance, you know that bishops can only move diagonally, so add {(1,1), (-1,1), (1,-1), and (-1,-1)} to everything. I suppose the error checking is probably best done there, so you don't go through the cycles figuring out all the spaces, when it turns out someone is blocking yoru path....)

I'm sorry, I came in halfway thinking it was a design question, not algorithmic.
Mar 16 '07 #12

P: 9
I kind of understand what you mean by this, but I have trouble converting this idea into code. Can you show me a simple representation of this in code?
Mar 16 '07 #13

sicarie
Expert Mod 2.5K+
P: 4,677
I kind of understand what you mean by this, but I have trouble converting this idea into code. Can you show me a simple representation of this in code?
Well, I'm preparing for St. Patrick's Day, so I'll try to give you a good pseudocode, because it's highly likely that any code I wrote now would not work.

So you declare characters for pawn, rook, knight, bishop, king, and queen (twice as many, actually - because you'll need 2 sets of each). Then you can have functions set up to find the spaces. (You can either pass the array and return it each time, or declare the array globally... it's your call) So, for instance, pawn:

Pawns, on the first move, can jump two spaces (or not), and they attack one space diagonally. But every time, they'll want the option to move so

Expand|Select|Wrap|Line Numbers
  1. get the space of the pawn
  2. if it's the first space              // you could keep track 
  3.                 of this with a boolean, but then you'd have 16 to declare, etc...
  4.     open squares = /* assuming first is vertical - 
  5.                  this could be different in your prog */ (+1,0) and (+2,0)
  6. otherwise
  7.     open spaces = (+1,0)
  8. if a black piece is at (+1, +1) // diagonal right in my representation
  9.                     or is at (+1, -1) // diagonal left in my representation
  10.    attack = whichever (+1, +1) or (+1,-1) - or both possibly....
  11.  
and you'd call that every time you moved a pawn to get the spaces.

This will get frustrating with castling - the whole figuring out if they are on their first moves or not will get annoying, but once you find a way to keep track of that, you'll be good.

Also, remember that the knight is the only piece that can 'jump' other pieces.
Mar 16 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.