Computer Science Canada

Here's my Tic Tac Toe game

Author:  Anonymous [ Wed May 10, 2006 10:00 pm ]
Post subject:  Here's my Tic Tac Toe game

code:

View.Set ("nocursor,graphics:700,600")
Mouse.ButtonChoose ("multibutton")

var x, y, button, left, middle, right : int
var xColour := brightred
var oColour := brightblue
var backColour := white
var gridColour := black
var X, O : array 1 .. 9 of boolean
for i : 1 .. 9
    X (i) := false
    O (i) := false
end for

process BackgroundMusic
    loop
        for i : 1 .. 1000
            var x : int := Rand.Int (0, 400)
            Music.Sound (i * x, 40)
        end for
    end loop
end BackgroundMusic

%fork BackgroundMusic

process XSound
    Music.Sound (5, 1)
end XSound

process OSound
    Music.Sound (54, 12)
end OSound

process BlankSound
    Music.Sound (1, 19)
end BlankSound

% GRID
procedure Grid
    drawbox (0, 0, maxx - 100, maxy, gridColour)
    Draw.ThickLine (0, 200, maxx - 100, 200, 2, gridColour)
    Draw.ThickLine (0, 400, maxx - 100, 400, 2, gridColour)
    Draw.ThickLine (200, 0, 200, maxy, 2, gridColour)
    Draw.ThickLine (400, 0, 400, maxy, 2, gridColour)
end Grid

drawfillbox (0, 0, maxx, maxy, backColour)

