الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مسألة شطرنج

بدأه cold0zero في 2 ديسمبر 2009 · 6 رد · 2,146 مشاهدة · في لغة C و ++C
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

مرحبا يا زملاء في عندي مسألة برمجة عن الشطرنج و حابب تساعدوني فيها لأني نسيان كل البرمجة و اساسياتها و مجالي صار بغير مجال الحاسب و البرمجة بس هالمسألة هية لشخص عزيز علي بده حلها اليوم إن أمكن و شكرا على كل حال المسألة واضحة و مرتبة بملف وورد بالمرفقات يا ريت تساعدوني فيها

نرى توضعا افتراضيا لأحجار الشطرنج لن ننشغل بموافقة التو ضع مع قواعد لعبة الشطرنج السؤال : هنا لدينا 8 اسطر ب 8 أعمدة وبافتراض مايلي :

القطع السوداء نرقمها من 1 حتى 16

القطع البيضاء نرقمها من 17 حتى 32

بالنسبة للقطع المتشابهة كالجنود إن كان لدينا 3 جنود من اللون الأبيض فإنها تأخذ الأرقام من 17 ثم 18 ثم 19 بالنسبة للقلعتين من اللون الأبيض الأولى وتأخذ الرقم 25 أما الثانية فتأخذ الرقم 32 بالنسبة لرقعة الشطرنج ترتيب السطر يوافق الرسم أما الأعمدة فترتب رقميا حسب ترتيب الحرف دليل العمود .

المطلوب كتابة برنامج يتضمن :

1- التصريح عن مصفوفة عناصرها تمثل خانات رقعة الشطرنج .

2- اسناد لكل خانة من رقعة الشطرنج الرقم الموافق لقطعة الشطرنج المتوضع في تلك الخانة أما الخانات التي لا تتوضع فيها أية قطعة شطرنج فيتم اسنادها للرقم صفر – رقما – لتلك الخانات في الطلب 2 استخدم حلقات التكرار لإسناد الرقم صفر لكل خانات رقعة الشطرنج بعد ذلك اسند بشكل فردي لكل خانة على حدا للخانات المتوضع عليها قطع الشطرنج الرقم الموافق للقطع

3- باستخدام الحلقات اوجد وأطبع عدد الخانات الفارغة وأماكنها – أي اطبع دليل السطر ودليل العمود لتلك الخانات

4- أوجد وأطبع عدد قطع كل لون

5- باستخدام الحلقات أوجد وأطبع عدد الجنود اللونين الأبيض والأسود الجندي ينتقل ضمن وفق العمود الذي يحتويه وبخطوة واحدة باتجاه الخصم في حالة خلو المربع امامه من أية قطع ويمكن للجندي من الانتقال إلى المربع الذي على يمينه أو على يساره إلى الأمام في حالة إشغال ذلك المربع بقطعة للخصم يتم إزاحة تلك القطعة من اللعبة بانتقال الجندي لتلك الخانة . بالنسبة للجندي

6- اكتب الجزء البرمجي المتعلق بحركة الجندي (D,4) أي أن هذا الجزء البرمجي يخدم حركة أي جندي في اللعبة

7- في الطلب 6 نسمي التابع S_move يأخذ التابع كمتحولات صحيحة رقمي السطر و العمود للخانة التي يوجد فيها الجندي هنا (D,4) حيث نستبدل دليل العمود الحرفي بالرقم الموافق حسب الترتيب . ضمن التابع يجب تجنيب الجندي من التحرك إلى خارج رقعة الشطرنج . تابعا يمكننا من تحديد وطباعة الأماكن – دليل الأسطر والأعمدة لتلك الأماكن – التي يمكن الجندي في (D,4) أن ينتقل إليها وإيجاد وطباعة عدد هذه الأماكن .

8- اكتب الخطوات التي ترتئيها - الخوارزمية – التي تصف حركة الجندي وما ينتج عنها من تغير مكان الجندي واحتمال إزاحة قطعة من قطع الخصم من اللعبة .

شطرنج.rar

تم تعديل هذه المشاركة بواسطة cold0zero في 2 ديسمبر 2009 في 18:53

#2

1- التصريح عن مصفوفة عناصرها تمثل خانات رقعة الشطرنج .بظن هاد سهل كتير و بينكتب هيك

int a[8][8];

صح هيك مصوفة عرفتها من نوع int اسمها a تحتوي 64 عنصر بيمثلوا عناصر الشطرنج 

بالطلب التاني بدي حلقتين for متداخلتين بحل الموضوع كل حلقة بتروح من الصفر لل 8 بدون الرقم 8 و ضمن الحلقة الداخلية بسند القيمة صفر أو رقم القطعة 

و بالطلب التالت نفس الطلب الثاني لكن هنا نطبع القيم مباشرة بدون أية عملية إسناد

رح اكتب الكود اليوم و حطه لنشوف اذا كان صح و ياريت اللي عنده افكار لباقي الطلبات يساعدني

تم تعديل هذه المشاركة بواسطة cold0zero في 4 ديسمبر 2009 في 17:09

#3
#include <iostream>
using namespace std;

 /*
 * chess board follows the diagram:
 *	_ _ _ _ _ _ _ _
 * 8 |_|_|_|_|_|_|_|_|
 * 7 |_|_|_|_|_|_|_|_|
 * 6 |_|_|_|_|_|_|_|_|
 * 5 |_|_|_|_|_|_|_|_|
 * 4 |_|_|_|_|_|_|_|_|
 * 3 |_|_|_|_|_|_|_|_|
 * 2 |_|_|_|_|_|_|_|_|
 * 1 |_|_|_|_|_|_|_|_|
 *	a b c d e f g h
 *
 * assuming black pieces are from (A,1) to (H,2)
 * white pieces are from (A,7) to (H,8)
 *////////////////////*
 /* by: Xacker - Arabteam2000 forums
 *////////////////////*

//global vars
int chessBoard[8][8];	//step 1
bool vertical = false,leftCorner = false,rightCorner = false;

	//constants
#define	UP		1
#define	DOWN	2

//prototypes
void S_move(int,int);
int S_move_check(int,int);

void main()
{
	int row,col,loc;
	//step 2
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			chessBoard[row][col] = 0;

	int b = 1;
	for (row = 0; row < 2; row++)
		for (col = 0; col < 8; col++)
			chessBoard[row][col] = b++;

	int w = 17;
	for (row = 6; row < 8; row++)
		for (col = 0; col < 8; col++)
			chessBoard[row][col] = w++;

	//step 3
	int tot = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			if (chessBoard[row][col] == 0)
			{
				tot++;
				cout << "empty block found at (" << (char)(col + 65) << "," << row+1 << ")\n";
			}
	cout << "total empty blocks is: " << tot << "\n\n";

	//step 4
	b = 0, w = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			{
				if ((chessBoard[row][col] > 0) && (chessBoard[row][col] < 17))
					b++;
				else if ((chessBoard[row][col] > 16) && (chessBoard[row][col] < 33))
					w++;
			}
	cout << "there are " << b << " black pieces & " << b << " white pieces.\n\n";

	//step 5
	b = 0,w = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
		{
			if ((8 < chessBoard[row][col]) && (chessBoard[row][col] < 17))
				b++;
			else if ((16 < chessBoard[row][col]) && (chessBoard[row][col] < 25))
				w++;
		}
	cout << "black soldiers are: " << b << "\nwhite soldiers are: " << w << "\n\n";

	//step 6
	char c;
	c = 'D';
	row = 4;
	//row = 2;					//test an empty block ahead situation
	//chessBoard[2][2] = 20;	//test left corner
	//chessBoard[2][4] = 22;	//test right corner
	S_move(((int) c - 65),row);	//cast col to int then subtract 65 (decimal value of char 'A')
								//this should give same result as if you write a switch..case statement :)
}

//function to move a soldier
void S_move(int col,int row)
{
	int direction = S_move_check(col,row);

	//step 8
	char k;
	cout << "next move? \n";
	if (vertical || leftCorner || rightCorner)
	{
		if (vertical && direction == UP)
			cout << "[m]ove ahead\n";
		else if (vertical && direction == DOWN)
			cout << "[m]ove down\n";

		if (leftCorner)
			cout << "check [l]eft corner piece\n";

		if (rightCorner)
			cout << "check [r]ight corner piece\n";

		cout << "press any of the defined keys, or any other key to exit: ";
		cin >> k;

		if (k == 'm' || k == 'M')
		{
			char c = (char)(col + 65);
			cout << "move: (" << c << "," << row << ") to (" << c << "," << (direction == UP ? row+1 : row-1) << ")\n\n"; //reg exp is used
		} else if (k == 'l' || k == 'L') {
			char c = (char)(col - 1 + 65);
			cout << "move: (" << (char)(col + 65) << "," << row << ") to (" << c << "," << (direction == UP ? row+1 : row-1) << ")"
				<<	" - check (" << c << "," << (direction == UP ? row+1 : row-1) << ")\n\n";
		} else if (k == 'r' || k == 'R') {
			char c = (char)(col + 1 + 65);
			cout << "move: (" << (char)(col + 65) << "," << row << ") to (" << c << "," << (direction == UP ? row+1 : row-1) << ")"
				<<	" - check (" << c << "," << (direction == UP ? row+1 : row-1) << ")\n\n";
		} else {
			cout << "no valid key was entered, nothing done.\n\n";
		}
	}
}

//function to check a soldier possible moves
//step 7
int S_move_check(int col,int row)
{
	//we should note the color of the piece in order to define the direction of movement
	//this can be done through the number ranges
	cout << "possible moves:\n";
	int totalMoves = 0;
	if ((chessBoard[row-1][col] > 8) && (chessBoard[row-1][col] < 17))	//range of black soldiers
	{
		//direction is up
		//be sure its within the proper range
		if (row < 8)	//moving up is still allowed
		{
			if (chessBoard[row][col] == 0)	//see if block ahead is empty or not
			{
				vertical = true;	//moving ahead is allowed
				char c = (char)(col + 65);
				cout << "(" << c << "," << row << ") to (" << c << "," << row+1 << ")\n";
				totalMoves++;
			}

			//be sure we are not at the edge of the board
			if (col > 0)
				if (chessBoard[row][col-1] != 0)	//see if there is a piece to check
				{
					leftCorner = true;	//check of left corner is allowed
					char c = (char)(col - 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row+1 << ")\n";
					totalMoves++;
				}
			if (col < 7)
				if (chessBoard[row][col+1] != 0)
				{
					rightCorner = true;	//check of right corner is allowed
					char c = (char)(col + 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row+1 << ")\n";
					totalMoves++;
				}

			cout << "total moves allowed are: " << totalMoves << "\n\n";
			return UP;
		} else	//moving up is not possible
			cout << "moving up is not possible.\n\n";
			return 0;

	} else if ((chessBoard[row-1][col] > 16) && (chessBoard[row-1][col] < 25)) {	//range of white soldiers
		//direction is down
		//be sure its within the proper range
		if (row > 1)	//moving down is still allowed
		{
			if (chessBoard[row-2][col] == 0)	//see if block below (ahead) is empty or not
			{
				vertical = true;
				char c = (char)(col + 65);
				cout << "(" << c << "," << row << ") to (" << c << "," << row-1 << ")\n";
				totalMoves++;
			}

			if (col > 0)
				if (chessBoard[row-2][col-1] != 0)	//see if there is a piece to check
				{
					leftCorner = true;	//check of left corner is allowed
					char c = (char)(col - 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row-1 << ")\n";
					totalMoves++;
				}
			if (col < 7)
				if (chessBoard[row-2][col+1] != 0)
				{
					rightCorner = true;	//check of right corner is allowed
					char c = (char)(col + 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row-1 << ")\n";
					totalMoves++;
				}

			cout << "total moves allowed are: " << totalMoves << "\n\n";
			return DOWN;
		} else
			cout << "moving down is not possible.\n\n";
			return 0;
	} else if (chessBoard[row-1][col] != 0) {
		cout << "there is a piece but not a soldier though!\n\n";
		return 0;
	} else {
		cout << "there is no piece to move!\n\n";
		return 0;
	}
}

حسب ما فهمت من الملف المرفق الفاشل بالتوضيح، طلبات متشابهة أو مكررة ومتداخلة ببعضها البعض :wacko:

لا تنساني من دعوة صادقة في ظهر الغيب، وأكلة سمك عالفلوكة :P

تم تعديل هذه المشاركة بواسطة Xacker في 5 ديسمبر 2009 في 15:49

2

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#4

الله يعطيك العافية يا شريك رح شوف الكود وردلك خبرو اكلة السمك جاهزة من هلأ حول

#5

تعديلات، بعد ما شوي صحصحنا على المطلوب:

#include <iostream>
using namespace std;

 /*
 * chess board follows the diagram:
 * / _ _ _ _ _ _ _ _
 * 1 |_|_|_|_|_|_|_|_|
 * 2 |_|_|_|_|_|_|_|_|
 * 3 |_|_|_|_|_|_|_|_|
 * 4 |_|_|_|_|_|_|_|_|
 * 5 |_|_|_|_|_|_|_|_|
 * 6 |_|_|_|_|_|_|_|_|
 * 7 |_|_|_|_|_|_|_|_|
 * 8 |_|_|_|_|_|_|_|_|
 * \ a b c d e f g h
 *
 *//////////////////////////////////*
 /* by: Xacker - Arabteam2000 forums *
 *//////////////////////////////////*

//global vars
int chessBoard[8][8];	//step 1
bool vertical = false,leftCorner = false,rightCorner = false;

	//constants
#define	UP		1
#define	DOWN	2

//prototypes
void S_move(int,int);

void main()
{
	int row,col;
	//step 2
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			chessBoard[row][col] = 0;

	//manual positioning
	chessBoard[0][3] = 1;
	chessBoard[0][5] = 2;
	chessBoard[0][7] = 3;
	chessBoard[1][2] = 4;
	chessBoard[1][3] = 5;
	chessBoard[1][4] = 6;
	chessBoard[1][5] = 7;
	chessBoard[1][6] = 8;
	chessBoard[1][7] = 9;
	chessBoard[2][1] = 10;
	chessBoard[2][2] = 11;
	chessBoard[2][3] = 12;
	chessBoard[2][4] = 13;
	chessBoard[2][7] = 14;
	chessBoard[3][3] = 15;
	chessBoard[3][5] = 16;
	chessBoard[3][6] = 17;
	chessBoard[4][2] = 18;
	chessBoard[4][4] = 19;
	chessBoard[4][7] = 20;
	chessBoard[5][0] = 21;
	chessBoard[5][2] = 22;
	chessBoard[5][5] = 23;
	chessBoard[6][0] = 24;
	chessBoard[6][2] = 25;
	chessBoard[6][4] = 26;
	chessBoard[6][6] = 27;
	chessBoard[6][7] = 28;
	chessBoard[7][0] = 29;
	chessBoard[7][3] = 30;
	chessBoard[7][5] = 31;
	chessBoard[7][6] = 32;

	//step 3
	int totalEmptyBlocks = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			if (chessBoard[row][col] == 0)
			{
				totalEmptyBlocks++;
				cout << "empty block found at (" << (char)(col + 65) << "," << row+1 << ")\n";
			}
	cout << "total empty blocks is: " << totalEmptyBlocks << "\n\n";

	//step 4
	int b = 0,w = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			if ((chessBoard[row][col] > 0) && (chessBoard[row][col] < 17))
				b++;
			else if ((chessBoard[row][col] > 16) && (chessBoard[row][col] < 33))
				w++;

	cout << "black pieces are: " << b << ".\nwhite pieces are: " << b << ".\n\n";

	//step 5
	b = 0,w = 0;
	for (row = 0; row < 8; row++)
		for (col = 0; col < 8; col++)
			if ((8 < chessBoard[row][col]) && (chessBoard[row][col] < 17))
				b++;
			else if ((16 < chessBoard[row][col]) && (chessBoard[row][col] < 25))
				w++;

	cout << "black soldiers are: " << b << ".\nwhite soldiers are: " << w << ".\n\n";

	//step 6
	char c;
	c = 'D';
	row = 4;
	/* can be used with any soldier */
	/* column letter however should be CAPS */
	S_move(((int) c - 65),row);	//cast col to int then subtract 65 (decimal value of char 'A')
								//this should give same result as if you write a switch..case statement :)
}

//function to move a soldier
void S_move(int col,int row)
{
	int direction,piece;
	//step 7
	//we should note the color of the piece in order to define the direction of movement
	//this can be done through the number ranges
	cout << "possible moves:\n";
	int totalMoves = 0;
	if ((chessBoard[row-1][col] > 8) && (chessBoard[row-1][col] < 17))	//range of black soldiers
	{
		//direction is down
		//be sure its within the proper range
		if (row > 1)	//moving down is still allowed
		{
			if (chessBoard[row][col] == 0)	//see if block ahead is empty or not
			{
				vertical = true;	//moving ahead (below) is allowed
				char c = (char)(col + 65);
				cout << "(" << c << "," << row << ") to (" << c << "," << row+1 << ")\n";
				totalMoves++;
			}

			//be sure we are not at the edge of the board
			if (col > 0)
			{
				piece = chessBoard[row][col-1];
				if ((piece != 0) && (piece > 16) && (piece < 25))	//see if there is a piece to check, from the opposit color
				{
					leftCorner = true;	//check of left corner is allowed
					char c = (char)(col - 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row+1 << ")\n";
					totalMoves++;
				}
			}
			if (col < 7)
			{
				piece = chessBoard[row][col+1];
				if ((piece != 0) && (piece > 16) && (piece < 25))
				{
					rightCorner = true;	//check of right corner is allowed
					char c = (char)(col + 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row+1 << ")\n";
					totalMoves++;
				}
			}

			cout << "total moves allowed are: " << totalMoves << "\n\n";
			direction = DOWN;
		} else {	//moving down is not possible
			cout << "moving down is not possible.\n\n";
			direction = 0;
		}

	} else if ((chessBoard[row-1][col] > 16) && (chessBoard[row-1][col] < 25)) {	//range of white soldiers
		//direction is up
		//be sure its within the proper range
		if (row < 8)	//moving up is still allowed
		{
			if (chessBoard[row-2][col] == 0)	//see if block ahead is empty or not
			{
				vertical = true;
				char c = (char)(col + 65);
				cout << "(" << c << "," << row << ") to (" << c << "," << row-1 << ")\n";
				totalMoves++;
			}

			if (col > 0)
			{
				piece = chessBoard[row-2][col-1];
				if ((piece != 0) && (piece > 8) && (piece < 17))	//see if there is a piece to check, from the opposit color
				{
					leftCorner = true;	//check of left corner is allowed
					char c = (char)(col - 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row-1 << ")\n";
					totalMoves++;
				}
			}
			if (col < 7)
			{
				piece = chessBoard[row-2][col+1];
				if ((piece != 0) && (piece > 8) && (piece < 17))
				{
					rightCorner = true;	//check of right corner is allowed
					char c = (char)(col + 1 + 65);
					cout << "(" << (char)(col + 65) << "," << row << ") to (" << c << "," << row-1 << ")\n";
					totalMoves++;
				}
			}

			cout << "total moves allowed are: " << totalMoves << "\n\n";
			direction = UP;
		} else {
			cout << "moving up is not possible.\n\n";
			direction = 0;
		}
	} else if (chessBoard[row-1][col] != 0) {
		cout << "there is a piece, but it's not a soldier though!\n\n";
		direction = 0;
	} else {
		cout << "there is no piece to move!\n\n";
		direction = 0;
	}

	//step 8
	char k;
	cout << "next move? \n";
	if (vertical || leftCorner || rightCorner)
	{
		if (vertical && direction == UP)
			cout << "[m]ove ahead.\n";
		else if (vertical && direction == DOWN)
			cout << "[m]ove down.\n";

		if (leftCorner)
			cout << "check [l]eft corner piece.\n";

		if (rightCorner)
			cout << "check [r]ight corner piece.\n";

		cout << "press any of the defined keys to continue, or any other key to exit: ";
		cin >> k;

		if ((k == 'm' || k == 'M') && vertical)
		{
			char c = (char)(col + 65);
			cout << "move: (" << c << "," << row << ") to (" << c << "," << (direction == UP ? row-1 : row+1) << ")\n\n"; //reg exp is used

			if (direction == UP)
				chessBoard[row-2][col] = chessBoard[row-1][col];
			else
				chessBoard[row][col] = chessBoard[row-1][col];

			chessBoard[row-1][col] = 0;
		} else if ((k == 'l' || k == 'L') && leftCorner) {
			char c = (char)(col - 1 + 65);
			cout << "move: (" << (char)(col + 65) << "," << row << ") to (" << c << "," << (direction == UP ? row-1 : row+1) << ")"
				<<	" - check (" << c << "," << (direction == UP ? row-1 : row+1) << ")\n\n";

			if (direction == UP)
				chessBoard[row-2][col-1] = chessBoard[row-1][col];
			else
				chessBoard[row][col-1] = chessBoard[row-1][col];

			chessBoard[row-1][col] = 0;
		} else if ((k == 'r' || k == 'R') && rightCorner) {
			char c = (char)(col + 1 + 65);
			cout << "move: (" << (char)(col + 65) << "," << row << ") to (" << c << "," << (direction == UP ? row-1 : row+1) << ")"
				<<	" - check (" << c << "," << (direction == UP ? row-1 : row+1) << ")\n\n";

			if (direction == UP)
				chessBoard[row-2][col+1] = chessBoard[row-1][col];
			else
				chessBoard[row][col+1] = chessBoard[row-1][col];

			chessBoard[row-1][col] = 0;
		} else {
			cout << "no valid key was entered, nothing done.\n\n";
		}
	}
}

التعديلات تمت بناء على الصورة التي أرسلها cold0zero (احكي ولا خليني ساكت يا cold0zero؟) والتي كانت مع المشروع..

الصورة وملفات المشروع في المرفقات.

* تم تصحيح بعض الأخطاء في الكود السابق لم أنتبه إليها إلا حين إعادة المشروع بالأفكار الجديدة.

السلام عليكم.

chess.rar

scan00011v.rar

تم تعديل هذه المشاركة بواسطة Xacker في 5 ديسمبر 2009 في 19:44

1

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#6

كلمة شكرا قليلة بحقك كتير يا X

1
#7
cold0zero كتب:

كلمة شكرا قليلة بحقك كتير يا X

إن ما كانت علامتي بامتحان الثلاثاء عالية بدي طالع أكلة السمك من عيونك.. طول بالك بس :P

2

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

مواضيع مشابهة