Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   help in rook movement in chess (http://www.go4expert.com/forums/help-rook-movement-chess-t27278/)

learner guy 4Dec2011 12:40

help in rook movement in chess
 
hi i am making my rook move with the help of validRook() and rookMove() functions..
i have placed validRook inside rookMove so that it is only executed when validRook return s true..

plz see if my validRook algorithm is correct(Note: this is phase 1 of my game and i just wand the rook to move if and only if all vertical path is clear (no killing yet and no horizontal movement yet ))
plz ask questions to clarify yourself and also ignore the other functions for movement of other pieces ,they are commented


Code:

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <sstream>


using namespace std;


void printBoard(char board[8][8],int a[],int i,int j);
bool getMove(int arr[],char sr,int sc,char dr,int dc);
bool emptySource(char a[][8],int arr[],int & i,int & j);
bool emptyDestiny(char a[][8],int arr[],int & i,int & j);
//movements
void rookMove(char board[8][8],int a[],bool rook);
void nightMove(char board[8][8],int a[]);
void bishopMove(char board[8][8],int a[]);
void kingMove(char board[8][8],int a[]);
void queenMove(char board[8][8],int a[]);
void pawnMove(char board[8][8],int a[]);
//cheks on movements
bool validRook(char board[][8],int a[],int i);


void main (){

    char sr='0',dr='0';
        int sc=0,dc=0;
        int n=4;
        char again;
        int move[4]={sr,sc,dr,dc};
        bool ok =false;
        int row=0;int col=0;
    char board[8][8]={
        {'R','N','B','Q','K','B','N','R' },
    {'P','P','P','P','R','P','P','R' },
        {' ','R',' ',' ',' ',' ',' ',' ' },
        {' ',' ',' ',' ',' ',' ',' ',' ' },
    {' ',' ',' ',' ',' ',' ',' ',' ' },
    {' ','R',' ',' ',' ',' ',' ',' ' },
        {'p','p','p','p','p','p','p','p' },
        {'r','n','b','q','k','b','n','r' }
    };

       
        printBoard(board,move,row,col);
        cout << endl;

        do{
    tryAgain:
        cout << endl;
        cout <<"Enter source coordinates      :"  ;
        cin >> sr;
        cout << "&  ";
        cin >> sc;

        cout <<"Enter destination coordinates :" ;
        cin >> dr;
        cout << "&  ";
        cin >> dc;
       
        getMove( move, sr, sc, dr, dc);
        emptySource(board,move,row,col);
        emptyDestiny(board,move,row,col);
        bool hi = getMove( move, sr, sc, dr, dc);
        bool bi = emptySource(board,move,row,col);
        bool di = emptyDestiny(board,move,row,col);
       
        cout << endl;
        if(hi == true && bi == true && di == true ){
        ok = true;
        }else{
        ok=false;
        }

        if(ok==true){
                int k=move[0];
                bool rook = validRook(board,move,k);
                rookMove(board,move,rook);       
                // nightMove( board,move);
                //bishopMove(board,move);
                //kingMove(board,move);
                //queenMove(board,move);
                //pawnMove(board,move);
                cout << "\n\n";
  printBoard(board,move,row,col);
  cout << "\n";
   
       
        goto tryAgain;
       
        }else if(ok==false) {
        cout << "Oops u messed that up !Input r to retry:";
        cin>>again;
        //system("cls");
        }
        cout << endl;
        }while(again=='r' || again=='R');       
       
}       
       



void printBoard(char board[8][8],int a[],int i,int j){

        char l;
        cout << "  ";
        for(int n=1;n<9;n++){
       
        cout <<" " << n<< "  ";

        }
        cout << endl;cout << endl;
        for( i=0,l=65 ;i<8,l<=72;i++,l++){
                cout << l << ":" << " ";
          for( j=0;j<8;j++){
                cout << " " << board[i][j]  << '|' << " ";
               

          }
          cout << endl ;
          cout << "  _______________________________";
          cout << endl ;
        }

       
}

bool getMove(int arr[],char sr,int sc,char dr,int dc){


        sr=toupper((unsigned char)sr);//capitalize char coordinates
        dr=toupper((unsigned char)dr);
       
        if ((sr < 'A' || sr > 'H') ||//source is in range
        (sc < 1 || sc > 8))
    {
        cout << "source out of range\n";
                return false;
        }
        if((dr < 'A' || dr > 'H') ||//destiny is in range
        (dc < 1 || dc > 8)){
       
        cout << "destiny out of range\n";
                return false;
       
        }
        int x=sr-'A';
        int z=dr-'A';
        arr[0]=x;//get ascii of rows
        arr[1]=sc-1;//get index started from 0
        arr[2]=z;
        arr[3]=dc-1;
       
        if (arr[0]==arr[2] && arr[1]==arr[3]){//sourcebox and destinationbox are same
       
        cout << "source and destiantion are same\n";
                return false;
       
        }
        return true;

}

bool  emptySource(char a[][8],int arr[],int & i,int & j){
       
 
        i=arr[0]; 
        j=arr[1] ; 

        if (a[i][j] != ' ')
        {//source must be filled
                return true;
       
        }else if (a[i][j] == ' ')
        {
        cout << "Source must be filled \n";
        return false;
        }else  {
        return false;}
       
           

}
bool  emptyDestiny(char a[][8],int arr[],int & i,int & j){
       
 
        i=arr[2]; 
        j=arr[3] ; 

        if (a[i][j] == ' ')
       
{//source must be filled
                return true;
       
        }else if (a[i][j] != ' ')
        {
        cout << "Destiny must be empty\n";
        return false;
        }else {
        return false;
        }
       
}


void rookMove(char board[8][8],int a[],bool rook){
       
       
        if(board[a[0]][a[1]] =='R' &&  rook == true){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='R';
        }
       

}


void nightMove(char board[8][8],int a[]){

        if(board[a[0]][a[1]] =='N'){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='N';
        }
       

}


void bishopMove(char board[8][8],int a[]){

        if(board[a[0]][a[1]] =='B'){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='B';
        }
       

}

void kingMove(char board[8][8],int a[]){

        if(board[a[0]][a[1]] =='K'){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='K';
        }
       

}

void queenMove(char board[8][8],int a[]){

        if(board[a[0]][a[1]] =='Q'){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='Q';
        }
       

}


void pawnMove(char board[8][8],int a[]){

        if(board[a[0]][a[1]] =='P'){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='P';
        }
       

}

bool validRook(char board[][8],int a[],int i){

        if(a[0]<a[2]){

        for( i=a[0];i<a[2]; i++){
       
                    if(board[i][a[1]] != ' '){
                    return false;
                    }else{
                    return true;
                    }
      }
        }else if (a[0]>=a[2]){
                for( i=a[0];i>a[2]; i--){
            if(board[i][a[1]] != ' '){
                return false;
                }else
                {
                        cout << "destiny is biggerrrrrrrrrrrrrrrrrrrrrr";
                return true;
                }
        }
        }else{
                cout << "source is biggerrrrrrrrrrrrrrrrrrrrrr";
        return false;
        }
       

       
}


xpi0t0s 5Dec2011 01:27

Re: help in rook movement in chess
 
300 lines is rather a lot of code to look through just to "see if my validRook algorithm is correct".

Does it return the correct values in all scenarios you've tested? If so then it's correct (as far as the testing goes). If not then it isn't.

So what, if any, problems are there with your code?

learner guy 5Dec2011 11:05

Re: help in rook movement in chess
 
Now i have removed all unnecessary functions ..plz check validRook() and rookMove() funciton..my algorithm is that i have put validRook() inside rookMove() ...rookMove() will work only when it gets a "true" return from validRook()..
so majorly plz check validRook()
the idea i have tried to implement is that i have created an array of size 4 which stores source and destination coordinates..
now as far as the board is empty going upward or downward the function should return true
(Note: this is phase 1 of my game and i just wand the rook to move if and only if all vertical path is clear (no killing yet and no horizontal movement yet ))

Code:

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <sstream>


using namespace std;


void printBoard(char board[8][8],int a[],int i,int j);
bool getMove(int arr[],char sr,int sc,char dr,int dc);
bool emptySource(char a[][8],int arr[],int & i,int & j);
bool emptyDestiny(char a[][8],int arr[],int & i,int & j);
//movements
void rookMove(char board[8][8],int a[],bool rook);

//cheks on movements
bool validRook(char board[][8],int a[],int i);


void main (){

    char sr='0',dr='0';
        int sc=0,dc=0;
        int n=4;
        char again;
        int move[4]={sr,sc,dr,dc};
        bool ok =false;
        int row=0;int col=0;
    char board[8][8]={
        {'R','N','B','Q','K','B','N','R' },
    {'P','P','P','P','R','P','P','R' },
        {' ','R',' ',' ',' ',' ',' ',' ' },
        {' ',' ',' ',' ',' ',' ',' ',' ' },
    {' ',' ',' ',' ',' ',' ',' ',' ' },
    {' ','R',' ',' ',' ',' ',' ',' ' },
        {'p','p','p','p','p','p','p','p' },
        {'r','n','b','q','k','b','n','r' }
    };

       
        printBoard(board,move,row,col);
        cout << endl;

        do{
    tryAgain:
        cout << endl;
        cout <<"Enter source coordinates      :"  ;
        cin >> sr;
        cout << "&  ";
        cin >> sc;

        cout <<"Enter destination coordinates :" ;
        cin >> dr;
        cout << "&  ";
        cin >> dc;
       
        getMove( move, sr, sc, dr, dc);
        emptySource(board,move,row,col);
        emptyDestiny(board,move,row,col);
        bool hi = getMove( move, sr, sc, dr, dc);
        bool bi = emptySource(board,move,row,col);
        bool di = emptyDestiny(board,move,row,col);
       
        cout << endl;
        if(hi == true && bi == true && di == true ){
        ok = true;
        }else{
        ok=false;
        }

        if(ok==true){
                int k=move[0];
                bool rook = validRook(board,move,k);
                rookMove(board,move,rook);       
               
                cout << "\n\n";
  printBoard(board,move,row,col);
  cout << "\n";
   
       
        goto tryAgain;
       
        }else if(ok==false) {
        cout << "Oops u messed that up !Input r to retry:";
        cin>>again;
        //system("cls");
        }
        cout << endl;
        }while(again=='r' || again=='R');       
       
}       
       



void printBoard(char board[8][8],int a[],int i,int j){

        char l;
        cout << "  ";
        for(int n=1;n<9;n++){
       
        cout <<" " << n<< "  ";

        }
        cout << endl;cout << endl;
        for( i=0,l=65 ;i<8,l<=72;i++,l++){
                cout << l << ":" << " ";
          for( j=0;j<8;j++){
                cout << " " << board[i][j]  << '|' << " ";
               

          }
          cout << endl ;
          cout << "  _______________________________";
          cout << endl ;
        }

       
}

bool getMove(int arr[],char sr,int sc,char dr,int dc){


        sr=toupper((unsigned char)sr);//capitalize char coordinates
        dr=toupper((unsigned char)dr);
       
        if ((sr < 'A' || sr > 'H') ||//source is in range
        (sc < 1 || sc > 8))
    {
        cout << "source out of range\n";
                return false;
        }
        if((dr < 'A' || dr > 'H') ||//destiny is in range
        (dc < 1 || dc > 8)){
       
        cout << "destiny out of range\n";
                return false;
       
        }
        int x=sr-'A';
        int z=dr-'A';
        arr[0]=x;//get ascii of rows
        arr[1]=sc-1;//get index started from 0
        arr[2]=z;
        arr[3]=dc-1;
       
        if (arr[0]==arr[2] && arr[1]==arr[3]){//sourcebox and destinationbox are same
       
        cout << "source and destiantion are same\n";
                return false;
       
        }
        return true;

}




void rookMove(char board[8][8],int a[],bool rook){
       
       
        if(board[a[0]][a[1]] =='R' &&  rook == true){
    board[a[0]][a[1]] =' ';
        board[a[2]][a[3]] ='R';
        }
       

}













bool validRook(char board[][8],int a[],int i){

        if(a[0]<a[2]){

        for( i=a[0];i<a[2]; i++){
       
                    if(board[i][a[1]] != ' '){
                    return false;
                    }else{
                    return true;
                    }
      }
        }else if (a[0]>=a[2]){
                for( i=a[0];i>a[2]; i--){
            if(board[i][a[1]] != ' '){
                return false;
                }else
                {
                        cout << "destiny is biggerrrrrrrrrrrrrrrrrrrrrr";
                return true;
                }
        }
        }else{
                cout << "source is biggerrrrrrrrrrrrrrrrrrrrrr";
        return false;
        }
       

       
}


xpi0t0s 5Dec2011 12:55

Re: help in rook movement in chess
 
Your code would be a lot easier to understand if it was indented correctly. Sure, YOU know what's going on, but nobody else does. So write your code like this in future:

Code:

bool validRook(char board[][8],int a[],int i)
{
        if(a[0]<a[2])
        {
                for( i=a[0];i<a[2]; i++) // really from a[0]?
                {
                        if(board[i][a[1]] != ' ') // a[1]????
                        {
                                return false;
                        }
                        else
                        {
                                return true; // Are you sure this should be here?
                        }
                }
        }
        else if (a[0]>=a[2])
        {
                for( i=a[0];i>a[2]; i--)
                {
                        if(board[i][a[1]] != ' ') // a[1]????
                        {
                                return false;
                        }
                        else
                        {
                                // Why is this here?  Seems like the "wrong message" - the error should be*
                                cout << "destiny is biggerrrrrrrrrrrrrrrrrrrrrr";
                                return true;
                        }
                }
        }
        else
        {
                cout << "source is biggerrrrrrrrrrrrrrrrrrrrrr";
                return false;
        }
}

*something like "can't make that move - there's a piece in the way".

Since all you're checking for is spaces between a[0] and a[2], to avoid duplication you could loop from the smaller of those two, to the larger. Then you don't need to duplicate code.

The "return true" commented on will return true at the first space you encounter. So "R N x" where x is the destination will return TRUE because of the space just after the R.

Do not substitute posting your code on a forum and asking "plz check it" for testing. You could have discovered the obvious errors in the code with just a small amount of testing.

xpi0t0s 5Dec2011 12:56

Re: help in rook movement in chess
 
note tabs don't need to be 8 columns; that's the site's choice. 4 spaces is my preference (either use spaces, or set your editor's tab width to 4)., but you could use 3 or 2. I find indenting tends to get undecipherable at 2 though.

xpi0t0s 5Dec2011 12:58

Re: help in rook movement in chess
 
Code:

// a[1]????
On second thoughts that could be correct. Try using m v n i o a s l - and if you don't have a clue what I'm saying there, that's the point. Meaningful variable names instead of a single letter.

learner guy 5Dec2011 16:29

Re: help in rook movement in chess
 
thanks a lot for ur reply :)
your suggestion works


All times are GMT +5.5. The time now is 12:08.