Computer Science Canada

Game

Author:  batman [ Thu Jun 08, 2006 8:16 pm ]
Post subject:  Game

I need help fiquire out how to prevent an x and a o being drawn over each other if the user clicks a boc twice.

code:

colour (brightgreen)
put "PLEASE ENTER THE NAMES OF THE TWO PLAYERS!"
get name1
get name2

var x1, y1, button, buttonnumber, buttonupdown : int
var counter : int := 1
var x : array 1 .. 8 of int
var o : array 1 .. 8 of int
var ch : string(1)


for loc : 1 .. 8
    x (loc) := 0
    o (loc) := 0
end for

setscreen ("graphics:400,400")
font := Font.New ("Palatino:38:bold")
Draw.FillBox (maxx, maxy, 0, 0, black)
Draw.FillBox (135, 0, 131, maxy, brightgreen)
Draw.FillBox (268, 0, 264, maxy, brightgreen)
Draw.FillBox (0, 135, maxx, 131, brightgreen)
Draw.FillBox (0, 268, maxx, 264, brightgreen)


procedure one
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 50, 320, font, brightgreen)
        x (3) := x (3) + 1
        x (4) := x (4) + 1
        x (8) := x (8) + 1
    else
        Draw.Text ("O", 50, 320, font, brightgreen)
        o (3) := o (3) + 1
        o (4) := o (4) + 1
        o (8) := o (8) + 1
    end if
end one

procedure two
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 175, 320, font, brightgreen)
        x (2) := x (2) + 1
        x (4) := x (4) + 1
    else
        Draw.Text ("O", 175, 320, font, brightgreen)
        o (2) := o (2) + 1
        o (4) := o (4) + 1
    end if
end two

procedure three
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 300, 320, font, brightgreen)
        x (1) := x (1) + 1
        x (4) := x (4) + 1
        x (7) := x (7) + 1
    else
        Draw.Text ("O", 300, 320, font, brightgreen)
        o (1) := o (1) + 1
        o (4) := o (4) + 1
        o (7) := o (7) + 1
    end if
end three

procedure four
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 50, 175, font, brightgreen)
        x (3) := x (3) + 1
        x (5) := x (5) + 1
    else
        Draw.Text ("O", 50, 175, font, brightgreen)
        o (3) := o (3) + 1
        o (5) := o (5) + 1
    end if
end four

procedure five
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 175, 175, font, brightgreen)
        x (2) := x (2) + 1
        x (5) := x (5) + 1
        x (7) := x (7) + 1
        x (8) := x (8) + 1
    else
        Draw.Text ("O", 175, 175, font, brightgreen)
        o (2) := o (2) + 1
        o (5) := o (5) + 1
        o (7) := o (7) + 1
        o (8) := o (8) + 1
    end if
end five

procedure six
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 300, 175, font, brightgreen)
        x (1) := x (1) + 1
        x (5) := x (5) + 1
    else
        Draw.Text ("O", 300, 175, font, brightgreen)
        o (1) := o (1) + 1
        o (5) := o (5) + 1
    end if
end six

procedure seven
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 50, 50, font, brightgreen)
        x (3) := x (3) + 1
        x (6) := x (6) + 1
        x (7) := x (7) + 1
    else
        Draw.Text ("O", 50, 50, font, brightgreen)
        o (3) := o (3) + 1
        o (6) := o (6) + 1
        o (7) := o (7) + 1
    end if
end seven

procedure eight
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 175, 50, font, brightgreen)
        x (2) := x (2) + 1
        x (6) := x (6) + 1
    else
        Draw.Text ("O", 175, 50, font, brightgreen)
        o (2) := o (2) + 1
        o (6) := o (6) + 1

    end if
end eight

