Computer Science Canada

connect 5

Author:  person [ Sun Feb 06, 2005 10:54 am ]
Post subject:  connect 5

i did a connect 5 game... i still need to make the AI better

code:

var win := Window.Open ("position:0;0,graphics:560;560")
for x : 1 .. 480 by 80
    drawline (80 + x, 0, 80 + x, 560, 7)
end for
for x : 1 .. 480 by 80
    drawline (0, 80 + x, 560, 80 + x, 7)
end for
loop
    var x, y, button, c : int := 0
    var v : boolean := false
    loop
        mousewhere (x, y, button)
        for h : 1 .. 560 by 80
            for i : 1 .. 560 by 80
                if x > 0 + h and x < 80 + h and y > 0 + i and y < 80 + i and button = 1 and whatdotcolor (40 + h, 40 + i) not= 14 and whatdotcolor (40 + h, 40 + i) not= 1 then
                    drawfilloval (40 + h, 40 + i, 35, 35, 44)
                    v := true
                end if
            end for
        end for
        exit when button = 1 and x < 560 and x > 0 and y > 0 and y < 560 and v = true
    end loop
    v := false
    loop
        c := Rand.Int (1, 48)
        var m := 0
        for h : 0 .. 560 by 80
            for i : 1 .. 560 by 80
                m := m + 1
                if c = 1 + m and whatdotcolor (40 + h, 40 + i) not= 14 and whatdotcolor (40 + h, 40 + i) not= 1 then
                    drawfilloval (40 + h, 40 + i, 35, 35, 1)
                    v := true
                end if
                exit when v = true
            end for
            exit when v = true
        end for
        exit when v = true
    end loop
    for h : 1 .. 560
        for i : 1 .. 560
            if whatdotcolour (h, i) = 14 then
                if whatdotcolour (h, i) = 14 and whatdotcolour (h + 80, i) = 14 and whatdotcolour (h + 160, i) = 14 and whatdotcolour (h + 240, i) = 14 and whatdotcolour (h + 320, i) = 14 then
                    locate (1, 1)
                    put "u win"
                elsif whatdotcolour (h, i) = 14 and whatdotcolour (h, i + 80) = 14 and whatdotcolour (h, i + 160) = 14 and whatdotcolour (h, i + 240) = 14 and whatdotcolour (h, i + 320) = 14 then
                    locate (1, 1)
                    put "u win"
                elsif whatdotcolour (h, i) = 14 and whatdotcolour (h + 80, i + 80) = 14 and whatdotcolour (h + 160, i + 160) = 14 and whatdotcolour (h + 240, i + 240) = 14 and whatdotcolour (h + 320,
                        i + 320) = 14 then
                    locate (1, 1)
                    put "u win"
                elsif whatdotcolour (h, i) = 14 and whatdotcolour (h - 80, i + 80) = 14 and whatdotcolour (h - 160, i + 160) = 14 and whatdotcolour (h - 240, i + 240) = 14 and whatdotcolour (h - 320,
                        i + 320) = 14 then
                    locate (1, 1)
                    put "u win"
                end if
            end if
        end for
    end for
    for h : 1 .. 560
        for i : 1 .. 560
            if whatdotcolour (h, i) = 1 then
                if whatdotcolour (h, i) = 1 and whatdotcolour (h + 80, i) = 1 and whatdotcolour (h + 160, i) = 1 and whatdotcolour (h + 240, i) = 1 and whatdotcolour (h + 320, i) = 1 then
                    locate (1, 1)
                    put "u lose"
                elsif whatdotcolour (h, i) = 1 and whatdotcolour (h, i + 80) = 1 and whatdotcolour (h, i + 160) = 1 and whatdotcolour (h, i + 240) = 1 and whatdotcolour (h, i + 320) = 1 then
                    locate (1, 1)
                    put "u lose"
                elsif whatdotcolour (h, i) = 1 and whatdotcolour (h + 80, i + 80) = 1 and whatdotcolour (h + 160, i + 160) = 1 and whatdotcolour (h + 240, i + 240) = 1 and whatdotcolour (h + 320,
                        i + 320) = 1 then
                    locate (1, 1)
                    put "u lose"
                elsif whatdotcolour (h, i) = 1 and whatdotcolour (h - 80, i + 80) = 1 and whatdotcolour (h - 160, i + 160) = 1 and whatdotcolour (h - 240, i + 240) = 1 and whatdotcolour (h - 320,
                        i + 320) = 1 then
                    locate (1, 1)
                    put "u lose"
                end if
            end if
        end for
    end for
end loop


btw..does anyone know a way of making it run faster?

Author:  ssr [ Wed Feb 09, 2005 8:27 pm ]
Post subject: 

There are few bugs
1. Well Not really a bug, just the AI is Sooooooooooooooo easy to beat.
2. Yes It is going really slow, I think its because there are too mnay loops
3. Somehow, the computer doesnt stop after "u win!"

Hopefully to c u update soon, it would be a fun game Very Happy

Author:  Cervantes [ Thu Feb 10, 2005 5:31 pm ]
Post subject: 

I've only glanced at the code, but it seems you are checking every single pixel and comparing it to every pixel around it (that is, 80 pixels away from it in each direction). Don't. That's madness. Instead, just check the pixels at the centres of each grid location.
It would be best if you stored all your information in terms of locations on the grid. To elaborate, that would mean that the top right corner would be (7,7), and the bottom left corner would be (1,1). Use those values throughout your program, and when you want to draw the pieces, draw them at (x * gridXSize + gridXSize / 2, y * gridYSize + gridYSize / y). Also, this approach means you ditch whatdotcolour, so there's another bonus. 8) (The way to go would simply be to compare each spot (ex. (6,5)) to each spot around it (ex 6,6)).)

Author:  ssr [ Thu Feb 10, 2005 11:36 pm ]
Post subject: 

I think I've heard somthign about a connect 4,
that make an array, and store all the info about each square, it would look better I guess ( the code I mean) 8) Laughing


: