R
Rompo
Написал код, помогите разбить его на элементарные процедуры и функции. Только мне нужно без усложнения моего написания, т.е без динамического выделения памяти... короче как можно проще! Заранее спасибо.
C++:
#include <iostream>
#include <windows.h>
#include <vector>
#include <cmath>
using namespace std;
string to_rus(const char *str)
{
char buf[1024];
string res;
CharToOemA(str, buf);
res = buf;
return res;
}
int main()
{
int x, y;
int WK[9][9], BK[9][9], EL[9][9], H[9][9];
bool bl = false;
for (x = 1; x < 9; x++){
for (y = 1; y < 9; y++){
WK[x][y] = 0;
EL[x][y] = 0;
H[x][y] = 0;
BK[x][y] = 0;
}
}
//________________________________________________________________________
cout << to_rus("Белый король : ");
cin >> x >> y;
cout << endl;
int WKx = x, WKy = y;
while ((x <= 0) | (x > 8) | (y <= 0 ) | (y > 8)){
cout << to_rus("Введены неверные координаты! Повторите...") << endl;
cout << endl;
cout << to_rus("Белый король : ");
cin >> x >> y;
WKx = x, WKy = y;
}
WK[x][y] = 1;
int WK_kol = 0;
//cout << "WHITE_KING-0[" << x << "][" << y << "]" << endl;
if ((x - 1 > 0) & (y - 1 > 0)){
WK[x - 1][y - 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x - 1) << "][" << (y - 1) << "]" << endl;
}
if (x - 1 > 0){
WK[x - 1][y] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x - 1) << "][" << y << "]" << endl;
}
if ((x - 1 > 0) & (y + 1 < 8)){
WK[x - 1][y + 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x - 1) << "][" << (y + 1) << "]" << endl;
}
if (y - 1 > 0){
WK[x][y - 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << x << "][" << (y - 1) << "]" << endl;
}
if (y + 1 < 8){
WK[x][y + 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << x << "][" << (y + 1) << "]" << endl;
}
if ((x + 1 < 8) & (y - 1 > 0)){
WK[x + 1][y - 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x + 1) << "][" << (y - 1) << "]" << endl;
}
if (x + 1 < 8){
WK[x + 1][y] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x + 1) << "][" << y << "]" << endl;
}
if ((x + 1 < 8) & (y + 1 < 8)){
WK[x + 1][y + 1] = 2;
++WK_kol;
//cout << "WHITE_KING-" << ++WK_kol << "[" << (x + 1) << "][" << (y + 1) << "]" << endl;
}
//________________________________________________________________________
int n = 0;
int ELx, ELy;
cout << to_rus("Черный слон : ");
cin >> x >> y;
cout << endl;
ELx = x; ELy = y;
while ((x <= 0) | (x > 8) | (y <= 0 ) | (y > 8) | ((ELx == WKx) & (ELy == WKy))){
cout << to_rus("> ...Введены неверные координаты! Повторите... <") << endl;
if ((ELx == WKx) & (ELy == WKy))
cout << to_rus("> ...Координаты Черного Слона совпадают с координатами Белого Короля!... <") << endl;
cout << endl;
cout << to_rus("Черный слон : ");
cin >> x >> y;
cout << endl;
ELx = x; ELy = y;
}
EL[x][y] = 1;
//cout << "ELEPHANT-0[" << x << "][" << y << "]" << endl;
while ((x > 1) & (y > 1)) {
//n++;
--x; --y;
//cout << "ELEPHANT-" << n << "[" << --x << "][" << --y << "]" << endl;
EL[x][y] = 2;
}
x = ELx; y = ELy;
while ((x < 8) & (y < 8)) {
//n++;
++x; ++y;
//cout << "ELEPHANT-" << n << "[" << ++x << "][" << ++y << "]" << endl;
EL[x][y] = 2;
}
x = ELx; y = ELy;
while ((x > 1) & (y < 8)) {
//n++;
--x; ++y;
//cout << "ELEPHANT-" << n << "[" << --x << "][" << ++y << "]" << endl;
EL[x][y] = 2;
}
x = ELx; y = ELy;
while ((x < 8) & (y > 1)) {
//n++;
++x; --y;
//cout << "ELEPHANT-" << n << "[" << ++x << "][" << --y << "]" << endl;
EL[x][y] = 2;
}
//________________________________________________________________________
cout << to_rus("Черный конь : ");
cin >> x >> y;
cout << endl;
int Hx = x, Hy = y;
while ((x <= 0) | (x > 8) | (y <= 0 ) | (y > 8) | ((Hx == ELx) & (Hy == ELy)) | ((Hx == WKx) & (Hy == WKy))){
cout << to_rus("> ...Введены неверные координаты! Повторите... <") << endl;
if ((Hx == WKx) & (Hy == WKy))
cout << to_rus("> ...Координаты Черного Коня совпадают с координатами Белого Короля!... <") << endl;
if ((Hx == ELx) & (Hy == ELy))
cout << to_rus("> ...Координаты Черного Коня совпадают с координатами Черного Слона!... <") << endl;
cout << endl;
cout << to_rus("Черный конь : ");
cin >> x >> y;
cout << endl;
Hx = x, Hy = y;
}
H[x][y] = 1;
//cout << "HORSE-0[" << x << "][" << y << "]" << endl;
if ((x - 1 > 0) & (y - 2 > 0)){
H[x - 1][y - 2] = 2;
//cout << "HORSE-1[" << (x - 1) << "][" << (y - 2) << "]" << endl;
}
if ((x - 2 > 0) & (y - 1 > 0)){
H[x - 2][y - 1] = 2;
//cout << "HORSE-2[" << (x - 2) << "][" << (y - 1) << "]" << endl;
}
if ((x + 1 < 9) & (y - 2 > 0)){
H[x + 1][y - 2] = 2;
//cout << "HORSE-3[" << (x + 1) << "][" << (y - 2) << "]" << endl;
}
if ((x - 2 > 0) & (y + 1 < 9)){
H[x - 2][y + 1] = 2;
//cout << "HORSE-4[" << (x - 2) << "][" << (y + 1) << "]" << endl;
}
if ((x + 2 < 9) & (y + 1 < 9)){
H[x + 2][y + 1] = 2;
//cout << "HORSE-5[" << (x + 2) << "][" << (y + 1) << "]" << endl;
}
if ((x + 1 < 9) & (y + 2 < 9)){
H[x + 1][y + 2] = 2;
//cout << "HORSE-6[" << (x + 1) << "][" << (y + 2) << "]" << endl;
}
if ((x + 2 < 9) & (y - 1 > 0)){
H[x + 2][y - 1] = 2;
//cout << "HORSE-7[" << (x + 2) << "][" << (y - 1) << "]" << endl;
}
if ((x - 1 > 0) & (y + 2 < 9)){
H[x - 1][y + 2] = 2;
//cout << "HORSE-8[" << (x - 1) << "][" << (y + 2) << "]" << endl;
}
//________________________________________________________________________
cout << to_rus("Черный король: ");
cin >> x >> y;
cout << endl;
int BKx = x, BKy = y;
while ((x <= 0) | (x > 8) | (y <= 0 ) | (y > 8) | ((BKx == ELx) & (BKy == ELy)) | ((BKx == Hx) & (BKy == Hy)) | ((BKx == WKx) & (BKy == WKy))){
cout << to_rus("> ...Введены неверные координаты! Повторите... <") << endl;
if ((BKx == Hx) & (BKy == Hy))
cout << to_rus("> ...Координаты Черного Короля совпадают с координатами Черного Коня!... <") << endl;
if ((BKx == ELx) & (BKy == ELy))
cout << to_rus(">... Координаты Черного Короля совпадают с координатами Черного Слона!... <") << endl;
if ((BKx == WKx) & (BKy == WKy))
cout << to_rus(">... Координаты Черного Короля совпадают с координатами Белого Короля!... <") << endl;
cout << endl;
cout << to_rus("Черный король : ");
cin >> x >> y;
cout << endl;
BKx = x, BKy = y;
}
BK[x][y] = 1;
//cout << "BLACK_KING-0[" << x << "][" << y << "]" << endl;
if ((x - 1 > 0) & (y - 1 > 0)){
BK[x - 1][y - 1] = 2;
//cout << "BLACK_KING-1[" << (x - 1) << "][" << (y - 1) << "]" << endl;
}
if (x - 1 > 0){
BK[x - 1][y] = 2;
//cout << "BLACK_KING-2[" << (x - 1) << "][" << y << "]" << endl;
}
if ((x - 1 > 0) & (y + 1 < 8)){
BK[x - 1][y + 1] = 2;
//cout << "BLACK_KING-3[" << (x - 1) << "][" << (y + 1) << "]" << endl;
}
if (y - 1 > 0){
BK[x][y - 1] = 2;
//cout << "BLACK_KING-4[" << x << "][" << (y - 1) << "]" << endl;
}
if (y + 1 < 8){
BK[x][y + 1] = 2;
//cout << "BLACK_KING-5[" << x << "][" << (y + 1) << "]" << endl;
}
if ((x + 1 < 8) & (y - 1 > 0)){
BK[x + 1][y - 1] = 2;
//cout << "BLACK_KING-6[" << (x + 1) << "][" << (y - 1) << "]" << endl;
}
if (x + 1 < 8){
BK[x + 1][y] = 2;
//cout << "BLACK_KING-7[" << (x + 1) << "][" << y << "]" << endl;
}
if ((x + 1 < 8) & (y + 1 < 8)){
BK[x + 1][y + 1] = 2;
//cout << "BLACK_KING-8[" << (x + 1) << "][" << (y + 1) << "]" << endl;
}
//________________________________________________________________________
for (x = 1; x < 9; ++x)
for(y = 1; y < 9; ++y){
if(((WK[x][y] == EL[x][y] - 1) & (WK[x][y] != 0)) | ((WK[x][y] == H[x][y] - 1) & (WK[x][y] != 0))){
bl = true;
}
}
float WK_EL, WK_H, EL_H, WK_BK, EL_BK;
WK_EL = sqrt((WKx - ELx)*(WKx - ELx) + (WKy - ELy)*(WKy - ELy));
EL_H = sqrt((Hx - ELx)*(Hx - ELx) + (Hy - ELy)*(Hy - ELy));
WK_H = sqrt((Hx - WKx)*(Hx - WKx) + (Hy - WKy)*(Hy - WKy));
WK_BK = sqrt((WKx - BKx)*(WKx - BKx) + (WKy - BKy)*(WKy - BKy));
EL_BK = sqrt((BKx - ELx)*(BKx - ELx) + (BKy - ELy)*(BKy - ELy));
if (bl){
for (x = 1; x < 9; ++x)
for(y = 1; y < 9; ++y){
if(((H[x][y] == EL[x][y] - 1) & (H[x][y] != 0)) | ((BK[x][y] == EL[x][y] - 1) & (BK[x][y] != 0))){
if(((WK_EL > EL_H) & (WK_EL > WK_H)) | ((WK_EL > EL_BK) & (WK_EL > WK_BK)))
bl = false;
}
}
}
for (x = 1; x < 9; ++x)
for(y = 1; y < 9; ++y){
if(((WK[x][y] == EL[x][y]) | (WK[x][y] == H[x][y]) | (WK[x][y] == BK[x][y])) & (WK[x][y] != 0))
--WK_kol;
}
cout << endl;
cout << to_rus("Результат: ");
if (bl){
if (WK_kol == 0)
cout << to_rus("ШАХ и МАТ! Черные победили...") << endl;
else
cout << to_rus("ШАХ! Белый король под угрозой...") << endl;
}
else {
if (WK_kol == 0)
cout << to_rus("ПАТ! Ничья...") << endl;
else
cout << to_rus("Обычная позиция") <<endl;
}
cout << endl;
system("PAUSE");
return 0;
}