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