Chess moves using array

Hi, im trying to write a program that uses an array to show all of the possible moves of a chess piece. Im working on the queen right now and could use some help. Im using an 8 x 8 array. I prompt the user for a starting position and my array places a Q in that spot. So if they were to choose row 4, column 4, the output should look something like this, with *'s in all of the places a queen piece would be able to move to.


* . . * . . * .
. * . * . * . .
. . * * * . . .
* * * Q * * * *
. . * * * . . .
. * . * . * . .
* . . * . . * .
. . . * . . . *


Im having a couple different problems. I know the elements in the array start at [0] and not 1, so when the user inputs the starting position, it is incorrect, if they wanted it to start at 4,4 , it would actually be placed in 3,3 .

Also, my funtion to place the *'s in the array does not work properly. I know im going to need multiple for loops, so i just started off with 1 to try and figure out how to do this. Im not good with for loops in my functions, so please, any help is apreciated.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std; 

void init_board (char board [8][8]);
void move_queen (char board [8][8], int row, int col);
void move_knight (char board [8][8], int row, int col);
void print_board (char board [8][8]);

int main()
{
    char board [8][8];
    char input;
    
    init_board (board);
    print_board (board);
    
    cout << "Enter q (queen), k (knight) or e (end): " << endl; // read input (k or q)
    cin >> input;
    while (input != 'e')
    {
          
          if (input == 'q')
          {
             move_queen (board, 8, 8);
             print_board (board);
             system ("PAUSE");
             return 1;
          }

          
    }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void init_board (char board [8][8])
{
     int row, col;
     
     for (row = 0; row < 8; row++)
         for (col = 0; col < 8; col++)
         board[row][col] = '.';

}



void move_queen (char board [8][8], int row, int col)
{
     int r, c;
     cout << "Enter starting row: " << endl;
     cin >> r;
     cout << "Enter starting column: " <<endl;
     cin >> c;
     
     board [r][c] = 'q';
     
     for (row = r + 1; row < 8; row++)
         cout << board [r][c] << "*";
}
         
     



void print_board (char board [8][8])
{
     int row, col;
     
     for (row = 0; row < 8; row++)
     {
         for (col = 0; col < 8; col++)
         cout << board[row][col] << " ";
         
     cout << endl << endl;
     }
     
     cout << endl;
}



Last edited on
I know the elements in the array start at [0] and not 1, so when the user inputs the starting position, it is incorrect, if they wanted it to start at 4,4 , it would actually be placed in 3,3 .
This is corrected by subtracting 1 from the row/col they enter.

1
2
3
4
5
6
7
8
9
10
 int r, c;
cout << "Enter starting row: " << endl;
cin >> r;
cout << "Enter starting column: " <<endl;
cin >> c;

if(r)r-=1; //if r is not already zero, decrement...
if(c)c-=1;//same
     
board [r][c] = 'q';


As far as the "where the queen can move" part. Tackle each direction one at a time. Get it working for North South East West. Then use what you've learned there to do the diagonal moves.
ok, so in my move_queen function, i thought maybe this for loop would work to increase the row and enter in *'s but it instead prints it befor the array. Im very confused on how to eneter *'s into a full row of an array.

1
2
for (row = r + 1; row < 8; row++)
         board [row][col] = ' * ';


Topic archived. No new replies allowed.