A co povies na takyto jednoduchy sach:
Kód:
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
int tah[4]; //pole na ulozenie suradnic pohybu odkial, kam.
bool pohyb=true; //urcuje kto je na tahu true=cerveny
string b,c; //retazce na nacitanie suradnic pohybu
//funkcia na prevod suradnic beznej sachovnice na suradnice matice pola pointerov na figurky.
bool prevod(string a,string b)
{
bool najdena1=false;
bool najdena2=false;
string prevodnik[8][8]={"A8","B8","C8","D8","E8","F8","G8","H8",
"A7","B7","C7","D7","E7","F7","G7","H7",
"A6","B6","C6","D6","E6","F6","G6","H6",
"A5","B5","C5","D5","E5","F5","G5","H5",
"A4","B4","C4","D4","E4","F4","G4","H4",
"A3","B3","C3","D3","E3","F3","G3","H3",
"A2","B2","C2","D2","E2","F2","G2","H2",
"A1","B1","C1","D1","E1","F1","G1","H1",
};
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(a==prevodnik[i][j])
{
tah[0]=i; tah[1]=j;
najdena1=true;
}
if(b==prevodnik[i][j])
{
tah[2]=i; tah[3]=j;
najdena2=true;
}
}
}
return najdena1&&najdena2;
}
//abstraktna trieda figurka.
class TFigurka
{
protected:
bool f; // 0- modra, 1- cervena
int pozx;
int pozy;
int vyhodena; // 1- stoji figurka, 0- vyhodena figurka , 2- prazdne policko
char meno;
bool stojim;
public:
virtual void zisti(){cout<<"Ziadne tahy\n";};
TFigurka(){};
TFigurka(int a, int b,int f):pozx(a), pozy(b),f(f){stojim=false;};
virtual void tahaj(int a,int b){cout<<"Neviem ako mam tahat\n";};
virtual void vypis(void)
{
cout<<meno;
}
int ukazfarbu(void)
{
return f;
}
bool ukaz_stojim()
{
return stojim;
}
char ukazmeno()
{
return meno;
}
};
TFigurka *sach[8][8]; //pole pointerov na figurky
class TPesiak:public virtual TFigurka
{
public:
TPesiak(){};
TPesiak(int a, int b,int f):TFigurka(a,b,f){meno='P',vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TStrelec: public virtual TFigurka
{
public:
TStrelec(){};
TStrelec(int a, int b,int f):TFigurka(a,b,f){meno='B',vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TKon: public virtual TFigurka
{
public:
TKon(){};
TKon(int a, int b,int f):TFigurka(a,b,f){meno='H';vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TVeza: public virtual TFigurka
{
public:
TVeza(){};
TVeza(int a, int b,int f):TFigurka(a,b,f){meno='V',vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TDama: public virtual TFigurka
{
public:
TDama(){};
TDama(int a, int b,int f):TFigurka(a,b,f){meno='Q',vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TKral: public virtual TFigurka
{
public:
TKral(){};
TKral(int a, int b,int f):TFigurka(a,b,f){meno='K',vyhodena=0;stojim=true;};
void tahaj(int a,int b)
{
sach[a][b]=this;
}
char ukazmeno()
{
return meno;
}
};
class TZiadna: public virtual TFigurka
{
public:
TZiadna(){};
TZiadna(int a, int b, int f):TFigurka(a,b,f){meno=' ';vyhodena=2;stojim=true;}
void tahaj(int a,int b)
{
cout<<"Tu nie je ziadna figurka!\n";
for(unsigned int i=0;i<300000000;i++); //delay
}
char ukazmeno()
{
return meno;
}
};
void vykresli(void)
{
bool policko=true; //flase- cierne, true- biele
int f;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,1);
for(int i=0;i<8;i++)
{
SetConsoleTextAttribute(hConsole,15);
cout<<8-i;
for(int j=0;j<8;j++)
{
if(policko==1 && sach[i][j]->ukazfarbu()==1) f=244;
if(policko==0 && sach[i][j]->ukazfarbu()==1) f=4;
if(policko==1 && sach[i][j]->ukazfarbu()==0) f=241;
if(policko==0 && sach[i][j]->ukazfarbu()==0) f=1;
SetConsoleTextAttribute(hConsole,f);
cout<<" ";
sach[i][j]->vypis();
cout<<" ";
policko=!policko;
}
policko=!policko;
cout<<endl;
}
SetConsoleTextAttribute(hConsole,15);
cout<<' ';
for(char i='A';i<'I';i++) cout<<" "<<i<<" ";
cout<<endl;
SetConsoleTextAttribute(hConsole,15);
}
int main(int argc, char *argv[])
{
//inicializacia sachovnice:
for(int i=2;i<6;i++) //prazdne policka
{
for(int j=0;j<8;j++)
{
sach[i][j]=new TZiadna(i,j,0);
}
}
for(int i=0;i<8;i++) //pesiaci
{
sach[1][i]=new TPesiak(1,i,0); //modri
sach[6][i]=new TPesiak(6,i,1); //cerveni
}
sach[0][2]=new TStrelec(0,2,0); //strelci
sach[0][5]=new TStrelec(0,5,0);
sach[7][2]=new TStrelec(7,2,1);
sach[7][5]=new TStrelec(7,5,1);
sach[0][1]=new TKon(0,1,0); //jazdci
sach[0][6]=new TKon(0,6,0);
sach[7][1]=new TKon(7,1,1);
sach[7][6]=new TKon(7,6,1);
sach[0][0]=new TVeza(0,0,0); //veze
sach[0][7]=new TVeza(0,7,0);
sach[7][0]=new TVeza(7,0,1);
sach[7][7]=new TVeza(7,7,1);
sach[0][3]=new TDama(0,3,0); //dama
sach[7][3]=new TDama(7,3,1);
sach[0][4]=new TKral(0,4,0); //krali
sach[7][4]=new TKral(7,4,1);
//koniec inicializacie sachovnice
//hra
do
{
system("cls");
vykresli();
if(pohyb) cout<<"Tah cerveny.\n";
else cout<<"Tah modry.\n";
cout<<"Ktorou mam tahat 0-koniec: ";
cin>>b;
if(b=="0") break; //koniec cyklu
cout<<endl<<"Kam mam tahat: ";
cin>>c;
if(prevod(b,c))
{
if(sach[tah[0]][tah[1]]->ukazfarbu()==!pohyb && sach[tah[0]][tah[1]]->ukazmeno()!=' ')
{
cout<<"Nemozno hybat superovou figurkou\n";
for(unsigned int i=0;i<300000000;i++); //delay
}
if(sach[tah[0]][tah[1]]->ukazmeno()==' ')
sach[tah[0]][tah[1]]->tahaj(tah[2],tah[3]);
if(sach[tah[0]][tah[1]]->ukazfarbu()==pohyb)
{
sach[tah[0]][tah[1]]->tahaj(tah[2],tah[3]);
sach[tah[0]][tah[1]]=new TZiadna(tah[0],tah[1],0);
pohyb=!pohyb;
}
}
else
{
cout<<"Nespravne suradnice"<<endl;
for(unsigned int i=0;i<300000000;i++); //delay
}
}while(b!="0");
return EXIT_SUCCESS;
}
http://www.danciwo.ic.cz