loop
    Grid
    Mouse.Where (x, y, button)
    left := button mod 10
    middle := (button - left) mod 100
    right := button - middle - left

    % BOX 1
    if (x < 200) and (x > 0) and (y > 400) and (y < 600) and (left = 1) then
        drawfillbox (0, 600, 200, 400, backColour)
        Draw.ThickLine (0, 400, 200, maxy, 3, xColour)
        Draw.ThickLine (200, 400, 0, maxy, 3, xColour)
        X (1) := true
        O (1) := false
        fork XSound
    elsif (x < 200) and (x > 0) and (y > 400) and (y < 600) and (right = 100) then
        drawfillbox (0, 600, 200, 400, backColour)
        drawoval (100, 500, 100, 100, oColour)
        O (1) := true
        X (1) := false
        fork OSound
    elsif (x < 200) and (x > 0) and (y > 400) and (y < 600) and (middle = 10) then
        drawfillbox (0, 600, 200, 400, backColour)
        X (1) := false
        O (1) := false
        fork BlankSound
    end if

    % BOX 2
    if (x > 200) and (x < 400) and (y > 400) and (y < 600) and (left = 1) then
        drawfillbox (200, 600, 400, 400, backColour)
        Draw.ThickLine (200, 400, 400, maxy, 3, xColour)
        Draw.ThickLine (400, 400, 200, maxy, 3, xColour)
        X (2) := true
        O (2) := false
        fork XSound
    elsif (x > 200) and (x < 400) and (y > 400) and (y < 600) and (right = 100) then
        drawfillbox (200, 600, 400, 400, backColour)
        drawoval (300, 500, 100, 100, oColour)
        O (2) := true
        X (2) := false
        fork OSound
    elsif (x > 200) and (x < 400) and (y > 400) and (y < 600) and (middle = 10) then
        drawfillbox (200, 600, 400, 400, backColour)
        X (2) := false
        O (2) := false
        fork BlankSound
    end if

    % BOX 3
    if (x > 400) and (x < 600) and (y > 400) and (y < 600) and (left = 1) then
        drawfillbox (400, 600, 600, 400, backColour)
        Draw.ThickLine (400, 400, 600, maxy, 3, xColour)
        Draw.ThickLine (600, 400, 400, maxy, 3, xColour)
        X (3) := true
        O (3) := false
        fork XSound
    elsif (x > 400) and (x < 600) and (y > 400) and (y < 600) and (right = 100) then
        drawfillbox (400, 600, 600, 400, backColour)
        drawoval (500, 500, 100, 100, oColour)
        O (3) := true
        X (3) := false
        fork OSound
        Music.SoundOff
    elsif (x > 400) and (x < 600) and (y > 400) and (y < 600) and (middle = 10) then
        drawfillbox (400, 600, 600, 400, backColour)
        X (3) := false
        O (3) := false
        fork BlankSound
    end if

    % BOX 4
    if (x < 200) and (x > 0) and (y < 400) and (y > 200) and (left = 1) then
        drawfillbox (0, 200, 200, 400, backColour)
        Draw.ThickLine (0, 200, 200, 400, 3, xColour)
        Draw.ThickLine (0, 400, 200, 200, 3, xColour)
        X (4) := true
        O (4) := false
        fork XSound
    elsif (x < 200) and (x > 0) and (y < 400) and (y > 200) and (right = 100) then
        drawfillbox (0, 200, 200, 400, backColour)
        drawoval (100, 300, 100, 100, oColour)
        O (4) := true
        X (4) := false
        fork OSound
    elsif (x < 200) and (x > 0) and (y < 400) and (y > 200) and (middle = 10) then
        drawfillbox (0, 200, 200, 400, backColour)
        X (4) := false
        O (4) := false
        fork BlankSound
    end if

    % BOX 5
    if (x > 200) and (x < 400) and (y > 200) and (y < 400) and (left = 1) then
        drawfillbox (200, 200, 400, 400, backColour)
        Draw.ThickLine (200, 200, 400, 400, 3, xColour)
        Draw.ThickLine (400, 200, 200, 400, 3, xColour)
        X (5) := true
        O (5) := false
        fork XSound
    elsif (x > 200) and (x < 400) and (y > 200) and (y < 400) and (right = 100) then
        drawfillbox (200, 200, 400, 400, backColour)
        drawoval (300, 300, 100, 100, oColour)
        O (5) := true
        X (5) := false
        fork OSound
    elsif (x > 200) and (x < 400) and (y > 200) and (y < 400) and (middle = 10) then
        drawfillbox (200, 200, 400, 400, backColour)
        X (5) := false
        O (5) := false
        fork BlankSound
    end if

    % BOX 6
    if (x > 400) and (x < 600) and (y > 200) and (y < 400) and (left = 1) then
        drawfillbox (600, 200, 400, 400, backColour)
        Draw.ThickLine (400, 200, 600, 400, 3, xColour)
        Draw.ThickLine (600, 200, 400, 400, 3, xColour)
        X (6) := true
        O (6) := false
        fork XSound
    elsif (x > 400) and (x < 600) and (y > 200) and (y < 400) and (right = 100) then
        drawfillbox (600, 200, 400, 400, backColour)
        drawoval (500, 300, 100, 100, oColour)
        O (6) := true
        X (6) := false
        fork OSound
    elsif (x > 400) and (x < 600) and (y > 200) and (y < 400) and (middle = 10) then
        drawfillbox (600, 200, 400, 400, backColour)
        X (6) := false
        O (6) := false
        fork BlankSound
    end if

    % BOX 7
    if (x > 0) and (x < 200) and (y > 0) and (y < 200) and (left = 1) then
        drawfillbox (0, 0, 200, 200, backColour)
        Draw.ThickLine (0, 0, 200, 200, 3, xColour)
        Draw.ThickLine (0, 200, 200, 0, 3, xColour)
        X (7) := true
        O (7) := false
        fork XSound
    elsif (x > 0) and (x < 200) and (y > 0) and (y < 200) and (right = 100) then
        drawfillbox (0, 0, 200, 200, backColour)
        drawoval (100, 100, 100, 100, oColour)
        O (7) := true
        X (7) := false
        fork OSound
    elsif (x > 0) and (x < 200) and (y > 0) and (y < 200) and (middle = 10) then
        drawfillbox (0, 0, 200, 200, backColour)
        X (7) := false
        O (7) := false
        fork BlankSound
    end if

    % BOX 8
    if (x > 200) and (x < 400) and (y > 0) and (y < 200) and (left = 1) then
        drawfillbox (200, 0, 400, 200, backColour)
        Draw.ThickLine (200, 0, 400, 200, 3, xColour)
        Draw.ThickLine (200, 200, 400, 0, 3, xColour)
        X (8) := true
        O (8) := false
        fork XSound
    elsif (x > 200) and (x < 400) and (y > 0) and (y < 200) and (right = 100) then
        drawfillbox (200, 0, 400, 200, backColour)
        drawoval (300, 100, 100, 100, oColour)
        O (8) := true
        X (8) := false
        fork OSound
    elsif (x > 200) and (x < 400) and (y > 0) and (y < 200) and (middle = 10) then
        drawfillbox (200, 0, 400, 200, backColour)
        X (8) := false
        O (8) := false
        fork BlankSound
    end if

    % BOX 9
    if (x > 400) and (x < 600) and (y > 0) and (y < 200) and (left = 1) then
        drawfillbox (600, 0, 400, 200, backColour)
        Draw.ThickLine (400, 0, 600, 200, 3, xColour)
        Draw.ThickLine (600, 0, 400, 200, 3, xColour)
        X (9) := true
        O (9) := false
        fork XSound
    elsif (x > 400) and (x < 600) and (y > 0) and (y < 200) and (y < 400) and (right = 100) then
        drawfillbox (600, 0, 400, 200, backColour)
        drawoval (500, 100, 100, 100, oColour)
        O (9) := true
        X (9) := false
        fork OSound
    elsif (x > 400) and (x < 600) and (y > 0) and (y < 200) and (y < 400) and (middle = 10) then
        drawfillbox (600, 0, 400, 200, backColour)
        X (9) := false
        O (9) := false
        fork BlankSound
    end if

    % SCORES
    var xScore, oScore : int := 0

    % X's
    if (X (1) = true) and (X (2) = true) and (X (3) = true) then
        xScore += 1
    end if
    if (X (4) = true) and (X (5) = true) and (X (6) = true) then
        xScore += 1
    end if
    if (X (7) = true) and (X (8) = true) and (X (9) = true) then
        xScore += 1
    end if
    if (X (1) = true) and (X (4) = true) and (X (7) = true) then
        xScore += 1
    end if
    if (X (2) = true) and (X (5) = true) and (X (8) = true) then
        xScore += 1
    end if
    if (X (3) = true) and (X (6) = true) and (X (9) = true) then
        xScore += 1
    end if
    if (X (1) = true) and (X (5) = true) and (X (9) = true) then
        xScore += 1
    end if
    if (X (3) = true) and (X (5) = true) and (X (7) = true) then
        xScore += 1
    end if

    % O's
    if (O (1) = true) and (O (2) = true) and (O (3) = true) then
        oScore += 1
    end if
    if (O (4) = true) and (O (5) = true) and (O (6) = true) then
        oScore += 1
    end if
    if (O (7) = true) and (O (8) = true) and (O (9) = true) then
        oScore += 1
    end if
    if (O (1) = true) and (O (4) = true) and (O (7) = true) then
        oScore += 1
    end if
    if (O (2) = true) and (O (5) = true) and (O (8) = true) then
        oScore += 1
    end if
    if (O (3) = true) and (O (6) = true) and (O (9) = true) then
        oScore += 1
    end if
    if (O (1) = true) and (O (5) = true) and (O (9) = true) then
        oScore += 1
    end if
    if (O (3) = true) and (O (5) = true) and (O (7) = true) then
        oScore += 1
    end if

    % PLACES SCORES
    locate (1, 80)
    colour (xColour)
    colourback (backColour)
    put "X:  ", xScore
    colour (oColour)
    locate (2, 80)
    put "O:  ", oScore

    var getKey : array char of boolean
    var clr : int
    Input.KeyDown (getKey)
    % CLEARS SCORE
    if getKey ('c') then
        for n : 1 .. 9
            X (n) := false
            O (n) := false
        end for
        xScore := 0
        oScore := 0
        cls
        % CHANGING X/O COLOUR
    elsif getKey ('x') and getKey (KEY_UP_ARROW) then
        xColour += 1
    elsif getKey ('x') and getKey (KEY_DOWN_ARROW) then
        xColour -= 1
    elsif getKey ('o') and getKey (KEY_UP_ARROW) then
        oColour += 1
    elsif getKey ('o') and getKey (KEY_DOWN_ARROW) then
        oColour -= 1
    end if
    if (gridColour = backColour) then
        backColour := gridColour + 1
    end if
    if (xColour = 256) then
        xColour := 1
    elsif (xColour = 0) then
        xColour := 255
    elsif (oColour = 256) then
        oColour := 1
    elsif (oColour = 0) then
        oColour := 255
        % GET NEW COLOUR
    elsif getKey ('x') and getKey ('g') then
        colour (black)
        locate (15, 80)
        get clr
        if (clr > 255) then
            clr := 255
        elsif (clr < 0) then
            clr := 0
        end if
        xColour := clr
        cls
    elsif getKey ('o') and getKey ('g') then
        colour (black)
        locate (15, 80)
        get clr
        if (clr > 255) then
            clr := 255
        elsif (clr < 0) then
            clr := 0
        end if
        oColour := clr
        cls
    end if
    Music.SoundOff
end loop



Author:  Anonymous [ Wed May 10, 2006 10:04 pm ]
Post subject:  OOPS

Heh I was bored and it took me awhile to make those 'X' and 'O' s.

Author:  do_pete [ Wed May 10, 2006 10:36 pm ]
Post subject: 

Not bad. You should use a proc for drawing your grid. Interesting "background music" Laughing

Author:  upthescale [ Thu May 11, 2006 1:52 pm ]
Post subject: 

use some offscreenonly....and two dots after the 0score and x score so u can actualyl see the x or o when it is near the score

Author:  monkeynamedsteve [ Thu May 11, 2006 2:22 pm ]
Post subject: 

Hey, I tried out your code for the program. Just a suggestion, maybe you might want to clear the x's and o's once someone gets three in a row. Also it would be good to specify at the start of the program that rightclick is x and left is o.

Author:  BenLi [ Thu May 11, 2006 2:35 pm ]
Post subject: 

Overkill of sorts don't you think?

so many lines.... lol
anyways, you should try a win detect mechanism, and maybe prevent the changing of already occupied squares

Author:  monkeynamedsteve [ Thu May 11, 2006 2:59 pm ]
Post subject: 

I say its a pretty good program, you just need to knock it down a few lines for those who actaully care. It runs fine for me right now, but adding any more might make you want to question the length.

Author:  Clayton [ Thu May 11, 2006 3:17 pm ]
Post subject: 

if you want any ideas for short coding games such as this, check out the old contests and their submissions, as they were pretty much all programmed by excellent programmers Very Happy

Author:  Anonymous [ Thu May 11, 2006 4:51 pm ]
Post subject:  Secrets

If you had noticed some of my code has secret commands, such as holding 'x' or 'o' and pressing the arrow keys to change colour =) That add's to some length. Other than that I can't really see a way to trim any of it off.

Author:  monkeynamedsteve [ Thu May 11, 2006 5:01 pm ]
Post subject: 

Maybe use a for loop with an array when setting up the boxes.
Also it needs some explination of rules at the start. Think user friendly.

Author:  do_pete [ Thu May 11, 2006 5:31 pm ]
Post subject: 

You can cut down on the if statements with the help of for loops.

Author:  TheRedOne [ Thu Jun 08, 2006 1:01 pm ]
Post subject: 

Pointless, truly pointless. Confused

Author:  Clayton [ Thu Jun 08, 2006 3:34 pm ]
Post subject: 

^the above post is spam^ not only to mention it could qualify as flaming, read [The Rules] before you post Confused also read the part on reviving dead topics, generally anything over a week since the last post has no need to be posted in

Author:  - IzAk - [ Mon Jun 12, 2006 11:20 am ]
Post subject: 

it didn't work.... all i got was x's, not one single o appeared. it let me fill everybox with an x and told me my score was 8 instead of 9??

Author:  - IzAk - [ Mon Jun 12, 2006 11:22 am ]
Post subject: 

scratch that i got both x's & o's to work, but now it doesnt tell my i win after i get 3 in a row, it just keeps the game playing.


: