#Help with virtual chessboard file input/output

134 messages · Page 1 of 1 (latest)

still bobcat
#

seeking help with accurate file/input output for a .txt file representation of a chess game

glacial spruceBOT
#

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.

still bobcat
#

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);          ```
analog umbra
#

Alright if you can tell me when something goes wrong vs how it's supposed to look

still bobcat
#

unfortunately this project is due in 3.5 hours, so i don't think i'll get it done in time

analog umbra
#

It'll be a big help

still bobcat
#

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

analog umbra
#

So your columns are shifting but somehow e2 works blink1

#

Is e column the only one that works?

still bobcat
#

i think so

#

nope, d2 works too

#

only for white though

analog umbra
#

Just a thought

#

Is H_input a string or char?

#

As in the type

still bobcat
#

char

analog umbra
#

Try changing letter to char

still bobcat
#

aw man i really thought i had it for a sec

analog umbra
#

I think your main issue is that your board representation is super funky

#

Making you do some weird ways to manipulate it

still bobcat
#

yeah haha that's fair

analog umbra
#

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?

still bobcat
#

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

analog umbra
#

Okay I think I can't really help anymore

still bobcat
#

yeah you know that's really fair

analog umbra
#

Try compiling your code with -Wall -Wextra

#

And use a debugger

#

Vscode should be able to interface with it

analog umbra
still bobcat
#

that's fair

analog umbra
#

Well good luck

#

Hope yoy get your assignment in on time

still bobcat
#

i appreciate you spending the time trying to help me

mighty bolt
#

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

still bobcat
#

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

mighty bolt
#

that skips 4 bytes iirc

#

not 4 characters

still bobcat
#

oh?

#

how would i move forward on a fixed scale?

mighty bolt
#

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);```
glacial spruceBOT
#

@mighty bolt

It looks like you may have code formatting errors in your message

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

Markup

```cpp
int main() {}
```

Result
int main() {}
mighty bolt
#
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?

still bobcat
#

tbh not really

mighty bolt
#

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)

still bobcat
#
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);     ```
mighty bolt
#

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

still bobcat
#

this is what i was trying but this still gives me irregular results

mighty bolt
#

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

last geyser
#

@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).

mighty bolt
#

atlas this is part of a school project right

#

do they specify if you're allowed to use other projects

still bobcat
#

yeah, i can't do that

mighty bolt
#

dang

last geyser
#

Ah. I see. Are you sure your piece movement logic is correct? @still bobcat

still bobcat
#

yeah, i'm pretty sure, but the visuals aren't solidly connected to the movement logic

mighty bolt
#

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)

still bobcat
#

it's a little odd and janky but it's what i have for having worked on this for the past 15 hours straight

mighty bolt
#

so its 6 lines to get to rank 2

#

7 sorry

last geyser
still bobcat
#

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

last geyser
#

Also, your row and column is +1 at least, maybe even +2 because you have an extra label row/column

mighty bolt
#
int rowInFile = 1 + (8 - V_input);  // 1 = skip header row
for (int j = 0; j < rowInFile; j++) {
    getline(file, line);
}
still bobcat
#

sorry for the .txt file

#

i'm not a professional, evidently

last geyser
#

i mean, i can't really validate that for correction..

#

If you can match depth 6-7 results for most positions, your move-generation is correct.

mighty bolt
#

oh dude

still bobcat
#

imma be honest, i got no clue what any of that is

mighty bolt
#

(positionH - 95)

still bobcat
#

this is just a pass-and-play chess game

mighty bolt
#

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

still bobcat
#

okay i will

mighty bolt
#
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);
glacial spruceBOT
#

@mighty bolt

It looks like you may have code formatting errors in your message

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

Markup

```cpp
int main() {}
```

Result
int main() {}
mighty bolt
#

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

still bobcat
#

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);

mighty bolt
#

ah sorry

still bobcat
#

all good, i just don't know what it means

mighty bolt
#

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));

still bobcat
#

ah, now it's worse

#

it's not in the right row anymore either now

mighty bolt
#

wat

still bobcat
#

idk but i do have to go and i'm so sorry and so thankful for your guys' help

mighty bolt
#

alright man well atleast you have something to hand in