Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Connect 4 check
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cycro1234




PostPosted: 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????
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jan 19, 2005 10:17 am   Post subject: (No 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
cycro1234




PostPosted: Wed Jan 19, 2005 12:02 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: