I am so new in C++ programming.
I am trying to write a code for a chess engine..
here is my class declaration:
//for the board class
struct boardTemp
{ private:
sqrTemp boardSqr[64]; //[63] was inaccurate representation.
int blkPoints, whtPoint;
string role; //white or black
string turn; //while or black
int epSqr; //en passant square
clock_t wtime, btime; //time left to the next control
int wMoveremaining, bMoveremaining;
bool bCastleQ, bCastleK, wCastleQ, wCastleK;
void sqrSetPiece(string a)
{pieceSqr = a;}
void sqrSetColor(string b)
{pieceColorSqr=b;}
void sqrSetSqrNum(int c)
{sqrNum=c;}
string getPiece(){
return pieceSqr;}
string getColor(){
return pieceColorSqr;}
int getSqrNum() {
return sqrNum;}
};
my main file contains this:
boardTemp * mainBoard;
mainBoard = new boardTemp;
int i=1,j=8; // j=rank counter, i= file counter
string pieceMB, colorMB; // piece and the color of the piece.
do{i=1; //loops from rank 8 file1 to file 8 then moves to rank 7 file 1 - to file 8
do{
cout <<"Enter the Piece in square "<< flrnkSqr(i,j)<<endl;
cin >> pieceMB;cout <<endl;
mainBoard->boardSetPiece(flrnkSqr(i,j),pieceMB);
cout << &mainBoard->boardGetPiece(flrnkSqr(i,j));
cout <<"Enter the Piece Color in square "<< flrnkSqr(i,j)<<endl;
mainBoard->boardSetColor(flrnkSqr(i,j),colorMB);
mainBoard->boardSetSqrNum(flrnkSqr(i,j));
i+=1;
} while (i<=8); j-=1;
} while (j>=1);
----
but after i reaches 8, it gives a message saying unhandled exception....access violation reading allocation...
By the way, the classes are declared in a header file.
Thanks.
Start by not using pointers unnecessarily. To create an object of type boardTemp, write boardTemp mainBoard;
Then write some constructors: chess board squares whose color and contents are unset do not exist in real world, why do they exist in your program?
The board itself has a well-known default configuration, so the default constructor of the boardTemp class should already populate, or at least colorize every square.
As for tracking down the error you're seeing, use the debugger, or post a compilable program that reproduces the error so that someone else would run it through a debugger for you.
when you create an array the index extends from 0 to n, where n is 1- the amount you requested. So when you have your loop go through to the maximum increment it rEads past the end of the array. Try setting the while statement so that it loop until i < 8.
@ Cubbi >> the colour does not pertain to the colour of the square, it pertains to the colour of the piece in the square. In abstract chess, squares don't really need to be coloured but the pieces should be. Not really colour but at least something to discriminate the piece, from you own or opponent.
@ pogrady and TheIdeasMan >> there are 64 squares on a chess board, created a board class with an array of 64 (0 -63) sqrTemp boardSqr[63]; this mean that square 1 would be [0] in the array. but I guess you are right, when I changed " sqrTemp boardSqr[63]" to "sqrTemp boardSqr[64];" I was able to provide what piece and what colour. It is just that, it has 1 extra square that what it represents in real world. By the way, this is in preparation to to accommodate FEN notation (if you have heard of it). I am just making sure that it would accept data transfer in that manner.
@ Jikax >> sorry, I am very new to C++ programming or programming for that matter and I am not aware yet of the conventions that are used by professional coders. I just started the code after I watched some videos on youtube about C++ programming.
I meant that the less than equal operator causes the overflow. Realise that the loop ends when the condition becomes false. So making the array bigger is not really a solution.
IF you post your code with code tags as I and others have said, you might get more replies & advice.
there are 64 squares on a chess board, created a board class with an array of 64 (0 -63) sqrTemp boardSqr[63]; this mean that square 1 would be [0] in the array.
sqrTemp boardSqr[63]; does not create an array with 64 elements, addressable by indices 0-63. It creates an array of 63 elements addressable by indices 0-62.
jrfrago wrote:
but I guess you are right, when I changed " sqrTemp boardSqr[63]" to "sqrTemp boardSqr[64];" I was able to provide what piece and what colour. It is just that, it has 1 extra square that what it represents in real world
No. It would then have exactly the right number of elements, but as you didn't change it in the code above, I would suspect that is still your problem.
@cire >> that was my mistake. I was confused with the array[n], I thought it would have n+1 elements from 0 to n. Thanks. That explains why sqrTemp boardSqr[64] worked and why sqrTemp boardSqr[63] gives access violation reading allocation.
@TheIdeasMan>> cannot use 1<8 as it would miss the squares in file 8: 08, 16, 24, 32, 40, 48, 56, 64. I have tried moving the squares from left to right then up (instead of moving left to right then down) and it works till it reaches square 64. The array allocation explains why, I had a wrong understanding of the array element reservation.