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

Username:   Password: 
 RegisterRegister   
 Connect Four Function
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericfourfour




PostPosted: Sat Mar 24, 2007 1:42 pm   Post subject: Connect Four Function

This function returns true if there is a connect four within a grid. It can actually work with any number of connections (it doesn't have to be 4).

All you have to do is send in the amount of connections, the colour id, the position to search from, and the grid.

Parameters:
amount: the amount of connections in the line required.
id: the number the chip is represented by in the grid.
x: the starting x position.
y: the starting y position.
xInc: leave as 0.
yInc: leave as 0.
grid: the grid containing all of the chip numbers.

Turing:
fcn connect (amount, id, x, y, xInc, yInc : int, grid : array 0 .. *, 0 .. * of int) : boolean

    % If the coordinate is in the grid and the position is the correct id.
    if x >= 0 and x <= upper (grid, 1) and y >= 0 and y <= upper (grid, 2) and id = grid (x, y) then


        % If it has found the correct amount in one line.
        if amount <= 0 then
            result true

            % If it is at the start square.
        elsif xInc = 0 and yInc = 0 then

            % Search:
            result
                connect (amount - 1, id, x, y, 1, 0, grid) or % Right.
                connect (amount - 1, id, x, y, 1, 1, grid) or % Up to the right.
                connect (amount - 1, id, x, y, 0, 1, grid) or % Up.
                connect (amount - 1, id, x, y, -1, 1, grid) or % Up to the left.
                connect (amount - 1, id, x, y, -1, 0, grid) or % Left.
                connect (amount - 1, id, x, y, -1, 1, grid) or % Down to the left.
                connect (amount - 1, id, x, y, 0, -1, grid) or % Down.
                connect (amount - 1, id, x, y, 1, -1, grid) % Down to the right.
        end if

        % Move one position in the line.
        result connect (amount - 1, id, x + xInc, y + yInc, xInc, yInc, grid)
    end if
    result false
end connect


It only searches from the position specified and it only searches in one direction. This means there must be three chips after the one specified with the same id for the function to return true.

Ex.

If you are searching for 4 reds in a row, the grid is given, black is 0, red is 1, and you want to check the chip at (4, 4), you would call the function like this:

Turing:
connect (4, 1, 4, 4, 0, 0, grid)


Here is a sample program (I wasn't sure about the dimensions of the connect four game so I guessed 8x6). It searches every single square and outputs the squares where it finds a connect 4. It also outputs the board so you can see if it is telling the truth.

Turing:
View.Set ("text")
const BLACK := 0
const RED := 1

var grid : array 0 .. 7, 0 .. 5 of int
for i : 0 .. upper (grid, 1)
    for j : 0 .. upper (grid, 2)
        grid (i, j) := Rand.Int (BLACK, RED)
    end for
end for
for decreasing j : upper (grid, 2) .. 0
    put j, " " ..
    for i : 0 .. upper (grid, 1)
        put grid (i, j), " " ..
    end for
    put ""
end for
put " " ..
for i : 0 .. upper (grid, 1)
    put " ", i ..
end for
put ""
for i : 0 .. upper (grid, 1)
    for j : 0 .. upper (grid, 2)
        if connect (4, BLACK, i, j, 0, 0, grid) then
            put "Black: (", i, ", ", j, ")"
        elsif connect (4, RED, i, j, 0, 0, grid) then
            put "Red: (", i, ", ", j, ")"
        end if
    end for
end for


Here is a function that works with the connect function and returns whether or not there is a connection of a specified coloured chip on the grid. This one is just for simplicity.
Turing:
fcn hasConnection (amount, id : int, grid : array 0 .. *, 0 .. * of int) : boolean
    for i : 0 .. upper (grid, 1)
        for j : 0 .. upper (grid, 2)
            if (connect (amount, id, i, j, 0, 0, grid)) then
                result true
            end if
        end for
    end for
    result false
end hasConnection
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: