Computer Science Canada

Tic-Tac-Toe

Author:  Amarylis [ Fri Mar 09, 2012 9:10 am ]
Post subject:  Tic-Tac-Toe

So here's a basic tic-tac-toe game, with a bot included...

Turing:


/* Tic-Tac-Toe game
 * By: A. Prado
 * AI Included
 * AI Instructions: Turn the variable named "Steve" off to play a two-player game
 * AI Code breakdown:
 * The AI, herein named "Steve", works by a combination of
 * random cell location and intelligent processing. It will,
 * at first, check all rows/columns/diagonals to see if they are full,
 * then it will check the ones that arent full for possibilities of
 * blocking the player, or winning. If it can do neither, it will
 * randomize a location, check if it's available, and place the O
 * in the cell if it is, repeat the process if it is not.
 */





View.Set ("graphics:500;600")
var x, y, b : int  % X and Y coordinates, button variables
var winsX, winsO, winNo : int := 0 % Counter for wins, losses, and ties
var font, fontWins : int
var turn : boolean := false % True for X, false for O
var picX, picO : int % Picture variable for the X and O
var randCell : int % Random cell variable for AI
var steve : boolean := true % Set Steve to "False" to play 2 player game
var turns : int := 1 % Variable counting who's turn it is
var gameOn, exitVar : boolean := false % Flags for exiting the whole game loop, or single game instance
var keyCheck : array char of boolean % Array controlling keyboard input
var AICheck : array 1 .. 8 of boolean :=
    init (false, false, false, false, false, false, false, false) % Row/Column/Diagonal check array
var posX, posO : array 1 .. 8 of int :=
    init (0, 0, 0, 0, 0, 0, 0, 0) % Row/Column/Diagonal points counter (O and X)
var square : array 1 .. 9 of boolean :=
    init (false, false, false, false, false, false, false, false, false)% Row/Column/Diagonal cell check


font := Font.New ("Times New Roman:14:bold")
fontWins := Font.New ("Times New Roman:12")

/* Draws X picture then saves it in a variable */
Draw.ThickLine (5, 5, 95, 95, 3, 16)
Draw.ThickLine (5, 95, 95, 5, 3, 16)
picX := Pic.New (0, 0, 100, 100)

cls

/* Draws O picture then saves it in a variable */
Draw.FillOval (50, 50, 45, 45, 16)
Draw.FillOval (50, 50, 42, 42, 0)
picO := Pic.New (0, 0, 100, 100)

cls


/* Sets all required game variables to default values */
procedure Restart
    for i : 1 .. 9
        square (i) := false
    end for

    for i : 1 .. 8
        AICheck (i) := false
        posX (i) := 0
        posO (i) := 0
    end for
    if turns mod 2 = 0 then
        turn := true
    else
        turn := false
    end if
end Restart



