Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 6 months ago.
C++ multidimensional array in class
Hello, I'm trying to write a chess game. I initialized the pieces as inegers, but now I have to create a board containing them. I don't know how to initialize the 8x8 array in other way than writing 64 elements in method. Please help the newbie. PS The names are in Polish.
#include <iostream> using namespace std; class szachy { int plansza[8][8]; static const int pionek=1; static const int skoczek=2; static const int goniec=3; static const int wieza=4; static const int dama=5; static const int krol=6; void figury() { string figura; for (int a = 7; a > -1; a--) { cout << endl; for (int b = 0; b < 8; b++) { switch (plansza[a][b]) { case 0: figura = "-"; break; case pionek: figura = "P"; break; case skoczek: figura = "S"; break; case goniec: figura = "G"; break; case wieza: figura = "W"; break; case dama: figura = "D"; break; case krol: figura = "K"; break; case -pionek: figura = "p"; break; case -skoczek: figura = "s"; break; case -goniec: figura = "g"; break; case -wieza: figura = "w"; break; case -dama: figura = "d"; break; case -krol: figura = "k"; break; } //cout << " " << figura << " "; } } cout<<endl<<endl; } public: int nowa[8][8]; void wpisz(); void rysuj() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { plansza[i][j] = 1;//nowa[i][j]; cout<<plansza[i][j]; } } } }; /* int szachy::wpisz() { nowa[8][8]= { {wieza,skoczek,goniec,dama,krol,goniec,skoczek,wieza}, {pionek,pionek,pionek,pionek,pionek,pionek,pionek,pionek}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {-pionek,-pionek,-pionek,-pionek,-pionek,-pionek,-pionek,-pionek}, {-wieza,-skoczek,-goniec,-dama,-krol,-goniec,-skoczek,-wieza} }; } */