It only has one player, and no it isn't that technical on the rules of soccer. If the ball gets out of bounds there is no penalty shot, it just respawns randomly on the field. My problem is in my lookatball funtion I think, but I am not sure. Sorry for the long code I am about to post :S

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

using namespace std;
/*This class stores and send sends out coordinates of any objects of type point*/
class point{
        private:
		        int x, y;
        public:
                void set(int u, int v) {x = u; y = v;}
                void print(){cout << "x: " << x << ", " << "y: " << y<<endl;}
                int getx(){return x;}//returns x coord of object
		        int gety(){return y;}//returns y coord of object



};
class ball{
      public:
              point location;
              inline void print()//print location of type point by first asking for it coordinates from the point class 
              //and assigning the variables to temp values to be printed out.
              {
                    int u, v;
                    u=location.getx();
                    v=location.gety();
                    cout << "x: " << u << ", " << "y:" << v <<endl;
              }
              void set(int u, int v){location.set(u,v);}//recieves coord and sets them to a the point object
};
class field{
      public:
             ball ballz;
             inline field()//initializes random location of ball
             {
                    int u, v;
                    srand(rand()+time(NULL));
                    u=(rand() % 11 + 0);
                    v=(rand() % 7 + 0);
                    ballz.location.set(u,v);
             }
             inline void printBall()//recieves the balls coordinates and then assigns to temp values to be printed out
             {
                   int u, v;
                   u=ballz.location.getx();
                   v=ballz.location.gety();
                   cout << "x: " << u << ", " << "y:" << v <<endl;
             }
             void setBall(int u, int v){ballz.set(u,v);}//recieves two integers and then passes to a function that will set the coord of ball
             int getBallX(){ballz.location.getx();}//sends out X coordinates of ball
             int getBallY(){ballz.location.gety();}//sends out X coordinates of ball
             inline bool isGoal()//checks to see if the ball has landed on a point that is a goal
             {
                  int u,v;
                  u=ballz.location.getx();
                  v=ballz.location.gety();
                  if(((u==0)&&(v==3))||((u==0)&&(v==4))||((u==11)&&(v==3))||((u==11)&&(v==4)))//This asks wether the ball
                  //has been kicked into a goal or not and returns to the bool in main wether it is true or false
                  return true;
                  else
                  return false;
             }
};      
class robot{
      public:
             point bot;
             enum orientation_type{north, east, south, west};
             orientation_type orientationNow;//stores the orientation which the ball is face
             robot()//initializes random location and orientation of robot
             {
                  int u, v, x;
                  srand(rand()+time(NULL));
                  u=(rand() % 11 + 0);
                  v=(rand() % 7 + 0);
                  bot.set(u,v);
                  x =(rand() % 3 + 0);
                  switch(x)
                  {
                           case 0:orientationNow=north;break;
                           case 1:orientationNow=east;break;
                           case 2:orientationNow=south;break;
                           case 3:orientationNow=west;break;
                  }
             }
             void print()//this prints the location and orientation of the robot
             {
                  int u, v;
                  string x;
                  u=bot.getx();
                  v=bot.gety();
                  switch(orientationNow)//converts the enum value orientationNow to a string for printing
                  {
                           case north:x="north";break;
                           case south:x="south";break;
                           case east:x="east";break;
                           case west:x="west";break;
                  }
                  cout<< "I am at ("<<u<<","<<v<<"), and I am facing "<<x<<endl;
             }
             bool forward()//asks wether the robot can step forward.
             {
                  int u, v;
                  u=bot.getx();
                  v=bot.gety();
                  switch(orientationNow)//says where it could step forward based on it's orientation
                  {
                           case north:v++;break;
                           case south:v--;break;
                           case east:u++;break;
                           case west:u--;break;
                  }
                  
                  if((u>=0)&&(u<=11)&&(v<=7)&&(v>=0))//checks if it is within the bounderies of the field once stepping forward.
                  //If it is, it returns true followed by it's new coordinates other wise it just returns false.
                  {
                           return true;
                           bot.set(u,v);
                  }
                  else
                  return false;
                  print();//prints coordinates of robot
             }
             inline void turn()//changes the direction of the robot through switch statement 
             //then it prints out it's coord and orientation
             {
                    switch(orientationNow)
                  {
                           case north:orientationNow=east;break;
                           case south:orientationNow=west;break;
                           case east:orientationNow=south;break;
                           case west:orientationNow=north;break;
                  }
                  print();//prints out it's coord and orientation
             }
             bool kick(field& bawl)//recieves a reference of the field type "bawl"
             //from main and then sees if it can kick the ball
             {
                   bool kickball;
                   int botX, botY, ballX, ballY;
                   ballX=bawl.getBallX();//gets X coord of ball
                   ballY=bawl.getBallY();;//gets Y coord of ball
                   botX=bot.getx();//gets Y coord of robot
                   botY=bot.gety();//gets Y coord of robot
                   if((botX==ballX)&&(botY==ballY))//checks to see if the robot is standing on the same point that the ball
                   //is on then calls teamGoal to make sure it is kicking towards it's goal, not enemy's
                   {
                            //teamGoal();// 
                            switch(orientationNow)//kicks ball in direction it's facing
                            {
                                   case north:ballY++;break;
                                   case south:ballY--;break;
                                   case east:ballX++;break;
                                   case west:ballX--;break;
                            }
                            kickball=true;
                            if((ballX<0)&&(ballX>11)&&(ballY>7)&&(ballY<0))//checks to see it is kicking out of bounds
                            {
                                   kickball=false;
                                   srand(rand()+time(NULL));
                                   ballX=(rand() % 11 + 0);
                                   ballY=(rand() % 7 + 0);
                            }
                                   
                            bawl.setBall(ballX,ballY);//setBall(ballX,ballY);
                   }
                   else
                            kickball=false;
                   return kickball;//returns whether it is true or not that the ball was kicked;
             }       
};








