diff --git a/AI_v1 b/AI_v1 new file mode 100644 index 0000000..2528ca4 --- /dev/null +++ b/AI_v1 @@ -0,0 +1,164 @@ + +import Data.List +printgame state i j n =do + if(i Int -> Int -> Int -> Int -> IO Int +minimax state i n bestscore besti= do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if (ibestscore) then do + minimax state (i+1) n score i + else minimax state (i+1) n bestscore besti + else return besti + + +minplay ::[[Int]] -> Int -> Int -> Int -> Int -> IO Int +minplay state i n bestscore besti = do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if(i==num) then do + res <- evaluate state n 0 0 + return res + else do + if (i Int -> Int -> Int -> Int -> IO Int +maxplay state i n bestscore besti = do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if(i==num) then do + res <- evaluate state n 0 0 + return res + else do + if (ibestscore) then do + maxplay state (i+1) n score besti + else maxplay state (i+1) n bestscore i + else return bestscore +change::[[Int]]->Int->Int->Int->[[Int]] +change state i j val=(take i state)++[(take j (state!!i))++[val]++(drop (j+1) (state!!i))]++(drop (i+1) state) + +getmovei :: [[Int]] -> Int -> Int -> Int -> [Int] -> IO [Int] +getmovei state n i j list=do + if(i==n)then return list + else do + if(j==n-1) then do + if(state!!i!!j==8) then getmovei state n (i+1) 0 (list ++ [i]) + else getmovei state n (i+1) 0 (list) + else do + if(state!!i!!j==8) then getmovei state n (i) (j+1) (list ++ [i]) + else getmovei state n (i) (j+1) (list) + +getmovej :: [[Int]] -> Int -> Int -> Int -> [Int] -> IO [Int] +getmovej state n i j list=do + if(i==n)then return list + else do + if(j==n-1) then do + if(state!!i!!j==8) then getmovej state n (i+1) 0 (list ++ [j]) + else getmovej state n (i+1) 0 (list) + else do + if(state!!i!!j==8) then getmovej state n (i) (j+1) (list ++ [j]) + else getmovej state n (i) (j+1) (list) + +numberofx :: [Int] -> Int -> Int -> Int -> IO Int +numberofx row i n nx=do + if(i Int -> Int -> Int -> IO Int +numberof0 row i n nx=do + if(i Int -> Int ->Int -> IO Int +evaluate state n i current = do + if (i==n) then do + let l1=diag1list state (n-1) n + let l2=diag2list state (n-1) n + ox <- numberofx l1 0 n 0 + o0 <- numberof0 l1 0 n 0 + px <- numberofx l2 0 n 0 + p0 <- numberof0 l2 0 n 0 + return (current + (10^o0) + (10^p0) - (10^px) - (10^ox)) + else do + nx <- numberofx (state!!i) 0 n 0 + n0 <- numberof0 (state!!i) 0 n 0 + let clone = transpose state + mx <- numberofx (clone!!i) 0 n 0 + m0 <- numberof0 (clone!!i) 0 n 0 + let result = current + (10^n0) - (10^nx) + (10^m0) - (10^mx) + evaluate state n (i+1) result + + + +input state n= do + str2 <- getLine + let i=read str2::Int + str3 <- getLine + let j=read str3::Int + let clone = change state i j 3000 + print "Com 's turn" + b <- minimax clone 0 n 0 0 + movesi <- getmovei clone n 0 0 [] + movesj <- getmovej clone n 0 0 [] + let clone2 = change clone (movesi!!b) (movesj!!j) 3001 + printgame clone2 0 0 n + input clone2 n + + + + +main = do + print "please enter dimension of the Grid" + str<-getLine + let n = read str::Int + let state = (replicate n) ((replicate n) 8) + printgame state 0 0 n + input state n + print "OVER" + diff --git a/AI_v2 b/AI_v2 new file mode 100644 index 0000000..fd4f078 --- /dev/null +++ b/AI_v2 @@ -0,0 +1,174 @@ + +import Data.List +printgame state i j n =do + if(i Int -> IO Int +max2 a b = do + if (a>b)then return a + else return b + +minimax :: [[Int]] -> Int -> Int -> Int -> Int -> IO Int +minimax state i n bestscore besti= do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if (ibestscore) then do + minimax state (i+1) n score i + else minimax state (i+1) n bestscore besti + else do + return besti + + +minplay ::[[Int]] -> Int -> Int -> Int ->Int -> Int -> IO Int +minplay state i n bestscore besti depth= do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if(i==num || depth >8) then do + res <- evaluate state n + return res + else do + if (i Int -> Int -> Int ->Int -> Int -> IO Int +maxplay state i n bestscore besti depth= do -- moves are list of all valid moves + movesi <- getmovei state n 0 0 [] + movesj <- getmovej state n 0 0 [] + let num = length movesj + if(i==num || depth > 8) then do + res <- evaluate state n + return res + else do + if (ibestscore) then do + maxplay state (i+1) n score besti depth + else maxplay state (i+1) n bestscore i depth + else return bestscore +change::[[Int]]->Int->Int->Int->[[Int]] +change state i j val=(take i state)++[(take j (state!!i))++[val]++(drop (j+1) (state!!i))]++(drop (i+1) state) + +getmovei :: [[Int]] -> Int -> Int -> Int -> [Int] -> IO [Int] +getmovei state n i j list=do + if(i==n)then return list + else do + if(j==n-1) then do + if(state!!i!!j==8) then getmovei state n (i+1) 0 (list ++ [i]) + else getmovei state n (i+1) 0 (list) + else do + if(state!!i!!j==8) then getmovei state n (i) (j+1) (list ++ [i]) + else getmovei state n (i) (j+1) (list) + +getmovej :: [[Int]] -> Int -> Int -> Int -> [Int] -> IO [Int] +getmovej state n i j list=do + if(i==n)then return list + else do + if(j==n-1) then do + if(state!!i!!j==8) then getmovej state n (i+1) 0 (list ++ [j]) + else getmovej state n (i+1) 0 (list) + else do + if(state!!i!!j==8) then getmovej state n (i) (j+1) (list ++ [j]) + else getmovej state n (i) (j+1) (list) + + + + + +dlist1 board 0 n=[board!!0!!0] +dlist1 board i n=[board!!i!!i]++dlist1 board (i-1) n +dlist2 board 0 n=[board!!0!!(n-1)] +dlist2 board i n=[board!!i!!(n-i-1)]++dlist2 board (i-1) n +------------------------------------------------------------------------------- +ones row=length (filter (==3000) row) +negs row=length (filter (==(3001)) row) +scr o n=(20^o)-(20^n) + +scor::[[Int]]->Int->Int->Int->IO Int +scor a i s dim=do + if(iInt->IO Int +evaluate a dim= do + t1<-scor a 0 0 dim + t2<-scor (transpose a) 0 0 dim + let o=ones (dlist1 a (dim-1) (dim)) + let n=negs (dlist1 a (dim-1) (dim)) + let t3=scr o n + let o=ones (dlist2 a (dim-1) (dim)) + let n=negs (dlist2 a (dim-1) (dim)) + let t4=scr o n + let m=(t1+t2+t3+t4) + return m + +input state n= do + str2 <- getLine + let i=read str2::Int + str3 <- getLine + let j=read str3::Int + let clone = change state i j 3000 + b <- minimax clone 0 n 0 0 + movesi <- getmovei clone n 0 0 [] + movesj <- getmovej clone n 0 0 [] + let num = length movesj + if(num==0) then do + printgame clone 0 0 n + print "GAME" + else do + print "Com 's turn" + let clone2 = change clone (movesi!!b) (movesj!!b) 3001 + printgame clone2 0 0 n + input clone2 n + + +main = do + print "please enter dimension of the Grid" + str<-getLine + let n = read str::Int + let state = (replicate n) ((replicate n) 8) + printgame state 0 0 n + input state n + print "OVER" + diff --git a/NxNinHaskell b/NxNinHaskell new file mode 100644 index 0000000..d92e0de --- /dev/null +++ b/NxNinHaskell @@ -0,0 +1,111 @@ +import Data.List +sum' row =foldl (\acc x -> acc+x) 0 (filter (/=8) row) + +diag1list state 0 n =[state!!0!!0] +diag1list state i n = [state!!i!!i] ++ diag1list state (i-1) n +diag2list state 0 n = [state!!0!!(n-1)] +diag2list state i n = [state !!i!!(n-1-i)] ++ diag2list state (i-1) n + +windiag l n=do + let s=sum' l + if(s==2*n) then do + print "X has won" + return () + else do + if(s==3*n) then do + print "0 has won" + return () + else + return () + +checkwin state n i=do + if(i +int arr[3][3]; +int options[9]; +int choice; +int x[9]; +int y[9]; +int pass=1; +void initialize() +{ + int i,j; + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + arr[i][j]=0; + } + } +} + + +void printing() +{ + int i,j; + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { if(arr[i][j]==1) + printf(" 0 "); + else if(arr[i][j]==2) + printf(" X "); + else + printf(" "); + } + printf("\n\n"); + } +} + + +void displayoptions() +{ + int c=1,i,j; + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + if((arr[i][j]!=1)&&(arr[i][j]!=2)) + { + printf("%d.(%d,%d)\n",c,i+1,j+1); + x[c-1]=i; + y[c-1]=j; + options[c-1]=c++; + } + } + } + printf("Enter Your Choice "); + scanf("%d",&choice); +pass++; + +} + + +int update() +{ +if(pass%2==0) +arr[x[choice-1]][y[choice-1]]=1; +else +arr[x[choice-1]][y[choice-1]]=2; +} + + +int main() +{ + +printf(" *-*-*-*-*-*-*- O has First turn *-*-*-*-*-*-*-*\n"); + +int i=0; + initialize(); + printing(); +while(i<10) +{ + displayoptions(); +update(); +printing(); + +i++; +} +} + + + + + + + + + + diff --git a/try b/try new file mode 100644 index 0000000..9808d5e --- /dev/null +++ b/try @@ -0,0 +1,4 @@ +git remote add Ahsan https://github.com/ahsanbarkati/ACA_Haskell.git +git push serversbrnchname loclbrnchname +git pull +git push