TU-CSCI2322-FL22 / solver-connect-three

solver-connect-three created by GitHub Classroom
0 stars 0 forks source link

Indexing is bad, and indexing every time through a list is even worse, and this doesn't work #16

Closed sfogarty closed 1 year ago

sfogarty commented 1 year ago

https://github.com/TU-CSCI2322-FL22/solver-connect-three/blob/3b398642bada87d610c043c993389cc52d8ec5ec/Main.hs#L79

 newBoard    = [if loc == numOfTile then Just player else piece | (loc, piece) <- zip [0..] board ] 

Note this is not safe! What if there's already a piece there?

 newBoard    = [squareFor loc piece | (loc, piece) <- zip [0..] board ]
          where squareFor loc piece  
                    | loc == numOfTile && isNothing piece = Just player 
                    | loc /= numOfTile = piece  
                    | otherwise = error -- or Nothing

Or recurse through until numOfTile hits zero, then check

updateBoard 0 p (Nothing:rest) = (Just p:rest)
updateBoard 0 p _ = error "AAAAAAH" 
updateBoard n p (h:rest) = h:(updateBoard (n-1) p rest)