/* Artificial Intelligence, Steve. Used for Single-Player games */
procedure AI
    if turn = false then
        if square (1) = true and square (2) = true and square (3) = true then
            AICheck (1) := true
        end if
        if square (4) = true and square (5) = true and square (6) = true then
            AICheck (2) := true
        end if
        if square (7) = true and square (8) = true and square (9) = true then
            AICheck (3) := true
        end if
        if square (1) = true and square (4) = true and square (7) = true then
            AICheck (4) := true
        end if
        if square (2) = true and square (5) = true and square (8) = true then
            AICheck (5) := true
        end if
        if square (3) = true and square (6) = true and square (9) = true then
            AICheck (6) := true
        end if
        if square (1) = true and square (5) = true and square (9) = true then
            AICheck (7) := true
        end if
        if square (7) = true and square (5) = true and square (3) = true then
            AICheck (8) := true
        end if
        loop
            if AICheck (1) = false then
                if posO (1) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                elsif posO (1) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posO (1) = 14 then
                    Pic.Draw (picO, 200, 100, picMerge)
                    posO (1) += 1
                    posO (5) += 1
                    turn := true
                    square (2) := true
                    exit
                end if
            end if
            if AICheck (2) = false then
                if posO (2) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posO (2) = 12 then
                    Pic.Draw (picO, 300, 200, picMerge)
                    posO (2) += 3
                    posO (6) += 3
                    turn := true
                    square (6) := true
                    exit
                elsif posO (2) = 8 then
                    Pic.Draw (picO, 100, 200, picMerge)
                    posO (2) += 7
                    posO (4) += 7
                    turn := true
                    square (4) := true
                    exit
                end if
            end if
            if AICheck (3) = false then
                if posO (3) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                elsif posO (3) = 6 then
                    Pic.Draw (picO, 200, 300, picMerge)
                    posO (3) += 9
                    posO (5) += 9
                    turn := true
                    square (8) := true
                    exit
                elsif posO (3) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (4) = false then
                if posO (4) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posO (4) = 8 then
                    Pic.Draw (picO, 100, 200, picMerge)
                    posO (2) += 7
                    posO (4) += 7
                    turn := true
                    square (4) := true
                    exit
                elsif posO (4) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                end if
            end if
            if AICheck (5) = false then
                if posO (5) = 14 then
                    Pic.Draw (picO, 200, 100, picMerge)
                    posO (1) += 1
                    posO (5) += 1
                    turn := true
                    square (2) := true
                    exit
                elsif posO (5) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posO (5) = 6 then
                    Pic.Draw (picO, 200, 300, picMerge)
                    posO (3) += 9
                    posO (5) += 9
                    turn := true
                    square (8) := true
                    exit
                end if
            end if
            if AICheck (6) = false then
                if posO (6) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                elsif posO (6) = 12 then
                    Pic.Draw (picO, 300, 200, picMerge)
                    posO (2) += 3
                    posO (6) += 3
                    turn := true
                    square (6) := true
                    exit
                elsif posO (6) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (7) = false then
                if posO (7) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posO (7) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posO (7) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (8) = false then
                if posO (8) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                elsif posO (8) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posO (8) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                end if
            end if
            if AICheck (1) = false then
                if posX (1) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                elsif posX (1) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posX (1) = 14 then
                    Pic.Draw (picO, 200, 100, picMerge)
                    posO (1) += 1
                    posO (5) += 1
                    turn := true
                    square (2) := true
                    exit
                end if
            end if
            if AICheck (2) = false then
                if posX (2) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posX (2) = 12 then
                    Pic.Draw (picO, 300, 200, picMerge)
                    posO (2) += 3
                    posO (6) += 3
                    turn := true
                    square (6) := true
                    exit
                elsif posX (2) = 8 then
                    Pic.Draw (picO, 100, 200, picMerge)
                    posO (2) += 7
                    posO (4) += 7
                    turn := true
                    square (4) := true
                    exit
                end if
            end if
            if AICheck (3) = false then
                if posX (3) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                elsif posX (3) = 6 then
                    Pic.Draw (picO, 200, 300, picMerge)
                    posO (3) += 9
                    posO (5) += 9
                    turn := true
                    square (8) := true
                    exit
                elsif posX (3) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (4) = false then
                if posX (4) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posX (4) = 8 then
                    Pic.Draw (picO, 100, 200, picMerge)
                    posO (2) += 7
                    posO (4) += 7
                    turn := true
                    square (4) := true
                    exit
                elsif posX (4) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                end if
            end if
            if AICheck (5) = false then
                if posX (5) = 14 then
                    Pic.Draw (picO, 200, 100, picMerge)
                    posO (1) += 1
                    posO (5) += 1
                    turn := true
                    square (2) := true
                    exit
                elsif posX (5) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posX (5) = 6 then
                    Pic.Draw (picO, 200, 300, picMerge)
                    posO (3) += 9
                    posO (5) += 9
                    turn := true
                    square (8) := true
                    exit
                end if
            end if
            if AICheck (6) = false then
                if posX (6) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                elsif posX (6) = 12 then
                    Pic.Draw (picO, 300, 200, picMerge)
                    posO (2) += 3
                    posO (6) += 3
                    turn := true
                    square (6) := true
                    exit
                elsif posX (6) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (7) = false then
                if posX (7) = 9 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif posX (7) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posX (7) = 11 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
            if AICheck (8) = false then
                if posX (8) = 13 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                elsif posX (8) = 10 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif posX (8) = 7 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                end if
            end if
            randCell := Rand.Int (1, 9)
            if square (randCell) = false then
                if randCell = 1 then
                    Pic.Draw (picO, 100, 100, picMerge)
                    posO (1) += 6
                    posO (4) += 6
                    posO (7) += 6
                    turn := true
                    square (1) := true
                    exit
                elsif (randCell = 2 and posX (2) ~= 5) or (randCell = 2 and posO (4) > 0) then
                    Pic.Draw (picO, 200, 100, picMerge)
                    posO (1) += 1
                    posO (5) += 1
                    turn := true
                    square (2) := true
                    exit
                elsif randCell = 3 then
                    Pic.Draw (picO, 300, 100, picMerge)
                    posO (1) += 8
                    posO (6) += 8
                    posO (8) += 8
                    turn := true
                    square (3) := true
                    exit
                elsif (randCell = 4 and posX (2) ~= 5) or (randCell = 4 and posO (2) > 0) then
                    Pic.Draw (picO, 100, 200, picMerge)
                    posO (2) += 7
                    posO (4) += 7
                    turn := true
                    square (4) := true
                    exit
                elsif randCell = 5 then
                    Pic.Draw (picO, 200, 200, picMerge)
                    posO (2) += 5
                    posO (5) += 5
                    posO (7) += 5
                    posO (8) += 5
                    turn := true
                    square (5) := true
                    exit
                elsif (randCell = 6 and posX (2) ~= 5) or (randCell = 6 and posO (2) > 0) then
                    Pic.Draw (picO, 300, 200, picMerge)
                    posO (2) += 3
                    posO (6) += 3
                    turn := true
                    square (6) := true
                    exit
                elsif randCell = 7 then
                    Pic.Draw (picO, 100, 300, picMerge)
                    posO (3) += 2
                    posO (4) += 2
                    posO (8) += 2
                    turn := true
                    square (7) := true
                    exit
                elsif (randCell = 8 and posX (4) ~= 5) or (randCell = 8 and posO (4) > 0) then
                    Pic.Draw (picO, 200, 300, picMerge)
                    posO (3) += 9
                    posO (5) += 9
                    turn := true
                    square (8) := true
                    exit
                elsif randCell = 9 then
                    Pic.Draw (picO, 300, 300, picMerge)
                    posO (3) += 4
                    posO (6) += 4
                    posO (7) += 4
                    turn := true
                    square (9) := true
                    exit
                end if
            end if
        end loop
    end if
end AI



/*  */
procedure DrawStuff
    drawbox (100, 100, 200, 200, 1)                         % bottom-left
    drawbox (200, 100, 300, 200, 1)                         % bottom-centre
    drawbox (300, 100, 400, 200, 1)                         % bottom-right
    drawbox (100, 200, 200, 300, 1)                         % mid-left
    drawbox (200, 200, 300, 300, 1)                         % mid-centre
    drawbox (300, 200, 400, 300, 1)                         % mid-right
    drawbox (100, 300, 200, 400, 1)                         % top-left
    drawbox (200, 300, 300, 400, 1)                         % top-centre
    drawbox (300, 300, 400, 400, 1)                         % top-right
end DrawStuff


loop
    cls
    Restart
    gameOn := true
    DrawStuff
    Font.Draw ("Tic Tac Toe", maxx div 2 - 40, maxy - 70, font, 16)
    Font.Draw ("Wins for X: " + intstr (winsX), maxx div 2 - 90, maxy - 100, fontWins, 16)
    Font.Draw ("Wins for O: " + intstr (winsO), maxx div 2 + 30, maxy - 100, fontWins, 16)
    Font.Draw ("Ties: " + intstr (winNo), maxx div 2 - 10, maxy - 120, fontWins, 16)
    Font.Draw ("Press 'E' to exit and 'R' to play again", maxx div 2 - 100, maxy - 140, fontWins, 16)
    loop
        exit when gameOn = false
        Mouse.Where (x, y, b)
        if steve = true then
            AI
        end if
        if x > 100 and x < 200 and y > 100 and
                y < 200 and b = 1 and square (1) = false then
            if turn = true then
                Pic.Draw (picX, 100, 100, picMerge)
                posX (1) += 6
                posX (4) += 6
                posX (7) += 6
                turn := false
            else
                Pic.Draw (picO, 100, 100, picMerge)
                posO (1) += 6
                posO (4) += 6
                posO (7) += 6
                turn := true
            end if
            square (1) := true
        elsif x > 200 and x < 300 and y > 100 and
                y < 200 and b = 1 and square (2) = false then
            if turn = true then
                Pic.Draw (picX, 200, 100, picMerge)
                posX (1) += 1
                posX (5) += 1
                turn := false
            else
                Pic.Draw (picO, 200, 100, picMerge)
                posO (1) += 1
                posO (5) += 1
                turn := true
            end if
            square (2) := true
        elsif x > 300 and x < 400 and y > 100 and
                y < 200 and b = 1 and square (3) = false then
            if turn = true then
                Pic.Draw (picX, 300, 100, picMerge)
                posX (1) += 8
                posX (6) += 8
                posX (8) += 8
                turn := false
            else
                Pic.Draw (picO, 300, 100, picMerge)
                posO (1) += 8
                posO (6) += 8
                posO (8) += 8
                turn := true
            end if
            square (3) := true
        elsif x > 100 and x < 200 and y > 200 and
                y < 300 and b = 1 and square (4) = false then
            if turn = true then
                Pic.Draw (picX, 100, 200, picMerge)
                posX (2) += 7
                posX (4) += 7
                turn := false
            else
                Pic.Draw (picO, 100, 200, picMerge)
                posO (2) += 7
                posO (4) += 7
                turn := true
            end if
            square (4) := true
        elsif x > 200 and x < 300 and y > 200 and
                y < 300 and b = 1 and square (5) = false then
            if turn = true then
                Pic.Draw (picX, 200, 200, picMerge)
                posX (2) += 5
                posX (5) += 5
                posX (7) += 5
                posX (8) += 5
                turn := false
            else
                Pic.Draw (picO, 200, 200, picMerge)
                posO (2) += 5
                posO (5) += 5
                posO (7) += 5
                posO (8) += 5
                turn := true
            end if
            square (5) := true
        elsif x > 300 and x < 400 and y > 200 and
                y < 300 and b = 1 and square (6) = false then
            if turn = true then
                Pic.Draw (picX, 300, 200, picMerge)
                posX (2) += 3
                posX (6) += 3
                turn := false
            else
                Pic.Draw (picO, 300, 200, picMerge)
                posO (2) += 3
                posO (6) += 3
                turn := true
            end if
            square (6) := true
        elsif x > 100 and x < 200 and y > 300 and
                y < 400 and b = 1 and square (7) = false then
            if turn = true then
                Pic.Draw (picX, 100, 300, picMerge)
                posX (3) += 2
                posX (4) += 2
                posX (8) += 2
                turn := false
            else
                Pic.Draw (picO, 100, 300, picMerge)
                posO (3) += 2
                posO (4) += 2
                posO (8) += 2
                turn := true
            end if
            square (7) := true
        elsif x > 200 and x < 300 and y > 300 and
                y < 400 and b = 1 and square (8) = false then
            if turn = true then
                Pic.Draw (picX, 200, 300, picMerge)
                posX (3) += 9
                posX (5) += 9
                turn := false
            else
                Pic.Draw (picO, 200, 300, picMerge)
                posO (3) += 9
                posO (5) += 9
                turn := true
            end if
            square (8) := true
        elsif x > 300 and x < 400 and y > 300 and
                y < 400 and b = 1 and square (9) = false then
            if turn = true then
                Pic.Draw (picX, 300, 300, picMerge)
                posX (3) += 4
                posX (6) += 4
                posX (7) += 4
                turn := false
            else
                Pic.Draw (picO, 300, 300, picMerge)
                posO (3) += 4
                posO (6) += 4
                posO (7) += 4
                turn := true
            end if
            square (9) := true
        end if
        for i : 1 .. 8
            if posX (i) = 15 then
                gameOn := false
                winsX += 1
                if i = 1 then
                    Draw.ThickLine (100, 150, 400, 150, 7, 12)
                elsif i = 2 then
                    Draw.ThickLine (100, 250, 400, 250, 7, 12)
                elsif i = 3 then
                    Draw.ThickLine (100, 350, 400, 350, 7, 12)
                elsif i = 4 then
                    Draw.ThickLine (150, 100, 150, 400, 7, 12)
                elsif i = 5 then
                    Draw.ThickLine (250, 100, 250, 400, 7, 12)
                elsif i = 6 then
                    Draw.ThickLine (350, 100, 350, 400, 7, 12)
                elsif i = 7 then
                    Draw.ThickLine (100, 100, 400, 400, 7, 12)
                elsif i = 8 then
                    Draw.ThickLine (100, 400, 400, 100, 7, 12)
                end if
            elsif posO (i) = 15 then
                gameOn := false
                winsO += 1
                if i = 1 then
                    Draw.ThickLine (100, 150, 400, 150, 7, 12)
                elsif i = 2 then
                    Draw.ThickLine (100, 250, 400, 250, 7, 12)
                elsif i = 3 then
                    Draw.ThickLine (100, 350, 400, 350, 7, 12)
                elsif i = 4 then
                    Draw.ThickLine (150, 100, 150, 400, 7, 12)
                elsif i = 5 then
                    Draw.ThickLine (250, 100, 250, 400, 7, 12)
                elsif i = 6 then
                    Draw.ThickLine (350, 100, 350, 400, 7, 12)
                elsif i = 7 then
                    Draw.ThickLine (100, 100, 400, 400, 7, 12)
                elsif i = 8 then
                    Draw.ThickLine (100, 400, 400, 100, 7, 12)
                end if
            end if
        end for
        if square (1) = true and square (2) = true and
                square (3) = true and square (4) = true and
                square (5) = true and square (6) = true and square (7) = true and
                square (8) = true and square (9) = true then
            gameOn := false
            winNo += 1
        end if
    end loop
    turns += 1
    loop
        Input.KeyDown (keyCheck)
        if keyCheck ('r') then
            exit
        end if
        if keyCheck ('e') then
            exitVar := true
            exit
        end if
    end loop
    exit when exitVar
end loop



set "steve" to false to turn off the AI and have a two player game

Author:  Aange10 [ Fri Mar 09, 2012 5:25 pm ]
Post subject:  RE:Tic-Tac-Toe

Useless without giving us the pictures

Author:  Amarylis [ Fri Mar 09, 2012 9:24 pm ]
Post subject:  RE:Tic-Tac-Toe

Uhm... It creates it's own graphics....


The graphics in itself are pretty basic, it's just an O created from two Draw.FillOval's and the X is just two Draw.ThickLines, it's the AI that is it's main feature

Author:  Aange10 [ Fri Mar 09, 2012 9:30 pm ]
Post subject:  RE:Tic-Tac-Toe

Oh, I'm sorry. I didn't test the code; I saw the Pic.Draws and concluded you forgot to add the pictures. Most submissions who use Pic.Draw don't usually make their own pictures.

Author:  Amarylis [ Fri Mar 09, 2012 9:40 pm ]
Post subject:  RE:Tic-Tac-Toe

Yeah, I didn't have patience to look for graphics, so I just used the default ones that the teacher gave the class

Author:  klutzedufus [ Tue Mar 20, 2012 9:41 pm ]
Post subject:  RE:Tic-Tac-Toe

I'd just like to point out that if you get two rows complete in one game (using the same move), you get two wins added, as well as one tie. Of course, this isn't possible if you're playing against the AI. Looks great otherwise. And perhaps you could have a menu or something with the option to click on one player or two player as opposed to changing the variable in the code. Great work on the AI Smile

Author:  Amarylis [ Tue Mar 20, 2012 9:45 pm ]
Post subject:  RE:Tic-Tac-Toe

Yeah, the project was something for class, and I finished it WAY ahead of everyone else in the class, and no one would play with me.... So I made Steve. It was just so I could play by myself, didn't think I'd actually end up posting it anywhere, or have anyone else really use it. I might end up remaking this eventually one day when I'm really bored, but other than that I'm pretty much abandoning the project

Author:  TRUEAnonymous [ Tue May 01, 2012 3:49 pm ]
Post subject:  RE:Tic-Tac-Toe

Woah the code is long, also a pretty good coding you got there

Author:  Amarylis [ Tue May 01, 2012 5:25 pm ]
Post subject:  RE:Tic-Tac-Toe

Why thank you :3

Author:  Raknarg [ Tue May 01, 2012 8:00 pm ]
Post subject:  RE:Tic-Tac-Toe

Not a very elegant solution.
You should use an algorithm instead of a billion ifs.

Author:  Amarylis [ Tue May 01, 2012 8:03 pm ]
Post subject:  RE:Tic-Tac-Toe

I didn't know of any that I could use for that purpose, didn't feel like looking it up. It seemed, at the time, the best thing to do for just a simple game. Do any come to mind for you?

Author:  Raknarg [ Tue May 01, 2012 8:14 pm ]
Post subject:  RE:Tic-Tac-Toe

