0
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

char board[20][20];

void setBoard ()
{
    for (int j=0; j<20; j++)
            {
               board[0][j]  = 219;
               board[19][j] = 219;
            }

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

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

struct objects
{
    int  x_coord;
    int  y_coord;
    char z_dep  ;
    int  counter;
};

class Scorpion
{
 private:
    int  x_x;
    int  x_y;
    char x_z;
    int  x_c;
    int count1;
    int flag1;

 public:
    objects scorpionz [200];
    Scorpion()
    {
        count1 = 0;
        flag1  = 0;
        for (int i=0; i<200; i++)
        {
            scorpionz[i].x_coord =  0 ;
            scorpionz[i].y_coord =  0 ;
            scorpionz[i].z_dep   = ' ';
            scorpionz[i].counter =  0 ;
        }
    }

    void setArray (int l, int m, char n, int index1, int cntr)
    {
        scorpionz[index1].x_coord =  l  ;
        scorpionz[index1].y_coord =  m  ;
        scorpionz[index1].z_dep   =  n  ;
        scorpionz[index1].counter = cntr;
    }

    void move (int x_x, int x_y, int index2, int x_c)
    {
        int g = 1;
        int h = 1;
        g = getch();
        h = getch();

        x_x = scorpionz[index2].x_coord;
        x_y = scorpionz[index2].y_coord;
        x_z = scorpionz[index2].z_dep  ;
        x_c = scorpionz[index2].counter;

        if( h == 27 )
        {
            system ("CLS");
            cout<<endl<<"\n"<<"\n"<<"\n"<< "                     Thank You For Playing!"<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            exit(1);
        }

        if( h == 72 )
        {
            if(x_x > 1)
            {
                moveUp(x_x, x_y, index2, x_c);
                flag1 = 1;
                count1++;
            }
        }

        if( h == 80 )
        {
            if(x_x < 18)
            {
                moveDown(x_x, x_y, index2, x_c);
                flag1 = 2;
                count1++;
            }
        }
        if( h == 77)
        {
            if(x_y < 18)
            {
                moveRight(x_x, x_y, index2, x_c);
                flag1 = 3;
                count1++;
            }
        }
        if( h == 75 )
        {
            if(x_y > 1)
            {
                moveLeft(x_x, x_y, index2, x_c);
                flag1 = 4;
                count1++;
            }
        }
    }

    void moveUp( int x_x, int x_y, int index2, int x_c)
    {
        if(/*(board[x_x-1][x_y] == ' ' || board[x_x-1][x_y] == Bugs || board[x_x-1][x_y] == Ants) &&*/ board[x_x][x_y+1] != 12)
        {
            board[x_x][x_y] = ' ';
            x_c = x_c + 1;
            setArray ((x_x-1), x_y, 12, index2, x_c);
            board[x_x-1][x_y] = 12;
            x_x = x_x - 1;
            //delete Bug or Ant object in array
        }
        else
        {
            x_c = x_c + 1;
            setArray (x_x, (x_y), 12, index2, x_c);
            board[x_x][x_y] = 12;
        }
    }

    void moveDown( int x_x, int x_y, int index2, int x_c)
    {
        if(/*(board[x_x+1][x_y] == ' ' || board[x_x+1][x_y] == Bugs || board[x_x+1][x_y] == Ants) &&*/ board[x_x][x_y+1] != 12)
        {
            board[x_x][x_y] = ' ';
            x_c = x_c + 1;
            setArray ((x_x+1), x_y, 12, index2, x_c);
            board[x_x+1][x_y] = 12;
            x_x = x_x + 1;
            //delete Bug or Ant object in array
        }
        else
        {
            x_c = x_c + 1;
            setArray (x_x, (x_y), 12, index2, x_c);
            board[x_x][x_y] = 12;
        }
    }

    void moveRight( int x_x, int x_y, int index2, int x_c)
    {
        if(/*(board[x_x][x_y+] == ' ' || board[x_x][x_y+1] == Bugs || board[x_x][x_y+1] == Ants) &&*/ board[x_x][x_y+1] != 12)
        {
            board[x_x][x_y] = ' ';
            x_c = x_c + 1;
            setArray (x_x, (x_y+1), 12, index2, x_c);
            board[x_x][x_y+1] = 12;
            x_y = x_y + 1;
            //delete Bug or Ant object in array
        }
        else
        {
            x_c = x_c + 1;
            setArray (x_x, (x_y), 12, index2, x_c);
            board[x_x][x_y] = 12;
        }
    }

    void moveLeft( int x_x, int x_y, int index2, int x_c)
    {
        if(/*(board[x_x][x_y+1] == ' ' || board[x_x][x_y+1] == Bugs || board[x_x][x_y+1] == Ants) && */board[x_x][x_y+1] != 12)
        {
            board[x_x][x_y] = ' ';
            x_c = x_c + 1;
            setArray (x_x, (x_y-1), 12, index2, x_c);
            board[x_x][x_y-1] = 12;
            x_y = x_y - 1;
            //delete Bug or Ant object in array
        }
        else
        {
            x_c = x_c + 1;
            setArray (x_x, (x_y), 12, index2, x_c);
            board[x_x][x_y] = 12;
        }
    }

    void die (int x_x, int x_y, int index1, int counter)
    {

        x_x     = scorpionz[index1].x_coord;
        x_y     = scorpionz[index1].y_coord;
        counter = scorpionz[index1].counter;

        if( scorpionz[index1].counter == 12 )
        {
            setArray ( 0 , 0 , ' ' , index1, 0);
            board[x_x][x_y] = ' ';
        }
    }

    void breed(int x_x, int x_y, int index1, int x_c)
    {
        if( scorpionz[index1].counter % 24 == 0 )
        {
            x_x = scorpionz[index1].x_coord;
            x_y = scorpionz[index1].y_coord;
            x_c = scorpionz[index1].counter;

            int t;
            for(int j=0; j<200; j++)
            {
                if(scorpionz[j].x_coord == 0)
                {
                    t = j;
                    break;
                }
            }
            if(board[x_x+1][x_y] == ' ' )
            {
                x_x = x_x + 1;
                scorpionz[t].x_coord = x_x;
                scorpionz[t].y_coord = x_y;
                scorpionz[t].counter =  0 ;
                board[x_x][x_y] = 12;
            }
            else if(board[x_x][x_y+1] == ' ' )
            {
                x_x = x_y + 1;
                scorpionz[t].x_coord = x_x;
                scorpionz[t].y_coord = x_y;
                scorpionz[t].counter =  0 ;
                board[x_x][x_y] = 12;
            }
            else if(board[x_x-1][x_y] == ' ' )
            {
                x_x = x_x - 1;
                scorpionz[t].x_coord = x_x;
                scorpionz[t].y_coord = x_y;
                scorpionz[t].counter =  0 ;
                board[x_x][x_y] = 12;
            }
            else if(board[x_x][x_y-1] == ' ' )
            {
                x_y = x_y - 1;
                scorpionz[t].x_coord = x_x;
                scorpionz[t].y_coord = x_y;
                scorpionz[t].counter =  0 ;
                board[x_x][x_y] = 12;
            }
        }
    }

    bool check ( int index1 )
    {
        if ( scorpionz[index1].x_coord != 0 && scorpionz[index1].y_coord != 0 && scorpionz[index1].z_dep == 12 )
            return true;

        else
            return false;
    }
};

int main()
{
    int new_index   = 0;
    int moveCounter = 0;
    int x;
    int y;
    int z;
    int c;
    int cnt = 0;

    setBoard();

    Scorpion sco;

    sco.setArray(10, 10, 12, 0, 0);
    x = 10;
    y = 10;
    z =  0;
    c =  0;
    board[10][10] = 12;
    displayBoard();

    while (1)
    {
        while(new_index<200)
        {
            if(sco.check(new_index) == true)
            {
                sco.move(x, y, z, c);
                system("CLS");
                //sco.die(x, y, z, c);
                sco.breed(x, y, z, c);
                displayBoard();
                cout<<"x: "<<x<<"  "<<"  y: "<<y<<"  z: "<<z<<"  c: "<<c<<endl;
                break;
            }
        }
        new_index = new_index + 1;
        if(new_index = 200)
        {
            new_index = 0;
        }
        moveCounter++;
        cout<<endl<<" Number of Moves: "<<moveCounter<<endl;
    }
    return 0;
}

what this code is trying to do is displaying a charcter in an array,,,,then this character goes through the functions (move, breed),,,,,move: it makes the character move inside the array,,,,breed: it duplicates the character after 24 move in one of the four directions around it,,,,the problem is that i want to control(move) the new characters that is resulted from the breed function,,,& i dont know how??!!,,,i.e: i thought if i made the chech funtion & do as shown in the code that it would move but it didnt,,,why??,,,that is the problem,,,may anywone help me plzz with this code!!!

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by eng hassan
0

if anyone cant understand anything in my code & need me to make it clearer for him just ask:)

This topic has been dead for over six months. 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.