help needed with array manipulation through user input

This is a discussion on help needed with array manipulation through user input within the C++ Programming forums, part of the General Programming Boards category; I am looking for some help with regards to accessing values of an array through user input... The following code ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    help needed with array manipulation through user input

    I am looking for some help with regards to accessing values of an array through user input...

    The following code is a chessboard representation using cout << to create a chessboard (graphic/grid) and elements within an 8x8 array to represent the chess pieces.

    I would like to be able to input moves (ie: e2e4) and then have the chessboard display that the pawn has moved from the e2 square to the e4 square.

    ***I am not yet at the point of implementing legal move generation or trying to make this work through object oriented programming or by using mailbox arrays or bitboards... although any helpful advice would be greatly welcome!***

    Question #2: have i initialized the array correctly or do i need to define each empty space by using ' ' ?

    Question #3: is there a clearer way for me to write the code that represents the array of chess pieces within the cout << graphical grid ?

    * i first tried using nested for loops to accomplish this task but couldn't get the grid to align properly ;(

    Lastly, i am very new to programming so the more specific you can be with regards to help/code would be greatly appreciated.

    Thank you.

    Code:
    #include <string> 
    using namespace std; 
    
    int main() 
    
    { 
    
    const int RANKS = 8; 
    const int FILES = 8; 
    
    cout << "\n"; 
    
    char cb[RANKS][FILES] = { 
    
    {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r' }, 
    {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p' }, 
    { }, 
    { }, 
    { }, 
    { }, 
    {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' }, 
    {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R' } }; 
    
    
    cout << "**************************************\n" 
    << "* *\n" 
    << "* WELCOME TO CHESS !!! *\n" 
    << "* *\n" 
    << "**************************************\n\n"; 
    
    // Chessboard Representation 
    
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 8 | " << cb[0][0] << " | " << cb[0][1] << " | " << cb[0][2] << " | " << cb[0][3] << " | " << cb[0][4] << " | " << cb[0][5] << " | " << cb[0][6] << " | " << cb[0][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 7 | " << cb[1][0] << " | " << cb[1][1] << " | " << cb[1][2] << " | " << cb[1][3] << " | " << cb[1][4] << " | " << cb[1][5] << " | " << cb[1][6] << " | " << cb[1][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 6 | " << cb[2][0] << " | " << cb[2][1] << " | " << cb[2][2] << " | " << cb[2][3] << " | " << cb[2][4] << " | " << cb[2][5] << " | " << cb[2][6] << " | " << cb[2][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 5 | " << cb[3][0] << " | " << cb[3][1] << " | " << cb[3][2] << " | " << cb[3][3] << " | " << cb[3][4] << " | " << cb[3][5] << " | " << cb[3][6] << " | " << cb[3][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 4 | " << cb[4][0] << " | " << cb[4][1] << " | " << cb[4][2] << " | " << cb[4][3] << " | " << cb[4][4] << " | " << cb[4][5] << " | " << cb[4][6] << " | " << cb[4][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 3 | " << cb[5][0] << " | " << cb[5][1] << " | " << cb[5][2] << " | " << cb[5][3] << " | " << cb[5][4] << " | " << cb[5][5] << " | " << cb[5][6] << " | " << cb[5][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 2 | " << cb[6][0] << " | " << cb[6][1] << " | " << cb[6][2] << " | " << cb[6][3] << " | " << cb[6][4] << " | " << cb[6][5] << " | " << cb[6][6] << " | " << cb[6][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " 1 | " << cb[7][0] << " | " << cb[7][1] << " | " << cb[7][2] << " | " << cb[7][3] << " | " << cb[7][4] << " | " << cb[7][5] << " | " << cb[7][6] << " | " << cb[7][7] << " | \n"; 
    cout << " +---+---+---+---+---+---+---+---+\n"; 
    cout << " a b c d e f g h\n\n"; 
    
    int move; 
    
    cout << "Please enter a move: "; 
    cin >> move; 
    
    return 0; 
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by priorityversion View Post
    I am looking for some help with regards to accessing values of an array through user input...

    The following code is a chessboard representation using cout << to create a chessboard (graphic/grid) and elements within an 8x8 array to represent the chess pieces.

    I would like to be able to input moves (ie: e2e4) and then have the chessboard display that the pawn has moved from the e2 square to the e4 square.

    ***I am not yet at the point of implementing legal move generation or trying to make this work through object oriented programming or by using mailbox arrays or bitboards... although any helpful advice would be greatly welcome!***

    Question #2: have i initialized the array correctly or do i need to define each empty space by using ' ' ?

    Question #3: is there a clearer way for me to write the code that represents the array of chess pieces within the cout << graphical grid ?

    * i first tried using nested for loops to accomplish this task but couldn't get the grid to align properly ;(

    Lastly, i am very new to programming so the more specific you can be with regards to help/code would be greatly appreciated.

    Thank you.
    2. I would think you would need spaces, since the default initializer is '\0' (which doesn't print). Does your chessboard print correctly the way it is?

    3. Yes. Oh, you want to know what it is? It's nested for-loops. I guess your alignment problems might have come from #2.

    1. As to reading input, as long as we're dealing with computer algebraic notation such as e2e4, you'll need to parse character by character to get coordinates: e --> column 4, 2 --> row 1, e --> column 4, 4 --> row 3. Hint: characters are numbers "under the hood" and you are guaranteed to have your numbers 0-9 in order, and your letters a-z in order. So you can do things like input_character-'a' to find column numbers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    I would like to be able to input moves (ie: e2e4) and then have the chessboard display that the pawn has moved from the e2 square to the e4 square.
    For starters, you need to write the code that can interpret the user input.

    Question #2: have i initialized the array correctly or do i need to define each empty space by using ' ' ?
    I could be wrong, but I believe you should use the spaces for initialisation, otherwise at best they would be initialised to null characters (or perhaps left uninitialised).

    Question #3: is there a clearer way for me to write the code that represents the array of chess pieces within the cout << graphical grid ?
    Yes, by using a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Question #2: have i initialized the array correctly or do i need to define each empty space by using ' ' ?
    yes, you need to do that.

    Question #3: is there a clearer way for me to write the code that represents the array of chess pieces within the cout << graphical grid ?

    * i first tried using nested for loops to accomplish this task but couldn't get the grid to align properly ;(
    Nested for loops should be the way to go. Please post your code that uses nested for loops and doesn't align properly so we can try to fix it.

    I would like to be able to input moves (ie: e2e4) and then have the chessboard display that the pawn has moved from the e2 square to the e4 square.
    it may not be as simple as you thought =). Think about en-passant, castling, promotion.
    Here is some code I pulled directly out of my chess engine.
    Code:
    Move parseMove(string alg) { 
    	int srcx, srcy, dstx, dsty;
    	srcx = alg.at(0);
    	srcy = alg.at(1);
    	dstx = alg.at(2);
    	dsty = alg.at(3);
    
    	srcx -= 'a';
    	srcy -= '1';
    	dstx -= 'a';
    	dsty -= '1';
    
            ...
    }
    where alg is the user-input. Hope that gets you started.

    btw, I am Matthew from talkchess =).
    Last edited by cyberfish; 02-02-2008 at 10:00 PM.

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I've never written a chess engine, but I would rethink the design a bit here. If someone does move a piece, how is the board you've designed using the array and cout supposed to update the change (graphically anyway)?

    I would start off by creating a simple class that just keeps track of rank and file positions, and go from there. What you're attempting here will only obfuscate things as your code becomes more complex.

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Oh sorry, I didn't read your code correctly! It will work what you're trying to do. Although, starting out more object-oriented couldn't hurt...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21