I could explain the basics.
There's two that come to mind, neither which I'm too familiar with, so you'd have to look them up later.

The first is basically a resursive check. It picks a spot, and gives the move a score according to what it does. In a basic game like this, usually you'll just have two values: 1 for a good move, -1 for a bad move. You then decide which move to use based on the resulting scores. The best score gets the move. That's what they do in games such as chess, they score moves according to what happens.

The second is weird, but I've heard it works. It similar, but theres no recursion. Essentially, it decides what to do based on the center square, and the surrounding pieces. It looks for patterns. After it checks one edge, you rotate the entire board and check again. You keep doing this uuntil all the sides have been checked, and you pick one of those options.

Author:  Amarylis [ Tue May 01, 2012 8:16 pm ]
Post subject:  RE:Tic-Tac-Toe

I took a look at the "MiniMax Algorithm", and I'm having a bit of a problem understanding the pseudocode that was provided... Granted, I usually don't actually understand the pseudocode, but that's another issue entirely.

Here's the one that I found.

code:
function alphabeta(node, depth, a, b, Player)         
    if  depth = 0 or node is a terminal node
        return the heuristic value of node
    if  Player = MaxPlayer
        for each child of node
            a := max(a, alphabeta(child, depth-1, a, b, not(Player) ))     
            if b <= a
                break                             (* Beta cut-off *)
        return a
    else
        for each child of node
            b := min(b, alphabeta(child, depth-1, a, b, not(Player) ))     
            if b <= a
                break                             (* Alpha cut-off *)
        return b
(* Initial call *)
alphabeta(origin, depth, -infinity, +infinity, MaxPlayer)

Author:  Raknarg [ Wed May 02, 2012 8:45 pm ]
Post subject:  RE:Tic-Tac-Toe

Thats because this was made for people who are fairly proficient at programming.

Don't worry, I don't get it either. I'm sure they have better explanations somewhere.

Author:  Raknarg [ Thu May 03, 2012 1:43 pm ]
Post subject:  RE:Tic-Tac-Toe

Ok, so I can't exactly understand how that code works, but I understand the algorithm.

Essentially, you just go through each possible path and determine a direction by the possible outcomes.

In tic tac toe, you would just go through each open space in order, and rate the move by the amount of wins. So you just recursively go through the whole field until no moves are left or until someone wins. I think a loss results -1, a tie results 0 and a win results 1. You add them up for each space and then pick the space with the best score.

Author:  Tony [ Thu May 03, 2012 2:38 pm ]
Post subject:  Re: RE:Tic-Tac-Toe

Raknarg @ Thu May 03, 2012 1:43 pm wrote:
In tic tac toe, you would just go through each open space in order, and rate the move by the amount of wins.

No, since some move can guarantee a single possible win, while another move might give a potential for 2 win conditions, but only if the opponent makes a mistake.

tic-tac-toe is a solved game. For any possible board configuration, there is a known best move to make -- http://xkcd.com/832/

Author:  Raknarg [ Fri May 04, 2012 10:04 am ]
Post subject:  RE:Tic-Tac-Toe

Idk, thats what my teacher did, and it seemed to work pretty well.

At least, that's what I recall he did

Author:  Amarylis [ Fri May 04, 2012 11:17 am ]
Post subject:  Re: RE:Tic-Tac-Toe

Tony @ Thu May 03, 2012 2:38 pm wrote:
Raknarg @ Thu May 03, 2012 1:43 pm wrote:
In tic tac toe, you would just go through each open space in order, and rate the move by the amount of wins.

No, since some move can guarantee a single possible win, while another move might give a potential for 2 win conditions, but only if the opponent makes a mistake.

tic-tac-toe is a solved game. For any possible board configuration, there is a known best move to make -- http://xkcd.com/832/


I have no clue how to implement that without having a hell of a lot more code than I do now, something that I'm not particularly wanting to do, since I've already handed it in Razz

Author:  Tony [ Fri May 04, 2012 12:25 pm ]
Post subject:  Re: RE:Tic-Tac-Toe

Amarylis @ Fri May 04, 2012 11:17 am wrote:
I have no clue how to implement that without having a hell of a lot more code than I do now, something that I'm not particularly wanting to do, since I've already handed it in Razz

That should be very little new code, as it's all just data.


: