Finding The Probability of Moves

hi. I'm writing a program that finds the probability of moves a Knight can make on a chess board. The program asks the user to enter the row and the column and the program is supposed to output how many possible moves the Knight can make, but the output is wrong. Example(if I enter row 1 and column 1 the output is 8 and it should be 2) I know the problem is with my if statements, can someone please help me?

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
#include <iostream>
using namespace std;

int chess(int a, int b)
{ 
	int x =a;
	int y = b;
	int sum=0;

	{
		x+=2, y+=1;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x+=2, y-=1;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x-=2, y+=1;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x-=2, y-=1;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x+=1, y+=2;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x+=1, y-=2;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x-=1, y+=2;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	{
		x-=1, y-=2;
	if((x<=7 && y<=7)&&(x>=1 && y>=1))
	sum += 1;
	else sum+= 0;
	}

	return sum;

}

int main()
{
	int x, y, z;

	cout<<"Enter row: ";
	cin>>x;
	cout<<endl;
	cout<<"Enter column: ";
	cin>>y;
	cout<<endl;
	z = chess(x, y);
	cout<<z<<endl;

	return 0;
}
All you need to do is given a position and the possible "delta positions" (2 up/down and 1 left/right and 2 left/right and 1 up/down) check if they are on the board, if so add one to a counter.
@Mindy92

A couple of problems in the program.

if((x<=7 && y<=7)&&(x>=1 && y>=1))
1) A chessboard is 8x8, not 7x7

2) You keep adding or subtracting from the sent variable, instead of just checking what the results would be IF the locations sent were added to or subtracted from.

Here it is, corrected. Now, maybe you can add a representation of a board, and mark on it, where the knight could move, as well.
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
// Knight Moves.cpp : main project file.

#include <iostream>
using namespace std;

int chess(int a, int b)
{ 
	int sum=0;

	if(a+1<=8 && b+2<=8)
		sum += 1;
	if(a+2<=8 && b+1<=8)
		sum += 1;

	if(a+1<=8 && b-2>=1)
		sum += 1;
	if(a+2<=8 && b-1>=1)
		sum += 1;
	
	if(a-1>=1 && b+2<=8)
		sum += 1;
	if(a-2>=1 && b+1<=8)
		sum += 1;

	if(a-1>=1 && b-2>=1)
		sum += 1;
	if(a-2>=1 && b-1>=1)
		sum += 1;
	return sum;
}

int main()
{
	int x, y, z;

	cout<<"Enter row: ";
	cin>>x;
	cout<<endl;
	cout<<"Enter column: ";
	cin>>y;
	cout<<endl;
	z = chess(x, y);
	cout<<z<<endl;

	return 0;
}
Last edited on
If you don't want to use a lot of if statements, then here's a version with 2 (easily compacted to 1)

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
#include <iostream>
#include <cstdlib>
using namespace std;
int Chess (int row, int column)
{
    int possiblemoves[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    int moves=0;
    for (int i=0; i<8; i++)
    {
        if (row+possiblemoves[i][0]>=1 && row+possiblemoves[i][0]<=8)
        {
            if (column+possiblemoves[i][1]>=1 && column+possiblemoves[i][1]<=8)
            {
                moves++;
            }
        }
    }
    return moves;
}

int main()
{
    cout << "Row: ";
    int row, column;
    cin >> row;
    cout << "Column: ";
    cin >> column;
    cout << Chess(row, column) << endl;
    system("pause");
}
Topic archived. No new replies allowed.