procedure nine
    if counter = 1 or counter = 3 or counter = 5 or counter = 7 or counter = 9 then
        Draw.Text ("X", 300, 50, font, brightgreen)
        x (1) := x (1) + 1
        x (6) := x (6) + 1
        x (8) := x (8) + 1
    else
        Draw.Text ("O", 300, 50, font, brightgreen)
        o (1) := o (1) + 1
        o (6) := o (6) + 1
        o (8) := o (8) + 1
    end if
end nine
loop
    mousewhere (x1, y1, button)
    if button = 1 and x1 >= 0 and x1 <= 133 and y1 >= 268 and win not= 1 then
        one
    elsif button = 1 and x1 >= 134 and x1 <= 268 and y1 >= 268 and win not= 1 then
        two
    elsif button = 1 and x1 >= 267 and x1 <= maxx and y1 >= 268 and win not= 1 then
        three
    elsif button = 1 and x1 >= 0 and x1 <= 133 and y1 <= 268 and y1 >= 135 and win not= 1 then
        four
    elsif button = 1 and x1 >= 134 and x1 <= 268 and y1 >= 135 and y1 <= 268 and win not= 1 then
        five
    elsif button = 1 and x1 >= 267 and x1 <= maxx and y1 >= 135 and y1 <= 268 and win not= 1 then
        six
    elsif button = 1 and x1 >= 0 and x1 <= 133 and y1 >= 0 and y1 <= 134 and win not= 1 then
        seven
    elsif button = 1 and x1 >= 134 and x1 <= 268 and y1 >= 0 and y1 <= 134 and win not= 1 then
        eight
    elsif button = 1 and x1 >= 268 and x1 <= maxx and y1 >= 0 and y1 <= 134 and win not= 1 then
        nine
    end if
    if button = 1 then
        counter := counter + 1
        delay (500)
    end if

    exit when counter = 11

    if x (1) = 3 or x (2) = 3 or x (3) = 3 or x (4) = 3 or x (5) = 3 or x (6) = 3 or x (7) = 3 or x (8) = 3 then
        locate (25, 5)
        put name1, " WINS!!"
        win := 1
        exit
    elsif o (1) = 3 or o (2) = 3 or o (3) = 3 or o (4) = 3 or o (5) = 3 or o (6) = 3 or o (7) = 3 or o (8) = 3 then
        locate (25, 5)
        put name2, " WINS!!!"
        win := 1
        exit
    elsif counter = 10 then
        locate (25, 5)
        put "TIE GAME!"
        win := 1
        exit
    end if
end loop

Author:  TheOneTrueGod [ Thu Jun 08, 2006 8:27 pm ]
Post subject: 

Why not have a one dimensional array (0..8) that keeps track of what is in what square? Use some math (mod and div) to determine the mouse's co-ordinates, and you're all set.

Author:  Clayton [ Thu Jun 08, 2006 8:42 pm ]
Post subject: 

or how bout you have a record for each square, which includes a flag variable to check if the square has been used Very Happy

Author:  batman [ Thu Jun 08, 2006 8:44 pm ]
Post subject: 

thanks for the help but im a beginner and i need a way tahts easy and doesnt require a lot of changing!

Author:  TheOneTrueGod [ Thu Jun 08, 2006 9:03 pm ]
Post subject: 

The one dimensional array would function like a flag, but its a 3-setting flag.

0 = no one is in it
1 = X
2 = O

only change it if the square is a 0.

This would require VERY little effort, but a bit more thought on your part. If you don't try the "challenging" stuff, then you'll never get better.

code:

0 | 1 | 2
----------
3 | 4 | 5
----------
6 | 7 | 8


Those will be the square numbers. If you learn the mod and div commands, it becomes REALLY easy to implement. (much much easier than your current method)

Author:  Clayton [ Thu Jun 08, 2006 10:52 pm ]
Post subject: 

if you want to check out other peoples tic-tac-toe games (oh yes there's others) search tic-tac-toe and limit the search to Turing Very Happy this will give you an idea of how to more efficiently code your win algorithim


: