سلام یه توضیح در مورد الگوریتم حرکت اسب در صفحه ی شطرنج می خواستم
Printable View
سلام یه توضیح در مورد الگوریتم حرکت اسب در صفحه ی شطرنج می خواستم
سلام من دنبال کد سورس حرکت اسب در صفحه ی شطرنج هستم خیلی لازمش دارم. ممنون
این 64 حرکته :::::
[PHP]#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<iomanip.h>
#include<stdio.h>
#include<conio.h>
#define Cbakg 1
#define Cbakgtxt 2
#define Cbakgshattxt 4
#define Cbakgshatsiah 0
#define Cbakgshatsefid 7
void shat3(int x,int y,int nx,int ny,int tnx,int tny,int cse,int csi)
{
int i,j,k,l,m=0;
gotoxy(x,y);
for(i=0;i<tny;i++)
for(j=0;j<ny;j++){
m++;
for(k=0;k<tnx;k++){
if((i+k)%2==0)
textbackground(cse);
else
textbackground(csi);
for(l=0;l<nx;l++)
cprintf(" ");
}
gotoxy(x,y+m);
}
}
void main()
{
int sh[12][12]={0};
int h[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},
{-1,-2},{1,-2},{2,-1}};
int k=0,x,y,step=1,xNew,yNew,num,minNum=9,minK;
int l,i,j;
char ch;
textbackground(Cbakg);
textcolor(Cbakgtxt);
clrscr();
/////////////
//initialize
for(i=0;i<12;i++){
sh[i][0]=sh[i][1]=sh[i][10]=sh[i][11]=-1;
sh[0][i]=sh[1][i]=sh[10][i]=sh[11][i]=-1;
}
cprintf("inter x,y ");
scanf("%d%d",&x,&y);
x=(x-1)%8+1;
y=(y-1)%8+1;
clrscr();
cprintf("inter x,y x=%d y=%d",x,y);
x++;
y++;
sh[x][y]=step;
/////////////////
//begin calculate
for(step=2;step<=64;step++){
minNum=9;
for(k=0;k<8;k++){
xNew= x + h[k][0];
yNew= y + h[k][1];
if( sh[xNew][yNew] == 0){
sh[xNew][yNew] = step;
num=0;
for(l=0;l<8;l++)
if(sh[ xNew + h[l][0] ][ yNew + h[l][1] ]==0) num++;
if(num<minNum){
minNum=num;
minK=k;
}
sh[xNew][yNew] = 0;
}
}
x= x + h[minK][0];
y= y + h[minK][1];
sh[x][y]=step;
}
//end calculate
///////////////
///////////////
//output
textcolor(Cbakgshattxt);
shat3(10,1,8,3,8,8,Cbakgshatsiah,Cbakgshatsefid);
for(i=0;i<8;i++)
for(j=0;j<8;j++){
if((i+j)%2==0)
textbackground(Cbakgshatsiah);
else
textbackground(Cbakgshatsefid);
gotoxy(j*8+13,i*3+2);
cprintf("%2d",sh[i+2][j+2]);
}
textcolor(Cbakgtxt);
textbackground(Cbakg);
cprintf("\n\n");
gotoxy(20,25);
cprintf("To run Step By Step,press y...");
cprintf("\n");
cprintf("To Quit Press q ");
ch=getch();
if(ch=='y'||ch=='Y'){
clrscr();
gotoxy(20,27);
cprintf("To Quit Press q ");
shat3(10,1,8,3,8,8,Cbakgshatsiah,Cbakgshatsefid);
textcolor(Cbakgshattxt);
for(i=1;i<65;i++){
for(j=0;j<8;j++)
for(l=0;l<8;l++)
if(sh[j+2][l+2]==i){
if((l+j)%2==0)
textbackground(Cbakgshatsiah);
else
textbackground(Cbakgshatsefid);
gotoxy(l*8+13,j*3+2);
cprintf("%2d",i);
gotoxy(80,25);
}
ch=getch();
if(ch=='q'||ch=='Q')break;
}
}
}
[/PHP]
اینم از نوع دیگه که پروژه ی خودم بود. آدرس سطر ستون شروع رو میدی بعد هرچندتا حرکتی که میتونه انجام میده
[PHP]#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void hor(int,int);
int main()
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
//................rasme safheye shatrang
setcolor(1);
int x1=50,y1=20,x2=450,y2=20;
//line ofoghi
line(x1,y1,x2,y2);
line(x1,70,x2,70);
line(x1,120,x2,120);
line(x1,170,x2,170);
line(x1,220,x2,220);
line(x1,270,x2,270);
line(x1,320,x2,320);
line(x1,370,x2,370);
line(x1,420,x2,420);
//line amodi
line(x1,y1,x1,420);
line(100,y1,100,420);
line(150,y1,150,420);
line(200,y1,200,420);
line(250,y1,250,420);
line(300,y1,300,420);
line(350,y1,350,420);
line(400,y1,400,420);
line(450,y1,450,420);
setfillstyle(1,1);
int a1,b1;
int a2,b2;
//siah kardan khaneha
a1=50;
b1=20;
a2=100;
b2=70;
for(int ii=1;ii<=4;ii++)
{
for(int jj=1;jj<=4;jj++)
{
bar(a1,b1,a2,b2); // mostatil topor
a1+=100;
a2+=100;
}
a1=50,
a2=100;
b1+=100;
b2+=100;
}
//***************************
//siah;
x1=100;
y1=70;
x2=150;
y2=120;
for(int z=1;z<=4;z++)
{
for(int w=1;w<=4;w++)
{
bar(x1,y1,x2,y2);
x1+=100;
x2+=100;
}
x1=100,
x2=150;
y1+=100;
y2+=100;
}
//**********************************************
int s1,s2,xx,yy;
gotoxy(65,4);
cout<<"enter x:= ";
cin>>s1;
gotoxy(65,6);
cout<<"entr y:=";
cin>>s2;
int vertic[8]={2,1,-1,-2,-2,-1,1,2};
int horiz[8]={-1,-2,-2,-1,1,2,2,1};
int board[8][8];
if((s1>8) || (s1<1) || (s2>8) || (s2<1))
exit(1);
s1=s1-1;
s2=s2-1;
hor(s1,s2);
xx=s1;
yy=s2;
for(int i=0;i<8;i++)
{
s1=xx+horiz[i];
s2=yy+vertic[i];
if ((s1>=0)&&(s2>=0)&&(s1<8)&&(s2<8))
{
hor(s1,s2);
}
s1=xx;
s2=yy;
}
//********************************
getch();
closegraph();
return 0;
}
// rasm shekle asb va harkat
void hor(int s1,int s2)
{
int z;
z=getch();
int t1=50,t2=20;
t1+=s2*50;
t2+=s1*50;
setcolor(1);
setfillstyle(6,6);
bar(t1,t2,t1+50,t2+50);
setcolor(3);
rectangle(t1+2,t2+2,t1+48,t2+48);
setfillstyle(2,4);
bar(t1+4,t2+4,t1+46,t2+46);
rectangle(t1+6,t2+6,t1+45,t2+45);
}
[/PHP]
موفق باشی