Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [source] tictactoe ai
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zylum




PostPosted: Sat Jan 28, 2006 12:44 am   Post subject: [source] tictactoe ai

im gonna make a chess ai but first i wanted to see if i understand how minimax works so i made a tictactoe ai...

code:
setscreen ("offscreenonly,graphics:250;250,nobuttonbar")
type BestMove :
    record
        score, x, y : int
    end record

var b : array 1 .. 3, 1 .. 3 of int
for i : 1 .. 3
    for j : 1 .. 3
        b (i, j) := 0
    end for
end for
var mx, my, md, mn, moves : int
moves := 0
var nextMove : BestMove

fcn winner : int
    var ret := 0
    var h, v, d1, d2 : int
    for i : 1 .. 3
        h := 0
        v := 0
        d1 := 0
        d2 := 0
        for j : 1 .. 3
            h += b (i, j)
            v += b (j, i)
            d1 += b (j, j)
            d2 += b (4 - j, j)
        end for
        ret += h div 3 + v div 3 + d1 div 3 + d2 div 3
    end for
    result sign (ret)
end winner

proc drawBoard
    for i : 1 .. 3
        for j : 1 .. 3
            drawfillbox (i * 50, j * 50, i * 50 + 50, j * 50 + 50, (b (i, j) + 2) * abs (b (i, j)))
            drawbox (i * 50, j * 50, i * 50 + 50, j * 50 + 50, black)
        end for
    end for
    View.Update
end drawBoard

fcn fullBoard : boolean
    result moves = 9
end fullBoard

fcn minimax (p : int) : BestMove
    var w := winner
    if w ~= 0| fullBoard then
        var ret : BestMove
        ret.score := w
        result ret
    end if
    var found := false
    var bestMove, tempMove : BestMove
    bestMove.score := 0
    for i : 1 .. 3
        for j : 1 .. 3
            if b (i, j) = 0 then
                b (i, j) := p
                tempMove := minimax (p * -1)
                b (i, j) := 0
                if bestMove.score * p < tempMove.score * p or ~found then
                    found := true
                    bestMove.score := tempMove.score
                    bestMove.x := i
                    bestMove.y := j
                end if
            end if
        end for
    end for
    result bestMove
end minimax

loop
    drawBoard
    exit when winner ~= 0| fullBoard
    mousewhere (mx, my, md)
    if md > 0 & mx div 50 > 0 & mx div 50 < 4 & my div 50 > 0 & my div 50 < 4 & b (mx div 50, my div 50) = 0 then
        b (mx div 50, my div 50) := 1
        moves += 1
        drawBoard
        if winner = 0 & ~fullBoard then
            nextMove := minimax (-1)
            b (nextMove.x, nextMove.y) := -1
            moves += 1
        end if
    end if
end loop

if winner < 0 then
    put "BLUE WINS!!!"
elsif winner > 0 then
    put "GREEN WINS!!!"
else
    put "DRAW!!!"
end if


i also just recently made a connect 4 ai in java and will release it as soon as i get a graphical version made.. its text based as of now Laughing
Sponsor
Sponsor
Sponsor
sponsor
Isaac




PostPosted: Sat Jan 28, 2006 2:05 am   Post subject: (No subject)

I like it, nice job. Smile
cool dude




PostPosted: Sat Jan 28, 2006 12:45 pm   Post subject: (No subject)

thats awsome. that done in exactly 100 lines is darn good! i can't wait to see your chess ai! i wonder if i would be able to beat it? it prolly be like DEEP Blue computer (lol). wat language r u making chess in?
sylvester-27




PostPosted: Mon Feb 06, 2006 11:17 am   Post subject: (No subject)

pretty good game Very Happy . The computer ai always takes like 4 seconds before it places its move though. Might be just my computer. anyways, good game
zylum




PostPosted: Mon Feb 06, 2006 11:07 pm   Post subject: (No subject)

it takes that long for the first move because it searches the entire game tree. also it only uses the basic minimax algorithm with no optimizations such as alpha beta
Mr. T




PostPosted: Mon Feb 06, 2006 11:10 pm   Post subject: Alex's Opinion

Can't wait to see you chess AI, Zylum!!! Very Happy
MysticVegeta




PostPosted: Tue Feb 07, 2006 6:42 pm   Post subject: (No subject)

Good job as always... wish I was that good in programming Sad
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: