0

i want the charcter i display in the grid to move upwards when i press 'w' & downwards when i press's' & right when in press'd' & left when i press 'a',,,,i have written a code that doesnt work can anyone help me to find out what is the wrong

// Project !!!.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <conio.h>
#include <cstdlib>

using namespace std;


class Critter
{protected:
        int  x;
        int  y;
        char z;
        char board[20][20];

 public:


        Critter() {};

        void setBoard ()
        {

            for (int j=0; j<20; j++)
            {
               board[0][j]  = '@';
               board[19][j] = '@';
            }

            for (int i=0; i<20; i++)
            {
               board[i][0]  = '@';
               board[i][19] = '@';
            }

            for (int j=1; j<19; j++)
            {
                for (int i=1; i<19; i++)
                {
                    board[i][j] = ' ';
                }
                cout<<endl;
            }
        }

        void displayBoard()
        {
            for (int i=0; i<20; i++)
            {
                for (int j=0; j<20; j++)
                {
                    cout<< board[i][j];
                }
                cout<<endl;
            }
        }


        int set1st(int x, int y, char z)
        {
            int l = x;
            int m = y;
            char n = z;
            setBoard();
            board[l][m] = n;
            displayBoard();
            return x,y;

        };


        void move(){
            int t = 1;
            t = getch();
            if(t == 'w') moveup   (x, y);
            if(t == 's') movedown (x, y);
            if(t == 'd') moveright(x, y);
            if(t == 'a') moveleft (x, y);
        };


        void moveup(int x, int y)
        {
            x = x;
            y = y - 1;

            set1st(x, y, 'X');
        };

        void movedown(int x, int y)
        {
            x = x;
            y = y + 1;
        };

        void moveright(int x, int y)
        {
            x = x + 1;
            y = y;
        };

        void moveleft(int x, int y)
        {
            x = x - 1;
            y = y;
        };


        /*void breed();
        void starve();*/

};


/*class Predator : public Critter
{


};

class Prey : public Critter
{



};*/



int main()
{
    Critter c;

    c.set1st(5, 5, 'X');

    while(true)
    {
        c.move();
        system ("CLS");
    }

	return 0;
}
4
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by eng hassan
Featured Replies
  • the problem is with your set1st function. you need to set the member variables to the inputted variables. Also it should be a void function. [code=c++] void set1st(int x1, int y1, char z1) { x = x1; y = y1; z = z1; setBoard(); board[y][x] = z; displayBoard(); } [/code] … Read More

0

the code doesnt make the charcter move & it makes an overflow & ends the program,,,,so i want 2 know what is wrong or what i can do to fix this

0

One thing I see is that you are not checking the bounds of the movement - i.e.

void movedown(int x, int y)
        {
            x = x;
            y = y + 1;
        };

Should be something like:

void movedown(int x, int y)
{
  // x = x; //this line doesn't do anything, so it can be removed.
  if(y < maxY)
  {
    y = y + 1;
  }
};

Once you fix those, can you use a debugger to step though the code and tell us when the error occurs and what the error says?

Good luck,

Dave

0

Your problem is in your move functions. You have the members of the class x and y, then you have a local x and y in the move functions, and you aren't doing anything to distinguish the two. In reality, the member x and y should be enough. You should use those to handle all the location changing and referencing for your "Critter." Also, just for your information, l and m should be switched in the assignment in set1st() to get the desired result.

Edited by c++noobie: n/a

0

write now i have done what u have told me & the problem is the same,,,,no errors appears but when the program starts the grid appears & in it thier is a char. 'x' & whe i press any move key the grid disappears,,,,so what do u thing is wrong??

1

the problem is with your set1st function. you need to set the member variables to the inputted variables. Also it should be a void function.

void set1st(int x1, int y1, char z1)
        {
            x = x1;
            y = y1;
            z = z1;
            setBoard();
            board[y][x] = z;
            displayBoard();
        }

Then in your move function you don't need to pass the variables to it just use the member variables.

void moveup()
{
     if (y < 2)
     {
         set1st(x, y, 'X');
         return;
     }
     set1st(x, y - 1, 'X');
}

Also you should move the system("CLS"); to right after getch(t); in your move function. This should get you on your way.

0

the problem is same,,,,the grid keps on disappearing when i press any move key

0

really really thx,,,,i have resolved the problem & the char moves properly now,,,the problem.,,,thank u very much:):)

0

Please mark the thread as solved. It would also be nice to write a brief description of what you ended up doing to solve it so that the next reader can share your new knowledge.

Dave

0

I would also be interested in seeing your completed code just to compare with what I did with your code. If not I understand.

0

well tom i will post the code concerning this part of my program,,,,& when i finish all of it i shall post it to but i am still facing some problems that i am working on it,,,,as the program i am trying to make is a predators-preys game i have posted its rules in another thread name by "predators-preys game" u can hava a look if u r interested

0

& i wont mark this thread as solved as i shall need help concerning the code,,,,as i am adding new functions & conditions to it & i may need more help:S

0

That should be in a separate thread. Since the question you asked has been answered then you should mark the thread as solved. Then if you have another question about the same code you start a new thread with a title that explains the problem

0
// Project !!!.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <windows.h>

using namespace std;


class Critter
{protected:
        int  x;
        int  y;
        char z;
        char board[20][20];
        int count;

 public:

        Critter()
        {
            count = 0;
        };

        void setBoard ()
        {

            for (int j=0; j<20; j++)
            {
               board[0][j]  = '@';
               board[19][j] = '@';
            }

            for (int i=0; i<20; i++)
            {
               board[i][0]  = '@';
               board[i][19] = '@';
            }

            for (int j=1; j<19; j++)
            {
                for (int i=1; i<19; i++)
                {
                    board[i][j] = ' ';
                }
                cout<<endl;
            }

        }

        void displayBoard()
        {
            for (int i=0; i<20; i++)
            {
                for (int j=0; j<20; j++)
                {
                    cout<< board[i][j];
                }
                cout<<endl;
            }
        }

       void set1st(int x, int y, char z)
        {
            setBoard();
            board[x][y] = z;
            displayBoard();
        }



        void set2nd(int x, int y)
        {
            int l = x;
            int m = y;
            setBoard();
            board[l][m] = 'X';
            displayBoard();

        };


        void move()
        {
            while (1)
            {
                int t = 1;
                t = getch();

                system ("CLS");

                if( count == 12 )
                {
                    set1st(x, y, ' ');
                    break;
                }

                else if(t == 27)
                {
                    exit(1);
                }
                else if(t == 72)
                {
                    moveup(x, y);
                }
                else if(t == 80)
                {
                    movedown(x, y);

                }
                else if(t == 77)
                {
                    moveright(x, y);

                }
                else if(t == 75)
                {
                    moveleft(x, y);

                }
                count++;
            }
        };


        void moveup(int x, int y)
        {
            if(x<18 && y<18)
                set1st(x, y-1, 'X');

            if(x==18 || y==18)
                set1st(x, y-1, 'X');

            if(x == 18)
                set1st(x, y-1, 'X');

            if(y == 1)
                set1st(x, y, 'X');

        };

        void movedown(int x, int y)
        {
            if(x<18 && y<18)
                set1st(x, y+1, 'X');

            if(y == 18)
                set1st(x, y, 'X');

            if(x == 18 && y != 18)
                set1st(x, y+1, 'X');

            if(x == 1 && y != 18)
                set1st(x, y+1, 'X');

        };

        void moveright(int x, int y)
        {
            if(x<18 && y<18)
                set1st(x+1, y, 'X');

            if(x == 18)
                set1st(x, y, 'X');

            if(y == 18 && x != 18)
                set1st(x+1, y, 'X');

            if(x == 1)
                set1st(x+1, y, 'X');

        };

        void moveleft(int x, int y)
        {
            if(x<18 && y<18)
                set1st(x-1, y, 'X');

            if(x==18 || y==18)
                set1st(x-1, y, 'X');

            if(y == 1)
                set1st(x-1, y, 'X');

            if(x == 1)
                set1st(x, y, 'X');

        };


};


class Predator : public Critter
{private:
        int  x;
        int  y;
        char z;
        int count;

 public:
        void move  ();
        void starve()
        {
            if(count == 12)
                set1st(x, y, ' ');
        }
};

/*class Prey : public Critter
{



};*/



int main()
{
    Predator scorpion;
    scorpion.set1st(5, 5, 'X');


    scorpion.move();

	return 0;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.