need help with array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • furyzzz
    New Member
    • Nov 2006
    • 3

    need help with array

    Hi..i am new to c++..i wanted to limit the chesspiece to be able to move 1-tile adjcent to their current position..but my code does not work at all..can anyone give me any pointers?..thx

    const int row = 4;
    const int column = 3;
    int i, j;

    void printBoard(char chessboard[row][column]);
    void linearSearchRow (char chessboard[row][column], char target);
    void linearSearchCol umn(char chessboard[row][column], char target);
    void move(char chessboard[row][column], string piece, char target);

    int main()
    {

    char chessboard[row][column] = {
    {' ', ' ', ' '},
    {' ', ' ', ' '},
    {' ', ' ', ' '},
    {'a', 'b', 'c'}
    };
    move(chessboard , "assasin", 'a');
    printBoard(ches sboard);
    }

    int linearSearchRow (char chessboard[row][column], char target)
    {
    for (i = 0; i < row; i++)
    for(j = 0; j < column; j++)
    if(chessboard[i][j] == target)
    return i;
    return -1;
    }

    int linearSearchCol umn(char chessboard[row][column], char target)
    {
    for (i = 0; i < row; i++)
    for(j = 0; j < column; j++)
    if(chessboard[i][j] == target)
    return j;
    return -1;
    }

    void move(char chessboard[row][column], string piece, char target)
    {
    int result_r, result_c, new_r, new_c;
    do
    {
    cout << "\nIt is your " << piece << "'s turn\n";
    cout << "Where do you wish to move it(row): ";
    cin >> r;
    cout << "Where do you wish to move it(column): ";
    cin >> c;
    r = r - 1;
    c = c - 1;
    new_r = linearSearchRow (chessboard, target);
    new_c = linearSearchCol umn(chessboard, target);
    result_r = new_r - r;
    result_c = new_c - c;

    if(result_r <= 1 && result_c <= 1)
    chessboard[r][c] = target;
    else
    cout << "You cannot move there!" << endl;
    }while(result_r > 1 || result_c > 1);
    }

    void printBoard(char chessboard[row][column])
    {
    cout << "\t Column";
    cout << "\n\t 1 2 3 ";
    for(i = 0; i < row; i++)
    {
    cout << "\nRow" << i + 1;
    cout << "\t - - -";
    cout << endl << "\t";
    for(j = 0; j < column; j++)
    cout << "|" << chessboard[i][j] << "|";
    cout << "\n\t - - -";
    }
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    The functions linearSearchRow () and linearSearchCol umn() returned int yet the prototyes declared they returned void. Also variables r and c were not defined in function move(). Updated code is
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    const int row = 4;
    const int column = 3;
    int i, j;
    
    void printBoard(char chessboard[row][column]);
    int linearSearchRow(char chessboard[row][column], char target);  // *** int
    int linearSearchColumn(char chessboard[row][column], char target);  // ** int
    void move(char chessboard[row][column], string piece, char target);
    
    int main()
    {
    
    char chessboard[row][column] = {
    {' ', ' ', ' '},
    {' ', ' ', ' '},
    {' ', ' ', ' '},
    {'a', 'b', 'c'}
    };
    move(chessboard, "assasin", 'a');
    printBoard(chessboard);
    }
    
    int linearSearchRow(char chessboard[row][column], char target)
    {
    for (i = 0; i < row; i++)
    for(j = 0; j < column; j++)
    if(chessboard[i][j] == target)
    return i;
    return -1;
    }
    
    int linearSearchColumn(char chessboard[row][column], char target)
    {
    for (i = 0; i < row; i++)
    for(j = 0; j < column; j++)
    if(chessboard[i][j] == target)
    return j;
    return -1;
    }
    
    void move(char chessboard[row][column], string piece, char target)
    {
    int result_r, result_c, new_r, new_c;
    int r,c; // ***  define r and c
    do
    {
    cout << "\nIt is your " << piece << "'s turn\n";
    cout << "Where do you wish to move it(row): ";
    cin >> r;         
    cout << "Where do you wish to move it(column): ";
    cin >> c;
    r = r - 1;
    c = c - 1;
    new_r = linearSearchRow(chessboard, target);
    new_c = linearSearchColumn(chessboard, target);
    result_r = new_r - r;
    result_c = new_c - c;
    
    if(result_r <= 1 && result_c <= 1)
    chessboard[r][c] = target;
    else
    cout << "You cannot move there!" << endl;
    }while(result_r > 1 || result_c > 1);
    }
    
    void printBoard(char chessboard[row][column])
    {
    cout << "\t Column";
    cout << "\n\t 1 2 3 ";
    for(i = 0; i < row; i++)
    {
    cout << "\nRow" << i + 1;
    cout << "\t - - -";
    cout << endl << "\t";
    for(j = 0; j < column; j++)
    cout << "|" << chessboard[i][j] << "|";
    cout << "\n\t - - -";
    }
    }

    Comment

    • furyzzz
      New Member
      • Nov 2006
      • 3

      #3
      thx for the reply..i have already make those changes u mentioned.
      However, why the linearSearchRow and linearSearchCol umn keep returning the value -1..how can i make it to return the current row and column of the target?..
      thx again..

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by furyzzz
        thx for the reply..i have already make those changes u mentioned.
        However, why the linearSearchRow and linearSearchCol umn keep returning the value -1..how can i make it to return the current row and column of the target?..
        thx again..
        using the code I posted I then put a print startement after the calls to SearchRow and linearSearchCol umn , e.g.
        Code:
        new_r = linearSearchRow(chessboard, target);
        new_c = linearSearchColumn(chessboard, target);
        cout << " r=  " << r << " c= " << c << endl;
        and got the following result in which the value returned by linearSearchRow and linearSearchCol umn was not -1
        Code:
        It is your assasin's turn
        Where do you wish to move it(row):[B] 3[/B]
         Where do you wish to move it(column): [B]3[/B]
         r= 2 c= 2
        	 Column
        	 1 2 3 
        Row1	 - - -
        	| || || |
        	 - - -
        Row2	 - - -
        	| || || |
        	 - - -
        Row3	 - - -
        	| || ||a|
        	 - - -
        Row4	 - - -
        	|a||b||c|
        is that anything like what you expect

        Comment

        • furyzzz
          New Member
          • Nov 2006
          • 3

          #5
          yup..this is something like what i expect..

          Comment

          Working...