2D array (Chess Knight)

The user suppose to pick a place on the chessboard and the program should give all the possible moves the knight can make.
The If statement works only for number 19, can someone please help me to figure out how to make it work for all other numbers.
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
#include <iostream>
using namespace std;


int main()
{

	int x, i, j, a[8][8] = {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};
	
	for(i = 0; i <= 7; i = i + 1)
	  {
		  for(j = 0; j <= 7; j++)
			  cout<<a[i][j]<<"\t";

		  cout<<"\n";
	  }

	cout<<"Considering this a chessboard, pick a place: ";
	cin>>x;

	if(x>18 && x<23 || x>26 && x<31 || x>34 && x<38 || x>42 && x<47)
	{
		cout<<a[x-15][x-16]<<", "<<a[x-19][x-16]<<", "<<a[x-16][x-15]<<", "<<a[x-18][x-15]<<", "<<a[x-19][x-18]<<", "<<a[x-18][x-19]<<", "<<a[x-16][x-19]<<", "<<a[x-15][x-18]<<endl;
	}

	system("pause");
	return 0;
}
Last edited on
Just some style points:

You can split lines of code (just press enter) it won't make any difference to the compiler. Rule of thumb lines shouldn't be more than 80 characters. With the array - it would be easier to write for loops to initialise it.

I prefer to use Row & Col instead of i & j. The latter is harder to read, and this trivial tip will save you one day.

Set you indent tab to equal 4 spaces.

Variables x, i, j could all be unsigned.

Declare each variable on it's own line, initialise to something, and comment what it means.

for(i = 0; i <= 7; i = i + 1)

should be:

for(i = 0; i < 8; i++) the ++ operator increments by one.
same for this for loop:

for(j = 0; j <= 7; j = j++)

j = j++ is weird.

I can't see why the if statement isn't working - try using the debugger. Or try converting the if to a switch.

HTH

Edit: can you use code tags (the <> button) rather than quote tags?



Last edited on
Thanks, but the if statement code works correctly only for the number 19, and I cannot figure out how can I make it work for all other numbers from 1-64.
closed account (o1vk4iN6)
It should be relative to where the knight was placed. +-2 rows +- 1 column and +- 1 row +- 2 columns. You would also need to do a check to make sure it is still in the bounds of the board.

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

const int BOARD_SIZE = 64;
int board[ BOARD_SIZE ]; // keep the same dimension that you are using everywhere else

for(int i = 0; i < BOARD_SIZE; ++i)
    board[ i ] = i + 1;

// get x from user

int index = x - 1;
int pos;

pos = index + 8 + 2; //+1 row +2 columns
if( pos >= 0 && pos < BOARD_SIZE )
    cout << board[ index + 8 + 2 ]; // should probably use a constant for row_size (8 in this case)

// would be: 1 * ROW_SIZE 


It will probably be easier to create a Board class with some methods like, isValid( int x, int y ). Also better to keep everything in terms of x and y rather then switching between 1D and 2D (why is input only 1D).
Last edited on
ty
Topic archived. No new replies allowed.