Hello, I'm trying to make a FEN parser, and in tha...
# getting-started
o
Hello, I'm trying to make a FEN parser, and in that I have to parse something like this:- "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" What I do now is that I just split that string using split(' ') into a List<String> and on the first string of the list, any value can have 1,especially on the rnbqkbnr/ppp... and their uppercase counterparts, and when its 1 I have to just ignore it(I'm using a when statement). My question is how do I ignore a case in a when statement since break and continue aren't applicable(Yup, I know it's a noobish question).
p
Is it possible in your case to update
when
value to upper/lower case?
Copy code
when(x.toLowerCase()) {
    "abc" -> // ...
}
o
no, I just put one for 'q' and one for 'Q', so that when its 'q' it updates the whitePieces and allQueens. Im using bitboards
its not case sensitivity, I want that when the when statement finds a '1' it just skips that and continues to the next line.
On the other side if I dont put '1' then when it does find a '1' in the string it will go to the else part and give a error. Which I dont want.
h
Sounds like you need if+else+when or a nested when, hard to say w/o seeing the code
p
Ah, sorry, I understood incorrectly. Can you provide a code what you want to achieve, so it will be easier to help to improve it?
o
sure
Copy code
while((j<=64) && (i<= initialState.length)) {
        var letter = blocks[0].get(i)
        i++
        file = 1 + ((j-1)%8)
        rank = 8 - ((j-1)/8)
        var pieceIndex = getPieceIndex(rank,file)
        when(letter) {
            'p' -> {   // Pawn
                fenData.boards.allPawns or getBitmask(pieceIndex)
                fenData.boards.whitePieces or getBitmask(pieceIndex)
            }

            'P' -> {
                fenData.boards.allPawns or getBitmask(pieceIndex)
                fenData.boards.blackPieces or getBitmask(pieceIndex)
            }

            'r' -> {   // Rook
                fenData.boards.whitePieces or getBitmask(pieceIndex)
                fenData.boards.allRooks or getBitmask(pieceIndex)
            }

            'R' -> {
                fenData.boards.blackPieces or getBitmask(pieceIndex)
                fenData.boards.allRooks or getBitmask(pieceIndex)
            }

            'n' -> {   // Knight
                fenData.boards.whitePieces or getBitmask(pieceIndex)
                fenData.boards.allKnights or getBitmask(pieceIndex)
            }

            'N' -> {
                fenData.boards.blackPieces or getBitmask(pieceIndex)
                fenData.boards.allKnights or getBitmask(pieceIndex)
            }

            'b' -> {   // Bishop
                fenData.boards.whitePieces or getBitmask(pieceIndex)
                fenData.boards.allBishops or getBitmask(pieceIndex)
            }

            'B' -> {
                fenData.boards.blackPieces or getBitmask(pieceIndex)
                fenData.boards.allBishops or getBitmask(pieceIndex)
            }

            'q' -> {   // Queen
                fenData.boards.whitePieces or getBitmask(pieceIndex)
                fenData.boards.allQueens or getBitmask(pieceIndex)
            }

            'Q' -> {
                fenData.boards.allQueens or getBitmask(pieceIndex)
                fenData.boards.blackPieces or getBitmask(pieceIndex)
            }

            'k' -> {   // Kings
                fenData.boards.allKings or getBitmask(pieceIndex)
                fenData.boards.whitePieces or getBitmask(pieceIndex)
            }

            'K' -> {
                fenData.boards.allKings or getBitmask(pieceIndex)
                fenData.boards.blackPieces or getBitmask(pieceIndex)
            }

            // -----------------------------------------------------

            '/' -> j--
            '2' -> j++
            '3' -> j+=2
            '4' -> j+=3
            '5' -> j+=4
            '6' -> j+=5
            '7' -> j+=6
            '8' -> j+=7
            else -> return -1
        }
    }
there should be a '1' in there which I havent written
p
First, you can just write
Copy code
when(letter) {
    'a', 'A' -> // ...
    'b', 'B' -> // ...
}
this is the fen parser from my engine
o
Yeah but your code is quite different from mine, though its not bad. I just want a suggestion on what I want to do.
I'm sorry if I'm being demanding, but a suggestion could help very much
r
It is not that different, the first for does what you are trying to make.
a
You just want
'1' -> Unit
?