PDA

نسخه کامل مشاهده نسخه کامل : راهنمایی در مورد حرکت اسب در صفحه شطرنج



amirh67
16-06-2008, 09:47
کسی می تونه دونه دونه کدهایی رو که در زیر می نویسم رو توضیح بده.
همون برنامه حرکت اسب در صفحه شطرنج هست. بخوبی هم کار می کنه.

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int min,npos,l,m,counter,
board[9][9]={{0},{0}} ,nexti[9]={0},
exits[11][2] ,nextj[9]={0}, //main
ktmov1[9]={0,-2,-1,1,2,2,1,-1,-2}, // variables
ktmov2[9]={0,1,2,2,1,-1,-2,-2,-1};
int i,j,t,p,q,r,temp; //temporary required elements
void move(int min)
{ //This function
i = nexti[min]; //move horse place
j = nextj[min]; //if needed
board[i][j] = m;
}
void prompt(){
cout<<'\n'<<setw(22)<<"The chessboard is: "<<'\n'<<'\n';
for(p=1 ; p<=8 ; p++) {
for(q=1 ; q<=8 ; q++)
cout<<setw(4)<< board[p][q] ;
cout<< endl;
} // end_first for
} //end_prompt
void main(){
cout<<'\n'<<"Enter Number Of Row And Column You Want To Start From ? ";
cout<<'\n'<<" ( 0<row<9 and 0<column<9 ) >> ";
cin>>i>>j;
board[i][j] = 1;
for(m=2 ; m<=64 ; m++) //main loop
{
for(p=0 ; p<=10 ;p++) //giving
for(q=0 ; q<=1 ;q++) //zero to
exits[p][q]=0; //variables
for(p=0 ; p<=8 ;p++) //and arrays
nexti[r]=nextj[r]=0; //needed
l=1 ;npos=0;
for (r=1 ; r<=8 ; r++) //This loop
{ //find rows and columns of
p =i + ktmov1[r]; //places the horse
q =j + ktmov2[r]; //can go and save
if( p<=8 && p>0 //rows in 'nexti' array and
&& q<=8 && q>0 //column in 'nextj' array
&& board [p][q]==0)
{
nexti[l] = p;
nextj[l] = q;
++l;
++npos; //number of positions
} // end_if
} //end_for
if(npos == 0){ //This condition check
prompt(); //the next place .
return; }
if(npos == 1) { // This condition check
min = 1; // if there is only one
move(min); // place to go, move horse
continue; } // to it.
t=0;
for( l=1 ; l<=npos ; l++) // This loop find and
{ // save numbers of exits
counter=0; // from every next place
for( r=1 ; r<=8 ; r++)
{
p=nexti[l] + ktmov1[r];
q=nextj[l] + ktmov2[r];
if( p<=8 && p>0 // checking next
&& q<=8 && q>0 // move is in board
&& board [p][q]==0) // and is empty
++counter;} // or not
exits[t][0]=l;
exits[t][1]=counter;
t++;
} //end for l
t=0;
min=exits[t][0];
temp=exits[t][1];
t++;
while(exits[t][0]!=0){ // finding minimum exit
if(temp>exits[t][1]){ // in next place
temp=exits[t][1];
min=exits[t][0];} // end_if
++t;
} //end_while
move(min);

}
prompt(); //showing board
getch();
cin.get();
}