So I wrote a hexapawn game and I’m trying to make a function that returns True if the board is in a winning state, it looks like this at the moment:
checkWin :: BoardState -> Bool
checkWin b1@(blackPieces,whitePieces,turn,size,win)
|(length blackPieces) == 0 = True
|(length whitePieces) == 0 = True
|length (generateMoves b1) == 0 = True
|otherwise = False
So this works if there are no black or white pieces left or if no one can make a move but it doesn’t work if an opposing pawn reaches the end of the board(another way to win in hexapawn). The variables blackPieces and whitePieces are list of coordinates ie [(1,1),(2,1),(3,1)] of where those pawns are on the board of size n (turn is true if its whites turn)
I was tempted to add these conditions to the method but the compiler didn’t like it.
|(_,1) `elem` whitePieces = True
|(_,size) `elem` blackPieces = True
Is there any other way to say “Are there any tuples in whitePieces who’s second element is a 1(i.e reached the other side of the board).”
Thanks in advance for your helpful comments.
So we want a function that receives a list of something
[a], a predicate over somethings(a->Bool)and returns aBool. A quick check on Hoogle and we get backSo
becomes
or (as eternalmatt reminds me)
and so on
By the way, the best way to check is a list is empty is via pattern matching or the
nullfunction. The length == 0 method will walk through all of the linked list and might even enter an infinite loop if the list is infinite.