Yes You Guys Are Right This is an assignment.. But i'm Really Much Tensed & Frustrated,,
Guys I really need your suggestions to understand better ..
For now I've to draw a chess board && associate pieces of chess.. !!
I Hope There Should Be SomeOne Who Help Me To Understand Better .. !!
Actually I Don't Understand My Lecturer .. !!!
I'm Posting Two Codes .. !!
One That I've now it's just Chess Board && The OtherOne is From my lecturer its' about association .. !!!
CHESS BOARD
#include<string>
#include<iostream>
using namespace::std;
class ChessBoard
{
private:
string MyBoard[26];
public:
void setBoard(string []);
void DisplayBoard();
};
void ChessBoard::setBoard(string arr[])
{
for(int i=0;i<26;i++)
MyBoard[i]=arr[i];
}
void ChessBoard::DisplayBoard()
{
for(int i=0;i<26;i++)
cout<<MyBoard[i]<<endl;;
}
int main()
{ string arr[26]={"--1---2---3---4---5---6---7---8---",
"- #### #### #### ####-",
"1 #### #### #### ####1", "- #### #### #### ####-",
"-#### #### #### #### -",
"2#### #### #### #### 2", "-#### #### #### #### -",
"- #### #### #### ####-",
"3 #### #### #### ####3", "- #### #### #### ####-",
"-#### #### #### #### -",
"4#### #### #### #### 4", "-#### #### #### #### -",
"- #### #### #### ####-",
"5 #### #### #### ####5", "- #### #### #### ####-",
"-#### #### #### #### -",
"6#### #### #### #### 6", "-#### #### #### #### -",
"- #### #### #### ####-",
"7 #### #### #### ####7", "- #### #### #### ####-",
"-#### #### #### #### -",
"8#### #### #### #### 8", "-#### #### #### #### -",
"--1---2---3---4---5---6---7---8---"
};
ChessBoard b;
b.setBoard(arr);
b.DisplayBoard();
system("pause");
return 0;
}
ASSOCIATION
#include <iostream>
#include <string>
using namespace std;
class Piece;
class Position {
Piece *piece;
string name;
public:
Position (string s) {name = s; piece = NULL;}
void put(Piece *p) {piece = p;}
void remove() {piece = NULL;}
string getName() {return name;}
Piece *getPiece() {return piece;}
};
class Piece {
Position *pos;
string name;
public:
Piece (string s) {name = s; pos = NULL;}
void putAt(Position *p) {pos = p; pos -> put(this);}
void removeFrom() {pos->remove(); pos = NULL;}
void print() { cout << name << "is at " << pos->getName() << endl;}
Position *getPosition() {return pos;}
string getName() {return name;}
virtual void move(Position * from, Position * to) = 0;
};
class King : public Piece {
public:
King(string s) : Piece(s) {};
void move(Position *from, Position *to) {removeFrom(); putAt(to);}
};
class Queen : public Piece {
public:
Queen(string s) : Piece(s) {};
void move(Position *from, Position *to) {removeFrom(); putAt(to);}
};
void printBoard(Position *p[], int size) {
int i = 0;
Position *t;
Piece *x;
string pos, piece;
for (i = 0; i < size; i++) {
t = p[i];
pos = t->getName();
x = t->getPiece();
if (x == NULL) piece = "Nothing";
else piece = x->getName();
cout << piece << " is at " << pos << endl;
}
}
int main() {
Position *p[10];
p[0] = new Position("p1");
p[1] = new Position("p2");
p[2] = new Position("p3");
p[3] = new Position("p4");
p[4] = new Position("p5");
p[5] = new Position("p6");
p[6] = new Position("p7");
p[7] = new Position("p8");
p[8] = new Position("p9");
p[9] = new Position("p10");
printBoard(p, 10);
cout << endl;
Piece *pp1, *pp2;
pp1 = new King("Black King");
pp2 = new Queen("White Queen");
pp1->putAt(p[1]);
pp2->putAt(p[3]);
printBoard(p, 10);
cout << endl;
pp1->move(p[1], p[2]);
pp2->move(p[3], p[4]);
printBoard(p, 10);
cout << endl;
return 0;
}
Now I've to associate pieces of chess to this board .. ??
Now I just don't know where do i start .. !!!
< ANY EXPLANATION SUGGESTION HELP WOULD BE APPRECIATED. !! >