Chess program - Moves of the King

Hello everyone. I am a new member here, and I truly need your help.
I am a beginner in programming, and I got stuck in this program - which are the moves of the King in a chess game, after entering the position of the King. It should be written in C++, using complex FOR-loop statement.

This is my code, that unfortunately, is not working properly...

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
char letter,i;
int number, j;

cout<<"Enter the position of the King: ";
cin>>letter>>number;
while(number<1||number>8||letter<'A'||letter>'H')
{
cout<<"You entered incorrect position. Enter the correct position of the King: ";
cin>>letter>>number;
}
cout<<"You entered "<<letter<<number<<". According to that, the King can move to the following positions: ";
for ((i=int(letter)-1),(j=number-1);(i>=65)&&(i<=72)&&(j<=number+1);i++,j++)

{
if(j<1||j>8||i<'A'||j>'H')
cout<<"/";
else
{
if (!((i==int(letter))&&j==number))
cout<<(char)i<<j<<" ";
}
}

system ("pause");
return 0;
}



If I enter G4, it should print that the King can move to F3,F4,F5,G3,G5,H3,H4,H5. But it only prints that the King can move to F3 and H5. Where is my mistake? Help me, please. I am feeling desperate :(

You are my last hope.Thank you in advance.
Hi pang,
In my opinion your biggest mistake is that you are making the problem overly complex. It makes it more difficult to follow the program and gives you the feeling of desperation =) Try simplifying the problem by splitting it up into separate problems, and organize your code better.

First of all I would outsource the different problems to functions i.e:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void KingMoves(char column, int row){
   int col = column-64;
   int nCol,nRow;

   for(int x=-1;x<2;x++){ //its worth splitting up for loops to simplify
       for(int y=-1;y<2;y++){
            nCol=col+x;
            nRow=row+y;

            if(CheckMove(nCol,nRow)==1)cout << static_cast<char>(nCol+65) <<nRow;
      }
   }
}

bool CheckMove(int col, int row){
   if(column<1||column>8) return 0;
   if(row<1||row>8) return 0;
   return 1;
}


and then just implement KingMoves(letter,number) into Main, also remember to create function prototypes above main:
void KingMoves(char,int);
bool CheckMove(int,int);

I didn't try to run this, so I'm not 100% sure if it will work =) But you get the point organize your program into a structure and move the main problems into functions. It makes it easier to re-use code later in the development stage =)

Good luck
Last edited on
After splitting up - the program works perfectly. The problem is, I MUST use multi-control, complex for-loop. That means, one for-loop to include both columns and rows. And that way my program is not working. Could you help me with the multi-control for loop?
The problem with your method is that it only runs 3 times, it checks row-1,col-1 row,col and row+1,col+1.
You need the loop to run at least 8 times for your purpose, and increase row and col consecutively in order.

The easiest way to achieve this in two nested loops. If you have to make it in one loop I would just increase i from 0 to 8, and use dividers to achieve the wanted result.
Topic archived. No new replies allowed.