Vous êtes sur la page 1sur 5

#include"stdafx.

h"
#include <iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<list>
#include<math.h>
typedef long long int ll;
using namespace std;

bool ismoveghora(pair<char, char> arr[][8], int s1, int s2, int d1, int d2)
{
int dx[] = {-2,-1,1,2,2,1,-1,-2};
int dy[] = {1,2,2,1,-1,-2,-2,-1};

for (int i = 0; i < 8; i++)


{
int a = s1 + dx[i];
int b = s2 + dy[i];
if (a == d1 && b == d2)
return true;
}
return false;
}

bool ismovehaathi(pair<char, char> arr[][8], int s1, int s2, int d1, int d2)
{
if (s1 == d1 && s2 != d2)//same row
{
for (int i = min(s2, d2)+1; i < max(s2, d2); i++)
{
if (arr[s1][i].first != '0')
{
cout << "row me kuch h beech me";
return false;
}
}
}
if (s1 != d1 && s2 == d2)//same col
{
for (int i = min(s1, d1)+1; i < max(s1, d1); i++)
{
if (arr[i][s2].first != '0') {
cout << "col me kuch h beech me";
return false;
}
}
}
return true;

bool ismovepyada(pair<char, char> arr[][8], int s1, int s2, int d1, int d2)
{

// check for straight


if(arr[s1][s2].second=='a'&&arr[d1][d2].first=='0'&&d1==s1+1&&d2==s2)
return true;

if (arr[s1][s2].second == 'b'&&arr[d1][d2].first == '0'&&d1 == s1 - 1 && d2


== s2 - 1)
return true;

if (arr[s1][s2].second == 'a'&&arr[d1][d2].second == 'b' && ((d1 == s1 + 1 &&


d2 == s2 - 1) || (d1 == s1 + 1 && d2 == s2 + 1)))
return true;
if (arr[s1][s2].second == 'b'&&arr[d1][d2].second == 'a' && ((d1 == s1 - 1 &&
d2 == s2 - 1) || (d1 == s1 -1 && d2 == s2 + 1)))
return true;
return false;
}

void update_pos(pair<char, char> arr[][8], int s1, int s2, int d1, int d2)
{
arr[d1][d2].second = arr[s1][s2].second;
arr[d1][d2].first = arr[s1][s2].first;
arr[s1][s2].first = '0';
arr[s1][s2].second = '0';
}

int main()
{
int t;
cin >> t;
pair<char, char> arr[8][8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (i == 1)
{
arr[i][j].first = 'p';
arr[i][j].second = 'a';
}
else if (i == 6)
{
arr[i][j].first = 'p';
arr[i][j].second = 'b';
}
else
{
arr[i][j].first = '0';
arr[i][j].second = '0';
}
}
}
arr[0][0].first = 'h';
arr[0][0].second = 'a';
arr[0][7].first = 'h';
arr[0][7].second = 'a';

arr[0][1].first = 'g';
arr[0][1].second = 'a';
arr[0][6].first = 'g';
arr[0][6].second = 'a';

arr[7][0].first = 'h';
arr[7][0].second = 'b';
arr[7][7].first = 'h';
arr[7][7].second = 'b';

arr[7][1].first = 'g';
arr[7][1].second = 'b';
arr[7][6].first = 'g';
arr[7][6].second = 'b';

cout << "board looks like this" << endl;


for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << arr[i][j].first << " ";
}cout << endl;
}

while (t--)
{
int s1, s2, d1, d2;
cout << "enter source coordinates: ";
cin >> s1 >> s2;
cout << "enter destination coordinates " << endl;
cin >> d1 >> d2;

if (s1 < 0 || s2 < 0 || d1 < 0 || d2 < 0 || s1>7 || s2>7 || d1>7 ||


d2>7)
{
cout << "false" << endl;
}
else if(arr[s1][s2].first=='0')
{
cout << "false" << endl;
}
else
{
if (arr[s1][s2].first == 'g')
{
if (ismoveghora(arr, s1, s2, d1, d2))
{
if (arr[d1][d2].first == '0')
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else if (arr[s1][s2].second != arr[d1][d2].second)
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else
{
cout << "false" << endl;
}
}
else
{
cout << "false" << endl;
}
}
else if (arr[s1][s2].first == 'h')
{
if (ismovehaathi(arr, s1, s2, d1, d2))
{
if (arr[d1][d2].first == '0')
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else if (arr[s1][s2].second != arr[d1][d2].second)
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else
{
cout << "usi ka player h jise kaatra h";
cout << "false" << endl;
}
}
else
{
cout << "move function se false aaya h" << endl;
cout << "false" << endl;
}
}
else if (arr[s1][s2].first == 'p')
{
if (ismovepyada(arr, s1, s2, d1, d2))
{
if (arr[d1][d2].first == '0')
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else if (arr[s1][s2].second != arr[d1][d2].second)
{
cout << "true" << endl;
update_pos(arr, s1, s2, d1, d2);
}
else
{
cout << "false" << endl;
}
}
else
{
cout << "false" << endl;
}
}
else
{
cout << "false" << endl;
}
}

for (int i = 0; i < 8; i++)


{
for (int j = 0; j < 8; j++) {
cout << arr[i][j].first << " ";
}
cout << " ";
for (int j = 0; j < 8; j++) {
cout << arr[i][j].second << " ";
}cout << endl;

}
cout << endl;
}

return 0;
}

Vous aimerez peut-être aussi