Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
C++
#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()
   {             /////   2   6   10  14  18  22  26  30
   string arr[26]={"--1---2---3---4---5---6---7---8---",
                      "-    ####    ####    ####    ####-",
                      "1    ####    ####    ####    ####1",//2
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "2####    ####    ####    ####    2",//5
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "3    ####    ####    ####    ####3",//8
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "4####    ####    ####    ####    4",//11
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "5    ####    ####    ####    ####5",//14
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "6####    ####    ####    ####    6",//17
                      "-####    ####    ####    ####    -",
                      "-    ####    ####    ####    ####-",
                      "7    ####    ####    ####    ####7",//20
                      "-    ####    ####    ####    ####-",
                      "-####    ####    ####    ####    -",
                      "8####    ####    ####    ####    8",//23
                      "-####    ####    ####    ####    -",
                      "--1---2---3---4---5---6---7---8---"
                      };
   ChessBoard b;
   b.setBoard(arr);
   b.DisplayBoard();
   system("pause");
   return 0;
}


ASSOCIATION
C++
#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. !! >
Posted
Updated 18-Jun-13 7:08am
v2
Comments
Maximilien 18-Jun-13 13:48pm    
I suggest to ask question to your lecturer, re-read your notes, review all exercises that you should have done before this one.
I assume you know how chess works, how pieces are placed and how they "move" on the board.
It can be a good idea to write everything down on paper, the board, the initial state, the pieces positions, and then you simulate how the pieces move.
Good luck.
ZurdoDev 18-Jun-13 14:46pm    
Ask your professor. That is why they are there. I am not going to look through all of this code and try to figure it out for you.

First of all, you need to ask your professor. Ask your class mates first. If they get it, the problem is you. If not, you need to ask together for your teacher to move at a pace where his students can follow him. Talk to the school if need be.

Your teacher's design is really not very good. I can see several ways to improve it. However, you have to work with what he asked for. I don't see any easy way to draw a board with pieces on it from the code base you have. The board drawing seems removed from the board itself. Your position class lets any string be a position when really you want two co-ordinates. A string will also be harder to parse. However, you need to create pieces and assign a position on the board to each piece. Then you need to work with the pieces to know what is where and where it can be moved. If you're confused now, when you start to add rules for moving pieces, you will be totally lost. No amount of help from random strangers will get you past a teacher you don't understand, so deal with the root issue. If it's you, buckle down with the teacher and study more, so he also knows you need the help. If it's the teacher, complain if he won't try to improve.
 
Share this answer
 
Comments
Usman Hunjra 19-Jun-13 3:46am    
yes you are right sir.. Actually i'm having problem in association. !!
HOW CAN I ASSOCIATE PIECES WITH THE STRING TYPE BOARD ..?? Thats' the starting point problem.
i wana see the board with the pieces.. !!!!!!!!!
Christian Graus 19-Jun-13 16:18pm    
You draw the board and each square also knows how to draw pieces and which one is on there.
There is no sensibkle way to perform an association because you currently have no data structure representing the chess board. A chess board consists of eight rows with eight fields each. Neither your nor your lecturer's data structures reflect that.

In case of the latter, your lecturer may intentionally have represented the position as a string: in chess the usual notation for a position encodes the column as a character between 'a' and 'h' and the row as a number between 1 and 8. So "e2" represents the field in the 5th column of the second row. In any case, you need a function that translates the "name" of a position into a reference to a location on your chess board.

In case of your code, your string array only serves to print an empty board, but it is totally unsuitable for representing locations on a chess board or pieces associated to those. Rather than thinking of a way to print an entire board, you should have considered the structure of a chess board: it is a two-dimensional 8x8 array of fields. Your first concern should be the definition of that array, not how to print it.

Once you have that, the association is rather simple:
1. for every location in your chess board array ...
1.1 ... initialize it to hold no piece
2. for every piece,...
2.1 ... get the position
2.2 ... translate the position's name into a real location on your chess board
2.3 ... store a pointer to that piece within that location

Once you have that you can consider how best to print this. You can iterate over all rows, and for each row iterate repeatedly over all fields to get the first, second, third,... line of text needed to print that field as a whole. If the field holds no piece, each row would simply be a string of blanks or hashes. If not, each row of that field could hold a part of the name of the piece that it is associated to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900