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

Username:   Password: 
 RegisterRegister   
 error message help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sammi7




PostPosted: Thu Dec 08, 2011 6:07 pm   Post subject: error message help

What is it you are trying to achieve?
<im trying to make the game Snake and add in bonus food to make the snake grow more


What is the problem you are having?
<whenever i try and eat the bonus food(blue) it says variable has no value and i dont know how to fix it.>


Describe what you have tried to solve this problem
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:

View.Set ("graphics,offscreenonly")

var segX : array 1 .. 1000 of int % x-position of segments
var segY : array 1 .. 1000 of int % y-position of segments

var foodX : int  % x-position of the food
foodX := Rand.Int (30, 300)
var foodY : int % y-position of the food
foodY := Rand.Int (100, 300)

%BONUS FOOD
var bonusFoodX : int
bonusFoodX := Rand.Int (30, 300)
var bonusFoodY : int
bonusFoodY := Rand.Int (30, 300)

var numSegs : int % number of segments on the snake

% 1 - up, 2 - right, 3 - down, 4 - left
var direction : int
direction := 2


var keys : array char of boolean


Input.KeyDown (keys)
numSegs := 3 % starting number of segments

% STARTING POSITION OF THE 3 SEGMENTS
segX (1) := 200
segY (1) := 100
segX (2) := 180
segY (2) := 100
segX (3) := 180
segY (3) := 80

loop
    Input.KeyDown (keys) % update keyboard info

    % DRAWING THE BODY
    for i : 2 .. numSegs
        Draw.FillOval (segX (i), segY (i), 10, 10, black)
    end for

    %DRAWING THE HEAD
    Draw.FillOval (segX (1), segY (1), 10, 10, 13)

    % DRAWING THE FOOD
    Draw.FillOval (foodX, foodY, 12, 12, brightred)
    Draw.FillOval (bonusFoodX, bonusFoodY, 12, 12, blue)

    % user input
    if keys (KEY_UP_ARROW) = true then % moving the snake up
        direction := 1
    elsif keys (KEY_RIGHT_ARROW) = true then % moving the snake right
        direction := 2
    elsif keys (KEY_DOWN_ARROW) = true then % movng the snake down
        direction := 3
    elsif keys (KEY_LEFT_ARROW) = true then % moving the snake left
        direction := 4
    end if

    % To do : CHECK IF IT HIT THE WALLS
    if segX (1) >= maxx then
        segX (1) := -7

    elsif segX (1) <= -7 then
        segX (1) := maxx - 7
    end if

    if segY (1) >= maxy then
        segY (1) := -7

    elsif segY (1) <= -7 then
        segY (1) := maxy - 7
    end if

    % check if food is eaten and move the food
    if whatdotcolour (segX (1), segY (1)) = brightred then % TODO: actually check real food
        numSegs := numSegs + 1 % adding a segment when food is eaten
        foodX := Rand.Int (30, 400) % moving the food
        foodY := Rand.Int (60, 300) % moving the food
    end if
    %check if hit bonus food
    if whatdotcolour (segX (1), segY (1)) = blue then
        numSegs := numSegs + 3
        bonusFoodX := Rand.Int (30, 400) % moving the food
        bonusFoodY := Rand.Int (60, 300) % moving the food
    end if
   
    % MOVEMENT
    % Move Body
    for decreasing i : numSegs .. 2
        segX (i) := segX (i - 1)
        segY (i) := segY (i - 1)
    end for

    % Move Head
    if direction = 1 then     % up
        segY (1) := segY (1) + 20
    elsif direction = 2 then     % right
        segX (1) := segX (1) + 20
    elsif direction = 3 then     % down
        segY (1) := segY (1) - 20
    elsif direction = 4 then     % left
        segX (1) := segX (1) - 20
    end if

    % TODO: check if hit self
    if whatdotcolour (segX (1), segY (1)) = black then
        numSegs := 3
        delay (200)
    end if

    View.Update ()
    delay (100)
    cls ()
end loop



Please specify what version of Turing you are using
<4.1>
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Dec 08, 2011 6:11 pm   Post subject: Re: error message help

sammi7 @ Thu Dec 08, 2011 6:07 pm wrote:
it says variable has no value and i dont know how to fix it.

You should assign a value to that variable.

A more helpful question -- what do you think the value of the variable is, when it's accessed? Well, it turns out that it didn't have any value assigned before use. E.g.
code:

var value : int
put 5 + value

What should the answer be? Obviously that doesn't make sense until variable actually holds some value. Trace back through your program to see what's going on.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sammi7




PostPosted: Thu Dec 08, 2011 6:53 pm   Post subject: RE:error message help

but i already declared it at the top. and when i tried taking out the part about the whatdotclour for the bonus food it worked....Why does it only happen when i add the bonus food check in?
Tony




PostPosted: Thu Dec 08, 2011 7:00 pm   Post subject: RE:error message help

sure, I also declared the variable called "value" at the top of my example. What's the output?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sammi7




PostPosted: Thu Dec 08, 2011 7:07 pm   Post subject: RE:error message help

ya i know but it works with the original red food....thats what i dont understand.....How do i make it work with both foods?
Tony




PostPosted: Thu Dec 08, 2011 7:19 pm   Post subject: RE:error message help

well, you are doing something to grow the snake with regular food (assigning values to variables) that you are not doing to bonus food (variable doesn't get assigned a value before it is used).

Though looking at your implementation, it could be that regular food works just by a coincidence.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: