Computer Science Canada

Passing Variable and Saving Changes

Author:  Prince Pwn [ Mon Nov 23, 2009 11:54 pm ]
Post subject:  Passing Variable and Saving Changes

When I pass my variables (turtoisePos and harePos) through my MoveCreatures function, the work gets done inside the function but afterwards, the variable resets to it's default value. How can I fix this?

c:

/********************************
* Name:   Tortoise and the Hare *
* Author:                 *
* Date:   11/23/2009            *
********************************/



// VARIABLE POSITIONS KEEP RESETTING, POINTER?? ----------------------

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;

// Prototypes
void MoveCreatures(string,string,string[],string[],int,int,int);
void DrawTrack(int,int,int);
void CheckWinner(int,int,int);

int main()
{
        const int trackSize = 70;         // Size of the track
        string track[trackSize];          // The track array

        // Starting positions
        int tortoisePos = 1;
        int harePos = 1;

        // Move Sets
        string tortoiseMove[] = {"Fast plod", "Slip", "Slow plod"};
        string hareMove[] = {"Sleep", "Big hop", "Big slip", "Small hop", "Small slip"};

        // Current moves
        string currentTortoiseMove = "";
        string currentHareMove = "";
                               
        for (int i = 1; i <= trackSize - 1; i++)                                                                                                                                                     // Counts to the track size
                track[i] = ' ';                             // Creates an empty track

        cout << "BANG !!!!!" << endl << "AND THEY'RE OFF !!!!!" << endl << endl;                                                                                         // Starting message
        srand((unsigned)time(NULL));                                                                                         // Starts the random number generator

        //while (tortoisePos || harePos != trackSize)                                                          // While no one has finished
        for (int i=1; i<=10;i++)
        {
                MoveCreatures(currentTortoiseMove, currentHareMove, tortoiseMove, hareMove, tortoisePos, harePos, trackSize);    // Move the creature                                                                                               
                CheckWinner(tortoisePos, harePos, trackSize);                                                    // Checks for a winner
        }

        cout << endl << endl;
        system ("pause");

        return 0;
}

void MoveCreatures (string CURRENT_TORTOISE_MOVE, string CURRENT_HARE_MOVE, string TORTOISE_MOVE[], string HARE_MOVE[], int TORTOISE_POS, int HARE_POS, int TRACK_SIZE)
{

        int move;                                                                          // Used for character movement
       
        /* Determines moves */
        move = rand() % 10 + 1;           // Random number from 1 to 10
        if (move >= 1 && move <= 5)                                   // 50% Fast Plod
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[0];       
        else if (move >= 6 && move <= 7)                                        // 20% Slip
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[1];       
        else if (move >= 8 && move <= 10)                                   // 30% Slow plod
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[2];       

        move = rand() % 10 + 1;        // Random number from 1 to 10
        if (move >= 1 && move <= 2)                                   // 20% Snooze
                        CURRENT_HARE_MOVE = HARE_MOVE[0];                     
        else if (move >= 3 && move <= 4)                                        // 20% Big hop
                        CURRENT_HARE_MOVE = HARE_MOVE[1];                     
        else if (move == 5)                                             // 10% Big slip
                        CURRENT_HARE_MOVE = HARE_MOVE[2];                 
        else if (move >= 6 && move <= 8)                                        // 30% Small hop
                        CURRENT_HARE_MOVE = HARE_MOVE[3];                     
        else if (move >= 9 && move <= 10)                                   // 20% Small slip
                        CURRENT_HARE_MOVE = HARE_MOVE[4];                     

        /* Moving the creatures */
        if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[0])      // Moves turtle to the right 3 spaces
                TORTOISE_POS += 3;                                               
        else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[1]) // Moves turtle to the left 6 spaces
                TORTOISE_POS -= 6;                                               
        else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[2]) // Moves turtle to the right 1 space
                TORTOISE_POS += 1;                                               

        if (CURRENT_HARE_MOVE == HARE_MOVE[0])        // Hare is snoozing
                HARE_POS = HARE_POS;                           
        else if (CURRENT_HARE_MOVE == HARE_MOVE[1])               // Moves hare to the right 9 spaces
                HARE_POS += 9;                 
        else if (CURRENT_HARE_MOVE == HARE_MOVE[2])               // Moves hare to the left 12 spaces
                HARE_POS -= 12;         
        else if (CURRENT_HARE_MOVE == HARE_MOVE[3])               // Moves hare to the right 1 space
                HARE_POS += 1;                 
        else if (CURRENT_HARE_MOVE == HARE_MOVE[4])               // Moves hare to the left 2 spaces
                HARE_POS -= 2

        /* Boundaries */
        if (TORTOISE_POS < 1)                        // Makes sure turtle doesn't go too far left                                                        
                TORTOISE_POS = 1;

        if (TORTOISE_POS >= TRACK_SIZE)      // Makes sure turtle doesn't go too far right
                TORTOISE_POS = TRACK_SIZE;
       
        if (HARE_POS < 1)                                                               // Makes sure bunny doesn't go too far left
                HARE_POS = 1;
        if (HARE_POS >= TRACK_SIZE)                                   // Makes sure bunny doesn't go too far left
                HARE_POS = TRACK_SIZE;

       
        cout << "T: " << TORTOISE_POS << ' ' << CURRENT_TORTOISE_MOVE << "\nH: " << HARE_POS << ' ' << CURRENT_HARE_MOVE << endl << endl;
       

        //DrawTrack(TORTOISE_POS, HARE_POS, TRACK_SIZE);                // Draws the track
}

void DrawTrack(int TORTOISE_POS, int HARE_POS, int TRACK_SIZE)
{
        for (int i=1; i <= TRACK_SIZE; i++)
        {       
                if (TORTOISE_POS && HARE_POS == i)
                        cout << "O";
                else if (HARE_POS == i)
                    cout << 'H';
                else if (TORTOISE_POS == i)
                        cout << 'T';
                else
                        cout << ' ';
        }
        cout << endl;
}

void CheckWinner(int TORTOISE_POS, int HARE_POS, int TRACK_SIZE)
{
        if (TORTOISE_POS == TRACK_SIZE)
                cout << "TORTOISE WINS!!! YAY!!!";
        else if (HARE_POS == TRACK_SIZE)
                cout << "Hare Wins. Yuch!";
        else if (TORTOISE_POS == TRACK_SIZE && HARE_POS == TRACK_SIZE)
                cout << "It's a tie!";
        if (TORTOISE_POS == TRACK_SIZE || HARE_POS == TRACK_SIZE)
                system ("exit");
}

Author:  Tony [ Tue Nov 24, 2009 12:31 am ]
Post subject:  Re: Passing Variable and Saving Changes

Prince Pwn @ Mon Nov 23, 2009 11:54 pm wrote:
When I pass my variables (turtoisePos and harePos)

No, you are passing your variables' values.

Remember -- Pass by value vs. Pass by reference.

Author:  Prince Pwn [ Tue Nov 24, 2009 12:32 am ]
Post subject:  RE:Passing Variable and Saving Changes

OK.

Update: Thanks duh, it worked Very Happy

Here's the code for those who want it:

c:

/********************************
* Name:   Tortoise and the Hare *
* Author:                 *
* Date:   11/23/2009            *
********************************/


#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

// Prototypes
void MoveAnimals(string,string,string[],string[],int&,int&,int);
void DrawTrack(int,int,int);
void CheckWinner(int,int,int,bool&);

int main()
{
        const int trackSize = 70;         // Size of the track

        // Starting positions
        int tortoisePos = 1;
        int harePos = 1;

        bool winner = false;              // If a winner is found

        // Move Sets
        string tortoiseMove[] = {"Fast plod", "Slip", "Slow plod"};
        string hareMove[] = {"Sleep", "Big hop", "Big slip", "Small hop", "Small slip"};

        // Current moves
        string currentTortoiseMove = "";
        string currentHareMove = "";

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);                                                        // Grey
        cout << "BANG !!!!!" << endl << "AND THEY'RE OFF !!!!!" << endl << endl;                                                                                        // Starting message
        srand((unsigned)time(NULL));                                                                                        // Starts the random number generator
                                                                                                                                               
        while (!winner)                          // While no one has finished
        {
                MoveAnimals(currentTortoiseMove, currentHareMove, tortoiseMove, hareMove, tortoisePos, harePos, trackSize);          // Move the creature                
                CheckWinner(tortoisePos, harePos, trackSize, winner);                                             // Checks for a winner
        }

        cout << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);                                                        // White
        system ("pause");

        return 0;
}

void MoveAnimals (string CURRENT_TORTOISE_MOVE, string CURRENT_HARE_MOVE, string TORTOISE_MOVE[], string HARE_MOVE[], int& TORTOISE_POS, int& HARE_POS, int TRACK_SIZE)
{

        int move;                                                                          // Used for character movement
       
        /* Determines moves */
        move = rand() % 10 + 1;           // Random number from 1 to 10
        if (move >= 1 && move <= 5)                                   // 50% Fast Plod
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[0];       
        else if (move >= 6 && move <= 7)                                        // 20% Slip
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[1];       
        else if (move >= 8 && move <= 10)                                   // 30% Slow plod
                        CURRENT_TORTOISE_MOVE = TORTOISE_MOVE[2];       

        move = rand() % 10 + 1;        // Random number from 1 to 10
        if (move >= 1 && move <= 2)                                   // 20% Snooze
                        CURRENT_HARE_MOVE = HARE_MOVE[0];                     
        else if (move >= 3 && move <= 4)                                        // 20% Big hop
                        CURRENT_HARE_MOVE = HARE_MOVE[1];                     
        else if (move == 5)                                             // 10% Big slip
                        CURRENT_HARE_MOVE = HARE_MOVE[2];                 
        else if (move >= 6 && move <= 8)                                        // 30% Small hop
                        CURRENT_HARE_MOVE = HARE_MOVE[3];                     
        else if (move >= 9 && move <= 10)                                   // 20% Small slip
                        CURRENT_HARE_MOVE = HARE_MOVE[4];                     

        /* Moving the creatures */
        if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[0])      // Moves turtle to the right 3 spaces
                TORTOISE_POS += 3;                                               
        else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[1]) // Moves turtle to the left 6 spaces
                TORTOISE_POS -= 6;                                               
        else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE[2]) // Moves turtle to the right 1 space
                TORTOISE_POS += 1;                                               

        if (CURRENT_HARE_MOVE == HARE_MOVE[0])        // Hare is snoozing
                HARE_POS = HARE_POS;                           
        else if (CURRENT_HARE_MOVE == HARE_MOVE[1])               // Moves hare to the right 9 spaces
                HARE_POS += 9;                 
        else if (CURRENT_HARE_MOVE == HARE_MOVE[2])               // Moves hare to the left 12 spaces
                HARE_POS -= 12;         
        else if (CURRENT_HARE_MOVE == HARE_MOVE[3])               // Moves hare to the right 1 space
                HARE_POS += 1;                 
        else if (CURRENT_HARE_MOVE == HARE_MOVE[4])               // Moves hare to the left 2 spaces
                HARE_POS -= 2

        /* Boundaries */
        if (TORTOISE_POS < 1)                        // Makes sure turtle doesn't go too far left                                                        
                TORTOISE_POS = 1;

        if (TORTOISE_POS >= TRACK_SIZE)      // Makes sure turtle doesn't go too far right
                TORTOISE_POS = TRACK_SIZE;
       
        if (HARE_POS < 1)                                                               // Makes sure bunny doesn't go too far left
                HARE_POS = 1;
        if (HARE_POS >= TRACK_SIZE)                                   // Makes sure bunny doesn't go too far left
                HARE_POS = TRACK_SIZE;
       
        DrawTrack(TORTOISE_POS, HARE_POS, TRACK_SIZE);    // Draws the track
}

void DrawTrack(int TORTOISE_POS, int HARE_POS, int TRACK_SIZE)
{
        for (int i=1; i <= TRACK_SIZE; i++)                                                  // Scans through the track
        {       
                if (HARE_POS == i && TORTOISE_POS != i)        // Prints an H if the hare is there
                {
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    // White
                    cout << 'H';
                }
                if (TORTOISE_POS == i && HARE_POS != i)        // Prints a T if the tortoise is there
                {
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);    // Green
                        cout << 'T';   
                }
                if (TORTOISE_POS == i && HARE_POS == i)        // Collision!
                {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);        // Brown
                        cout << "OUCH!!!";
                }
                else                                                                // Open area
                        cout << ' ';                               
        }
        cout << endl << endl;
}

void CheckWinner(int TORTOISE_POS, int HARE_POS, int TRACK_SIZE, bool& WINNER)
{
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);    // Grey
        if (TORTOISE_POS == TRACK_SIZE)         // Checks if tortoise is at the end
        {
                cout << "TORTOISE WINS!!! YAY!!!\n";
                WINNER = true;
        }
        else if (HARE_POS == TRACK_SIZE)                                                                // Checks if hare is at the end
        {
                cout << "Hare Wins. Yuch!\n";
                WINNER = true;
        }
        else if (TORTOISE_POS == TRACK_SIZE && HARE_POS == TRACK_SIZE)  // If both have reached the finish line
                cout << "It's a tie!\n";               
}


: