Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called Position . Position holds two integers that represent x and

ID: 3821545 • Letter: W

Question

Write a class called Position. Position holds two integers that represent x and y coordinates. Use Position to write a class for a checker piece that will be called Checker.


Bold words are the names you must use for variables or functions. All class members must have the appropriate access, and all class methods must be defined out-of-line. All methods must be defined, not simply commented with a description of what it does. Cin or cout should not appear anywhere in here—“takes a parameter” means passing a parameter, not asking for user input.


The Checker’s attributes are:
• A team color. The color’s datatype is a scoped enumeration, PlayerColor, that holds red and black. This enumeration needs to be global, not class-specific.
• A currentPosition, which is a Position.
• A boolean for kinged or not.
• Two possible moves (Positions) that the piece may move to. Do not worry about calculating whether this is a jump or not.


The Checker’s methods are:
• A constructor that takes a Position parameter, and uses it to assign the piece’s starting position.
• A get_position function that returns the current position of the piece on the board.
• A move_to function that sets the pieces position to a Position that you pass to it.


The body of the helper function calc_moves is already written below. It should not be accessible outside the Checker class. You must place it in the appropriate section within the class.


calc_moves’s definition:
short int offset;
if(color == PlayerColor::red) offset = -1;
else offset = +1;
moves[0].y = moves[1].y = currentPosition.y + offset;
moves[0].x = currentPosition.x - offset;
moves[1].x = currentPosition.x + offset;

Explanation / Answer

class Position{
   public:
       int x;
       int y;
};
enum PlayerColor {red,black};
class Checker{
   private:
       Position currentPosition;
       void calc_moves(){
           short int offset;
           if(color == PlayerColor::red)
               offset = -1;
           else
               offset = +1;
           moves[0].y = moves[1].y = currentPosition.y + offset;
           moves[0].x = currentPosition.x - offset;
           moves[1].x = currentPosition.x + offset;
       }
   public:
       boolean kinged;
       PlayerColor color;
       Position moves[2];

       Checker(Position p)
       {
           currentPosition=p;
       }

       Position get_position(){
           return currentPosition;
       }

        void move_to (Position p){
           currentPosition=p;
       }
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote