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

#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;
	}
	

	
}

Aside from some extremely inconsistent formatting, I can see a number of issues with this:

  • You don't appear to be testing whether the destination (destiny?) is still in the same column as the original position. I realize that you aren't addressing horizontal movement yet, but you should at least confirm that it isn't moving horizontally.
  • The argument i is passed to the function, but then reassigned without checking it's original value; you simply use it as an index. Was this intentional, and if so, why? If you need to get the value of i after the function is ended, then you'll need to pass it as a reference.
  • On a slightly related note, I would rename a to something more descriptive, such as move . That's just me, of course.
  • The cases of a[0]<a[2] and a[0]>=a[2] are both exhaustive and mutually exclusive; thus, while the compiler won't detect it as such, the final else clause is 'dead code', that is to say, the program will never reach it.
  • You might want to consider using a structure to represent the moves, rather than just an array; something like this:
    struct Move {
        int startRow;
        int startColumn;
        int destRow;
        int destColumn;
    };

    This would generally be easier to read than a bare array.

plz see if my validRook algorithm is correct

It helps if your ask a specific question about your code that we can answer. Just asking us to check if your algorithm is correct by reading code is difficult -- at least from a beginner's code. It's best to
1) explain your algorithm first, so we can check it.
2) post the code that executes the algorithm, we now know what to look for.

And your code is very difficult to follow because of inconsistent indentation. See this to help with your formatting.

plz ask questions to clarify yourself and also ignore the other functions for movement of other pieces ,they are commented

Rather than forcing us to search through your entire program, why not post the code you have questions about? Posting all the code at the end of your post is OK (it gives us an overall idea of what you are doing) but also post the specific code first so we can zero in on the question you have.

While you're at it, see this, too.

And rework your code into a loop so you can get rid of the dreaded goto statements. You should not use them at all.

@Schoil-R-LEA


1.i dont want the value of i ,but still i passed it by reference but no results..
2. my major concern is that it should update my array ,whatever the algorithm is right or wrong atleast it shoud update my array ...thats what i am looking for


@waltp

my algorithm is that i stored my source and destination rows and columns (named as sr,sc ,dr,dc) in an array move[4](in function it is named a[])..in validRook function i am trying to check if all the path between source row and destination row is empty then it should update the board array to destination ..two if statements are to check if source row is bigger than destination row then it should move in a decreasing loop to move up else it should move down in increasing loop

Here is valid rook function

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;
	}
	

	
}

here is function of movement of rook which would update array only if it gets a true return from validRook() function

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';
	}
	

}

Edited 4 Years Ago by learner guy: n/a

i dont want the value of i ,but still i passed it by reference but no results..

I'm puzzled, then. If you don't want the value of i after the function, and you aren't using the passed value of i , why have it passed as an argument in the first place? Why not have a local variable to use as the index?

Oh, and you really need to fix your indentation. Here is your code with indentation in Allman style, with i changed to a local variable:

bool validRook(char board[][8],int a[])
{
    if(a[0]<a[2])
    {
        for(int i=a[0]; i<a[2]; i++)
        {
            if(board[i][a[1]] != ' ')
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    else if (a[0]>=a[2])
    {
        for(int 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;
    }
}

Which style you use isn't very important; what matters most is as consistency. Your current code indentation and brace style is very inconsistent, making it especially hard to read.

Edited 4 Years Ago by Schol-R-LEA: n/a

This article has been dead for over six months. Start a new discussion instead.