#Help with virtual chessboard file input/output
134 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
8 bR bN bB bQ bK bB bN bR 8
7 bp bp bp bp bp bp bp bp 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 wp wp wp wp wp wp wp wp 2
1 wR wN wB wQ wK wB wN wR 1
a b c d e f g h```
if(set[i]->getName() == "Pawn" || set[i]->getName() == "King" || set[i]->getName() == "Knight") {
cout << "Where would you like to move the " << set[i]->getName() << " at " << userInput << "?" << endl;
}
pos = evaluateMoves(set[i], board, set, turn);
for(int j = 0; j < ((8 - V_input) + 3); j++) { // skips rows to row position
getline(file,line);
}
string letter;
int intPutH = (H_input - 96);
file >> letter;
for(int j = 0; j < (intPutH - 2); j++) {
file >> letter;
}
file << " . ";
file.seekg(0); ```
Alright if you can tell me when something goes wrong vs how it's supposed to look
unfortunately this project is due in 3.5 hours, so i don't think i'll get it done in time
It'll be a big help
yeah for sure
so the rows are working fine
but the columns aren't
8 bR bN bB bQ bK bB bN bR 8
7 bp bp bp bp bp bp bp bp 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 . wp wp wp wp wp wp wp 2
1 wR wN wB wQ wK wB wN wR 1
a b c d e f g h```
this is the result after a move from b2.
it's not in the right spot, and it's not even accurate in the place it's set (a2)
8 bR bN bB bQ bK bB bN bR 8
7 bp bp bp bp . bp bp bp 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 . wp wp wp wp wp wp wp 2
1 wR wN wB wQ wK wB wN wR 1
a b c d e f g h```
calling a move for d7 from black results in another close yet inaccurate replacement
e2, however, works fine
So your columns are shifting but somehow e2 works 
Is e column the only one that works?
char
Try changing letter to char
aw man i really thought i had it for a sec
I think your main issue is that your board representation is super funky
Making you do some weird ways to manipulate it
yeah haha that's fair
I'd suggest another way but you're time bound
So we'll have to work with it
What's with the -2 and +3 when you're looping?
the 8 - V_input + 2 (fixed that earlier) is to reverse the orientation of the position
lemme word that better
it makes it so that the vertical values line up with the values of the board, since they're sort of inverse of each other
so a V_input of 7 corresponds to line 4 (since getline ignores the blank 2 line)
and V_input 3 would correspond to line 8
as for the -2, that's just me trying to figure the rows out
Okay I think I can't really help anymore
yeah you know that's really fair
Try compiling your code with -Wall -Wextra
And use a debugger
Vscode should be able to interface with it
Maybe I could've if I had my computer and was able to run the code locally but I'm in school rn
that's fair
i appreciate you spending the time trying to help me
file >> letter is reading tokens seperated by whitespace instead of chars
and your squares use fixed-spacing not whitespaced seperated tokens
but >> skips whitespace so it wont go through the columns properly
why dont u just read the entire line as a string and then change ur column logic based on a fixed-width format like each square = 4 characters
that's what i've been trying to do but it never seems consistent
i'm trying to use file.ignore(4) to iterate forward
treat each row as a fixed-width string and move forward in blocks of chars
so like
lets say:
int baseOffset = 8;
int squareWidth = 4;
file.seekg(lineStart + fromColOffset);```
@mighty bolt
Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks
```cpp
int main() {}
```
int main() {}
int baseOffset = 8;
int squareWidth = 4;
int rowOffset = (8 - V_input);
for(int j = 0; j < rowOffset + 1; j++) {
getline(file, line);
}
std::streampos lineStart = file.tellg();
int fromColOffset = baseOffset + (H_input - 'a') * squareWidth; //
file.seekg(lineStart + fromColOffset);
file << " . ";
file.seekg(0);
make sure you use fstream when you open the file btw cause you can't seekg and << with ifstream
does that make sense?
tbh not really
okay so
we have a fixed-width chessboard
each square is exactly 4 characters wide
we want to seek the exact char position in the file that maches a square, like b2
if we want to move a piece from b2, chess row 2 -> index = 8 - 2 = 6 starting from top as 8
in this case we want the 7th line (arrays start from 0)
char letter;
file.seekg (8, std::ios::cur);
for(char j = 'a'; j != H_input; j++) {
file.seekg (4, std::ios::cur);
}
file << ". ";
file.seekg(0); ```
i see somebody typing whos been going on about a chess engine for a while
he may be able to explain / add onto what i've said better than myself
this is what i was trying but this still gives me irregular results
yeah so the layout of the file and position math won't be matching up
if its irregular
i'll wait to see what this guy says
@still bobcat Another option, if you're interested, is using the backend of say my Chess Engine. If you want, I can get on call with you and help you quickly set this up.
That way, you can just handle the display of the board (which is as simple as reading from an array and printing stuff) and all the moving piece logic will be handled by the chess engine's backend (which is super fast if you care about that, but more importantly, battle-tested and correct).
atlas this is part of a school project right
do they specify if you're allowed to use other projects
yeah, i can't do that
dang
Ah. I see. Are you sure your piece movement logic is correct? @still bobcat
yeah, i'm pretty sure, but the visuals aren't solidly connected to the movement logic
a b c d e f g h ← line 0
8 bR bN bB bQ bK bB bN bR 8 ← line 1 (rank 8)
7 bp bp bp bp bp bp bp bp 7 ← line 2 (rank 7)
...
1 wR wN wB wQ wK wB wN wR 1 ← line 8 (rank 1)
a b c d e f g h ← line 9
if we want to modify rank 2 in this case, you should skip 1 (header) and + 6 (ranks 8 to 3)
it's a little odd and janky but it's what i have for having worked on this for the past 15 hours straight
I ask because it's one of the hardest thing to get right in chess.
i can send what i've got
i haven't worked in check or mate or any of that but i don't have time for that
Also, your row and column is +1 at least, maybe even +2 because you have an extra label row/column
int rowInFile = 1 + (8 - V_input); // 1 = skip header row
for (int j = 0; j < rowInFile; j++) {
getline(file, line);
}
i mean, i can't really validate that for correction..
Typically, you'd perform this test https://www.chessprogramming.org/Perft_Results
If you can match depth 6-7 results for most positions, your move-generation is correct.
oh dude
imma be honest, i got no clue what any of that is
(positionH - 95)
this is just a pass-and-play chess game
sort that
int colIndex = positionH - 'a' + 1;
that maps 'a'..'h' and 1..8 cleanly which would affect board.getOccupied() and board.flipOccupied()
@still bobcat do you get why -95 is an issue
board.getOccupied(positionV, positionH - 95);
with 'b' - 95 = 3 you check column 3 but b is column 2
you're shifting everything 1 column to the right
every call thats (positionH - 95) should be:
(positionH - 'a' + 1)
give that a go
okay i will
board.flipOccupied(piece->getPositionV(), (piece->getPositionH() - 96), true);
should be:
if(board.getOccupied(positionV + i, col))
board.flipOccupied(piece->getPositionV(), piece->getPositionH() - 'a' + 1, true);
@mighty bolt
Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks
```cpp
int main() {}
```
int main() {}
so:
positionH - 95 should be position H - 'a' + 1
positionH - 96 should be positionH - 'a' + 1
piece->getPositionH() - 95 should be piece->getPositionH() - 'a' + 1
piece->getPositionH() - 96 should be piece->getPositionH() - 'a' + 1
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: 421 | file.seekg(lineStart + fromColOffset);
note: candidate 2: 'operator+(std::streamoff {aka long long int}, int)' (built-in) 421 | file.seekg(lineStart + fromColOffset);
ah sorry
all good, i just don't know what it means
linestart is a std::streampos and fromColOffSet is an int so theres two candidates for + and it doesnt know what to use
change file.seekg(lineStart + fromColOffset);
to: file.seekg(lineStart + static_cast<std::streamoff>(fromColOffset));
wat
idk but i do have to go and i'm so sorry and so thankful for your guys' help
alright man well atleast you have something to hand in