implementation of robot class - 1

13
Announcements Homework 3 – Robot game will be assigned this week Due in 1 WEEK Start EARLY! Common Questions: Be aware of the flow of the game, read the document for all details The flow of the game will repeat inside a big while loop until the game ends, we call one run of this loop a “turn ” in the document: Do not use TurnRight in TurnFace member function Instead Turn the robot to a given direction by updating the private data member directly that you implemented for this HW in recitations Submit ALL files in your project: main.cpp, robot_modified.cpp, robot_modified.h, minifw_modified.cpp and minifw_modified.h Use the world.rw file in the homework zip to open an example world. If monsters move very fast, you may slow them down by Sleep(200);

Upload: robbin

Post on 08-Jan-2016

21 views

Category:

Documents


1 download

DESCRIPTION

Implementation of Robot Class - 1. Your next homework will be about updating the Robot class - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Implementation of Robot Class - 1

AnnouncementsHomework 3 – Robot game will be assigned this week

Due in 1 WEEK Start EARLY!Common Questions:

Be aware of the flow of the game, read the document for all detailsThe flow of the game will repeat inside a big while loop until the game

ends, we call one run of this loop a “turn” in the document:Do not use TurnRight in TurnFace member function

Instead Turn the robot to a given direction by updating the private data member directly that you implemented for this HW in recitations

Submit ALL files in your project: main.cpp, robot_modified.cpp, robot_modified.h, minifw_modified.cpp and minifw_modified.h

Use the world.rw file in the homework zip to open an example world.If monsters move very fast, you may slow them down by

Sleep(200);

Page 2: Implementation of Robot Class - 1

RandGen Class A Tapestry class for random number generation Add randgen.cpp to your project and have #include "randgen.h" in your program

Four member functionsint RandInt(int max = INT_MAX);

returns a random integer in [0..max) int RandInt(int low, int max);

returns a random integer in [low..max] double RandReal();

returns a random double value in [0..1) double RandReal(double low, double max);

returns a random double value in the range of [low..max]

see numberguess.cpp for an example program that use RandGen

Page 3: Implementation of Robot Class - 1

OverloadingIn RandGen class, there are two different functions

named RandInt so as RandReal

Using the same name for more than one function is called overloading. They are differentiated by parameter typesReturn types do not differentiate funtions

All member and free functions can be overloaded.

Page 4: Implementation of Robot Class - 1

Implementation of Robot Class - 1 Your next homework will be about updating the Robot class

you will add some new member functions that requires to deal with robots.h and robots.cpp files (actually in the homework, you will use an updated class for which the file names are robots_modified.h and robots_modified.cpp)

and you will use those newly added functions in an application It is a good idea to have a look at how this class is implemented

It is designed and implemented by Ersin Karabudak We have made some changes later

Robot class implementation is quite complexRobot, RobotWindow and RobotWorld are different structures

we will not deal with RobotWindow and RobotWorld, but the implementation file contains robot class implementation and the details of RobotWindow and RobotWorld too. Do not get confused.

Robots are maintained as a circular doubly linked list it is a data structure that uses pointers (probably will see in CS300) but do not get thrilled! you will not need those complex structures for the

member functions that you will add.

Some details you have to know will be given now and more details will be given in recitations this week

Page 5: Implementation of Robot Class - 1

Implementation of Robot Class - 2enum Direction { east, west, north, south };

enum Color { white, yellow, red, blue, green, purple, pink, orange };

class Robot

{

public:

Robot (int x, int y, Direction dir = east, int things = 0);

~Robot ();

void Move (int distance = 1);

bool Blocked ();

void TurnRight ();

bool PickThing ();

bool PutThing ();

void SetColor (Color color);

bool FacingEast ();

bool FacingWall ();

bool CellEmpty ();

bool BagEmpty ();

constr

uctor

Destructor (not needed in HW5)

member functions

continued on the next slide

Page 6: Implementation of Robot Class - 1

Implementation of Robot Class - 3private:int xPos; //x coordinate of the location of robotint yPos; //y coordinate of the location of robotDirection direction; //current direction of robotColor color; //current color of robotint bag; //current # of things in the bag of robotbool stalled; //true if the robot is deadbool visible; //true if the robot is visible

Robot *next;Robot *prev;static Robot *list;

friend struct RobotWindow; };

Private Data

pointers for the data structure you will not need them

RobotWindow may refer Robot’s private data

Page 7: Implementation of Robot Class - 1

Implementation of Robot Class - 4 Previous two slides were in the robots.h (now robots_modified.h). Now let’s go over the robots.cpp (now robots_modified.cpp) file in

VC++ environment

In the next homework, you are going to add 6-8 member functions to the robot class Some of the member functions will be done in recitations this week

Hints try to use currently available member functions

e.g. for PickThings, try to use PickThing in a loop rather than writing some thing similar to PickThing

do not hesitate to modify or access private data members when needed e.g. you will need such an update for TurnFace function

if you change the state of a robot within the current cell, use the following to update the windowtheRobotWindow->Redraw(this);

Page 8: Implementation of Robot Class - 1

Implementation of Robot Class - 5 Hints for the next homework (cont’d)

you will need to use the function called IsPressed defined in miniFW.h (it is going to be renamed as miniFW_modified.h) so include this header file to your main program file this function (IsPressed) is to check whether a key (e.g. an arrow key) is

pressed or not - details are in recitations

Some other changes in the Robot World and Robot Class If a robot hits another robot, both die! No automatic message is displayed when a robot dies Now the bag content is written in robots (if not zero)

Use robots_modified.h, robots_modified.cpp, miniFW_modified.h and miniFW_modified.cpp files in HW3 They will be provided to you in the homework and/or recitation package

Page 9: Implementation of Robot Class - 1

Example using robot class (see rectangularscan.cpp)Write a program in which the robot starts at 0,0 and

searches a rectangular space that covers n*n cellsn is input (in the example below, n is 8)during this journey the robot should pick or put things on the

cells so that all visited cells occupy one thing

Page 10: Implementation of Robot Class - 1

Example: Father’s day (not in book)Father’s day is the third Sunday of June

write a function that returns the date for the father’s day of a given year which is the parameter of the function

In main, input two years and display father’s days between those years

Date fathersday(int year)// post: returns fathers day of year{

Date d(6,1,year); // June 1

while (d.DayName() != "Sunday") { d += 1; }

//d is now the first Sunday, 3rd is 14 days later return d + 14;}

See fathersday.cpp for full program

Page 11: Implementation of Robot Class - 1

What if there were no date class?

It would be very cumbersome to deal with dates without a date classimagine banking applications where each transaction has

associated date fields

Classes simplify programming they are designed and tested.then they can be used by programmers

You are lucky if you can find ready-to-use classes for your needsotherwise ???

Page 12: Implementation of Robot Class - 1

Updating a Class (not in book)Suppose you want to add more functionality to the

date classneed to change the header file (date.h)need to add implementation of new function(s) to

date.cppExample: a new member function to calculate and

return the remaining number of days in the object’s month any ideas? do you think it is too difficult?have a look at the existing member functions and see if

they are useful for you

Page 13: Implementation of Robot Class - 1

Updating a Class (not in book)We can make use of DaysIn member function

Prototype in Date class (add to the header file)int RemainingDays () const;

Implementationint Date::RemainingDays () const{ return DaysIn() - myDay;

}

In a member function implementation private data and other member functions referred without the dot operator. They operate on the object for which the member function is called