added robot interface and robot base class
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "Robot.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
class BaseRobot : public Robot {
|
||||
/* A robot base class that implements functions common to all robots. */
|
||||
public:
|
||||
BaseRobot ( std::string name, int startX, int startY );
|
||||
|
||||
void move ( int direction, const World &world ) override;
|
||||
int decideNextMove ( const World &world ) const override;
|
||||
|
||||
void setPosition ( int x, int y ) override {
|
||||
x_ = x;
|
||||
y_ = y;
|
||||
}
|
||||
int getScore () const override { return score_; }
|
||||
void addScore ( int points ) override { score_ += points; }
|
||||
int getX () const override { return x_; }
|
||||
int getY () const override { return y_; }
|
||||
std::string getName () const override { return name_; }
|
||||
|
||||
protected:
|
||||
int x_, y_, score_;
|
||||
std::string name_;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <type_traits>
|
||||
|
||||
class World;
|
||||
|
||||
class Robot {
|
||||
/* A virtual interface for a robot */
|
||||
public:
|
||||
virtual ~Robot () = default;
|
||||
Robot ( const Robot & ) = default;
|
||||
Robot &operator= ( const Robot & ) = default;
|
||||
Robot ( Robot && ) = default;
|
||||
Robot &operator= ( Robot && ) = default;
|
||||
|
||||
virtual void move ( int direction, const World &world ) = 0;
|
||||
virtual int mine ( World &world ) = 0;
|
||||
virtual int decideNextMove ( const World &world ) const = 0;
|
||||
|
||||
virtual void setPosition ( int x, int y ) = 0;
|
||||
virtual int getScore () const = 0;
|
||||
virtual void addScore ( int points ) = 0;
|
||||
virtual int getX () const = 0;
|
||||
virtual int getY () const = 0;
|
||||
virtual std::string getName () const = 0;
|
||||
|
||||
protected:
|
||||
Robot () = default;
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "../include/BaseRobot.h"
|
||||
|
||||
class SortBot : public BaseRobot {
|
||||
public:
|
||||
SortBot ( int startX, int startY );
|
||||
int mine ( World &world ) override;
|
||||
};
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <vector>
|
||||
|
||||
class World {
|
||||
/* A 3D grid world where robots can mine blocks.
|
||||
Blocks have a value, some have special effects. */
|
||||
|
||||
public:
|
||||
explicit World ( int x = 5, int y = 5, int z = 10 );
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "../include/BaseRobot.h"
|
||||
#include "../include/World.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
BaseRobot::BaseRobot ( std::string name, int startX, int startY )
|
||||
: x_ ( startX ), y_ ( startY ), score_ ( 0 ), name_ ( std::move ( name ) ) {}
|
||||
|
||||
void BaseRobot::move ( int direction, const World &world ) {
|
||||
|
||||
switch ( direction ) {
|
||||
case 1:
|
||||
x_ = std::min ( x_ + 1, world.getSizeX() - 1 );
|
||||
break; // right
|
||||
case 2:
|
||||
x_ = std::max ( x_ - 1, 0 );
|
||||
break; // left
|
||||
case 3:
|
||||
y_ = std::min ( y_ + 1, world.getSizeY() - 1 );
|
||||
break; // down
|
||||
case 4:
|
||||
y_ = std::max ( y_ - 1, 0 );
|
||||
break; // up
|
||||
default:
|
||||
break; // no move
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// int BaseRobot::decideNextMove ( const World &world ) const
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "../include/SortBot.h"
|
||||
#include "../include/World.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
SortBot::SortBot ( int startX, int startY ) : BaseRobot ( "SortBot", startX, startY ) {}
|
||||
Reference in New Issue
Block a user