Computer Science Canada

Connect 4 check

Author:  cycro1234 [ Mon Jan 17, 2005 8:12 pm ]
Post subject:  Connect 4 check

Here is the code for my connect 4 game that checks to see if there are 4 of a colour vertically, horizontally, and diagonally. However, the diagonally check doesn't work

code:
if whatdotcolour (x, y) = 12 and whatdotcolour (x + 60, y) = 12 and whatdotcolour (x + 120, y) = 12 and whatdotcolour (x + 180, y) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x + 60, y) = 9 and whatdotcolour (x + 120, y) = 9 and whatdotcolour (x + 180, y) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x - 60, y) = 9 and whatdotcolour (x - 120, y) = 9 and whatdotcolour (x - 180, y) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x - 60, y) = 12 and whatdotcolour (x - 120, y) = 12 and whatdotcolour (x - 180, y) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x, y - 70) = 9 and whatdotcolour (x, y - 140) = 9 and whatdotcolour (x, y - 210) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x, y - 70) = 12 and whatdotcolour (x, y - 140) = 12 and whatdotcolour (x, y - 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x, y + 70) = 12 and whatdotcolour (x, y + 140) = 12 and whatdotcolour (x, y + 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x, y + 70) = 9 and whatdotcolour (x, y + 140) = 9 and whatdotcolour (x, y + 210) = 9 then
        blueWon
        %Diagonal check starts here
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x + 60, y + 70) = 12 and whatdotcolour (x + 120, y + 140) = 12 and whatdotcolour (x + 180, y + 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x + 60, y + 70) = 9 and whatdotcolour (x + 120, y + 140) = 9 and whatdotcolour (x + 180, y + 210) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x - 60, y - 70) = 12 and whatdotcolour (x - 120, y - 140) = 12 and whatdotcolour (x - 180, y - 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x - 60, y - 70) = 9 and whatdotcolour (x - 120, y - 140) = 9 and whatdotcolour (x - 180, y - 210) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x + 60, y - 70) = 12 and whatdotcolour (x + 120, y - 140) = 12 and whatdotcolour (x + 180, y - 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x + 60, y - 70) = 9 and whatdotcolour (x + 120, y - 140) = 9 and whatdotcolour (x + 180, y - 210) = 9 then
        blueWon
    elsif whatdotcolour (x, y) = 12 and whatdotcolour (x - 60, y + 70) = 12 and whatdotcolour (x - 120, y + 140) = 12 and whatdotcolour (x - 180, y + 210) = 12 then
        redWon
    elsif whatdotcolour (x, y) = 9 and whatdotcolour (x - 60, y + 70) = 9 and whatdotcolour (x - 120, y + 140) = 9 and whatdotcolour (x - 180, y + 210) = 9 then
        blueWon
    end if


Dam that's alotta code. Is there ANY easier way to check????

Author:  Tony [ Wed Jan 19, 2005 10:17 am ]
Post subject: 

with Math obviously.

for every one dot there are 3 directions in which you could win.

(x,y) (x+1, y), (x+2, y) (x+3,y)
(x,y) (x+1, y+1), (x+2, y+2) (x+3,y+3)
(x,y) (x, y+1), (x, y+2) (x,y+3)

forloop though your grid and watch out for those edges

Author:  cycro1234 [ Wed Jan 19, 2005 12:02 pm ]
Post subject: 

The largest problem I have rite now, is that the check occurs at the exact same time that the piece falls. So it checks at every empty space that the piece falls down. How do i make hte check, check after the pice fell down? I tried moving it outside the loop, but it still does not work.


: