@glorius
Moje iskustvo je pocetnicko.
U ovom trenutku mogu da ti postavim samo kod koji mi sluzi za testiranje svih mojih zamisli.
Koristim jednog pesaka i sve drzim u jednom fajlu. Nisu uradjene nikakve provere kao npr dali je potez legalan i slicne.
Kad istestiram tek se onda bacam na pravu implementaciju i bolje uredjenje koda:
Code:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
int convertChar(char c)
{
switch (c)
{
case '1':
case 'a': return 0;
case '2':
case 'b': return 1;
case '3':
case 'c': return 2;
case '4':
case 'd': return 3;
case '5':
case 'e': return 4;
case '6':
case 'f': return 5;
case '7':
case 'g': return 6;
case '8':
case 'h': return 7;
default: return -1;
}
}
class pawn {
int Mposx, Mposy;
char Mcolor;
string Mposition;
bool Meated;
public:
pawn(string position, char color)
{Mposy=convertChar(position[0]);
Mposx=convertChar(position[1]);
Mcolor=color; Mposition=position; Meated=false;
}
void setOnBoard(string board[8][8]){board[Mposx][Mposy]=(Mcolor=='b')?"Pb":"Pw";}
string getStrPosition(){return Mposition;}
void move(string toPosition, string board[8][8])
{
if ((Mposx+Mposy)%2) board[Mposx][Mposy]=" ";
else board[Mposx][Mposy]="//";
Mposy=convertChar(toPosition[0]);
Mposx=convertChar(toPosition[1]);
Mposition=toPosition;
board[Mposx][Mposy]=(Mcolor=='b')?"Pb":"Pw";
}
};
int whatPiece(string position, vector<pawn> pwn)
{
for (int i=0; i<pwn.size(); i++)
if (pwn[i].getStrPosition()==position) return i;
return -1;
}
void displayBoard (string board[8][8])
{
cout<<"\n"
" A B C D E F G H \n"
" ---------------------------------------------------------\n"
" | |//////| |//////| |//////| |//////|\n"
" | |// //| |// //| |// //| |//////|\n"
" 8| "<<board[7][0]<<" |- "<<board[7][1]<<" -| "<<board[7][2]<<" |- "<<board[7][3]<<" -| "<<board[7][4]<<" |- "<<board[7][5]<<" -| "<<board[7][6]<<" |- "<<board[7][7]<<" -|\n"
" | |// //| |// //| |// //| |// //|\n"
" | |//////| |//////| |//////| |//////|\n"
" ---------------------------------------------------------\n"
" |//////| |//////| |//////| |//////| |\n"
" |// //| |// //| |// //| |// //| |\n"
" 7|- "<<board[6][0]<<" -| "<<board[6][1]<<" |- "<<board[6][2]<<" -| "<<board[6][3]<<" |- "<<board[6][4]<<" -| "<<board[6][5]<<" |- "<<board[6][6]<<" -| "<<board[6][7]<<" |\n"
" |// //| |// //| |// //| |// //| |\n"
" |//////| |//////| |//////| |//////| |\n"
" ---------------------------------------------------------\n"
" | |//////| |//////| |//////| |//////|\n"
" | |// //| |// //| |// //| |// //|\n"
" 6| "<<board[5][0]<<" |- "<<board[5][1]<<" -| "<<board[5][2]<<" |- "<<board[5][3]<<" -| "<<board[5][4]<<" |- "<<board[5][5]<<" -| "<<board[5][6]<<" |- "<<board[5][7]<<" -|\n"
" | |// //| |// //| |// //| |// //|\n"
" | |//////| |//////| |//////| |//////|\n"
" ---------------------------------------------------------\n"
" |//////| |//////| |//////| |//////| |\n"
" |// //| |// //| |// //| |// //| |\n"
" 5|- "<<board[4][0]<<" -| "<<board[4][1]<<" |- "<<board[4][2]<<" -| "<<board[4][3]<<" |- "<<board[4][4]<<" -| "<<board[4][5]<<" |- "<<board[4][6]<<" -| "<<board[4][7]<<" |\n"
" |// //| |// //| |// //| |// //| |\n"
" |//////| |//////| |//////| |//////| |\n"
" ---------------------------------------------------------\n"
" | |//////| |//////| |//////| |//////|\n"
" | |// //| |// //| |// //| |// //|\n"
" 4| "<<board[3][0]<<" |- "<<board[3][1]<<" -| "<<board[3][2]<<" |- "<<board[3][3]<<" -| "<<board[3][4]<<" |- "<<board[3][5]<<" -| "<<board[3][6]<<" |- "<<board[3][7]<<" -|\n"
" | |// //| |// //| |// //| |// //|\n"
" | |//////| |//////| |//////| |//////|\n"
" ---------------------------------------------------------\n"
" |//////| |//////| |//////| |//////| |\n"
" |// //| |// //| |// //| |// //| |\n"
" 3|- "<<board[2][0]<<" -| "<<board[2][1]<<" |- "<<board[2][2]<<" -| "<<board[2][3]<<" |- "<<board[2][4]<<" -| "<<board[2][5]<<" |- "<<board[2][6]<<" -| "<<board[2][7]<<" |\n"
" |// //| |// //| |// //| |// //| |\n"
" |//////| |//////| |//////| |//////| |\n"
" ---------------------------------------------------------\n"
" | |//////| |//////| |//////| |//////|\n"
" | |// //| |// //| |// //| |// //|\n"
" 2| "<<board[1][0]<<" |- "<<board[1][1]<<" -| "<<board[1][2]<<" |- "<<board[1][3]<<" -| "<<board[1][4]<<" |- "<<board[1][5]<<" -| "<<board[1][6]<<" |- "<<board[1][7]<<" -|\n"
" | |// //| |// //| |// //| |// //|\n"
" | |//////| |//////| |//////| |//////|\n"
" ---------------------------------------------------------\n"
" |//////| |//////| |//////| |//////| |\n"
" |// //| |// //| |// //| |// //| |\n"
" 1|- "<<board[0][0]<<" -| "<<board[0][1]<<" |- "<<board[0][2]<<" -| "<<board[0][3]<<" |- "<<board[0][4]<<" -| "<<board[0][5]<<" |- "<<board[0][6]<<" -| "<<board[0][7]<<" |\n"
" |// //| |// //| |// //| |// //| |\n"
" |//////| |//////| |//////| |//////| |\n"
" ---------------------------------------------------------\n"
" A B C D E F G H \n";
}
int main()
{pawn p("a2",'w');
pawn p2("b2", 'w');
pawn p3 ("c2", 'w');
pawn p4 ("d2", 'w');
int index;
vector <pawn> pwn;
string move;
pwn.push_back(p);
pwn.push_back(p2);
pwn.push_back(p3);
pwn.push_back(p4);
string board[8][8]=
{"//", " ", "//", " ", "//", " ", "//", " ",
" ", "//", " ", "//", " ", "//", " ", "//",
"//", " ", "//", " ", "//", " ", "//", " ",
" ", "//", " ", "//", " ", "//", " ", "//",
"//", " ", "//", " ", "//", " ", "//", " ",
" ", "//", " ", "//", " ", "//", " ", "//",
"//", " ", "//", " ", "//", " ", "//", " ",
" ", "//", " ", "//", " ", "//", " ", "//"
};
for (int i=0; i<pwn.size(); i++) pwn[i].setOnBoard(board);
displayBoard(board);
cout<<endl<<endl<<"Your MOVE?"<<endl;
cin>>move; //Unosi se u formatu kao na primer a2a3
string from=move.substr(0,2);
index=whatPiece(from,pwn);
string to=move.substr(2,2);
pwn[index].move(to, board);
system("cls");
displayBoard(board);
cin>>move;
from=move.substr(0,2);
index=whatPiece(from,pwn);
to=move.substr(2,2);
pwn[index].move(to, board);
system("cls");
displayBoard(board);
return 0;
}
Znam da ne treba da koristim system funkciju ali ne volim kako se prikazuje bez nje.
Takodje, ovakva implementacija moze da prodje ako je za moje sopstvene potrebe.
Prava implementacija podrazumeva da programer koji koristi klasu ima slobodu da predstavi tablu i figure prema svojoj zamisli. To cu da radim kada zavrsim ovu verziju.