Computer Science Canada

4 in a row check

Author:  cycro1234 [ Sun Jan 16, 2005 10:38 pm ]
Post subject:  4 in a row check

How would I make a procedure that checks in my connect 4 game if there are 4 of the same colour in a row? Do I use whatdotcolour?

Author:  cycro1234 [ Sun Jan 16, 2005 10:42 pm ]
Post subject: 

I have something like this :

code:
if whatdotcolour (x, y) = 12 and whatdotcolour (x+70, y) = 12 and whatdotcolour (x+140, y) = 12 then
    put "Game Over"
end if


However, there are many possibilities, since the player can get 4 in a row left to right, diagonally, and up and down. Is there a shorter way than this? Or more effective?

Author:  Bacchus [ Sun Jan 16, 2005 11:19 pm ]
Post subject: 

instead of always checking them, when the token you watever you use to mark a space when the player chooses it, i would set an array to the color. then all you have to do is compare arrays

Author:  cycro1234 [ Sun Jan 16, 2005 11:44 pm ]
Post subject: 

How would I do that? An example please? I'm still kinda new to arrays.

Author:  cycro1234 [ Thu Jan 20, 2005 12:33 pm ]
Post subject: 

*Sigh* One day left til it's due, and I'm STILL stuck. I haven't figured out how to make my program work with arrays. ANy help at all would be greatly appreciated. Crying or Very sad

Author:  eNc [ Thu Jan 20, 2005 4:24 pm ]
Post subject: 

cycro1234 wrote:
*Sigh* One day left til it's due, and I'm STILL stuck. I haven't figured out how to make my program work with arrays. ANy help at all would be greatly appreciated. Crying or Very sad


code:

var a : array 1..4 of int
var b : array 1..4 or int

for i : 1..4
        if a(i) = b(i) then
        Do some stuff here
        end if
end for



I don't kno it that will help

Author:  cycro1234 [ Thu Jan 20, 2005 4:43 pm ]
Post subject: 

Ok, I found out where my problem is exactly, but now i need to know how to fix it. I'll describe the problem:

One of my very first pieces of code is:

code:
var y : int := 540


I need this y value to use later on in my code to control the drop of my piece. The drop always starts at 540. I also have a procedure before this called "check" that checks if there are 4 pieces of the same colour in a row. Now here is my procedure (shortened up) that controls the drop:

code:

procedure dropBall (x : int)
if playerTurn = 1 then
   loop
         y := y - 70
         drawoval (x, y + 70, 24, 24, 0)
         drawoval (x, y, 24, 24, 7)
         %If I put the check here, then it will try to check for 4 in a row every time the y value                 decreases.
          %check (x,y)
          exit when y < 130
   end loop
   y := 540 %I need to reset the value of y to 540 so that when       
            %playerTurn = 2, The piece will start at the top. Otherwise, 
            %the next piece  will drop from the height that the previous 
            %piece stopped at.   
   playerTurn = 2

else
     loop
         y := y - 70
         drawoval (x, y + 70, 24, 24, 0)
         drawoval (x, y, 24, 24, 7)
         %If I put the check here, then it will try to check for 4 in a row every time the y value                 decreases.
          %check (x,y)
          exit when y < 130
   end loop
   y := 540 %I need to reset the value of y to 540 so that when       
            %playerTurn = 2, The piece will start at the top. Otherwise, 
            %the next piece  will drop from the height that the previous 
            %piece stopped at.   
   playerTurn = 1
end if
%Normally I should put the check OUTSIDE the if statements. However, since I have the final statement as y := 540, it will perform the check at 540.


Any ideas on how I could possible fix this? My ISP is due tomorrow and I really need help!

Author:  Cervantes [ Thu Jan 20, 2005 5:00 pm ]
Post subject: 

How about adding a line to your check procedure, at the end. Add:
code:

y := 540

And take out the y := 540 in your dropBall procedure.

Author:  cycro1234 [ Thu Jan 20, 2005 5:20 pm ]
Post subject: 

I would, but my check procedure has a y as its variable.

procedure check (x, y: int)

Author:  cycro1234 [ Thu Jan 20, 2005 6:06 pm ]
Post subject: 

What do I do if the procedure has y as one of its parameters?


: