• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

مسابقات شطرنج

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
در اين تاپيك مي خواهيم با همكاري دوستان با كدهايمان يك مسابقه شطرنج بسازيم.

برنامه مركزي دو تابع سياه باز و سفيد باز را صدا مي زنه و آرايه صفحه شطرنج را بهش مي دهد تا هر يك از آن دو تابع ، در يك رشته مثلا e2e4 حركت خود را اعلام كند.
برنامه يك قسمت test دارد كه درستي حركت را تشخيص مي دهد و بعدها هم ميزان زمان مصرف شده توسط اين تابع را.

البته اين طرحي بود كه همين جوري به ذهنم رسيد. دوستان حتما روشهاي بهتري بلدند.
.
بايد يك نفر بياد اعلام آمادگي كنه تا دو نفري شروع كنيم و بعد دوستاند ديگر هم از راه برسند.
فعلا با تابع راندم يا بررسي اولين خانه در دسترس شطرنج بازها كار مي كنند و برد و باخت مهم نيست. چون مي خواهيم برنامه مركزي و tester را بسازيم. بعدا ليگ مي گذاريم. و يك صفحه سايت هم مي سازيم تا اگر شد بين فرومي باشه و برنامه نويسان فرومهاي ديگه هم شركت كنند.
و ريتينگ هم يك جوري تعيين مي كنيم. و در سايت به ترتيب نزولي شركت كننده ها اعلام مي شوند. به مدير ورزش هم اطلاع مي دهم تا قوانين را آموزش دهد.


.يك صفحه شبيه ايننمونه


بعدا شايد ديگر زبانها هم همه با تبديل به php شركت داده شد. تا محيط زنده تري در سايت داشته باشيم.
.
 

hamedsullivan

مدیر بازنشسته
تاریخ عضویت
16 دسامبر 2005
نوشته‌ها
6,664
لایک‌ها
137
سن
45
محل سکونت
on the road
بنده برای همکاری در خدمتم و خوشحال میشم کاری انجام بدم.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
ممنون حامد جان.
لطف كرديد تشريف آورديد.
يكسري سيستم هاي ريتينگ دهي و يكسري قوانين انجام مسابقات و انواع محدوديت زماني مسابقات مد نظر من بود.
من قبلا باشگاهي شطرنج بازي مي كردم . ولي از قوانين داوري زياد اطلاع ندارم.
مطمئنم كه شما اطلاعات خوبي داريد در اين زمينه.
من به كمك شما براي اين كارها نياز دارم.
.
=================
به دوستان برنامه نويس عرض كنم كه اصلا برد و باخت مهم نيست. مهم ايجاد انگيزه براي كدنويسي و تبادل مهارتهاي فردي ما در اين زمينه است و در كنارش ايجاد يك جو صميمي ورزشي.
من 4 تا سورس الان دانلود كردم كه در سطوح مختلف كار شده بود.
آنها را آماده مي كنم تا بشود هسته برنامه .
شما هم كدها را آماده كنيد.
همان طور كه گفتم بايد آرايه صفحه شطرنج را بگيرد ومحاسباتي بكند و حركت را اعلام كند.
.
و نيازي نيست كه كاسپاروف را ببرد . همين كه كار كند و يك ليگ با آنها بشه برگزار كرد كافيه.
درود.
.
 

saeedsmk

مدیر بازنشسته
تاریخ عضویت
6 سپتامبر 2003
نوشته‌ها
1,518
لایک‌ها
4
با سلام
خوب هستید
به نظر من ایده خوبیه
امیدوارم عملی بشه

منتظر کامنت دیگر دوستان هم هستم
فعلا
 

shahab_faghihi

کاربر تازه وارد
تاریخ عضویت
12 اکتبر 2005
نوشته‌ها
16
لایک‌ها
0
محل سکونت
Tehran
سلام.

فکر جالبیه ... من هستم :blush:
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
سلام آقا سعيد.
ممنون از حمايت .
--------------------
و با سلام خدمت مديريت هيئت امناي انجمن سي مجيدآنلاين.
خوشحال مي شوم با دوستان قديمي خود بيشتر در ارتباط باشم.
مخصوصا آقا حميد كه اهل شطرنج هم هست و استيل برنامه نويسي قشنگي هم دارد.
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
بنام خدا.
يك طرح پيشنهادي از برنامه مركزي را نوشتم.
يك آرايه دو بعدي گلوبال با حرف x گرفتم كه 9 در 9 است . كه حكم صفحه شطرنجي را دارد كه بازيكن ها مي بينند.
به اين علت بجاي 8 خانه اي ، 9 خانه اي گرفتم تا با اعداد 1 تا 8 كار كنيم تا راحت تر باشد.
تابع هاي بازيكن به اين آرايه چون گلوبال است دسترسي دارند. من نخواستم آرايه به تابع هاي بازيكن بفرستم ، پس صفحه نمايش را گلوبال گرفتم.
البته يك آرايه دو بعدي كه دقيقا مشابه آن است ، هم به شكل استاتيك در ((تابع مخزن)) تعريف كردم.
كه در واقع آن آرايه اصلي است و آن آرايه گلوبال فقط براي مشاهده تابع هاي بازيكن است.
تابع مخزن در شروع برنامه اصلي با صدا كردن با عدد 1 كه همان haalat در تعريف ورودي تابع مخزن است ، آرايه مخفي را با چيدن مهره ها مقدار اوليه مي دهد.
و با عدد 2 اگر تابع مخزن را بخوانيم باعث تغيير آرايه اصلي و در حقيقت ، حركت مهره ها مي شود.
و وقتي با عدد 3 صدا بزنيم ، باعث نو شدن آرايه دو بعدي كاذب x مي شود. اين نو شدن بعد احضار تابع هاي بازيكن به اين علت است كه ممكن است تابع هاي بازيكن به طور تصادفي آرايه گلوبال را تغيير داده باشند.

چهار متغير int گلوبال هم داريم كه تابع هاي بازيكن در آنها اعداد مبدا و مقصد حركت را وارد مي كنند.
تابع هاي بازيكن فقط يك ورودي مي گيرند كه به آنها مي گويد كه آنها سفيد هستند يا سياه. و بعد با نگاه به آرايه گلوبال و تغيير 4 متغير int گلوبال با بقيه توابع ارتباط برقرار مي كنند.

شرح برنامه اصلي:
در برنامه اصلي در كل چيزي به جز يك حلقه while نداريم ، كه
اول سفيدباز را صدا مي كند.(با ارسال عدد يك به تابع مي گويد كه سفيد است. چون بعدا بازيكناني بايد بنويسيم كه هم بتوانند سفيد بازي كنند و هم سياه. )
بعد آرايه گلوبال را ترميم مي كند ،
بعد تابع چك كردن صحت حركت سفيد را احضار مي كند
و بعد اگر تابع چك ،تاييد كرد حركت سفيد را ،
تابع مخزن را براي انجام حركت صدا مي زند.

همين كارها براي سياه هم انجام مي شود.

اگر برگشتي از تابع هاي چك نشان دهنده خطا باشد ، حلقه قطع شده و وارد اعلام نتايج مي
شويم.

فعلا تابع هاي بازيكن اضافه نشده و فقط آنها يك حركت براي نمايش طرز كار برنامه برمي گردانند.

همچنين تابع هاي چك كننده نوشته نشده اند و فعلا به طور دائمي عدد 1 به نشانه تاييد را برمي گردانند.

برنامه زير اجرا مي شود و دو حركت اول سفيد و سياه را نشان مي دهد. براي داشتن حركات بعدي بايد تابع هاي بازيكن نوشته شود.



کد:
#include <iostream.h>
#include <conio.h>
#include <math.h>
int x[9][9];int a,b,c,d;
void makhzan(int haalat);
void namayesh();
  void player1(int rang);
  void player2(int rang);
	  int white_checker();
	  int black_checker();

void main()
{
int white_checker_return, black_checker_return;
 makhzan(1);
 namayesh();
 getch();
  while(1)
	 {
	 player1(1);
	 makhzan(3);
	 white_checker_return=white_checker();
	 if (white_checker_return != 1) break;
	 if (white_checker_return==1) makhzan(2);
	 namayesh();
	 getch();

	 player2(2);
	 makhzan(3);
	 black_checker_return=black_checker();
	  if (black_checker_return != 1) break;
	 if (black_checker_return==1) makhzan(2);
	 namayesh();
	 getch();
	 }
	 if (white_checker_return == 2) cout<<endl<<" white loses";
	 if (black_checker_return ==2 ) cout<<endl<<" black loses";
	getch();

}

void makhzan(int haalat)
  {
  static int y[9][9];  // main array
int counter;
	switch (haalat)
			{
case 1:       // chidan
for ( counter=1 ; counter<9 ;counter++)
{   y[counter][2]=1; y[counter][7]=11 ; }   // chidane sarbaz haa
y[1][1]=4;y[8][1]=4; y[1][8]=14;y[8][8]=14; // chidane rokh haa
y[2][1]=2;y[7][1]=2; y[2][8]=12;y[7][8]=12; // chidane asb haa
y[3][1]=3;y[6][1]=3; y[3][8]=13;y[6][8]=13; // chidane fil haa
y[4][1]=5;y[5][1]=6; y[4][8]=15;y[5][8]=16; // chidane shaah va vazir
  break;
case 2:      // mover

  y[c][d]=y[a][b];
  y[a][b]=0;
  break;
case 3:
		  // none =global reset
  break;
default:
  cout<<"incorrect call of <<makhzan>>  "<<endl;
		  }
	for (int counter1=0 ; counter1<9 ; counter1++)
	for (int counter2=0 ; counter2<9 ; counter2++)
		x[counter1][counter2]=y[counter1][counter2]  ;
  }  // end makhzan

void namayesh()
 {
 clrscr();
 int counter;
		for ( int counter1=1 ; counter1<9 ; counter1++)
		for ( int counter2=1 ; counter2<9 ; counter2++)
			  {
				 gotoxy(5*counter1 ,counter2*2);
					cout<<x[counter1][9-counter2];
			  }
 }  // end namayesh

void player1(int rang)
  {
  a=1;b=2;c=1;d=3;
  }
void player2(int rang)
  {
  a=1;b=7;c=1;d=6;
  }
int white_checker()
  {
 return 1;
  }
int black_checker()
  {
  return 1;
  }
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
اولا بايد اسكلت برنامه مركزي اصلاح شود.
دوما با برنامه هاي ويژوالي ، نمايش زيباتري داشته باشيم.
سوما من و شما بازيكن هايي ولو ساده بنويسيم.
چهارما چك گرها را با دقت بنويسيم تا گزارش گر خطاهاي تابع هاي بازيكن باشد.
.بعد خروج از حلقه ، انواع و اقسام گزارشات چك گرها بايد چاپ شود تا بفهميم بازيكنان چه خطاهايي داشته اند.
.
 

♂MAJID

کاربر تازه وارد
تاریخ عضویت
28 مارس 2005
نوشته‌ها
1,239
لایک‌ها
2
سن
37
محل سکونت
HellioN !!!
بازی جالبیه !!!
موفق باشید !!
 

saalek110

Registered User
تاریخ عضویت
10 آپریل 2007
نوشته‌ها
212
لایک‌ها
1
http://saalek110.250free.com/projects/chess.rar

یک سورس گرافیکی سی شطرنج.

