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

Username:   Password: 
 RegisterRegister   
 Connect 4 errors but if someone can look at it and let me know whats wrong!
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
customae7




PostPosted: Fri Mar 23, 2007 11:54 am   Post subject: Connect 4 errors but if someone can look at it and let me know whats wrong!

Just copy and paste the game into turing to run it you may have some errors let me know if you can fix them!

Freakman Edit: Please use [code] or [syntax] tags when posting code next time

Turing:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                     CONNECT 4                                     %
%                                      ICS 4M                                       %
%                                    March  2007                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

setscreen ("graphics:max;max")                  %setscreen to full size
setscreen ("offscreenonly")                     %allows smooth animation
setscreen ("nocursor")                          %hides cursor


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                DECLARATION OF CONSTANTS, VARIABLES, AND TYPES                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const radius : int := 25
const player1 : int := red
const player2 : int := black
const none : int := white
const space : int := 10

type boardCell :
    record
        x : int                                 %x coordinate of cell
        y : int                                 %y coordinate of cell
        owner : int                             %colour of cell
    end record



var board : array 1 .. 7, 1 .. 6 of boardCell   %sets the size of board


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                          PROCEDURES AND FUNCTIONS                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure introScreen ()
    %precondition:  none
    %postcondition: intro screen is displayed







end introScreen

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure rules ()
    %precondition:  none
    %postcondition: rules have been displayed








end rules

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure initBoard (var board : array 1 .. 7, 1 .. 6 of boardCell)
    %precondition:  board has been declared
    %postcondition: sets postion of cells and starts them all empty

    for i : 1 .. 7
        for j : 1 .. 6
            %the next two lines set the locations of the cells
            board (i, j).x := 185 + (i * (2 * radius + space)) - radius
            board (i, j).y := 115 + (j * (2 * radius + space)) - radius
            board (i, j).owner := none          %sets cells to empty
        end for
    end for

end initBoard

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure drawBoard (board : array 1 .. 7, 1 .. 6 of boardCell)
    %precondtion:   initBoard had been called
    %postcondtion:  board has been displayed on output screen

    drawbox (185, 115, 615, 485, yellow)        %main part of board

    for i : 1 .. 7
        for j : 1 .. 6
            %Outline cells
            drawoval (board (i, j).x, board (i, j).y, radius, radius, yellow)
        end for

        locatexy (board (i, 1).x, 100)          %displays column numbers below board
        put i
    end for


    drawfill (190, 120, yellow, yellow)         %Fills in board yellow
    drawfillbox (185, 50, 175, 485, blue)       %left leg of board
    drawfillbox (615, 50, 625, 485, blue)       %right leg of board
    drawfillbox (185, 120, 625, 110, blue)      %bottom stopper of board
    View.Update

end drawBoard

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure drawChip (x, y, owner : int)
    %precondtion:   dropchip has been called
    %postcondition: draws chip at location (x,y)

    drawfilloval (x, y, radius, radius, owner)

end drawChip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function findHeight (column : int) : int
    %precondition:  board has been initialized, and row has been entered
    %postcondition: determines which row chip must fall to

    var cellheight : int

    for decreasing j : 6 .. 1

        %Find which row the chip needs to fall to

    end for
    result cellheight
end findHeight

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure drawPreviousChips ()
    %precondition:  board has been initialized
    %postcondition: draws previously played chips
    for i : 1 .. 7
        for j : 1 .. 6
            %only draw cells that have chip in them (reduces flashing)

            %use an if statement to see what chips must be drawn

        end for
    end for
    View.Update

end drawPreviousChips

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

procedure dropChip (column, owner : int)
    %precondition:  isEmpty has been called
    %postcondition: animates chip falling down board
    var columnX : int

    columnX := board (column, 1).x              %determines x-coordinate of chip

    %animates chip dropping
    for decreasing y : 500 .. findHeight (column) by 2
        cls

        drawChip (columnX, y, owner)

        drawBoard (board)
        drawPreviousChips ()


        View.Update
    end for

    board (column, row).owner := owner

end dropChip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function isWinner (board : array 1 .. 7, 1 .. 6 of boardCell) : boolean
    %precondition:  board has been initialized
    %postcondition: determines if there is a winner

    %  Determines if there is a Horizontal winner


    %  Determines if there is a Vertical winner

    %  Determines if there is a Diagonal (up to the right) winner


    %  Determines if there is a Diagonal (up to the left) winner


    %no winner
    result false

end isWinner

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function isEmpty (row : int) : boolean
    %precondition:  board has been initialized, and user has entered column
    %postcondition: returns true if chip can be played in that row

    if board (row, 6).owner = none then         %if top cell is open, chip can be played
        result true
    else
        result false                            %Column is full
    end if

end isEmpty


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                   MAIN PROGRAM                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

var cellheight : int                            %height chip drops to
var turn : int := 0                             %keeps track of who's turn it is
var owner : int := 0                            %whose chip is being played (colour)
var ownerString : string                        %whose chip is being played (word)
var count : int                                 %number of chips played
var font1 : int := Font.New ("sans serif:36:bold")
var tempstr : string

introScreen ()
rules ()

%allows to play more than once
loop
    size1 := 210
    size2 := 210
    count := 0
    initBoard (board)                           %Initialize board
    drawBoard (board)                           %draws board
    %drawStacks (size1, size2)

    %loops single game
    loop

        if turn mod 2 = 0 then                  %Determines who turn it is
            owner := player1                    %sets colour of chip
            ownerString := "Player 1"           %sets name of current player
        else
            owner := player2                    %sets colour of chip
            ownerString := "Player 2"           %sets name of current player
        end if

        getch (input)                           %getch() do not need to hit enter
        if input = "q" or input = "Q" then      %allows players to quit game
            cls
            exit
        end if
        row := strint (input)                   %change input to an integer value


        if row >= 1 and row <= 7 then           %makes sure move is valid
            if isEmpty (row) then               %checks to see if column is empty
                dropChip (row, owner)           %animates chip dropping
                drawPreviousChips ()            %draws previously played chips
                turn := turn + 1                %changes it to next player
                count := count + 1
            end if

            if isWinner (board) then            %checks for winner
                cls
                tempstr := ownerString + " WINS!!!!!!"
                Font.Draw (tempstr, maxx div 2 - 200, maxy div 2 + 60, font1, red)     %displays winner
                View.Update
                exit                            %exit out of game loop
            elsif count = 42 then               %No more cells left
                cls
                put "TIE GAME"
                exit
            end if
        end if

    end loop




    Font.Draw ("press any key to continue", maxx div 2 - 300, maxy div 2 - 60, font1, black)
    View.Update
    getch (input)

    cls

    Font.Draw ("Do you want to play again (y/n)?", maxx div 2 - 350, maxy div 2 - 60, font1, red)
    View.Update
    getch (input)
    exit when input = "n" or input = "N"        %exits out of program

    cls
    View.Update

end loop


cls

Font.Draw ("Thanks for playing!", maxx div 2 - 250, maxy div 2 - 60, font1, black)
View.Update
Sponsor
Sponsor
Sponsor
sponsor
War_Caymore




PostPosted: Fri Mar 23, 2007 1:14 pm   Post subject: Re: Connect 4 errors but if someone can look at it and let me know whats wrong!

nearly every error in your program are simply variables that have not been declared.

Line 152-174 wrote:
procedure dropChip (column, owner : int)
%precondition: isEmpty has been called
%postcondition: animates chip falling down board
var columnX : int

columnX := board (column, 1).x %determines x-coordinate of chip

%animates chip dropping
for decreasing y : 500 .. findHeight (column) by 2
cls

drawChip (columnX, y, owner)

drawBoard (board)
drawPreviousChips ()


View.Update
end for

board (column, row).owner := owner

end dropChip
Line 172 has a variable called row that is not declared.


Line 228-235 wrote:
%allows to play more than once
loop
size1 := 210
size2 := 210
count := 0
initBoard (board) %Initialize board
drawBoard (board) %draws board
%drawStacks (size1, size2)
Line 230 and 231 have variables called size1 and size2 that are undeclared.


Line248 wrote:
getch (input) %getch() do not need to hit enter
another undeclared variable called input.


Line 253 wrote:
row := strint (input) %change input to an integer value
again, row is undeclared.


Line 282-284 wrote:
Font.Draw ("press any key to continue", maxx div 2 - 300, maxy div 2 - 60, font1, black)
View.Update
getch (input)
again, input is undeclared.


There are 2 other errors at line 249 and at 291, but i don't see how those could be wrong. I'm no vetran programmer so myself adding the few variables would most likely give it the windows 98 effect (fix one problem, 8 more pop up). This is all the help i can offer you. I hope this sheds some light on your predicament.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: