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
+ 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    int board[8][8];    char command[200], code, charwidth[2], charheight[2];      scanf("%s", command);      char *ptr = command;      for(int a = 0; a < 8; a++)    {       for(int b = 0; b < 8; b++)     {         board[a][b] = '.';     }        }      while(*ptr != '\0')    {         code = *ptr;         ptr++;         charwidth[0] = *ptr - '0';         ptr++;         charheight[0] = *ptr/* - '0'*/;         ptr++;         charwidth[0]--;         charheight[0]--;         int intwidth = atoi(charwidth);         int intheight = atoi(charheight);         board[8 - intheight][intwidth - 8] = code;        }     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
13 Replies

 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

 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 class ChessPiece {    private:       int x, y;      public:       int getX() const;       int getY() const; }; 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

 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

 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

 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

 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

 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 get the space of the pawn if it's the first space              // you could keep track                  of this with a boolean, but then you'd have 16 to declare, etc...     open squares = /* assuming first is vertical -                   this could be different in your prog */ (+1,0) and (+2,0) otherwise     open spaces = (+1,0) if a black piece is at (+1, +1) // diagonal right in my representation                     or is at (+1, -1) // diagonal left in my representation    attack = whichever (+1, +1) or (+1,-1) - or both possibly....   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.