void lookAtBall(int&,int&,int&,int&,robot&);//prototype for function to make sure the robot is heading towards the ball
int main()
{
    robot mech;
    int ballx, bally, botx, boty, steps;
    steps=0;
    field bawl;
    bool goal=false, kick=false, move=false;
    cout<<"Welcome to Robot Soccer Sim."<<endl<<endl<<endl;
    bawl.printBall();
    system("pause");
    while(goal==false)//keeps kickin ball in an attempt to score while goal is returned a false value by bawl.isGoal();
    {
          botx=mech.bot.getx();
          boty=mech.bot.getx();
          ballx=bawl.getBallX();
          bally=bawl.getBallY();
          lookAtBall(botx, boty, ballx, bally, mech);
          move=mech.forward();
          if(move==true)
               steps++;
          else
          mech.turn();
          kick=mech.kick(bawl);
          if(kick==true)
               cout<<"I kicked the ball!"<<endl;
          goal=bawl.isGoal();
    }
    cout<<"Gooooooaaal!"<<endl;
          
    system("pause");
    return 0;
}





void lookAtBall(int& botx,int& boty,int& ballx,int &bally,robot& mech)
//turns the ball in the right direction so that it can chase after the ball
{    
             n=north;
             s=south;
             e=east;
             w=west;
          if(bally<boty)
          {
                while(mech.orientationNow!=2)
                {
                      mech.turn();
                      cout<<"turn";
                }
          }
          else if(bally>boty)
          {
                while(mech.orientationNow!=0)
                {
                      mech.turn();
                      cout<<"turn";
                }
          }
          else if(bally==boty)
          {
               if(ballx<botx)
               {
                      while(mech.orientationNow!=3)
                      {
                            mech.turn();
                            cout<<"turn";
                      }
               }
               else if(ballx>botx)
               {
                      while(mech.orientationNow!=1)
                      {
                            mech.turn();
                            cout<<"turn";
                      }                                       
               }
          }
          return;
}

I already handed this in, I need to figure how to get this to work so that I can work on the last project he asked us to work on. Making this fully work will help me. I mean it feels right, I am telling it to turn untill it turns to face a certain direction by comparing the enum int to an int in while loops, and ask it turn while the enum does not equal that iny vslue. The enum orientationnow has 4 enum value: north, east, south, west. As you can see most of the project is done and it compiles, it just goes into an infinite loop.

Recommended Answers

All 8 Replies

Hi aleX_X :-)

I have not solved the problem for you, but I think I have figured out what is wrong.

First of all I've found a couple of bugs in your code:

1)
In your main-function, take a look at this code:

botx=mech.bot.getx();
boty=mech.bot.getx();

Erhm... Bot-Y = Get-X ?
Probably a small copy-paste error ;)

2)
In the forward-function you return from the function _before_ calling bot.set(u,v) or print()

I would suggest you change the last part, from the if-statement, to something like this:

if((u>=0)&&(u<=11)&&(v<=7)&&(v>=0))
{
    bot.set(u,v);
    print();
    return true;
}
else
    return false;

NOW the bot will actually chase the ball and kick it. The problem is that once the bot reaches the ball it will just kick it in the direction the bot is facing. Instead of the direction of the goal.

I would suggest that you make some changes to the kick-function, so that the bot kicks the ball towards the goal, instead of the direction it is facing.

Oh man...thanks a lot man!!!....I knew my code had to work...this explains exactly why I was getting problems...and once again it was the small things that got me T_T

Edit: Well...it works sometimes...oddly enough it still goes into an infinite loop most of the time, where it would print the same coordinates, yet different orientations. Then other times it kick and gets a goal.

Don't have much time right now, but... Read the last part of my reply again. The reason for the infinite loop is that the bot moves to the ball and kicks it in whatever direction the bot is facing. Instead the bot should move to the ball and kick it towards the goal.

If I get the time later, I'll try to help some more if I can :-)

I have had a closer look at your program and found a solution that will make the bot kick the ball into one of the goals.

An "easy" fix is to change the kick-function so that the bot turns to kick the ball in the right direction before actually kicking the ball.

Add a class member function called lookAtGoal to the robot-class.
Let that function change the orientation of the robot, so that it faces one of the goals.
Now call that function, in the kick-function, as soon as you know the bot is at the balls location.

void lookAtGoal()
{
    //Change the orientation of the robot, so that it moves towards one of the goals
    //Alot of inspiration can be found in the lookAtBall-function
}

bool kick(field& bawl)
{
    ...
    if((botX==ballX)&&(botY==ballY))
    {
        lookAtGoal(ballX, ballY);
        switch(orientationNow)
        ...
    }
    ...
}

I have implemented lookAtGoal like this:

void lookAtGoal()
{
    int boty;

    boty = bot.gety();

    //If ball is to the south of the goals
    if(boty < 3)
    {
        //Face north
        while(orientationNow != 0)
        {
            turn();
            cout << "turn";
        }
    }
    //Else if the ball is located to the north of the goals
    else if(boty > 4)
    {
        //Face south
        while(orientationNow != 2)
        {
            turn();
            cout << "turn";
        }
    }
    //Else (if the ball is right between the goals
    else
    {
        //And the bot is facing north or south
        if(orientationNow == 0 || orientationNow == 2)
        {
            //Turn the bot 90-degrees, so that it faces one of the goals
            turn();
            cout << "turn";
        }
    }
}

Here is the complete source for the robot-class:

class robot{
      public:
             point bot;
             enum orientation_type{north, east, south, west};
             orientation_type orientationNow;//stores the orientation which the ball is face
             robot()//initializes random location and orientation of robot
             {
                  int u, v, x;
                  srand(rand()+time(NULL));
                  u=(rand() % 11 + 0);
                  v=(rand() % 7 + 0);
				  bot.set(u,v);
                  x =(rand() % 3 + 0);
                  switch(x)
                  {
                           case 0:orientationNow=north;break;
                           case 1:orientationNow=east;break;
                           case 2:orientationNow=south;break;
                           case 3:orientationNow=west;break;
                  }
             }
             void print()//this prints the location and orientation of the robot
             {
                  int u, v;
                  string x;
                  u=bot.getx();
                  v=bot.gety();
                  switch(orientationNow)//converts the enum value orientationNow to a string for printing
                  {
                           case north:x="north";break;
                           case south:x="south";break;
                           case east:x="east";break;
                           case west:x="west";break;
                  }
                  cout<< "I am at ("<<u<<","<<v<<"), and I am facing "<<x<<endl;
             }
             bool forward()//asks wether the robot can step forward.
             {
                  int u, v;
                  u=bot.getx();
                  v=bot.gety();
                  switch(orientationNow)//says where it could step forward based on it's orientation
                  {
                           case north:v++;break;
                           case south:v--;break;
                           case east:u++;break;
                           case west:u--;break;
                  }

                  if((u>=0)&&(u<=11)&&(v<=7)&&(v>=0))//checks if it is within the bounderies of the field once stepping forward.
                  //If it is, it returns true followed by it's new coordinates other wise it just returns false.
                  {
					  bot.set(u,v);
					  print();
                      return true;
                  }
                  else
				      return false;
             }
             inline void turn()//changes the direction of the robot through switch statement 
             //then it prints out it's coord and orientation
             {
                    switch(orientationNow)
                  {
                           case north:orientationNow=east;break;
                           case south:orientationNow=west;break;
                           case east:orientationNow=south;break;
                           case west:orientationNow=north;break;
                  }
                  print();//prints out it's coord and orientation
             }
			 void lookAtGoal()
			 {
				 int boty;

				 boty = bot.gety();

				 if(boty < 3)
				 {
					 while(orientationNow != 0)
					 {
						 turn();
						 cout << "turn";
					 }
				 }
				 else if(boty > 4)
				 {
					 while(orientationNow != 2)
					 {
						 turn();
						 cout << "turn";
					 }
				 }
				 else
				 {
					 if(orientationNow == 0 || orientationNow == 2)
					 {
						 turn();
						 cout << "turn";
					 }
				 }
			 }
             bool kick(field& bawl)//recieves a reference of the field type "bawl"
             //from main and then sees if it can kick the ball
             {
                   bool kickball;
                   int botX, botY, ballX, ballY;
                   ballX=bawl.getBallX();//gets X coord of ball
                   ballY=bawl.getBallY();;//gets Y coord of ball
                   botX=bot.getx();//gets Y coord of robot
                   botY=bot.gety();//gets Y coord of robot
                   if((botX==ballX)&&(botY==ballY))//checks to see if the robot is standing on the same point that the ball
                   //is on then calls teamGoal to make sure it is kicking towards it's goal, not enemy's
                   {
							lookAtGoal();
                            //teamGoal();// 
                            switch(orientationNow)//kicks ball in direction it's facing
                            {
                                   case north:ballY++;break;
                                   case south:ballY--;break;
                                   case east:ballX++;break;
                                   case west:ballX--;break;
                            }
                            kickball=true;
                            if((ballX<0)&&(ballX>11)&&(ballY>7)&&(ballY<0))//checks to see it is kicking out of bounds
                            {
                                   kickball=false;
                                   srand(rand()+time(NULL));
                                   ballX=(rand() % 11 + 0);
                                   ballY=(rand() % 7 + 0);
                            }
                                   
                            bawl.setBall(ballX,ballY);//setBall(ballX,ballY);
                   }
                   else
                            kickball=false;
                   return kickball;//returns whether it is true or not that the ball was kicked;
             }       
};

Whoops!!! Please ignore the call to lookAtGoal where I pass ballX and ballY as arguments. I have implemented the function without function-parameters :-p

My last reply, where I include the source for the entire robot-class, has been corrected :-)

Lines 50 and 51 in your orginal post do not have a return statement in them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.