ChessBoard game

Hi,I'm trying to create a chess board program using pointer, object and class. This is my header file


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef BOARD_H
#define BOARD_H

class Board;
#include "Piece.h"
#include <iostream>
using namespace std;

#define MAXROWS 8
#define MAXCOLS 8

typedef Piece* PiecePtr;

class Board
{
public:
Board();
Board (const Board& other);
~Board();

bool move (int fromX, int fromY, int toX, int toY);
bool place (const PiecePtr& p, int x, int y);
bool remove (int x, int y);

void write (ostream& out) const;

Board& operator= (const Board& other);

private:
PiecePtr grid[MAXROWS][MAXCOLS];

void initBoard();
void clearBoard();
void copyBoard(const Board& other);
bool canMove (int fromX, int fromY, int toX, int toY);
};

ostream& operator<< (ostream& out, const Board& b);

#endif 




I want to draw a board like the link below but I don't know how.The link does not contain any virus, it's because I can't upload any picture in here so I have to convert it to html. Please help me, I really appreciate your help.

http://www.htmlpublish.com/convert-pdf-to-html/success.aspx?zip=DocStorage/3d537d7ca44b4597ae3f13c9d9080e44/Screen%20Shot%202015-04-12%20at%201.40.15%20AM.zip&app=pdf2word
Last edited on
first of please edit your post and use the code-tag
http://www.cplusplus.com/articles/jEywvCM9/

but I don't know how

What did you try?
Give us some code so we can help you to find the solution :)
Hi Gamer2015, I edited my post already. I believe my code does not work at all and they are just garbage because I don't really know how to do this. However, I will post my code here for you to see how bad I am.

1
2
3
#include "Board.h"
#include <iostream>
using namespace std;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Board::Board()
{
    MAXROWS=8;
    MAXCOLS=8;
 
/****
 Board initial;                                                                                                         

initial.initBoard();                                                                                                                                                             
***/
}

bool Board::move (int fromX, int fromY, int toX, int toY)
{
    Board Move;
    bool moved;
    moved= Move.canMove(fromX,fromY,toX,toY);
    return moved;
};

bool Board::place (const PiecePtr& p, int x, int y)
{
    bool placed=false;
    return placed;
};

bool Board::remove (int x, int y)
{
    bool removed;

    return removed;
};

void Board::write(ostream& out) const;
{
    out<<grid[MAXROWS][MAXCOLS]<<endl;
};


int main()
{
    Board game;
    cout<<"Welcome to the game!"<<endl;
    return;
}


void initBoard()
{
    Board game;
    int x;
    int y;

    for (x=0;x<MAXROWS;x++)
    {
        for (y=0;y<MAXCOLS;y++)
        {
            grid[i][j]=NULL;
            game.write(out);
        }
    }
    return;
}
bool canMove(int fromX, int fromY, int toX, int toY)
{
    bool canmove=false;
    PiecePtr end=grid[toX][toY];
    PiecePtr start=grid[fromX][fromY];
    if (start!=NULL)
    {
        start=endl;
        canmove=true;
    }
    return canmove;
}



Here is the specification:

The Board is implemented as an 2D array of pointers to Piece objects. The constants MAXROWS and MAXCOLS are both currently set to 8, the normal size of a chess board size. Using named constant makes it easy to change the board size and recompile. The board must be initialized to be empty, i.e. the 64 pointers must all start as NULL. Since the method of implementation uses dynamic memory a destructor is required. Constructors and destructors can call other functions so observe the first two private member functions. The purpose of the public member functions of place and remove should be obvious ā€“ adding or removing a Piece object at given x, y coordinates. The public member function
move allows the caller to move a piece at a given position to a new position.

Note that all three of these functions return a Boolean value that indicates the success of the operation: for
remove if there is no Piece at the given location, for place if there is already a Piece at the given location and for move either one of these issues. A Boolean is returned since the client code should inform the user of any problems and NOT
an object member function. The move function should use the private member function canMove to determine if the requested move is possible.

The public member function write will print the board to a given output stream, as shown in the link below. A ā€˜Pā€™ symbol is printed within squares where a piece exists, otherwise the squares should appear empty. Notice that the top and left borders of thegrid are annotated with column and row indices, respectively.

http://www.htmlpublish.com/convert-pdf-to-html/success.aspx?zip=DocStorage/3d537d7ca44b4597ae3f13c9d9080e44/Screen%20Shot%202015-04-12%20at%201.40.15%20AM.zip&app=pdf2word
Last edited on
Here is the specification:

okey, good to know ;)

code wrote:
So... Did you write all Methods of Board?

And where exactly is your Problem?
What do you think does not work?

I believe my code does not work at all and they are just garbage because I don't really know how to do this
"you believe"... have you tested it?
What is going wrong?

Note that these functions do nothing:
1
2
3
4
5
6
7
8
9
10
11
12
bool Board::place (const PiecePtr& p, int x, int y)
{
    bool placed=false;
    return placed;
};

bool Board::remove (int x, int y)
{
    bool removed;

    return removed;
};
Last edited on
Topic archived. No new replies allowed.