In this, gameMTree generates a score directly if depth reaches 0 or an endstate is reached. Else if it's X's turn then it finds the maximum possible score from future possible moves and stores it. If it is O's turn then it finds ,minimum of the possible score from future moves. The score of future moves is again in turn decided by gameMtree, in each turn, it reduces the depth by 1 and calls the function once again.
gameMtree :: Int-> Player -> Int-> Playgrid-> (Playgrid,Int)--can be easily edited to add playerturn ton the tuple if required by future functions gameMtree size p depth game | (not (isover size game)) && (depth > 0) = if (p == X) then (game, maxlist $ map getsecond $ map (gameMtree size (changeturn p) (depth-1)) [g'| g' <- possiblemoves size game p ]) else (game, minlist $ map getsecond $ map (gameMtree size (changeturn p) (depth-1)) [g'| g' <- possiblemoves size game p ]) | (isover size game) || (depth == 0) = (game, scoreFGrid size game)
--move selector automoveX :: Playgrid -> Int -> Int -> Playgrid automoveX g size depth = getfirst $ getmaxpair $ map (gameMtree size O (depth)) [g'| g' <- possiblemoves size g X ]
In this, gameMTree generates a score directly if depth reaches 0 or an endstate is reached. Else if it's X's turn then it finds the maximum possible score from future possible moves and stores it. If it is O's turn then it finds ,minimum of the possible score from future moves. The score of future moves is again in turn decided by gameMtree, in each turn, it reduces the depth by 1 and calls the function once again.
gameMtree :: Int-> Player -> Int-> Playgrid-> (Playgrid,Int)--can be easily edited to add playerturn ton the tuple if required by future functions gameMtree size p depth game | (not (isover size game)) && (depth > 0) = if (p == X) then (game, maxlist $ map getsecond $ map (gameMtree size (changeturn p) (depth-1)) [g'| g' <- possiblemoves size game p ]) else (game, minlist $ map getsecond $ map (gameMtree size (changeturn p) (depth-1)) [g'| g' <- possiblemoves size game p ]) | (isover size game) || (depth == 0) = (game, scoreFGrid size game)--move selector automoveX :: Playgrid -> Int -> Int -> Playgrid automoveX g size depth = getfirst $ getmaxpair $ map (gameMtree size O (depth)) [g'| g' <- possiblemoves size g X ]