کد:
/****************************************************************
					  Chess Game  V1.0

			 If u like this Program then pls vote it
/*****************************************************************/



//THIS PROGRAM WILL CREATE A TWO PLAYER CHESS


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
#include<dos.h>



void move_control (int[8][8],int[],int x1=171,int y1=171,int x2=221, int y2=221);	//specifying default values
void move_right (int&,int,int&,int,int);
void move_left (int&,int,int&,int,int);
void move_down (int,int&,int,int&,int);
void move_up (int,int&,int,int&,int);
void decolorize_shadow (int,int,int,int);
void chessboard (void);
void piece_position ( int[8][8] );
void place_pieces (int[8][8],int[8][8]);
void draw_pawn(int,int,int,int);
void draw_knight(int,int,int,int);
void draw_bishop(int,int,int,int);
void draw_rook(int,int,int,int);
void draw_king(int,int,int,int);
void draw_queen(int,int,int,int);
void coordinate_manager(int*,int*);
int verify_move (int,int,int,int,int[8][8]);
int jump_test (int,int,int,int,int[8][8]);
int move_pawn (int,int,int,int,int,int[8][8],int[8][8],int*);
void update_pawns(int[8][8],int,int,int,int,int);
void move_piece (int,int,int,int,int[8][8],int[8][8]);
void king_status(int[8][8]);
void rubber(int,int,int);
void messages (int);



extern int turn = 0;		//global variable



void main (void)
{
	clrscr();

	int positions[8][8] = {0};	//contains positions of all pieces on the board
	int colormap[8][8] = {0};	//contains color with which pieces will be drawn
	int move_info[4];		//contains informations of a piece's (that has been moved) old and new positions	[newx] [newy] [oldx] [oldy]
	int gatekeeper = 0;

	int gdriver = DETECT, gmode;
	initgraph (&gdriver, &gmode, "C:\\TC\\BGI");	//starting graphics engine

	while ( 1 )
	{
		if ( gatekeeper == 0 )
		{
			chessboard ();				//drawing chessboard
			piece_position ( positions ); 		//placing pieces (internally)
			place_pieces ( positions,colormap );	//placing pieces (graphically)
		}
		messages ( 0 );
		move_control ( positions,move_info );	//piece to move
		move_piece ( move_info[0],move_info[1],move_info[2],move_info[3],positions,colormap);	//moves a piece
		king_status ( positions );

		if ( turn == 0 )
			turn = 1;
		else
			turn = 0;
		gatekeeper++;
	}//end of while

}//end of main function



void chessboard (void)
{
	clrscr();

	setfillstyle(SOLID_FILL,LIGHTGREEN);	//colours the
	floodfill(320,240,2);            //background

	setlinestyle (SOLID_LINE,1,THICK_WIDTH);	//for thick edge of the board

	setcolor(BLACK);
	int x1=21,y1=21,x2=71,y2=71;
	for (int j=1; j <= 8; j++)
	{
		for (int i=1; i <= 8; i++)
		{
			rectangle(x1,y1,x2,y2);

			if ( j%2 == 0)
				if ( i%2 == 0 )
					setfillstyle (SOLID_FILL, BLACK);	//colouring
				else
					setfillstyle (SOLID_FILL, WHITE);
			else                                      //alternate
				if ( i%2 == 0 )
					setfillstyle (SOLID_FILL, WHITE);
				else                                   //boxes
					setfillstyle (SOLID_FILL, BLACK);

			floodfill (x1+3, y1+3, BLACK);

			x1 += 50;
			x2 += 50;
		}//end of loop (that prints boxes in horizontal)
		x1 = 21;
		x2 = 71;
		y1 += 50;
		y2 += 50;
	}//end of for loop (that prints boxes in vertical)

	setcolor(WHITE);
	setlinestyle (SOLID_LINE,1,THICK_WIDTH);
	setfillstyle (SOLID_FILL, BLACK);		//draws the
	rectangle (473,21,619,91);			//message window
	floodfill (483,25,WHITE);
	setlinestyle (SOLID_LINE,1,NORM_WIDTH);

}//end of chessboard function



void piece_position ( int positions[8][8] )
{
	enum  {WHITE_PAWN=1, WHITE_ROOK, WHITE_KNIGHT, WHITE_BISHOP,//specifying constants ranging
			 WHITE_QUEEN, WHITE_KING, BLACK_PAWN, BLACK_ROOK,		//from 1 to 12. e.g., 6 represent white_king
			 BLACK_KNIGHT, BLACK_BISHOP, BLACK_QUEEN, BLACK_KING};//and 11 represents black_queen


	positions[0][0] = ( positions[7][0] = BLACK_ROOK );
	positions[1][0] = ( positions[6][0] = BLACK_KNIGHT );
	positions[2][0] = ( positions[5][0] = BLACK_BISHOP );
	positions[3][0] = BLACK_QUEEN;
	positions[4][0] = BLACK_KING;
	for ( int xindx=0; xindx < 8; xindx++ )
		positions[xindx][1] = BLACK_PAWN;


	positions[0][7] = ( positions[7][7] = WHITE_ROOK );
	positions[1][7] = ( positions[6][7] = WHITE_KNIGHT );
	positions[2][7] = ( positions[5][7] = WHITE_BISHOP );
	positions[3][7] = WHITE_QUEEN;
	positions[4][7] = WHITE_KING;
	for ( xindx=0; xindx < 8; xindx++ )
		positions[xindx][6] = WHITE_PAWN;

}//end of piece_position function



void place_pieces (int positions[8][8],int colormap[8][8])
{
	int color, i, j;

	enum Team {BLACK, WHITE};	//specifying constants
	Team turn;			//declaring a variable whose 0 value is equal to BLACK and 1 equal to WHITE

	turn = BLACK;

	for (int y=0; y<8; y++)
		for (int x=0; x<8; x++)
		{
			if ( ((y+1)%2) == 0 )
			{
				if ( ((x+1)%2) == 0 )
					colormap[x][y] = 15;
				else
					colormap[x][y] = 0;
			}
			else
			{
				if ( ((x+1)%2) == 0 )
					colormap[x][y] = 0;
				else
					colormap[x][y] = 15;
			}
		}//end of 'x' for


	for (int Y=0; Y < 8; Y++)
		for (int X=0; X < 8; X++)
		{
			i = X;      //will be sent to
			j = Y;      //coordinate manager

			if ( positions[X][Y] >= 1 && positions[X][Y] <= 6 )
				turn = WHITE;

			color = colormap[X][Y];

			coordinate_manager ( &i,&j );	//converting into screen coordinates

			switch ( positions[X][Y] )
			{
				case	1:
				case	7:    rubber (i,j,color);
								draw_pawn (i, j, turn, color);
								break;
				case	2:
				case	8:    rubber (i,j,color);
								draw_rook (i, j, turn, color);
								break;
				case	3:
				case	9:    rubber (i,j,color);
								draw_knight (i, j, turn, color);
								break;
				case	4:
				case	10:   rubber (i,j,color);
								draw_bishop (i, j, turn, color);
								break;
				case	5:
				case	11:   rubber (i,j,color);
								draw_queen (i, j, turn, color);
								break;
				case	6:
				case	12:   rubber (i,j,color);
								draw_king (i, j, turn, color);
								break;
			}//end of draw_piece switch structure
		}//end of loop with X

}//end of draw_pieces function



void coordinate_manager (int *x, int *y)
{
	if (*x >= 0 && *x <= 7)		 //to differentiate if the coordinates are to be changed according to board or to the screen
	{
		*x = (*x * 50) + 21;		 //row-no = (row-no * horizontal lenght of a box) + distance from left side of screen
		*y = (*y * 50) + 21;     //converting board coordinates into screen coodinates
	}
	else
	{
		*x = (*x - 21) / 50;		 //converting screen coordinates into board coordinates
		*y = (*y - 21) / 50;
	}

}//end of coordinate_manager



void rubber(int x,int y, int color)
{
	int tempcolor=0;
	if ( color == 0 )
		tempcolor = 15;
	setfillstyle (SOLID_FILL,tempcolor);
	bar (x+10,y+10,x+40,y+40);		//rubbing the previous contents


}//end of rubber box



void draw_pawn (int x,int y,int turn,int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle (CLOSE_DOT_FILL,WHITE);
	floodfill (x+32,y+32,color);

	if (turn == 0)
		setfillstyle (SOLID_FILL,BLACK);
	else
		setfillstyle (SOLID_FILL,WHITE);

	setcolor(LIGHTGRAY);
	circle (x+25,y+25,7);
	floodfill (x+25,y+25,LIGHTGRAY);

}//end of draw_pawn function



void draw_rook (int x,int y,int turn,int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle (INTERLEAVE_FILL,WHITE);
	floodfill (x+25,y+25,color);

	if (turn == 0)
		setfillstyle (SOLID_FILL,BLACK);
	else
		setfillstyle (SOLID_FILL,WHITE);

	setcolor (LIGHTGRAY);
	bar (x+16,y+14,x+23,y+22);		//upper left
	bar (x+27,y+14,x+34,y+22);    //upper right
	bar (x+27,y+28,x+34,y+36);    //lower right
	bar (x+16,y+28,x+23,y+36);    //lower left

}//end of draw_rook function



void draw_knight (int x,int y,int turn,int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle (XHATCH_FILL,WHITE);
	floodfill (x+25,y+25,color);

	if (turn == 0)
		setfillstyle (SOLID_FILL,BLACK);
	else
		setfillstyle (SOLID_FILL,WHITE);

	setcolor (LIGHTGRAY);
	line (x+18,y+16,x+32,y+16);	//top
	line (x+18,y+16,x+18,y+27);   //left
	line (x+32,y+16,x+32,y+27);	//right
	arc (x+25,y+28,179,270,7);
	arc (x+25,y+28,270,0,7);
	floodfill(x+25,y+25,LIGHTGRAY);

	if ( turn == 0 )
		color = 15;
	else
		color = 0;

	setcolor (color);
	line (x+22,y+23,x+28,y+23);
	line (x+25,y+19,x+25,y+31);

}//end of draw_knight function



void draw_bishop (int x,int y,int turn,int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle (INTERLEAVE_FILL,WHITE);
	floodfill (x+25,y+25,color);

	if (turn == 0)
		setcolor(BLACK);
	else
		setcolor(WHITE);

	setlinestyle(SOLID_LINE,0,THICK_WIDTH);
	line (x+16,y+22,x+34,y+22);
	line (x+25,y+15,x+25,y+35);
	setlinestyle(SOLID_LINE,1,NORM_WIDTH);

}//end of draw_bishop function



void draw_queen (int x,int y,int turn,int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle (WIDE_DOT_FILL,WHITE);
	floodfill (x+25,y+25,color);

	setcolor (LIGHTGRAY);
	line (x+15,y+29,x+18,y+19);
	line (x+18,y+19,x+22,y+27);
	line (x+22,y+27,x+24,y+19);
	line (x+25,y+19,x+27,y+27);
	line (x+27,y+27,x+30,y+19);
	line (x+31,y+19,x+33,y+29);
	arc (x+24,y+23,207,347,10);

	if (turn == 0)
		setfillstyle (SOLID_FILL,BLACK);
	else
		setfillstyle (SOLID_FILL,WHITE);

	floodfill(x+25,y+24,LIGHTGRAY);
}//end of draw_queen function



void draw_king (int x,int y,int turn, int color)
{
	setcolor (color);
	rectangle (x+10,y+10,x+40,y+40);
	setfillstyle(WIDE_DOT_FILL,WHITE);
	floodfill(x+25,y+25,color);

	setcolor (LIGHTGRAY);
	line (x+18,y+31,x+20,y+18);
	line (x+20,y+18,x+22,y+26);
	line (x+22,y+26,x+24,y+18);
	line (x+24,y+18,x+26,y+26);
	line (x+26,y+26,x+28,y+18);
	line (x+28,y+18,x+30,y+31);
	arc (x+24,y+19,242,300,15);

	if (turn == 0)
		setfillstyle (SOLID_FILL,BLACK);
	else
		setfillstyle (SOLID_FILL,WHITE);

	floodfill(x+24,y+21,LIGHTGRAY);

	if ( turn == 0 )
		color = 15;
	else
		color = 0;

	setcolor(color);
	line (x+24,y+31,x+24,y+25);
	line (x+22,y+27,x+26,y+27);
}//end of draw_king function



void move_control (int positions[8][8],int move_info[],int x1,int y1,int x2, int y2)
{

	int call=0, x, y;
	int color = 2;
	static int xx1=x1,yy1=y1,xx2=x2,yy2=y2;	//to remove the image

	setcolor(color);
	setlinestyle (SOLID_LINE,1,THICK_WIDTH);
	rectangle(x1,y1,x2,y2);			//draws the box in its initial position as specified by the default values

	do
	{
		char detect_zero,direction;

		detect_zero = getch();

		if ( detect_zero != 27 && detect_zero == 0)		//avoiding the escape key
		{
			direction = getch();

			switch ( direction )
			{
				case	77:   move_right (x1,y1,x2,y2,color);
								break;
				case	80:	move_down (x1,y1,x2,y2,color);
								break;
				case	75:	move_left (x1,y1,x2,y2,color);
								break;
				case	72:	move_up (x1,y1,x2,y2,color);
								break;
				default :   messages ( 4 );
			}//end of switch
		}//end of 'avoiding escape key' if structure

		color = 2;
		setcolor(color);	//changing color back to 2 because it is turn 0 after the switch structure
		rectangle(x1,y1,x2,y2);


		x = x1;	//making copies
		y = y1;  //to send to coordinate manager

		if ( detect_zero == 27 )		//exiting on escape key
			exit(1);

		if ( detect_zero == 13 )		//detecting enter key
		{
			coordinate_manager( &x,&y );
			if ( call == 0 )
			{
				color = 4;
				setcolor(color);                  //leavs an image
				rectangle(x1,y1,x2,y2);       //on the selected box
				xx1=x1, xx2=x2, yy1=y1, yy2=y2;	//updating values

				if ( verify_move (x,y,turn,call,positions) == 0 )
				{
					color = 2;
					messages ( 1 );
					setcolor(color);
					rectangle(x1,y1,x2,y2);
				}
				else
				{
					call = 1;
					move_info[2] = x;
					move_info[3] = y;
				}
			}

			else
			{
				setcolor (0);
				rectangle(xx1,yy1,xx2,yy2);
				rectangle(x1,y1,x2,y2);

				if ( verify_move (x,y,turn,call,positions) == 0 )
				{
					messages ( 2 );
					call = 0;
				}
				else
				{
					move_info[0] = x;
					move_info[1] = y;
					setlinestyle(SOLID_LINE,1,NORM_WIDTH);
					break;
				}
			}//end of else
		}//end of if that detects enter key
	}//end of while loop	that controls the box movement
	while (1);

}//end of move_control function


void move_right (int &x1,int y1,int &x2,int y2,int color)		//accepting as refrence variables
{
	if ( x2 <= 371 )		//preventing the box to go out of the board
	{
		if ( color == 2 )
			decolorize_shadow (x1,y1,x2,y2);		//preventing the image from decolorizing
		x1 += 50;
		x2 += 50;
	}

}//end of move_left function


void move_down (int x1,int &y1,int x2,int &y2,int color)
{
	if ( y2 <= 371 )
	{
		if ( color == 2 )
			decolorize_shadow (x1,y1,x2,y2);
		y1 += 50;
		y2 += 50;
	}
}//end of move_down function


void move_left (int &x1,int y1,int &x2,int y2,int color)
{
	if ( x1 > 21 )
	{
		if ( color == 2 )
			decolorize_shadow (x1,y1,x2,y2);
		x1 -= 50;
		x2 -= 50;
	}
}//end of move_left function


void move_up (int x1,int &y1,int x2,int &y2,int color)
{
	if ( y1 > 21)
	{
		if ( color == 2 )
			decolorize_shadow (x1,y1,x2,y2);
		y1 -= 50;
		y2 -= 50;
	}
	color = 1;
}//end of move_up function


void decolorize_shadow (int x1,int y1,int x2,int y2)
{
	setcolor (0);
	rectangle (x1,y1,x2,y2);
}//end of decolorize_shadow



int verify_move (int x,int y,int turn,int call,int positions[8][8])
{
	int pid=0;

	static int knight_check;	//used later to aviod calling jump_test function
	static int oldx,oldy;		//stores x,y from 1st call to this function
	static int copy_array = 0;
	static int pawn_positions[8][8] = {0};


	if ( copy_array == 0 )
	{
		int pid = 1;
		for (int j=0; j<8; j++)
			for (int i=0; i<8; i++)
				if ( positions[i][j] == 1 || positions[i][j] == 7 )
				{
					pawn_positions[i][j] = pid;	//pawn ID
					pid++;
				}
	copy_array = 1;	//so that pawn_positions is not re-copied next time
	}

	if ( call == 0 )
	{
		oldx = x;
		oldy = y;   //storing old values for later use

		if ( positions[x][y] == 0 )
			return 0;			//invalid box

		else if ( turn == 0 )
			if ( positions[x][y] >= 7 && positions[x][y] <= 12 )
			{
				if ( positions[x][y] == 9 )
					knight_check = 1;		//black knight found
				else
					knight_check = 0;		//black knight not found
				return 1;		//valid piece
			}
			else
				return 0;		//invalid piece

		else
			if ( positions[x][y] >= 1 && positions[x][y] <= 6 )
			{
				if ( positions[x][y] == 3 )
					knight_check = 1;		//white knight found
				else
					knight_check = 0;		//white knight not found
				return 1;
			}
			else
				return 0;
	}//end of first call if structure

	else
	{
		switch ( positions[oldx][oldy] )
		{
			case	1:
			case	7:    if ( move_pawn(oldy,y,oldx,x,turn,positions,pawn_positions,&pid) )
								break;
							else
								return 0;
			case	2:
			case	8:		if ( oldx!=x && oldy==y )
								break;
							else if ( oldy!=y && oldx==x )
								break;
							else
								return 0;
			case	3:
			case	9:    if ( abs(oldx-x)==2 && abs(oldy-y)==1 )
								break;
							else if ( abs(oldx-x)==1 && abs(oldy-y)==2 )
								break;
							else
								return 0;
			case	4:
			case	10:   if ( abs(oldx-x) == abs(oldy-y) )
								break;
							else
								return 0;
			case	5:
			case	11:   if ( oldx!=x && oldy==y )
								break;
							else if ( oldx==x && oldy!=y )
								break;
							else if ( abs(oldx-x) == abs(oldy-y) )
								break;
							else
								return 0;
			case	6:
			case	12:   if ( abs(oldx-x) == 1 )
								break;
							else if ( abs(oldy-y) == 1 )
								break;
							else
								return 0;
		}//end of switch

		if ( positions[x][y] == 0 )
		{
			if ( knight_check != 1 )
			{
				if ( jump_test(oldx,oldy,x,y,positions) )
				{
					update_pawns(pawn_positions,oldx,oldy,x,y,pid);
					return 1;
				}
				else
					return 0;
			}//end of knight check
			else
				return 1;
		}//end of empty box found

		if ( turn == 0 )
		{
			if ( positions[x][y] >= 1 && positions[x][y] <= 6 )
			{
				if ( knight_check != 1 )
				{
					if ( jump_test(oldx,oldy,x,y,positions) )
					{
						update_pawns(pawn_positions,oldx,oldy,x,y,pid);
						return 1;
					}
					else
						return 0;
				}//end of knight check
				return 1;
			}//end of white piece found
		}//end of black team's turn

		else
		{
			if ( positions[x][y] >= 7 && positions[x][y] <= 12 )
			{
				if ( knight_check != 1 )
				{
					if ( jump_test(oldx,oldy,x,y,positions) )
					{
						update_pawns(pawn_positions,oldx,oldy,x,y,pid);
						return 1;
					}
					else
						return 0;
				}//end of knight check
				return 1;
			}//end of black piece found
		}//end of white team's turn
	}//end of second call if structure

return 0;
}//end of verify_move function



int jump_test (int oldx,int oldy,int x,int y,int positions[8][8])
{
	int jumps = 0;
	int temp;

	int piece_id = positions[oldx][oldy];	//because the following if structure may change the values of oldx and oldy (used in the switch)

	do
	{
		if ( positions[oldx][oldy]==4 || positions[oldx][oldy]==10 || positions[oldx][oldy]==5 || positions[oldx][oldy]==11 )	//don't want to swap the values in case of left to right diagonal	movements
		{
			if ( (oldx>x) && (oldy<y) )
				break;									//breaking out in case of left
			else if ( (oldx<x) && (oldy>y) )
				break;                           //to right diagonal movement
		}

		if ( positions[oldx][oldy]!=1 && positions[oldx][oldy]!=7 )	//excluding pawns because they work differently
		{
			if ( oldx < x )      //storing larger value in oldx & oldy to simplify the following switch structure
			{
				temp = oldx;
				oldx = x;
				x = temp;
			}
			if ( oldy < y )
			{
				temp = oldy;
				oldy = y;
				y = temp;
			}
		}
		break;
	}
	while ( 1 );	//the loop is used only because break statement was required


	switch ( piece_id )
	{
		case	1:    while ( oldy>y && oldx==x)
						{
							if ( positions[x][oldy-1] != 0 )
								jumps++;
							oldy--;
						}break;
		case	7:    while ( oldy<y && oldx==x)
						{
							if ( positions[x][oldy+1] != 0 )
								jumps++;
							oldy++;
						}break;
		case	2:
		case	8:		while ( oldx > x+1 )
						{
							if ( positions[oldx-1][y] != 0 )
								jumps++;
							oldx--;
						}
						while ( oldy > y+1 )
						{
							if ( positions[x][oldy-1] != 0 )
								jumps++;
							oldy--;
						}break;
		case	4:
		case	10:   while ( (oldx>x+1) && (oldy>y+1) )
						{
							if ( positions[oldx-1][oldy-1] != 0 )
							{
								jumps++;
								break;
							}
							oldx--;
							oldy--;
						}//end of while
						while ( (oldx<x-1) && (oldy>y+1) )
						{
							if ( positions[oldx+1][oldy-1] != 0 )
								jumps++;
							oldx++;
							oldy--;
						}//end of while
						while ( (oldx>x+1) && (oldy<y-1) )
						{
							if ( positions[oldx-1][oldy+1] != 0 )
								jumps++;
							oldx--;
							oldy++;
						}//end of while
						break;
		case	5:
		case	11:   while ( (oldx>x+1) && (oldy==y) )
						{
							if ( positions[oldx-1][y] != 0 )
								jumps++;
							oldx--;
						}
						while ( (oldy>y+1) && (oldx==x) )
						{
							if ( positions[x][oldy-1] != 0 )
								jumps++;
							oldy--;
						}
						while ( (oldx>x+1) && (oldy>y+1) )
						{
							if ( positions[oldx-1][oldy-1] != 0 )
								jumps++;
							oldx--;
							oldy--;
						}
						while ( (oldx<x-1) && (oldy>y+1) )
						{
							if ( positions[oldx+1][oldy-1] != 0 )
								jumps++;
							oldx++;
							oldy--;
						}//end of while
						while ( (oldx>x+1) && (oldy<y-1) )
						{
							if ( positions[oldx-1][oldy+1] != 0 )
								jumps++;
							oldx--;
							oldy++;
						}//end of while
						break;
	}//end of switch

	if ( jumps == 0 )
		return 1;

return 0;
}//end of jump_test function




int move_pawn (int oldy,int y,int oldx,int x,int turn,int positions[8][8],int pawn_positions[8][8],int *pid)
{
	static int firstmove[16] = {0};

	*pid =  pawn_positions[oldx][oldy];

	if ( turn == 0 )
	{
		if ( ((y-oldy)==1 || (y-oldy)==2) && oldx==x )
		{
			if ( (firstmove[*pid-1]==0) || (firstmove[*pid-1]!=0 && (y-oldy)==1) )
				if ( jump_test(oldx,oldy,x,y,positions) )
				{
					firstmove[*pid-1] = 1;
					return 1;
				}
			else if ( (y-oldy) == 1 )
				return 1;
		}

		else if ( (y-oldy)==1 && abs(oldx-x)==1 )
		{
			if ( positions[x][y]>=1&&positions[x][y]<=6&&positions[x][y]!=0 )
			{
				firstmove[*pid-1] = 1;
				return 1;
			}
		}

		else
			return 0;
	}//black end

	else
	{
		if ( ((oldy-y)==1 || (oldy-y)==2) && oldx==x )
		{
			if ( (firstmove[*pid-1]==0) || (firstmove[*pid-1]!=0 && (oldy-y)==1) )
				if ( jump_test(oldx,oldy,x,y,positions) )
				{
					firstmove[*pid-1] = 1;
					return 1;
				}
			else if ( (oldy-y) == 1 )
				return 1;
		}

		else if ( (oldy-y)==1 && abs(oldx-x)==1 )
		{
			if ( positions[x][y]>=7&&positions[x][y]<=12 )
			{
					firstmove[*pid-1] = 1;
					return 1;
			}
		}

		else
			return 0;
	}//white end

return 0;
}//end of move_pawn function



void update_pawns (int pawn_positions[8][8],int oldx,int oldy,int x,int y,int pid)
{
	for (int j=0; j<8; j++)
		for (int i=0; i<8; i++)
			if ( pawn_positions[i][j] == pid )
			{
				pawn_positions[x][y] = pid;
				pawn_positions[oldx][oldy] = 0;
				break;
			}
}//end of update_pawns function



void move_piece (int x,int y,int oldx,int oldy,int positions[8][8],int colormap[8][8])
{
	positions[x][y] = positions[oldx][oldy];	//moving a piece internally

	int i = oldx;
	int j = oldy;
	int color;

	for (int count=0; count<2; count++)			//moving graphically
	{
		color = colormap[i][j];

/*		if ( count == 0 )            	//to rub a piece from its
		{                             //old position
			if ( colormap[i][j] == 1 )
				color = 15;
			else
				color = 1;
		}*/

		switch ( positions[i][j] )
		{
			case	1:
			case	7:    coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_pawn(i,j,turn,color);
							break;
			case	2:
			case	8:		coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_rook(i,j,turn,color);
							break;
			case	3:
			case	9:		coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_knight(i,j,turn,color);
							break;
			case	4:
			case	10:   coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_bishop(i,j,turn,color);
							break;
			case	5:
			case	11:   coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_queen(i,j,turn,color);
							break;
			case	6:
			case	12:   coordinate_manager( &i,&j );
							rubber (i,j,color);
							if ( count )
								draw_king(i,j,turn,color);
							break;
		}//end of switch
		i = x;
		j = y;
		positions[oldx][oldy] = 0;		//rubbing a piece internally
	}//end of if

}//end of move-piece function


void king_status (int positions[8][8])
{
	int king=0;

	if ( turn == 0 )
	{
		for (int y=0; y<8; y++)
		{
			for (int x=0; x<8; x++)
				if ( positions[x][y] == 6 )
				{
					king = 1;
					break;
				}
			if ( x != 8 )//checking if upper break was executed
				break;
		}
		if ( king == 0 )
		{
			messages ( 3 );
			getch();
			exit(1);
		}
	}//black end

	else
	{
		for (int y=0; y<8; y++)
		{
			for (int x=0; x<8; x++)
				if ( positions[x][y] == 12 )
				{
					king = 1;
					break;
				}
			if ( x != 8 )
				break;
		}
		if ( king == 0 )
		{
			messages ( 4 );
			getch();
			exit(1);
		}
	}//white end
}//end of king_status function


void messages ( int msgid )
{
	gotoxy (61,5);

	switch ( msgid )
	{
		case	0:    gotoxy(63,3);
						if ( turn == 0 )
							cout<<"BLACK'S TURN";
						else
							cout<<"WHITE'S TURN";
						break;
		case	1:    cout<<"COULDN'T SELECT";
						break;
		case	2:		cout<<" ILLEGAL MOVE";
						break;
		case	3:		cout<<"  WHITE LOST";
						break;
		case	4:		cout<<"  BLACK LOST";
						break;
		case	5:		cout<<"   CAN'T DO";
						break;

	}//end of switch

	if ( msgid != 0 )
	{
		delay (1000);
		gotoxy (61,5);
		textcolor (0);
		cout<<"                 ";
	}

}//end of msgind function

ScreenShot.GIF

با توربوسی 3 اجرا کردم بی خطا اجرا شد.
به نوبت با دکمه های arrow و اینتر مهره های سیه و سفید را حرکت می دادیم.
(یک مربع هست که با کلیدهای جهت روی مهره ها می بیرید و مهره را انتخاب می کنید.)
 

saalek110

Registered User
تاریخ عضویت
10 آپریل 2007
نوشته‌ها
212
لایک‌ها
1
http://www.programmersheaven.com/download/52962/download.aspx

سورس شطرنج در سی.
تست شده با توربو سی 3 .
میشه جای سفید و سیاه بازی کرد. و در منوی اول بازی لود بازی save شده هم دارد ولی من این کار را تست نکردم.
راهنما: ستون ها را حروف الفبا بدهید و ردیف ها را عدد. ابتدا مبدا و سپس مقصد.
کد:
/*Project Name:-Chess Mania                                  */ 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include<dos.h>

struct game{
int cp[8][8][2];
int kingp[3][3];
int onep[8][8],turn;
int twop[8][8];
int checked,checkmate;
};

game n;
int p[4],goon=1,c=1;
int po[8][2];

void ffp(int x,int y)
{
floodfill(25+x,30+y,15);
floodfill(25+x,37+y,15);
floodfill(25+x,44+y,15);     }

void ffk(int x,int y)
{
   floodfill(25+x,40+y,15);
   floodfill(25+x,3+y,15);   }

void ffc(int x,int y)
{  floodfill(25+x,25+y,15);  }

void ffcam(int x,int y)
{
floodfill(25+x,13+y,15);
floodfill(25+x,20+y,15);
floodfill(23+x,28+y,15);
floodfill(25+x,40+y,15);
floodfill(25+x,47+y,15);    }

void ffq(int x,int y)
{
 floodfill(25+x,7+y,15);
 floodfill(25+x,12+y,15);
 floodfill(25+x,22+y,15);
 floodfill(25+x,24+y,15);
 floodfill(25+x,30+y,15);   }

void ffh(int x,int y)
{  floodfill(25+x,45+y,15);
}

void drawpawn(int x,int y)
{
circle(25+x,30+y,5);
line(15+x,35+y,35+x,35+y);
line(15+x,35+y,15+x,38+y);
line(35+x,35+y,35+x,38+y);
line(15+x,38+y,35+x,38+y);
arc(25+x,50+y,0,180,12);
line(13+x,50+y,37+x,50+y);  }

void drawking(int x,int y)
{
   line(25+x,0+y,22+x,2+y);
   line(25+x,0+y,28+x,2+y);
   line(22+x,2+y,25+x,5+y);
   line(28+x,2+y,25+x,5+y);
   line(25+x,5+y,15+x,9+y);
   line(25+x,5+y,35+x,9+y);
   line(15+x,9+y,21+x,15+y);
   line(35+x,9+y,29+x,15+y);
   line(21+x,15+y,15+x,18+y);
   line(29+x,15+y,35+x,18+y);
   line(15+x,18+y,15+x,20+y);
   line(35+x,18+y,35+x,20+y);
   line(15+x,20+y,20+x,20+y);
   line(35+x,20+y,30+x,20+y);
   line(20+x,20+y,18+x,40+y);
   line(30+x,20+y,32+x,40+y);
   line(18+x,40+y,10+x,45+y);
   line(32+x,40+y,40+x,45+y);
   line(10+x,45+y,13+x,48+y);
   line(40+x,45+y,37+x,48+y);
   line(13+x,48+y,10+x,50+y);
   line(37+x,48+y,40+x,50+y);
   line(10+x,50+y,40+x,50+y); }

void drawcastle(int x,int y)
{
line(15+x,50+y,35+x,50+y);
line(15+x,50+y,15+x,47+y);
line(35+x,50+y,35+x,47+y);
line(15+x,47+y,18+x,47+y);
line(35+x,47+y,32+x,47+y);
line(18+x,47+y,20+x,27+y);
line(32+x,47+y,30+x,27+y);
line(20+x,27+y,15+x,27+y);
line(30+x,27+y,35+x,27+y);
line(15+x,27+y,15+x,20+y);
line(35+x,27+y,35+x,20+y);
line(15+x,20+y,20+x,20+y);
line(35+x,20+y,30+x,20+y);
line(20+x,20+y,20+x,25+y);
line(30+x,20+y,30+x,25+y);
line(20+x,25+y,23+x,25+y);
line(30+x,25+y,27+x,25+y);
line(23+x,25+y,23+x,20+y);
line(27+x,25+y,27+x,20+y);
line(23+x,20+y,27+x,20+y);   }

void drawhorse(int x,int y)
{
line(13+x,50+y,37+x,50+y);
line(13+x,50+y,22+x,34+y);
line(37+x,50+y,32+x,26+y);
line(22+x,35+y,8+x,31+y);
line(32+x,26+y,36+x,23+y);
line(36+x,23+y,31+x,20+y);
line(31+x,20+y,28+x,15+y);
line(28+x,15+y,25+x,20+y);
line(25+x,20+y,22+x,15+y);
line(22+x,15+y,19+x,20+y);
line(19+x,20+y,8+x,27+y);
line(8+x,27+y,15+x,29+y);
line(15+x,29+y,8+x,31+y);
circle(25+x,25+y,1);        }

void drawcamel(int x,int y)
{
circle(25+x,13+y,2);
line(24+x,14+y,18+x,20+y);
line(26+x,14+y,32+x,20+y);
line(18+x,20+y,22+x,27+y);
line(32+x,20+y,28+x,27+y);
line(22+x,27+y,28+x,27+y);
line(22+x,27+y,22+x,30+y);
line(28+x,27+y,28+x,30+y);
line(22+x,30+y,28+x,30+y);
line(22+x,30+y,15+x,45+y);
line(28+x,30+y,35+x,45+y);
line(15+x,45+y,35+x,45+y);
line(15+x,45+y,15+x,50+y);
line(35+x,45+y,35+x,50+y);
line(15+x,50+y,35+x,50+y);  }

void drawqueen(int x,int y)
{
 line(10+x,50+y,40+x,50+y);
 line(10+x,50+y,10+x,45+y);
 line(40+x,50+y,40+x,45+y);
 line(10+x,45+y,15+x,45+y);
 line(40+x,45+y,35+x,45+y);
 line(15+x,45+y,20+x,40+y);
 line(35+x,45+y,30+x,40+y);
 line(20+x,40+y,20+x,25+y);
 line(30+x,40+y,30+x,25+y);
 line(15+x,25+y,35+x,25+y);
 line(15+x,25+y,15+x,23+y);
 line(35+x,25+y,35+x,23+y);
 line(15+x,23+y,35+x,23+y);
 line(15+x,23+y,20+x,20+y);
 line(35+x,23+y,30+x,20+y);
 line(20+x,20+y,30+x,20+y);
 line(20+x,20+y,20+x,15+y);
 line(30+x,20+y,30+x,15+y);
 line(20+x,15+y,15+x,13+y);
 line(30+x,15+y,35+x,13+y);
 line(15+x,13+y,15+x,10+y);
 line(35+x,13+y,35+x,10+y);
 line(15+x,10+y,20+x,13+y);
 line(35+x,10+y,30+x,13+y);
 line(20+x,13+y,24+x,8+y);
 line(30+x,13+y,26+x,8+y);
 circle(25+x,7+y,2);        }

void drawcoin(int x,int y,int i,int j,int f)
{
	 if(n.cp[i][j][1]==1)
	    { drawpawn(x,y);
		if(f)
		  ffp(x,y);  }
	 if(n.cp[i][j][1]==2)
	    { drawcastle(x,y);
	      if(f)
		 ffc(x,y);
		 }
	 if(n.cp[i][j][1]==3)
	   { drawhorse(x,y);
		if(f)
		ffh(x,y);  }
	 if(n.cp[i][j][1]==4)
	   { drawcamel(x,y);
	     if(f)
	      ffcam(x,y);}
	 if(n.cp[i][j][1]==5)
	  {  drawqueen(x,y);
	     if(f)
	       ffq(x,y); }
	 if(n.cp[i][j][1]==6)
	  {  drawking(x,y);
	    if(f)
	      ffk(x,y); }
}

void setcoins()
{
int i,j,c,x,y;
for(i=0;i<8;i++)
  for(j=0;j<8;j++)
    {    x=169+j*50;
	 y=39+i*50;
    if(n.cp[i][j][0]==15&&(i+j)%2==0)//white coin and white background
      { setcolor(0);
	floodfill(x+25,y+25,15);
	drawcoin(x,y,i,j,0);
    }
    if(n.cp[i][j][0]==15&&(i+j)%2!=0)  //white coin and black background
    {  setcolor(15);
       drawcoin(x,y,i,j,1);
    }
    if(n.cp[i][j][0]==0&&(i+j)%2!=0) // black coin and black background
    {  setcolor(15);
       drawcoin(x,y,i,j,0);
    }
    if(n.cp[i][j][0]==0&&(i+j)%2==0) // black coin and white background
    {  setcolor(15);
       drawcoin(x,y,i,j,0);
       floodfill(x+5,y+5,15);
       floodfill(x+45,y+5,15);
    }
    if(n.cp[i][j][0]==-1&&(i+j)%2==0)
    { setcolor(15);
      floodfill(x+25,y+25,15);
    }
    }
}

void drawboard()
{  int i,j;
   int xmin=169,xmax=569,ymin=39,ymax=439;
   int gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "\\tc\\bgi\\");
   errorcode = graphresult();
   if (errorcode != grOk) 
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); 
   }

     for(i=ymin;i<=ymax;i+=50)
	  line(xmin,i,xmax,i);
     for(i=xmin;i<=xmax;i+=50)
	  line(i,ymin,i,ymax);
  outtextxy(589,64,"eight");
   outtextxy(589,114,"seven");
    outtextxy(589,164,"six");
     outtextxy(589,214,"five");
      outtextxy(589,264,"four");
       outtextxy(589,314,"three");
     	  outtextxy(589,364,"two");
	   outtextxy(589,414,"one");
	   outtextxy(544,449,"H");
        outtextxy(494,449,"G");
       outtextxy(444,449,"F");
    outtextxy(394,449,"E");
   outtextxy(344,449,"D");
  outtextxy(294,449,"C");
 outtextxy(244,449,"B");
outtextxy(194,449,"A");
 setcoins();
 if(n.turn==1)
   c=8;
  else
   c=1;
  setfillstyle(CLOSE_DOT_FILL,c);
    floodfill(50,50,15);

}

void givevalue()
{
int i,j;
for(i=0;i<8;i++)          //making all null
  for(j=0;j<8;j++)
      n.cp[j][i][0]=-1;
for(i=0;i<8;i++)           //giving color to coins
  for(j=0;j<2;j++)
     n.cp[j][i][0]=0;

for(i=0;i<8;i++)
  for(j=6;j<8;j++)
     n.cp[j][i][0]=15;
for(i=0;i<8;i++)         // placing the coins on the board
  for(j=0;j<8;j++)         //starting positons
    { n.cp[j][i][1]=-1;
      if(j==1||j==6)
	n.cp[j][i][1]=1;
      if((j==0||j==7)&&(i==0||i==7))
	 n.cp[j][i][1]=2;
      if((j==0||j==7)&&(i==1||i==6))
	 n.cp[j][i][1]=3;
      if((j==0||j==7)&&(i==2||i==5))
	 n.cp[j][i][1]=4;
      if((j==0||j==7)&&i==3)
	 n.cp[j][i][1]=5;
      if((j==0||j==7)&&i==4)
	 n.cp[j][i][1]=6;
    }
}
void open()
{
	FILE *fp;
	fp=fopen("save.txt","r");
	 printf("\n\n\t\t\t");
	if(fp==NULL)
	 cprintf("Cannot open file");
	else
	{ fread(&n,sizeof(n),1,fp);
	  drawboard();
	}
}

void leaf(int f)
{
 int gdriver = DETECT, gmode, errorcode,i;
 initgraph(&gdriver, &gmode, "\\tc\\bgi\\");
    arc(370,540,120,160,330);
    arc(260,540,20,60,330);
//first leaf
    line(80,385,90,315);
    line(90,315,95,330);
    line(95,330,120,275);
    line(120,275,120,293);
    line(120,293,155,250);
    line(155,250,150,270);
    line(150,270,187,233);
    line(187,233,183,250);
    line(183,250,251,226);
    line(251,226,196,277);
    line(196,277,208,280);
    line(208,280,164,301);
    line(164,301,177,305);
    line(177,305,135,330);
    line(135,330,148,331);
    line(148,331,113,356);
    line(113,356,126,356);
    line(126,356,80,383);
//right leaf
		       line(550,380,500,360);
		       line(500,360,515,357);
		       line(515,357,470,333);
		       line(470,333,483,332);
		       line(483,332,440,304);
		       line(440,304,454,304);
			 line(454,304,414,277);
		       line(414,277,432,279);
		       line(432,279,372,225);
		       line(372,225,445,245);
		       line(445,245,438,230);
		       line(438,230,478,266);
		       line(478,266,473,250);
		       line(473,250,511,298);
		       line(511,298,508,283);
		       line(508,283,535,331);
		       line(535,331,535,315);
		       line(535,315,550,380);
			setfillstyle(3,2);
			 floodfill(440,250,15);
		    floodfill(90,350,15);
	       //king
   int x=79,y=50,x1=3,y1=4;
   line((25+x)*x1,(0+y)*y1,(25+x)*x1,(50+y)*y1);
   line((25+x)*x1,(0+y)*y1,(22+x)*x1,(2+y)*y1);
   line((25+x)*x1,(0+y)*y1,(28+x)*x1,(2+y)*y1);
   line((22+x)*x1,(2+y)*y1,(25+x)*x1,(5+y)*y1);
   line((28+x)*x1,(2+y)*y1,(25+x)*x1,(5+y)*y1);
   line((25+x)*x1,(5+y)*y1,(15+x)*x1,(9+y)*y1);
   line((25+x)*x1,(5+y)*y1,(35+x)*x1,(9+y)*y1);
   line((15+x)*x1,(9+y)*y1,(21+x)*x1,(15+y)*y1);
   line((35+x)*x1,(9+y)*y1,(29+x)*x1,(15+y)*y1);
   line((21+x)*x1,(15+y)*y1,(15+x)*x1,(18+y)*y1);
   line((29+x)*x1,(15+y)*y1,(35+x)*x1,(18+y)*y1);
   line((15+x)*x1,(18+y)*y1,(15+x)*x1,(20+y)*y1);
   line((35+x)*x1,(18+y)*y1,(35+x)*x1,(20+y)*y1);
   line((15+x)*x1,(20+y)*y1,(20+x)*x1,(20+y)*y1);
   line((35+x)*x1,(20+y)*y1,(30+x)*x1,(20+y)*y1);
   line((20+x)*x1,(20+y)*y1,(18+x)*x1,(40+y)*y1);
   line((30+x)*x1,(20+y)*y1,(32+x)*x1,(40+y)*y1);
   line((18+x)*x1,(40+y)*y1,(10+x)*x1,(45+y)*y1);
   line((32+x)*x1,(40+y)*y1,(40+x)*x1,(45+y)*y1);
   line((10+x)*x1,(45+y)*y1,(13+x)*x1,(48+y)*y1);
   line((40+x)*x1,(45+y)*y1,(37+x)*x1,(48+y)*y1);
   line((13+x)*x1,(48+y)*y1,(10+x)*x1,(50+y)*y1);
   line((37+x)*x1,(48+y)*y1,(40+x)*x1,(50+y)*y1);
   line((10+x)*x1,(50+y)*y1,(40+x)*x1,(50+y)*y1);
	setfillstyle(1,15);
   floodfill((21+x)*x1,(40+y)*y1,15);
   floodfill((23+x)*x1,(3+y)*y1,15);
    setfillstyle(CLOSE_DOT_FILL,8);
    floodfill(50,50,15);
printf("\n====================================Creators====================================");
printf("\n                       DERRICK====================SHRUTI                        ");
if(f)
{
printf("\n                       THANK YOU FOR PLAYING CHESS MANIA                        ");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n================================================================================");
}
else{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n================================================================================");
}      delay(3500);
       closegraph();
       }

void openmenu()
{
  clrscr();
   textcolor(CYAN + BLINK);
   printf("\n\n\n\n\n \t\t\t");
  cprintf("Welcome_to_Chess_Mania");
      textcolor(CYAN );
   printf("\n\n\t\t\t");
  cprintf("   Hit Enter to play");
  printf("\n\n\t\t\t");
  cprintf("    Hit Esc to exit");
     int a;

 if(getch()==27)
   {exit(0);
      }
 else
  {  clrscr();
     printf("\n\n");
     printf("\n\n\t\t\t");
     cprintf("Start new game hit enter");
     printf("\n\n\t\t\t");
     cprintf(" Load saved game hit L");
     printf("\n\n\t\t\t");
     cprintf("    Hit Esc to exit");
      a=getch();
      if(a==27)
	{exit(0);
	 }
      else if(a=='l'||a=='L')
      {
	open();
      }
      else
      { clrscr();
	printf("\n\n\t\t\t");
	 givevalue();
	  drawboard();
      }}}

void save()
{ FILE *fp;
  fp=fopen("save.txt","w");
  fwrite(&n,sizeof(n),1,fp);
  exit(0);
}
void menu()
{
 closegraph();

  printf("\n\n\t\t\t");
 cprintf("Save game and exit::s");
  printf("\n\n\t\t\t");
 cprintf(" Exit::E");
 printf("\n\n\t\t\t");
 cprintf("return to game:any key");
 char s=getch();
 if(s=='s'||s=='S')
    { leaf(1);  save(); }
 else if(s=='e'||s=='E')
       {
       leaf(1);
       exit(0);
       }
  else
   {drawboard();
    }
}
void calppp(int e)
{ int i,j;
    if(e)
      for(i=0;i<8;i++)
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;

  if(n.turn)  //white pawn
    { //reset
	  if(p[0]==6)
	{  if(n.cp[5][p[1]][0]!=15&&n.cp[5][p[1]][0]!=0)
	     n.onep[5][p[1]]=1;
		if(n.cp[4][p[1]][0]!=15&&n.cp[4][p[1]][0]!=0&&n.cp[5][p[1]][0]==-1)
	   n.onep[4][p[1]]=1;
	}
     if(p[0]-1>=0)
		 { i=p[0]-1;
		   j=p[1];
		if(n.cp[i][j][0]!=15&&n.cp[i][j][0]!=0)
		 n.onep[i][j]=1;
		if(p[1]+1<8)
		if(n.cp[p[0]-1][p[1]+1][0]==0)
		   n.onep[p[0]-1][p[1]+1]=1;
		if(p[1]-1>=0)
		if(n.cp[p[0]-1][p[1]-1][0]==0)
		   n.onep[p[0]-1][p[1]-1]=1;
		 }

    } //turn end
   else
   {         //for black pawn checkin possibility
    if(p[0]==1)
	{  if(n.cp[2][p[1]][0]!=15&&n.cp[2][p[1]][0]!=0)
	   n.onep[2][p[1]]=1;
		if(n.cp[3][p[1]][0]!=15&&n.cp[3][p[1]][0]!=0&&n.cp[2][p[1]][0]==-1)
	   n.onep[3][p[1]]=1;
	}
	  if(p[0]+1<8)
		 { i=p[0]+1;
		   j=p[1];
		if(n.cp[i][j][0]!=15&&n.cp[i][j][0]!=0)
		 n.onep[i][j]=1;
		if(p[1]+1<8)
		if(n.cp[p[0]+1][p[1]+1][0]==15)
		   n.onep[p[0]+1][p[1]+1]=1;
		if(p[1]-1>=0)
		if(n.cp[p[0]+1][p[1]-1][0]==15)
		   n.onep[p[0]+1][p[1]-1]=1;
		 }
   }
}
void calppc(int e)
{ int i,j,flag=1;
 if(e)
  for(i=0;i<8;i++)                 //reset
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;
 if(n.turn) {
for(i=p[1]+1;i<8&&n.cp[p[0]][i][0]!=15&&flag;i++)     //marking the right side
   {  if(n.cp[p[0]][i][0]==0)
	 flag=0;
      n.onep[p[0]][i]=1;
   }       flag=1;
for(i=p[1]-1;i>=0&&n.cp[p[0]][i][0]!=15&&flag;i--)     //marking the left side
   {  if(n.cp[p[0]][i][0]==0)
	 flag=0;
      n.onep[p[0]][i]=1;
   }          flag=1;
for(i=p[0]+1;i<8&&n.cp[i][p[1]][0]!=15&&flag;i++)     //marking the down side
   {  if(n.cp[p[0]][i][0]==0)
	 flag=0;
      n.onep[i][p[1]]=1;
   }          flag=1;
for(i=p[0]-1;i>=0&&n.cp[i][p[1]][0]!=15&&flag;i--)     //marking the top side
   {  if(n.cp[p[0]][i][0]==0)
	 flag=0;
      n.onep[i][p[1]]=1;
   }

	     }
	    else
  {  flag=1;
   for(i=p[1]+1;i<8&&n.cp[p[0]][i][0]!=0&&flag;i++)     //marking the right side
   {  if(n.cp[p[0]][i][0]==15)
	 flag=0;
      n.onep[p[0]][i]=1;
   }  flag=1;
for(i=p[1]-1;i>=0&&n.cp[p[0]][i][0]!=0&&flag;i--)     //marking the left side
   {  if(n.cp[p[0]][i][0]==15)
	 flag=0;
      n.onep[p[0]][i]=1;
   }  flag=1;
for(i=p[0]+1;i<8&&n.cp[i][p[1]][0]!=0&&flag;i++)     //marking the down side
   {  if(n.cp[p[0]][i][0]==15)
	 flag=0;
      n.onep[i][p[1]]=1;
   }  flag=1;
for(i=p[0]-1;i>=0&&n.cp[i][p[1]][0]!=0&&flag;i--)     //marking the top side
   {  if(n.cp[p[0]][i][0]==15)
	 flag=0;
      n.onep[i][p[1]]=1;
   }}}

void calpph(int a)
{
 int i,j;
  if(a)
  for(i=0;i<8;i++)                 //reset
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;
  if(n.turn)
  {  if(p[1]+1<8)
       { if(p[0]-2>=0)
	  if(n.cp[p[0]-2][p[1]+1][0]!=15)
	       n.onep[p[0]-2][p[1]+1]=1;
	 if(p[0]+2<8)
	   if(n.cp[p[0]+2][p[1]+1][0]!=15)
	       n.onep[p[0]+2][p[1]+1]=1;
       }
    if(p[1]+2<8)
       { if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]+2][0]!=15)
	       n.onep[p[0]-1][p[1]+2]=1;
	 if(p[0]+1<8)
	   if(n.cp[p[0]+1][p[1]+2][0]!=15)
	       n.onep[p[0]+1][p[1]+2]=1;
       }
      if(p[1]-1>=0)
       { if(p[0]-2>=0)
	  if(n.cp[p[0]-2][p[1]-1][0]!=15)
	       n.onep[p[0]-2][p[1]-1]=1;
	 if(p[0]+2<8)
	   if(n.cp[p[0]+2][p[1]-1][0]!=15)
	       n.onep[p[0]+2][p[1]-1]=1;
       }
    if(p[1]-2>=0)
       { if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]-2][0]!=15)
	       n.onep[p[0]-1][p[1]-2]=1;
	 if(p[0]+1<8)
	   if(n.cp[p[0]+1][p[1]-2][0]!=15)
	       n.onep[p[0]+1][p[1]-2]=1;
       }
  }
  else
  {
  if(p[1]+1<8)
       { if(p[0]-2>=0)
	  if(n.cp[p[0]-2][p[1]+1][0]!=0)
	       n.onep[p[0]-2][p[1]+1]=1;
	 if(p[0]+2<8)
	   if(n.cp[p[0]+2][p[1]+1][0]!=0)
	       n.onep[p[0]+2][p[1]+1]=1;
       }
    if(p[1]+2<8)
       { if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]+2][0]!=0)
	       n.onep[p[0]-1][p[1]+2]=1;
	 if(p[0]+1<8)
	   if(n.cp[p[0]+1][p[1]+2][0]!=0)
	       n.onep[p[0]+1][p[1]+2]=1;
       }
      if(p[1]-1>=0)
       { if(p[0]-2>=0)
	  if(n.cp[p[0]-2][p[1]-1][0]!=0)
	       n.onep[p[0]-2][p[1]-1]=1;
	 if(p[0]+2<8)
	   if(n.cp[p[0]+2][p[1]-1][0]!=0)
	       n.onep[p[0]+2][p[1]-1]=1;
       }
    if(p[1]-2>=0)
       { if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]-2][0]!=0)
	       n.onep[p[0]-1][p[1]-2]=1;
	 if(p[0]+1<8)
	   if(n.cp[p[0]+1][p[1]-2][0]!=0)
	       n.onep[p[0]+1][p[1]-2]=1;
       }
}}

void calppca(int e)
{
 int i,j,flag=1;
   if(e)
  for(i=0;i<8;i++)                 //reset
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;
   if(n.turn)
     {           //checkin left top
     for(i=p[0]-1,j=p[1]-1;i>=0&&j>=0&&flag&&n.cp[i][j][0]!=15;i--,j--)
	{      if(n.cp[i][j][0]==0)
		    flag=0;
	    n.onep[i][j]=1;
	}    flag=1;
	//right top
     for(i=p[0]-1,j=p[1]+1;i>=0&&j<8&&flag&&n.cp[i][j][0]!=15;i--,j++)
	{      if(n.cp[i][j][0]==0)
		    flag=0;
	    n.onep[i][j]=1;
	}   flag=1;
	// right bottom
      for(i=p[0]+1,j=p[1]+1;i<8&&j<8&&flag&&n.cp[i][j][0]!=15;i++,j++)
	{      if(n.cp[i][j][0]==0)
		    flag=0;
	    n.onep[i][j]=1;
	}  flag=1;
	// left bottom
       for(i=p[0]+1,j=p[1]-1;i<8&&j>=0&&flag&&n.cp[i][j][0]!=15;i++,j--)
	{      if(n.cp[i][j][0]==0)
		    flag=0;
	    n.onep[i][j]=1;
	}
		 //End of white
     }
   else    // begin of black
   {
   for(i=p[0]-1,j=p[1]-1;i>=0&&j>=0&&flag&&n.cp[i][j][0]!=0;i--,j--)
	{      if(n.cp[i][j][0]==15)
		    flag=0;
	    n.onep[i][j]=1;
	}    flag=1;
     for(i=p[0]-1,j=p[1]+1;i>=0&&j<8&&flag&&n.cp[i][j][0]!=0;i--,j++)
	{      if(n.cp[i][j][0]==15)
		    flag=0;
	    n.onep[i][j]=1;
	}   flag=1;
      for(i=p[0]+1,j=p[1]+1;i<8&&j<8&&flag&&n.cp[i][j][0]!=0;i++,j++)
	{      if(n.cp[i][j][0]==15)
		    flag=0;
	    n.onep[i][j]=1;
	}  flag=1;
       for(i=p[0]+1,j=p[1]-1;i<8&&j>=0&&flag&&n.cp[i][j][0]!=0;i++,j--)
	{      if(n.cp[i][j][0]==15)
		    flag=0;
	    n.onep[i][j]=1;
	}
}}

void calppq(int e)
{
 int i,j;
  if(e)
  for(i=0;i<8;i++)                 //reset  possibility array
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;
 calppc(0);
 calppca(0);
}
void calppk()
{
 int i,j;
  for(i=0;i<8;i++)                 //reset  possibility array
	for(j=0;j<8;j++)
	  n.onep[i][j]=-1;
   if(n.turn)
   {
	if(p[1]+1<8)
	 {  if(p[0]-1>=0)
	       if(n.cp[p[0]-1][p[1]+1][0]!=15)
		  n.onep[p[0]-1][p[1]+1]=1;
	    if(p[0]+1<8)
	       if(n.cp[p[0]+1][p[1]+1][0]!=15)
		  n.onep[p[0]+1][p[1]+1]=1;
	    if(n.cp[p[0]][p[1]+1][0]!=15)
		  n.onep[p[0]][p[1]+1]=1;
	 }
	 if(p[1]-1>=0)
	 {  if(p[0]-1>=0)
	       if(n.cp[p[0]-1][p[1]-1][0]!=15)
		  n.onep[p[0]-1][p[1]-1]=1;
	    if(p[0]+1<8)
	       if(n.cp[p[0]+1][p[1]-1][0]!=15)
		  n.onep[p[0]+1][p[1]-1]=1;
	    if(n.cp[p[0]][p[1]-1][0]!=15)
		  n.onep[p[0]][p[1]-1]=1;
	 }
	 if(p[0]+1<8)
	  if(n.cp[p[0]+1][p[1]][0]!=15)
	     n.onep[p[0]+1][p[1]]=1;
	 if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]][0]!=15)
	     n.onep[p[0]-1][p[1]]=1;
   }
   else
   { 	if(p[1]+1<8)
	 {  if(p[0]-1>=0)
	       if(n.cp[p[0]-1][p[1]+1][0]!=0)
		  n.onep[p[0]-1][p[1]+1]=1;
	    if(p[0]+1<8)
	       if(n.cp[p[0]+1][p[1]+1][0]!=0)
		  n.onep[p[0]+1][p[1]+1]=1;
	    if(n.cp[p[0]][p[1]+1][0]!=0)
		  n.onep[p[0]][p[1]+1]=1;
	 }
	 if(p[1]-1>=0)
	 {  if(p[0]-1>=0)
	       if(n.cp[p[0]-1][p[1]-1][0]!=0)
		  n.onep[p[0]-1][p[1]-1]=1;
	    if(p[0]+1<8)
	       if(n.cp[p[0]+1][p[1]-1][0]!=0)
		  n.onep[p[0]+1][p[1]-1]=1;
	    if(n.cp[p[0]][p[1]-1][0]!=0)
		  n.onep[p[0]][p[1]-1]=1;
	 }
	 if(p[0]+1<8)
	  if(n.cp[p[0]+1][p[1]][0]!=0)
	     n.onep[p[0]+1][p[1]]=1;
	 if(p[0]-1>=0)
	  if(n.cp[p[0]-1][p[1]][0]!=0)
	     n.onep[p[0]-1][p[1]]=1;
   }
}

int promote()
{ int value=1;
   r:
  printf("\n Promote Pawn to:\nQueen==5\nBishop==4\nKnight==3\nCastle==2");
  printf("\nEnter a no:");
  scanf("%d",&value);
  if(value>1&&value<6)
  return(value);
  else
   goto r;
}

int kingind(int h,int v)
{  int ret=0,count=0;
      int i,j,king,found=1,x,y,nc;
	//find the king
	 if(n.turn)
	 {  king=15;   nc=0;}
	 else
	 {  king=0;    nc=15;}
    for(i=0;i<8&&found;i++)
       for(j=0;j<8&&found;j++)
	  if(n.cp[i][j][0]==king&&n.cp[i][j][1]==6)
	       found=0;
   x=p[0];  y=p[1];
   p[0]=i-1;   p[1]=j-1;
   calppq(1);

   for(i=0;i<8;i++)
      for(j=0;j<8;j++)
	if(n.onep[i][j]==1&&n.cp[i][j][0]==nc&&n.cp[i][j][1]!=1&&n.cp[i][j][1]!=3&&n.cp[i][j][1]!=6)
		{  ret=1;  count++;}
	 else
	   n.onep[i][j]=-1;
       if(h)
     calpph(0);

   if(h)
   {  for(i=0;i<8;i++)
      for(j=0;j<8;j++)
	if(n.onep[i][j]==1&&n.cp[i][j][0]==nc&&n.cp[i][j][1]==3)
	       {	  ret=1;     count++;}

	     if(n.turn)
	    {	 if((n.cp[p[0]-1][p[1]+1][0]==nc&&n.cp[p[0]-1][p[1]+1][1]==1)||(n.cp[p[0]-1][p[1]-1][1]==1&&n.cp[p[0]-1][p[1]-1][1]==nc))
		 {  ret=1; count++; }   }
	    else
	    {  if((n.cp[p[0]+1][p[1]+1][1]==1&&n.cp[p[0]+1][p[1]+1][0]==nc)||(n.cp[p[0]+1][p[1]-1][1]==1&&n.cp[p[0]+1][p[1]-1][1]==nc))
		{ ret=1;   count++;}
	    }
	}
   p[0]=x;  p[1]=y;
       if(v==0)
	  return(ret);
       else
	  return count;
}


void restore(int coin,int rc,int rco)
{
printf("\n The move will\n cause the game to end!!");
getch();
 n.cp[p[2]][p[3]][0]=rc;
 n.cp[p[2]][p[3]][1]=rco;
 if(n.turn)
  n.cp[p[0]][p[1]][0]=15;
 else
  n.cp[p[0]][p[1]][0]=0;
 n.cp[p[0]][p[1]][1]=coin;
}

int checkmate()
{
int i,j,v,color,found,c=1,enemy;
v=kingind(1,1);
 if(n.turn)
 { color=15;  enemy=0;  }
 else
 { color=0;   enemy=15;}
//calculating the next move for all the pieces

for(i=0;i<8;i++)
  for(j=0;j<8;j++)
    n.onep[i][j]=-1;

for(i=0;i<8;i++)
   for(j=0;j<8;j++)
     if(n.cp[i][j][0]==color)
     { p[0]=i; p[1]=j;
       if(n.cp[i][j][1]==1)
	calppp(0);
       if(n.cp[i][j][1]==2)
	calppc(0);
       if(n.cp[i][j][1]==3)
       calpph(0);
       if(n.cp[i][j][1]==4)
       calppca(0);
       if(n.cp[i][j][1]==5)
       calppq(0);
     }
//find the coins attackin the king
  for(i=0;i<8&&found;i++)
       for(j=0;j<8&&found;j++)
	  if(n.cp[i][j][0]==color&&n.cp[i][j][1]==6)
	       found=0;
int x=i-1,y=j-1,z=0,no=1,ii;
for(i=x-1;i>=0&&no;i--)
  { po[z][0]=i;  po[z][1]=y;  z++;
    if(n.cp[i][y][0]==enemy&&n.cp[i][y][1]!=1&&n.cp[i][y][1]!=3&&n.cp[i][y][1]!=6&&n.cp[i][y][1]!=-1)
	 for(j=0;j<z;j++)
	    {n.twop[po[j][0]][po[j][1]]=1;
	    no=0;
	    }
  }        z=0; no=1;
for(i=x+1;i<8&&no;i++)
  { po[z][0]=i;  po[z][1]=y;  z++;
    if(n.cp[i][y][0]==enemy&&n.cp[i][y][1]!=1&&n.cp[i][y][1]!=3&&n.cp[i][y][1]!=6&&n.cp[i][y][1]!=-1)
	 for(j=0;j<z;j++)
	    {n.twop[po[j][0]][po[j][1]]=1;   no=0;
	    }
  }        z=0;  no=1;
for(i=y-1;i>=0&&no;i--)
  { po[z][0]=x;  po[z][1]=i;  z++;
    if(n.cp[x][i][0]==enemy&&n.cp[x][i][1]!=1&&n.cp[x][i][1]!=3&&n.cp[x][i][1]!=6&&n.cp[x][i][1]!=-1)
	 for(j=0;j<z;j++)
	    {n.twop[po[j][0]][po[j][1]]=1;  no=0;
	    }
  }      z=0; no=1;
for(i=y+1;i<8&&no;i++)
  { po[z][0]=x;  po[z][1]=i;  z++;
    if(n.cp[x][i][0]==enemy&&n.cp[x][i][1]!=1&&n.cp[x][i][1]!=3&&n.cp[x][i][1]!=6&&n.cp[x][i][1]!=-1)
	 for(j=0;j<z;j++)
	    {n.twop[po[j][0]][po[j][1]]=1;  no=0;
	    }
  }      z=0; no=1;
for(i=x+1,j=y+1;i<8&&j<8&&no;i++,j++)
  {
    po[z][0]=i; po[z][1]=j; z++;
    if(n.cp[i][j][0]==enemy&&n.cp[i][j][1]!=1&&n.cp[i][j][1]!=3&&n.cp[i][j][1]!=6&&n.cp[i][j][1]!=-1)
	 for(ii=0;ii<z;ii++)
	  {n.twop[po[ii][0]][po[ii][1]]=1;  no=0;
	  }
  }             z=0; no=1;
for(i=x-1,j=y-1;i>=0&&j>=0&&no;i--,j--)
  {
    po[z][0]=i; po[z][1]=j; z++;
    if(n.cp[i][j][0]==enemy&&n.cp[i][j][1]!=1&&n.cp[i][j][1]!=3&&n.cp[i][j][1]!=6&&n.cp[i][j][1]!=-1)
	 for(ii=0;ii<z;ii++)
	  {n.twop[po[ii][0]][po[ii][1]]=1;  no=0;
	  }
  }         z=0; no=1;
for(i=x+1,j=y-1;i<8&&j>=0&&no;i++,j--)
  {
    po[z][0]=i; po[z][1]=j; z++;
    if(n.cp[i][j][0]==enemy&&n.cp[i][j][1]!=1&&n.cp[i][j][1]!=3&&n.cp[i][j][1]!=6&&n.cp[i][j][1]!=-1)
	 for(ii=0;ii<z;ii++)
	  {n.twop[po[ii][0]][po[ii][1]]=1;  no=0;
	  }
  }        z=0;no=1;
for(i=x-1,j=y+1;i>=0&&j<8&&no;i--,j++)
  {
    po[z][0]=i; po[z][1]=j; z++;
    if(n.cp[i][j][0]==enemy&&n.cp[i][j][1]!=1&&n.cp[i][j][1]!=3&&n.cp[i][j][1]!=6&&n.cp[i][j][1]!=-1)
	 for(ii=0;ii<z;ii++)
	  {n.twop[po[ii][0]][po[ii][1]]=1;  no=0;
	  }
  }
int h=0;
if(n.cp[x-2][y-1][0]==enemy&&n.cp[x-2][y-1][1]==3&&x-2>=0&&y-1>=0)
   { h=1;  n.twop[x-2][y-1]=3;}
if(n.cp[x-1][y-2][0]==enemy&&n.cp[x-1][y-2][1]==3&&x-1>=0&&y-2>=0)
   { h=1;  n.twop[x-1][y-2]=3;}
if(n.cp[x+1][y-2][0]==enemy&&n.cp[x+1][y-2][1]==3&&x+1<8&&y-2>=0)
   { h=1;  n.twop[x+1][y-2]=3;}
if(n.cp[x+2][y-1][0]==enemy&&n.cp[x+2][y-1][1]==3&&x+2<8&&y-1>=0)
   { h=1;  n.twop[x+2][y-1]=3;}
if(n.cp[x-2][y+1][0]==enemy&&n.cp[x-2][y+1][1]==3&&x-2>=0&&y+1<8)
   { h=1;  n.twop[x-2][y+1]=3;}
if(n.cp[x-1][y+2][0]==enemy&&n.cp[x-1][y+2][1]==3&&x-1>=0&&y+2<8)
   { h=1;  n.twop[x-1][y+2]=3;}
if(n.cp[x+1][y+2][0]==enemy&&n.cp[x+1][y+2][1]==3&&x+1<8&&y+2<8)
   { h=1;  n.twop[x+1][y+2]=3;}
if(n.cp[x+2][y+1][0]==enemy&&n.cp[x+2][y+1][1]==3&&x+2<8&&y+1<8)
   { h=1;  n.twop[x+2][y+1]=3;}

if(n.turn)
{ if(n.cp[x-1][y-1][0]==enemy&&n.cp[x-1][y-1][1]==1&&x-1>=0&&y-1>=0)
      n.twop[x-1][y-1]=1;
  if(n.cp[x-1][y+1][0]==enemy&&n.cp[x-1][y+1][1]==1&&x-1>=0&&y+1<8)
      n.twop[x-1][y+1]=1;
}
else
{
if(n.cp[x+1][y-1][0]==enemy&&n.cp[x-1][y-1][1]==1&&x+1<8&&y-1>=0)
      n.twop[x-1][y-1]=1;
  if(n.cp[x+1][y+1][0]==enemy&&n.cp[x-1][y+1][1]==1&&x+1<8&&y+1<8)
      n.twop[x-1][y+1]=1;
}
// check if any coin can block the single attack
if(v==1)
for(i=0;i<8;i++)
   for(j=0;j<8;j++)
    { if(n.twop[i][j]==1&&n.onep[i][j]==1)
	c=0;
      if(h)
	if(n.twop[i][j]==3&&n.onep[i][j]==1)
	  c=0;
     }
// see if the king can move
  for(i=0;i<8&&found;i++)
       for(j=0;j<8&&found;j++)
	  if(n.cp[i][j][0]==color&&n.cp[i][j][1]==6)
	       found=0;
 p[0]=i-1-1; p[1]=j-1-1;
 n.kingp[1][1]=0;
 for(i=0;i<3;i++)
   for(j=0;j<3;j++)
     if((p[0]+i)>=0&&(p[0]+i)<8&&(p[1]+j)>=0&&(p[1]+j)<8)
       if(n.cp[p[0]+i][p[1]+j][0]!=color)
	 {  p[0]=p[0]+i;  p[1]=p[1]+j;
		if(!kingind(1,0))
		  n.kingp[i][j]=1;
	 }
 for(i=0;i<3;i++)
   for(j=0;j<3;j++)
      if(n.kingp[i][j]==1&&i!=1&&j!=1)
	c=0;

return c;
}

void calpos()
{   int rc,rco;
     if(n.cp[p[0]][p[1]][1]==-1)
      printf("\nNo Coin\nselected\n");
     else
   {
   if(n.cp[p[0]][p[1]][1]==1) //coin to move is a pawn
	    {   calppp(1); //calculate possible position for that pawn
	       if(n.onep[p[2]][p[3]]==1)
		 {  int j=1;

		    if(p[2]==0||p[2]==7)
		      j=promote();
		    n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		    if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=j;
		    if(kingind(1,0))
		    {  restore(1,rc,rco);
		       return;
			 }
		   n.turn=(n.turn+1)%2;
		 }
		 else
		 printf("\nNot a\n correct\nmove ");
      }
  if(n.cp[p[0]][p[1]][1]==2) //coin to move is a castle
	    {   calppc(1);
	    if(n.onep[p[2]][p[3]]==1)
		 {
		    n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		       if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=2;

		     if( kingind(1,0))
		    {  restore(2,rc,rco);
		       return;
			 }
		    n.turn=(n.turn+1)%2;
		 }
		 else
		 printf("\nNot a\n correct\nmove ");
	    }
  if(n.cp[p[0]][p[1]][1]==3) //coin to move is a horse
	    {  calpph(1);
	    if(n.onep[p[2]][p[3]]==1)
		 {  n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		      if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=3;
		     if( kingind(1,0))
		    {  restore(3,rc,rco);
		       return;
			 }
		    n.turn=(n.turn+1)%2;
		 }
	     else
	     printf("\nNot a\n correct\nmove ");

	    }
  if(n.cp[p[0]][p[1]][1]==4) //coin to move is a camel
	    { calppca(1);
	     if(n.onep[p[2]][p[3]]==1)
		 {  n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		       if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=4;
		     if( kingind(1,0))
		    {  restore(4,rc,rco);
		       return;
			 }
		    n.turn=(n.turn+1)%2;
		 }
		 else
		 printf("\nNot a\n correct\nmove ");
	    }
  if(n.cp[p[0]][p[1]][1]==5) //coin to move is a queen
	    {  calppq(1);
	    if(n.onep[p[2]][p[3]]==1)
		 {  n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		       if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=5;
		      if( kingind(1,0))
		    {  restore(5,rc,rco);
		       return;
			 }
		    n.turn=(n.turn+1)%2;
		 }
		 else
		 printf("\nNot a\n correct\nmove ");
	    }
  if(n.cp[p[0]][p[1]][1]==6) //coin to move is a king
	    {  calppk();
	    if(n.onep[p[2]][p[3]]==1)
		 {  n.cp[p[0]][p[1]][0]=-1;
		    n.cp[p[0]][p[1]][1]=-1;
		       if(n.turn)
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=15;    }
		    else
		    { rc=n.cp[p[2]][p[3]][0]; n.cp[p[2]][p[3]][0]=0;      }
		     rco=n.cp[p[2]][p[3]][1]; n.cp[p[2]][p[3]][1]=66;
		     if( kingind(1,0))
		    {  restore(6,rc,rco);
		       return;
			 }
		    n.turn=(n.turn+1)%2;
		 }
		 else
		 printf("\nNot a\n correct\nmove ");
	    }

       if(kingind(1,0))
	  {  n.checked=1;
	   n.checkmate=checkmate();
	  }
       else
	    n.checked=0;
      if(n.checkmate)
	{ closegraph();
	  drawboard();
	  if(n.turn)
	    printf("\n White Loses ");
	  else
	   printf("\n Black loses");
	  openmenu();
	}
}}


void main()
{
clrscr();
n.turn=1;
 n.checked=0;
leaf(0);
openmenu();
int a,b,c,d;

while(1){
  goon=1;
  printf("\nEnter coordinates");
  if(n.turn)
     printf(" for white");
  else
     printf(" for black");

   if(n.checked)
    printf(" YOU HAVE BEEN CHECKED");

   if(goon)
   {
   printf("\nrow:");
   a=getch();
   if(a==27)
     { menu();
       goon=0;
     }
   if(goon)
     printf("%d",a-48);
   }
  if(goon)
     {
      printf("\ncolumn:");
      b=getche();
     if(b==27)
      {  menu();
	 goon=0;
      }
     if(goon)
       printf("%c",b);
     }
  if(goon)
     {
     printf("\nrow:");
     c=getche();
     if(c==27)
       {  menu();
	  goon=0;
       }
     if(goon)
       printf("%d",c-48);}
  if(goon)
    {
     printf("\ncolumn:");
     d=getche();
    if(d==27)
      { menu();
	goon=0;
      }
    if(goon)
    printf("%c",d);
    }
 if(goon)
   { p[0]=(a-48-8)*-1;
     b=toupper(b);
     p[1]=b-65;
     p[2]=(c-48-8)*-1;
     d=toupper(d);
     p[3]=d-65;
     calpos();
     getch();
   }
closegraph();
drawboard();
}
}
 

seyyedali

کاربر تازه وارد
تاریخ عضویت
24 ژوئن 2007
نوشته‌ها
13
لایک‌ها
0
نمی دونم چرا اما با
کد:
#include<graphics.h>
مشکل داره من از توربی سی پلاس پلاس ویندوز استفاده می کنم میشه بگید چطوری مشکل رو حل کنم
 

EASPORTS

کاربر تازه وارد
تاریخ عضویت
13 فوریه 2007
نوشته‌ها
1,473
لایک‌ها
1
سن
42
محل سکونت
Brazil
بسیار ایده ی جالبیه ، من قبلاً در مسابقات استانی شرکت کردم و کم و بیش از قوانین داوریش اطلاع دارم ، البته استاد عزیزم ، جناب حامد خان ،هم هستند و هیچگونه جای نگرانی نیست.
 

Mehdi.T

کاربر فعال برنامه نویسی
کاربر فعال
تاریخ عضویت
30 سپتامبر 2005
نوشته‌ها
506
لایک‌ها
3
محل سکونت
In Search of Sunrise
نمی دونم چرا اما با
کد:
#include<graphics.h>
مشکل داره من از توربی سی پلاس پلاس ویندوز استفاده می کنم میشه بگید چطوری مشکل رو حل کنم

سلام
می تونید از Turbo C استفاده کنید
 

saalek110

Registered User
تاریخ عضویت
10 آپریل 2007
نوشته‌ها
212
لایک‌ها
1
بسیار ایده ی جالبیه ، من قبلاً در مسابقات استانی شرکت کردم و کم و بیش از قوانین داوریش اطلاع دارم ، البته استاد عزیزم ، جناب حامد خان ،هم هستند و هیچگونه جای نگرانی نیست.

با سلام.
انجام این مسابقات برای من خیلی مهمه و ساعتهای زیادی وقت گذاشتم روی ساخت برنامه آن. ولی پست نزدم. در یکی از سورس هایی که در پست های قبلی این تاپیک گذاشتم برنامه بررسی درستی حرکات وجود دارد. میشه از آن استفاده کرد که قسمتی از وظیفه داور است.

من قصدم فعلا اینه که برنامه مرکزی را با سی بیلدر بنویسم(تا جلوه ویندوزی داشته باشه حرکات مهره ها- البته سی شارپ و وی بی هم ممکن است ولی می خواهم برنامه روی هر کامپیوتری بدون نیاز به نصب چیزی اجرا بشود.برای همین دلفی و سی بیلدر و وی سی 6 برایم ارجح است. ) و دو بازیکن را هم که تیم های رقابت کننده می نویسند(مثلا به زبان سی). برنامه مرکزی به نوبت player ها را اجرا می کند و موقعیت مهره ها را در اختیار آنها قرار می دهد. فعلا تصمیمم اینه که با فایل تکست ارتباط 3 برنامه برقرار بشه. یکی از مزایای این طرح اینه که player1 می تواند مثلا با وی بی نوشته بشود توسط فردی و player2 مثلا با پاسکال. حتی با php . یعنی انتخاب زبان بسته به فرد شرکت کننده دارد.
اگر دوستان اصلاحی بر حرفها و روشهای من دارند بنویسند.
شاید بشود در گام بعد کدها را تبدیل به زبانی مثل php کرد تا آنلاین رقابت ها قابل مشاهده باشد.

اما در قسمت روش انجام مسابقات به کمک شما و آقا حامد نیاز دارم. من خودم عضو تیم شطرنج دانشگاه بودم ولی خیلی چیزها را نیاز به کمک دارم.
 
بالا