CS代考 COMP1100)),

Module : AI
Description : AIs for Fanorona
Copyright : (c) 2022 ANU Yuan : AllRightsReserved
module AI where

Copyright By PowCoder代写 加微信 powcoder

import Fanorona
import GHC.IO.Encoding (BufferCodec(getState))
import Control.Arrow (ArrowChoice(left))
import Data.List

— | Type of AI functions you can choose to write.
data AIFunc
= NoLookahead (GameState -> Move)
— ^ Simple AIs that do not need lookahead.
| WithLookahead (GameState -> Int -> Move)
— ^ AIs that want to look ahead. The assignment framework will
— call the function over and over with increasing integer
— arguments @1, 2, 3, …@ until your AI’s time limit is up.

— | The table of all AIs that your assignment provides. The AI named
— “default” in this table is the one your tutor will dedicate most of
— his or her attention to marking.
ais :: [(String, AIFunc)]
ais = [ (“NoLookahead”, NoLookahead (firstLegalMove COMP1100)),
(“default”, WithLookahead (myAI COMP1100))

— | A very simple AI, which passes whenever it can, and if not,
— picks the first move returned by the ‘legalMoves’ function.
— By default, this function is called on COMP1100 rules, and so
— will never pass.
— AIs can rely on the ‘legalMoves’ list being
— non-empty; if there were no legal moves, the framework would have
— ended the game.
firstLegalMove :: Course -> GameState -> Move
firstLegalMove course state = case applyMove course Pass state of
Nothing -> head (legalMoves state)

— | heuristic function for
heuristic :: GameState -> Player -> Int
heuristic state player = case player of
Player1 -> fst (countPieces state)
Player2 -> snd (countPieces state)

myAI :: GameState -> Int -> Move
myAI t c (x,y) b h ) depth = case t of
GameOver _ -> error “myAI: Game is over.”
Turn p -> legalMoves g !! miniMax g p depth minBound maxBound
targetList = map
(\x -> miniMax (player state) (player state) (applyMove x state) depth)
(legalMoves state)

type Alpha = Int
type Beta = Int
type Depth = Int

miniMax :: GameState -> Player -> Depth -> Alpha -> Beta -> Int
miniMax state player depth minBound maxBound
| depth == 0 || turn state == GameOver _ = case player of

Player1 -> countPB Player1 state + round ((0.5 * fromIntegral (loopSorround Player1 state 1) :: Double) + 0.7 * fromIntegral (loopSorround Player1 state 2))
Player2 -> countPB Player2 state + round ((0.5 * fromIntegral (loopSorround Player2 state 1) :: Double) + 0.7 * fromIntegral (loopSorround Player2 state 2))

| turn state == Turn player =
miniMax (state player (depth- 1) 0) maximum (miniMax’ p d (c+1) (nextStates g))

| turn state == Turn player = maximum
(-100000 : map
miniMax state player (otherPlayer player) (applyMove x state) (depth – 1))
(legalMoves state)
| otherwise = minimum
(100000 : map(\x ->
miniMax state player (otherPlayer player) (applyMove x state) (depth – 1))
(legalMoves state)
miniMax state player _ _ _ = if player == then 100000 else -100000

maximum (miniMax’ p d (c+1) (nextStates g))
minimum (miniMax’ p d (c+1) (nextStates g))
miniMax’:: Player -> Int -> Int -> [GameState] -> [Int]
miniMax’ player d c states = case states of
[] -> []
x : xs -> (heuristic states player) : miniMax’ player d c xs

indexBest :: [Int] -> Int
indexBest l = unMaybe (elemIndex (maximum l) l)

unMaybe :: Maybe a -> a
unMaybe (Just a) = a

— getState :: GameState -> [Move] -> GameState
— getState state [move] = unMaybe (applyMove course move state)

maybeNextStates :: GameState -> [Maybe GameState]
maybeNextStates turn _ _ _ _) = case turn of
GameOver _ -> [Nothing]
_ -> maybeNextStates’ s (legalMoves s)

maybeNextStates’ :: GameState -> [Move] -> [Maybe GameState]
maybeNextStates’ s l = case l of
x : xs -> applyMove COMP1100 x s : maybeNextStates’ s xs

unMaybe’ :: [Maybe a] -> [a]
unMaybe’ l = case l of
Nothing :xs -> unMaybe’ xs
(Just x):xs -> x : unMaybe’ xs

nextStates :: GameState -> [GameState]
nextStates a = unMaybe (maybeNextStates a)

— depthSearch :: [GameState] -> Player -> Int -> Alpha -> Beta -> Int
— — lookahead :: GameState -> Int -> Move
— — lookahead t c (x,y) b h) depth = case t of
— — Turn p ->
— — GameOver _ -> error “lookahead: AI should not be called on a end of a game”

— gametree :: GameState -> Int -> RoseTree [GameState]
— gametree = undefined

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com