Attacker jumper -solution
Area: two-dimensional arrays - matrices
#include <iostream>
#include<string>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
string table[9][9];
for(int i=8; i>=1; i--)
{
for(int j=1; j<=8; j++)
{
if(i==m && j==n)
{
int v=0;
for(int r=i+2; r>=i-2; r--)
{
table[i][j]=" 0";
if(r>i)
v++;
else if(r<i-1)
v--;
else if(r==i)
{
table[i][j]=" *";
continue;
}
if(r<1||r>8)continue;
int k1=n+v;
int k2=n-v;
char kol1='a'+k1-1;
char kol2='a'+k2-1;
char red='0'+r;
string s1="";
s1+=kol1;
s1+=red;
string s2="";
s2+=kol2;
s2+=red;
if(k1<=8)
table[r][k1]=s1;
if(k2>=1)
table[r][k2]=s2;
}
}
else
{
if((table[i][j]).length()==0)
{
table[i][j]=" *";
}
}
}
}
for(int i=8; i>=1; i--)
{
for(int j=1; j<=8; j++)
{
cout<<table[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
#include<string>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
string table[9][9];
for(int i=8; i>=1; i--)
{
for(int j=1; j<=8; j++)
{
if(i==m && j==n)
{
int v=0;
for(int r=i+2; r>=i-2; r--)
{
table[i][j]=" 0";
if(r>i)
v++;
else if(r<i-1)
v--;
else if(r==i)
{
table[i][j]=" *";
continue;
}
if(r<1||r>8)continue;
int k1=n+v;
int k2=n-v;
char kol1='a'+k1-1;
char kol2='a'+k2-1;
char red='0'+r;
string s1="";
s1+=kol1;
s1+=red;
string s2="";
s2+=kol2;
s2+=red;
if(k1<=8)
table[r][k1]=s1;
if(k2>=1)
table[r][k2]=s2;
}
}
else
{
if((table[i][j]).length()==0)
{
table[i][j]=" *";
}
}
}
}
for(int i=8; i>=1; i--)
{
for(int j=1; j<=8; j++)
{
cout<<table[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
© 2019 by Slobodan izprogramiranja.weebly.com
All rights reserved