Computer Science Canada

Connect 4 game variable problems

Author:  IBeMily [ Tue Oct 31, 2017 12:38 pm ]
Post subject:  Connect 4 game variable problems

I am trying to fix a problem with my variables

Turing:
% Global variables
var mx, my, mbut : int % mbut = 1 if clicked
var board : array 1 .. 6, 1 .. 7 of int % connect 4 board (6 rows, 7 columns)
for row : 1 .. 6
    for col : 1 .. 7
        board (row, col) := 0 % 0 = empty, 1 = red, 2 = yellow
        var column : int
        var player : int

    end for
end for

% procedures and functions
proc updateBoard
    var row : int := 6
    var x, y : int
    var done : boolean := false
    loop
        if board (row, column) = 0 then %empty spot
            x := (column - 1) * 100 + 372
            y := (row - 1) * 100 - 27
            if player = 1 then % red
                Draw.FillOval (x, y, 40, 40, brightred)
            elsif player = 2 then % yellow
                Draw.FillOval (x, y, 40, 40, yellow)
            end if
            delay (50) % wait
            Draw.FillOval (x, y, 40, 40, white) % erase
            row := row - 1
        else
            if board (row, column) = 1 then % red
                Draw.FillOval (x, y, 40, 40, brightred)
            elsif board (row, column) = 2 then % yellow
                Draw.FillOval (x, y, 40, 40, yellow)
            end if
            done := true
        end if
        exit when done = true
    end loop
end updateBoard

proc drawBoard
    var row, col : int
    Draw.FillBox (322, 23, 1022, 623, blue)
    for y : 73 .. 573 by 100
        for x : 372 .. 972 by 100
            row := (y - 23) div 100 + 1
            col := (x - 322) div 100 + 1
            if board (row, col) = 0 then % empty
                Draw.FillOval (x, y, 40, 40, white)
            elsif board (row, col) = 1 then % red
                Draw.FillOval (x, y, 40, 40, brightred)
            end if
        end for
    end for
end drawBoard

proc updateMemory
    var row, col : int
    row := (my - 23) div 100 + 1
    col := (mx - 322) div 100 + 1
    if board (row, col) = 0 then %empty spot
        board (row, col) := 1
    end if
end updateMemory

proc drawChip
    Draw.FillOval (mx, my, 40, 40, red)
end drawChip

% mainline
drawBoard
loop
    Mouse.Where (mx, my, mbut)
    locate (1, 1)
    put mx, " ", my, " ", mbut
    if mbut = 1 then
        %drawChip
        updateMemory
        drawBoard
    end if
end loop




My variables column and player aren't working. Do you have a suggestion. I've tried and failed to incorporate the new variables, but it didn't work

Turing 4.1.1

Author:  Clayton [ Tue Oct 31, 2017 2:13 pm ]
Post subject:  RE:Connect 4 game variable problems

The issue you are running into has to do with a concept known as scope in programming. Look at where you've declared your variables. Where should they be able to be seen to the rest of the program?


: