
-----------------------------------
SuperGenius
Fri Mar 12, 2004 8:16 pm

CONNECT 4
-----------------------------------
This is my finished connect 4 game. It rules. Try it out if you want, and if you spot a glitch please let me know, although I'm fairly certain that i've taken care of them all. There is a process to play music when you win, which I have commented for your convenience. Change the filename and uncomment it if that;s the kind of thing that floats your boat. 


% CONNECT 4 by Chris
% stupid mistake pointed out by Paul Bian... thanks x2
setscreen ("graphics:800;600,nobuttonbar")
var col, stop, tally : int
var act, colchoice : string (1) := ""
var actcolour, actname, checkcolour, winner : string := ""
var board : array 1 .. 8, 1 .. 8 of string
var height : array 1 .. 8 of int
var names : array 1 .. 2 of string
var wins : array 1 .. 2 of int := init (0, 0)
var winlocc, winloch : array 1 .. 4 of int
proc colget
    loop
        locate (33, 1)
        put actname, "(", actcolour, ")", " Enter col choice" ..
        getch (colchoice)
        locate (34, 1)
        put ""
        if colchoice = "1" or colchoice = "2" or colchoice = "3" or colchoice = "4" or colchoice = "5" or colchoice = "6" or colchoice = "7" or colchoice = "8" then
            exit
        else
            locate (34, 1)
            put "numbers 1-8 only"
        end if
    end loop
    col := strint (colchoice)
end colget
proc drawboard
    for h : 1 .. 8
        for c : 1 .. 8
            if board (h, c) not= " " then
                if board (h, c) = "RED" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, red)
                elsif board (h, c) = "GREEN" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, green)
                end if
            end if
        end for
    end for
end drawboard
proc switch
    if actcolour = "RED" then
        actcolour := "GREEN"
        actname := names (2)
    else
        actcolour := "RED"
        actname := names (1)
    end if
end switch
proc wincheck
    loop
        for a : 1 .. 8
            for b : 1 .. 5
                if board (a, b) = checkcolour and
                        board (a, b + 1) = checkcolour and
                        board (a, b + 2) = checkcolour and
                        board (a, b + 3) = checkcolour then
                    winner := checkcolour
                    for c : 1 .. 4
                        winlocc (c) := a
                    end for
                    winloch (1) := b
                    winloch (2) := b + 1
                    winloch (3) := b + 2
                    winloch (4) := b + 3
                    exit
                end if
            end for
        end for
        for a : 1 .. 5
            for b : 1 .. 8
                if board (a, b) = checkcolour and
                        board (a + 1, b) = checkcolour and
                        board (a + 2, b) = checkcolour and
                        board (a + 3, b) = checkcolour then
                    winner := checkcolour
                    for c : 1 .. 4
                        winloch (c) := b
                    end for
                    winlocc (1) := a
                    winlocc (2) := a + 1
                    winlocc (3) := a + 2
                    winlocc (4) := a + 3
                    exit
                end if
            end for
        end for
        for a : 1 .. 5
            for b : 1 .. 5
                if board (a, b) = checkcolour and
                        board (a + 1, b + 1) = checkcolour and
                        board (a + 2, b + 2) = checkcolour and
                        board (a + 3, b + 3) = checkcolour then
                    winner := checkcolour
                    winloch (1) := a
                    winloch (2) := a + 1
                    winloch (3) := a + 2
                    winloch (4) := a + 3
                    winlocc (1) := b
                    winlocc (2) := b + 1
                    winlocc (3) := b + 2
                    winlocc (4) := b + 3
                    exit
                end if
            end for
        end for
        for a : 1 .. 5
            for decreasing b : 8 .. 4
                if board (a, b) = checkcolour and
                        board (a + 1, b - 1) = checkcolour and
                        board (a + 2, b - 2) = checkcolour and
                        board (a + 3, b - 3) = checkcolour then
                    winner := checkcolour
                    winloch (1) := b
                    winloch (2) := b - 1
                    winloch (3) := b - 2
                    winloch (4) := b - 3
                    winlocc (1) := a
                    winlocc (2) := a + 1
                    winlocc (3) := a + 2
                    winlocc (4) := a + 3
                    exit
                end if
            end for
        end for
        exit
    end loop
end wincheck
process flash
    loop
        Draw.FillOval (130 + winloch (1) * 60, 80 + winlocc (1) * 60, 25, 25, 9)
        Draw.FillOval (130 + winloch (2) * 60, 80 + winlocc (2) * 60, 25, 25, 9)
        Draw.FillOval (130 + winloch (3) * 60, 80 + winlocc (3) * 60, 25, 25, 9)
        Draw.FillOval (130 + winloch (4) * 60, 80 + winlocc (4) * 60, 25, 25, 9)
        delay (100)
        Draw.FillOval (130 + winloch (1) * 60, 80 + winlocc (1) * 60, 25, 25, 42)
        Draw.FillOval (130 + winloch (2) * 60, 80 + winlocc (2) * 60, 25, 25, 42)
        Draw.FillOval (130 + winloch (3) * 60, 80 + winlocc (3) * 60, 25, 25, 42)
        Draw.FillOval (130 + winloch (4) * 60, 80 + winlocc (4) * 60, 25, 25, 42)
        delay (100)
        exit when act = "y" or act = "n"
    end loop
end flash
%process xMusic
   % Music.PlayFile ("lc.mp3")
%end xMusic
loop
    Music.PlayFileStop
    %%%startup%%%
    col := 0
    stop := 0
    tally := 0
    winner := ""
    for a : 1 .. 8
        height (a) := 0
        for b : 1 .. 8
            board (a, b) := " "
        end for
    end for
    for a : 1 .. 4
        winlocc (a) := 0
        winloch (a) := 0
    end for
    %%%get names%%%
    if act not= "y" then
        for a : 1 .. 2
            put "ENTER NAME ", a
            get names (a)
        end for
    end if
    cls
    %%%end names
    act := ""
    actcolour := "RED"
    actname := names (1)
    %%%Draws the grid of the board%%%
    Draw.Line (160, 590, 160, 110, black)
    Draw.Line (640, 590, 640, 110, black)
    Draw.Line (160, 590, 640, 590, black)
    Draw.Line (160, 110, 640, 110, black)
    for a : 1 .. 7
        Draw.Line (a * 60 + 160, 110, a * 60 + 160, 590, black)
        Draw.Line (160, a * 60 + 110, 640, a * 60 + 110, black)
    end for
    locate (32, 1)
    put " " : 23, "1" : 8, "2" : 7, "3" : 8, "4" : 7, "5" : 8, "6" : 7, "7" : 8, "8"
    %%%end grid%%%
    %%%end startup%%%
    loop
        loop
            colget
            if height (col) = 8 then
                locate (34, 1)
                put "That col is full."
            else
                height (col) := height (col) + 1
                exit
            end if
        end loop
        board (height (col), col) := actcolour
        drawboard
        checkcolour := "RED"
        wincheck
        checkcolour := "GREEN"
        wincheck
        if winner = "RED" then
            locate (34, 1)
            put names (1), " WINS!"
            wins (1) := wins (1) + 1
            exit
        elsif winner = "GREEN" then
            locate (34, 1)
            put names (2), " WINS!"
            wins (2) := wins (2) + 1
            exit
        end if
        switch
    end loop
    locate (34, 1)
    for a : 1 .. 2
        put names (a), "'s wins: ", wins (a)
    end for
    if act not= "y" and winlocc (1) not= 0 then
        fork flash
        fork xMusic
    end if
    locate (33, 1)
    put "WOULD YOU LIKE TO PLAY AGAIN?(y/n)" ..
    loop
        getch (act)
        if act = "n" or act = "y" then
            exit
        else
            locate (33, 1)
            put "enter y or n, lower case       " ..
        end if
    end loop
    exit when act = "n"
    delay (500)
end loop

-----------------------------------
Delos
Fri Mar 12, 2004 8:52 pm


-----------------------------------
Oh my...that's...a lot...of code.

In other words:
- please attatch .t files if they are going to be that long.  It makes everyone's lives easier.
- if you're going to put the code right into the post (which isn't always a bad idea), try using the 'code' tags.  Not only does it look neater, but it's more presentable as well!  That and you won't get flamed as much...

Otherwise...look's like a hopeful proggie.

-----------------------------------
jonos
Fri Mar 12, 2004 10:55 pm


-----------------------------------
nice program, you should make it with ai though, that would be a nice challenge, and using the mouse which wouldn't be too hard. very smooth and nice looking.

-----------------------------------
Paul
Fri Mar 12, 2004 11:05 pm


-----------------------------------
Yay! Good to see it up and running after all that work. :D

-----------------------------------
Delos
Sat Mar 13, 2004 4:26 pm


-----------------------------------
Nice!

Just found a bug though:

I won using a diagonal win with these coords:
(1, 4) (2, 3) (3, 2) (4, 1)

And then the flashy dots came, but they appeared at the following coords:
(5, -1) (6, -2) [and I'm assuming...] (7, -3) (8, -4)

So...yeah...that was odd.  Shouldn't be too hard to fix though.

-----------------------------------
Paul
Sat Mar 13, 2004 8:40 pm


-----------------------------------
Methinks its the problem with the array, where if you played a piece in the analog of 1,8 in the chart, it would fill variable (1,1)

-----------------------------------
SuperGenius
Sat Mar 13, 2004 8:48 pm


-----------------------------------
1. I noticed and  fixed that little problem where the wrong squares flashed orange and blue
2. I thought I had nailed down the problem where a circle would flash at (0,0) at the start of the second game, but apperently not.
3. sorry about not using [code]tags. ill edit my post right now and put them in.
4. are you kidding me about A.I.? I almost broke the monitor with my face doing this much.
5. thanks guys

-----------------------------------
SuperGenius
Sat Mar 13, 2004 9:07 pm


-----------------------------------
there... i think i've taken care of the issues you guys brought up, and ive updated the source in the first post. As for my decision to post the soucre and not attatch it, there are two reasons.

1. I didnt think to attatch it
2. I'll be able to work on it at school if I want without having to use disks. one decided to go and have an error when I was working on this, so I lost about a hundred lines of code, but I rebuilt my program more efficently, so I guess it worked out. 
3. Everyone in my class will end up seeing it probably, because they are lazy and all they do is go on compsci and play games. Or look at web pages titled "Lord of the Blings" when the teacher is talking.

-----------------------------------
the_short1
Sun Mar 14, 2004 7:21 pm


-----------------------------------
good game. i like...it would be nice with a mouse.. and not too hard either... good job.. + 2 bits... also.. that GTO in ur avtar rocks.. the new one.. (in sig) sucks i hate the look of it... it lost its Muscle Car look...

-----------------------------------
Cervantes
Sun Mar 14, 2004 7:39 pm


-----------------------------------
talk about cheap :P  You recieved 2 bits for telling him that he got 2 bits from you :?

anyways, great job on your connect 4 program SuperGenius.  
+20 Bits, and + 5 for saying "I almost broke my monitor with my face doing this much" :P

What's this Lord of the Blings?  It sounds interesting :wink:

-----------------------------------
shorthair
Mon Mar 15, 2004 9:22 am


-----------------------------------
Great , just great , good fun for 20 mintes ( thatas a long time for a turing program ) , 

Just to ket you know , a good way of using code in different places is to send yourself the code in a private message , that way to ca npost it as a file here , and then have hte source in youyr private messages folder  ( ive done it a fewtimes ) to get  stuff from school to home !!11 :D  :D

-----------------------------------
the_short1
Mon Mar 15, 2004 12:18 pm


-----------------------------------
once dan gets back he mioght re enable the attachment mod to emails... and have it delete the attachments every week... but yea... thats smart sending yourself the source///


BTW: you can just download to the temp folder and open??? you NEED to download to ur floppy... that SUCKS...


PS: ... i did that with my school email... i sent big files to myself it has unlikmited attaachment size and inbox size... and its great... i sent myself games and 150 Mp3's/// i had 300 MB still in there... but at the end of the semester the board told me to clean it up.. bring it down... to 50... i understand that...
