Valhalla Legends Forums Archive | C/C++ Programming | What NOT to do

AuthorMessageTime
iago
I've been marking assignments, wherein a chessboard is read in from a file that looks something like:
rbnqknbrppppppppxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxPPPPPPPPRBNQKNBR
And it has to process the board. Problem is, a lot of people change the boards they download so they look like this:
rbnqknbr
ppppppp
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
PPPPPPPP
RBNQKNBR
And they assume there will be endlines there or spaces between the characters.

On most assignments, don't make assumptions about the format of the input file, and make sure it works with given input files. I can't believe how many people don't :(
February 19, 2004, 6:51 PM
Grok
The bishops go in C1, F1, C8, F8. Swap them with the Knights.
February 19, 2004, 7:07 PM
iago
[quote author=Grok link=board=30;threadid=5368;start=0#msg45064 date=1077217652]
The bishops go in C1, F1, C8, F8. Swap them with the Knights.
[/quote]

I always forget chess boards, and I had a feeling I was doing it wrong. But that's not the point! :P

I also did the king/queen backwards, probably
February 19, 2004, 8:32 PM
Grok
[quote author=iago link=board=30;threadid=5368;start=0#msg45071 date=1077222755]
[quote author=Grok link=board=30;threadid=5368;start=0#msg45064 date=1077217652]
The bishops go in C1, F1, C8, F8. Swap them with the Knights.
[/quote]

I always forget chess boards, and I had a feeling I was doing it wrong. But that's not the point! :P

I also did the king/queen backwards, probably
[/quote]

Looks fine, assuming white is on bottom edge. Queen always goes on her own color. Bottom-right corner of chessboard (as viewed by either player) is a white square.
February 19, 2004, 10:11 PM
iago
[quote author=Grok link=board=30;threadid=5368;start=0#msg45085 date=1077228677]
[quote author=iago link=board=30;threadid=5368;start=0#msg45071 date=1077222755]
[quote author=Grok link=board=30;threadid=5368;start=0#msg45064 date=1077217652]
The bishops go in C1, F1, C8, F8. Swap them with the Knights.
[/quote]

I always forget chess boards, and I had a feeling I was doing it wrong. But that's not the point! :P

I also did the king/queen backwards, probably
[/quote]

Looks fine, assuming white is on bottom edge. Queen always goes on her own color. Bottom-right corner of chessboard (as viewed by either player) is a white square.
[/quote]

Well, I can't quite see the colors from here. I don't even remember which is white :)
February 19, 2004, 10:49 PM
Grok
Chess is racist anyway. White always gets the first move. ;p
February 20, 2004, 12:26 AM
iago
it makes sense, white is better! Wait, that's not nice :(

Anyway, here's a sample of a piece of code (no, the forum didn't eat the indents, there weren't any to begin with)
[code]int checkBlack() {
int tmp[8][8];
int returnValue = -1;

for (i=0;i<8;i++)
for (k=0;k<8;k++) {
tmp[7-i][7-k] = board[i][k];
if (tmp[7-i][7-k]>96) tmp[7-i][7-k] -= 32;
else if (tmp[7-i][7-k]<97 && tmp[7-i][7-k] != 'X') tmp[7-i][7-k] += 32;
}
[/code]

That's AFTER he got 0 for last assignment with comments like, "indent, don't use constants, comment your code"
February 20, 2004, 12:32 AM
Adron
[quote author=iago link=board=30;threadid=5368;start=0#msg45108 date=1077237123]
it makes sense, white is better! Wait, that's not nice :(

Anyway, here's a sample of a piece of code (no, the forum didn't eat the indents, there weren't any to begin with)
[code]int checkBlack() {
int tmp[8][8];
int returnValue = -1;

for (i=0;i<8;i++)
for (k=0;k<8;k++) {
tmp[7-i][7-k] = board[i][k];
if (tmp[7-i][7-k]>96) tmp[7-i][7-k] -= 32;
else if (tmp[7-i][7-k]<97 && tmp[7-i][7-k] != 'X') tmp[7-i][7-k] += 32;
}
[/code]

That's AFTER he got 0 for last assignment with comments like, "indent, don't use constants, comment your code"
[/quote]

Where do you want him to use non-constant expressions? Making the size of a chessboard or the ascii difference between lowercase and uppercase variable doesn't seem all that necessary...
February 20, 2004, 7:45 PM
iago
[quote author=Adron link=board=30;threadid=5368;start=0#msg45188 date=1077306356]
[quote author=iago link=board=30;threadid=5368;start=0#msg45108 date=1077237123]
it makes sense, white is better! Wait, that's not nice :(

Anyway, here's a sample of a piece of code (no, the forum didn't eat the indents, there weren't any to begin with)
[code]int checkBlack() {
int tmp[8][8];
int returnValue = -1;

for (i=0;i<8;i++)
for (k=0;k<8;k++) {
tmp[7-i][7-k] = board[i][k];
if (tmp[7-i][7-k]>96) tmp[7-i][7-k] -= 32;
else if (tmp[7-i][7-k]<97 && tmp[7-i][7-k] != 'X') tmp[7-i][7-k] += 32;
}
[/code]

That's AFTER he got 0 for last assignment with comments like, "indent, don't use constants, comment your code"
[/quote]

Where do you want him to use non-constant expressions? Making the size of a chessboard or the ascii difference between lowercase and uppercase variable doesn't seem all that necessary...

[/quote]

Sorry, I typed that fast; I meant USE constants. Don't use magic numbers. Like, 96 and 97 for instance when 'a' would have worked much better. And the size of a chessboard should be #define 8, because that's how they were taught to do things in class.
February 20, 2004, 8:07 PM
Adron
Ah. Somewhat a matter of opinions - his code seems more readable that way than with constants and comments:

[code]
// Width of chessboard
#define CHESSBOARD_WIDTH 8
// Height of chessboard
#define CHESSBOARD_HEIGHT 8
// Start of range for black pieces
#define CHESSBOARD_MINBLACK 'a'
// Start of range for white pieces
#define CHESSBOARD_MINWHITE 'A'
// End of range for white pieces
#define CHESSBOARD_MAXWHITE (CHESSBOARD_MINBLACK - 1)
// Difference used to convert pieces between black and white
#define CHESSBOARD_DIFFBLACKWHITE (CHESSBOARD_MINBLACK - CHESSBOARD_MINWHITE)
// Some magic value, used slot?
#define CHESSBOARD_MAGICVALUE 'X'
// The default error return value from the checkBlack function
#define CHECKBLACK_RETURN_ERROR -1

int checkBlack() {
int tmp[CHESSBOARD_WIDTH][CHESSBOARD_HEIGHT];
int returnValue = CHECKBLACK_RETURN_ERROR;

// Copy the board matrix to a temporary matrix
// At the same time:
// Swap the colors
// Invert the board
for (i=0;i<CHESSBOARD_WIDTH;i++)
for (k=0;k<CHESSBOARD_HEIGHT;k++) {
tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k] = board[i][k];
if (tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k]>CHESSBOARD_MAXWHITE) tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k] -= CHESSBOARD_DIFFBLACKWHITE;
else if (tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k]<CHESSBOARD_MINBLACK && tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k] != CHESSBOARD_MAGICVALUE) tmp[CHESSBOARD_WIDTH-1-i][CHESSBOARD_HEIGHT-1-k] += CHESSBOARD_DIFFBLACKWHITE;
}
[/code]
February 20, 2004, 8:24 PM
iago
Yeah, his algorithm is bad, too. He has a lot of problems.
February 21, 2004, 1:52 AM
MrRaza
For a school programming projects, my friend created a four player chess game with either 1, 2, 3 computers and the rest human players, he added a GUI aswell.
February 24, 2004, 11:18 PM
Eli_1
oh yea? well my daddy is cooler than your daddy... ;)
February 25, 2004, 3:25 AM
K
[quote author=Eli_1 link=board=30;threadid=5368;start=0#msg45920 date=1077679521]
oh yea? well my daddy is cooler than your daddy... ;)
[/quote]

No, it's TCC!

[quote]
Two Clown Complex. It is derived thusly: imagine that you will have a clown at your birthday party. Should this joyous event be overheard by a person in the throes of TCC, they must declare that they had not one but two clowns, in addition to a bouncy castle.[/quote]- (penny arcade)
February 25, 2004, 3:31 AM

Search