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.