Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ACA_Project.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions Sine function
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fact 0 = 1
fact n = n * fac (n-1)
power x 0 = 1
power x n = x*(pow x (n-1))
sine error n x cursum = do
let an=(pow x (2*n-1))
let bn=(fac (2*n-1))
let cn=(pow (-1) (n+1))
let tn=cn*an/bn
if(error<abs tn)
then do
print tn
sine error (n+1) x (cursum+tn)
else print (cursum+tn)





main = do
print "Enter value of x in radians"
str1<-getLine
print "Enter maximum error value"
str2<-getLine
let x=read str1::Double
let error=read str2::Double
sine error 1 x 0
111 changes: 111 additions & 0 deletions Tic Tac toe NxN
Original file line number Diff line number Diff line change
@@ -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<n) then do
let l1=diag1list state (n-1) n
let l2=diag2list state (n-1) n
winrow state n i
wincol state n i
windiag l1 n
windiag l2 n
checkwin state n (i+1)
else return ()

winrow state n i=do
let s= sum' (state!!i)
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()

wincol state n i =do
let m = transpose state
let s=sum' (m!!i)
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()




game state i j c n= do
if(state!!i!!j==8) then do

let row=state !! i
let p1=take j row
let p2= drop (j+1) row
let nrow=p1++[2+c `mod` 2]++p2
let py1=take i state
let py2=drop (i+1) state
let f=py1++[nrow]++py2
printgame f 0 0 n
checkwin f n 0
input f n (c+1)
else
input state n c



input state n c= do
str2<-getLine
let i=read str2::Int
str3<-getLine
let j=read str3::Int
game state i j (c `mod` 2) n


printgame state i j n =do
if(i<n&&j<n-1)then do
if(state!!i!!j==8) then putStr("__ ")
else do
if(state!!i!!j==2) then putStr(" X ")
else putStr(" 0 ")
printgame state i (j+1) n
else do
if(j==n-1&&i<n)then do
if(state!!i!!j==8) then putStr("__ ")
else do
if(state!!i!!j==2) then putStr(" X ")
else putStr(" 0 ")
putStrLn("")
putStrLn("")
printgame state (i+1) 0 n
else
return ()

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 0
172 changes: 172 additions & 0 deletions Tic-Tac-Toe(AI).hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import Data.List
printgame state i j n =do
if(i<n&&j<n-1)then do
if(state!!i!!j==8) then putStr("__ ")
else do
if(state!!i!!j==3000) then putStr(" X ")
else putStr(" 0 ")
printgame state i (j+1) n
else do
if(j==n-1&&i<n)then do
if(state!!i!!j==8) then putStr("__ ")
else do
if(state!!i!!j==3000) then putStr(" X ")
else putStr(" 0 ")
putStrLn("")
putStrLn("")
printgame state (i+1) 0 n
else
return ()

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






max2 :: Int -> 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 (i<num) then do
let clone1 = change state (movesi!!i) (movesj!!i) 3000
score1 <- minplay clone1 0 n (100000) (0) 0-- do a proper call
let clone2 = change state (movesi!!i) (movesj!!i) 3001
score2 <- maxplay clone2 0 n (-100000) (0) 0-- do a proper call
score <- max2 score1 score2
if(score>bestscore) 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<num) then do
let clone = change state (movesi!!i) (movesj!!i) (3001)
score <- maxplay clone 0 n (-100000) (0) (1+depth) -- do a proper call
if(score<bestscore) then do
minplay state (i+1) n score besti depth
else minplay state (i+1) n bestscore i depth
else return bestscore


maxplay ::[[Int]] -> 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 (i<num) then do
let clone = change state (movesi!!i) (movesj!!i) (3000)
score <- minplay clone 0 n 100000 (0) (1+depth)-- do a proper call
if(score>bestscore) 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(i<dim) then do
let b=s+scr (ones (a!!i)) (negs (a!!i))
scor a (i+1) b dim
else return s
evaluate::[[Int]]->Int->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"
57 changes: 57 additions & 0 deletions TicTacToe.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
sm a x y c=do
let check1 =0
let check2 =0
let check3 =0
let check4 =0
let check5 =0
let check6 =0
let check7 =0
let check8 =0
if (a!!x!!y/=2)
then do
print "INVALID COORDINATES!! enter unfilled coordinates"
y<-getLine
x<-getLine
sm a (read x::Int) (read y::Int) c
else return()

let row=a!!y
let p1=take x row
let p2= drop (x+1) row
let nrow=p1++[c `mod` 2]++p2
let py1=take y a
let py2=drop (y+1) a
let f=py1++[nrow]++py2
print (f!!0)
print (f!!1)
print (f!!2)
let b = c+1
check1 <- if (f!!0!!0==f!!0!!1)&&(f!!0!!1==f!!0!!2)&&(f!!0!!2/=2) then return (1) else return (0)
check2 <- if (f!!1!!0==f!!1!!1)&&(f!!1!!1==f!!1!!2)&&(f!!1!!2/=2) then return (1) else return (0)
check3 <- if (f!!2!!0==f!!2!!1)&&(f!!2!!1==f!!2!!2)&&(f!!2!!2/=2) then return (1) else return (0)
check4 <- if (f!!0!!0==f!!1!!0)&&(f!!2!!0==f!!1!!0)&&(f!!1!!0/=2) then return (1) else return (0)
check5 <- if (f!!0!!1==f!!1!!1)&&(f!!1!!1==f!!2!!1)&&(f!!2!!1/=2) then return (1) else return (0)
check6 <- if (f!!0!!2==f!!1!!2)&&(f!!2!!2==f!!1!!2)&&(f!!1!!2/=2) then return (1) else return (0)
check7 <- if (f!!0!!0==f!!1!!1)&&(f!!1!!1==f!!2!!2)&&(f!!2!!2/=2) then return (1) else return (0)
check8 <- if (f!!0!!2==f!!1!!1)&&(f!!1!!1==f!!2!!0)&&(f!!2!!0/=2) then return (1) else return (0)
let check = check1+check2+check3+check4+check5+check6+check7+check8
if (check/=0)
then do
print "Game Over"
print (c `mod` 2)
print "Wins"
else return()
print "Please enter x and y coordinates for next turn "
y<-getLine
x<-getLine
if (check==0) then sm f (read x::Int) (read y::Int) b else print ("GAME OVER ")

main=do
let f=[[2,2,2],[2,2,2],[2,2,2]]
print (f!!0)
print (f!!1)
print (f!!2)
print "Please enter x and y coordinates for first turn "
x<-getLine
y<-getLine
sm f (read x::Int) (read y::Int) 0
Loading