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

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




PostPosted: Tue Dec 12, 2006 2:44 pm   Post subject: Connect 4 Winning Help

I have all the basic workings of connect 4 figured out, but I can't, for the life of me, figure out how to detect if there's 4 in a row.

(spot (i,j) = 1 means said spot is occupied by black, and 2 means it's occupied by red)

code:

View.Set ("graphics:780;590,position:middle;middle,nobuttonbar,offscreenonly")
var font := Font.New ("Comic Sans MS:20")
var playerColor : int
var mx, my, b, sx, playerTurn, column := 0
var spot : array 1 .. 7, 1 .. 6 of int
var row : array 1 .. 7 of int
const width := 570
const height := 490
const radius := 35

for x : 1 .. 7
    for y : 1 .. 6
        spot (x, y) := 0
    end for
end for

for i : 1 .. 7
    row (i) := 1
end for

proc drawBoard
    for y : 1 .. 6
        for x : 1 .. 7
            Draw.Oval ((x - 1) * 80 + (2 * radius) + 25, (y - 1) * 80 + (2 * radius) + 25, radius, radius, 7)
        end for
    end for
    if playerTurn mod (2) not= 1 then
        playerColor := 7
    elsif playerTurn mod (2) not= 0 then
        playerColor := 12
    end if
    drawbox (50, 50, maxx - 160, maxy - 50, 7)
    Draw.Fill (51, 51, yellow, 7)
    Font.Draw ("Player Turn", 630, 550, font, 7)
    Draw.FillOval (700, 500, radius, radius, playerColor)
    Draw.Oval (700, 500, radius, radius, 7)
end drawBoard

procedure drawSpot
    sx := column
    if b = 1 then
        if row (column) = 7 then
            return
        end if
        if playerTurn = 0 then
            spot (sx, row (column)) := 1
        elsif playerTurn = 1 then
            spot (sx, row (column)) := 2
        end if
        if playerTurn = 0 then
            playerTurn := 1
        else
            playerTurn := 0
        end if
        row (column) += 1
    end if
end drawSpot

procedure playerPut
    Mouse.Where (mx, my, b)
    if mx > 50 and mx < 135 then
        Draw.Box (50, 25, 135, maxy - 25, 7)
        column := 1
        drawSpot
    elsif mx > 135 and mx < 215 then
        Draw.Box (135, 25, 215, maxy - 25, 7)
        column := 2
        drawSpot
    elsif mx > 215 and mx < 295 then
        Draw.Box (215, 25, 295, maxy - 25, 7)
        column := 3
        drawSpot
    elsif mx > 295 and mx < 375 then
        Draw.Box (295, 25, 375, maxy - 25, 7)
        column := 4
        drawSpot
    elsif mx > 375 and mx < 455 then
        Draw.Box (375, 25, 455, maxy - 25, 7)
        column := 5
        drawSpot
    elsif mx > 455 and mx < 535 then
        Draw.Box (455, 25, 535, maxy - 25, 7)
        column := 6
        drawSpot
    elsif mx > 535 and mx < maxx - 160 then
        Draw.Box (535, 25, maxx - 160, maxy - 25, 7)
        column := 7
        drawSpot
    else
    end if

    for i : 1 .. 7
        for j : 1 .. 6
            if spot (i, j) = 1 then
                drawfilloval ((i - 1) * 80 + (radius * 2) + 25, (j - 1) * 80 + (radius * 2) + 25, radius - 1, radius - 1, 7)
            end if
        end for
    end for

    for i : 1 .. 7
        for j : 1 .. 6
            if spot (i, j) = 2 then
                drawfilloval ((i - 1) * 80 + (radius * 2) + 25, (j - 1) * 80 + (radius * 2) + 25, radius - 1, radius - 1, 12)
            end if
        end for
    end for
end playerPut

loop
    cls
    drawBoard
    playerPut
    View.Update
    delay (100)
end loop
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Sat Dec 16, 2006 8:42 pm   Post subject: (No subject)

For Connect 4 there is easy Ways to Do This

consider this... there is diagonal and horizontal and vertical ways to get this
you can only get connect 4 within the placement of the peice

so say you place a peice at 4,4 your gonna need to check how many peices are diagonally up and down on left and right, and see if they add up to 4 and same with vertically and horizontal... its good logic just think for a bit draw a few diagrams on paper if that is too help.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: