### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include 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<>y; cout<
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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````// Knight Moves.cpp : main project file. #include 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<>y; cout<
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)

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include 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.