Question

Suppose an antiques dealer decides to reuse class Checker, that you wrote last week, for a...

Suppose an antiques dealer decides to reuse class Checker, that you wrote last week, for a program they are writing to keep track of fancy or antique checker pieces. Modify your checker game from last week as described below.

Add a class FancyChecker that is a child of class Checker. In addition to the members and member functions of class Checker, class FancyChecker should have a member named material which stores strings such as "wood" or "plastic". FancyChecker should also have a member named antique which contains a character, either 'y' or 'n', indicating whether or not the checker is an antique.

Write a constructor for FancyChecker. Additionally, write a copy constructor for class FancyChecker. Set all member variables in your constructors. You can decide the details of how these constructor set variables. Also, write both setters and getters for member material and for member antique.

Also for class FancyChecker, overload both the = operator and the + operator of class. You can choose the details for how these operators behave. However, try to choose behaviors that reflect that the operator = is used for assignments and the operator + is used for adding or combining.

Add lines to main.cpp to create an object of class FancyChecker and to demonstrate the use of one of the setters and getters that you wrote. (You may also want to make members of Checker protected instead of private.)

Main.cpp

#include <QCoreApplication>
#include "checker.h"
#include <iostream>
using namespace std;

//Function declarations
void displayBoard(int (*bb)[8][8]);
void clearBoard(int (*bb)[8][8]);

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Declare the board and fill it with zeros
int board[8][8];
int (*bptr)[8][8];
bptr=&board;

//Declare two objects of class checker
Checker redPiece(0,0);
Checker blackPiece(2,5);

//Clear board, put pieces on it, and display
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);

//Move piece, clear board, and display
blackPiece.move(2);
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);

//Move piece, clear board, and display
redPiece.move(1);
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);

return a.exec();
}

//Function to display the board
void displayBoard(int (*bb)[8][8])
{ cout<<"Here's the board"<<endl;
for (int ii=0;ii<8;ii++)
{
for (int jj=0;jj<8;jj++)
{
cout<<(*bb)[ii][jj]<<" ";
}
cout<<endl;
}
return;
}


//Function to clear out the board
void clearBoard(int (*bb)[8][8])
{
for(int ii=0;ii<8;ii++)
{
for(int jj=0;jj<8;jj++)
{
(*bb)[ii][jj]=0;
}
}

return;
}

checker.cpp

#include "Checker.h"

Checker::Checker()
{
xposition = 0;
yposition = 0;
queen = 0;
}

Checker::Checker(int xpos, int ypos)
{
xposition = xpos;
yposition = ypos;
queen = 0;
}

int Checker::getX() const
{
return xposition;
}

int Checker::getY() const
{
return yposition;
}

int Checker::getQueen() const
{
return queen;
}

void Checker::setQueen(int queen_val)
{
queen = queen_val;
}

int Checker::move(int steps)
{
int local_xposition = xposition;
int local_yposition = yposition;

switch (steps)
{
case 0:
local_xposition += 1;
local_yposition -= 1;
break;
case 1:
local_xposition -= 1;
local_yposition -= 1;
break;
case 2:
local_xposition += 1;
local_yposition += 1;
break;
case 3:
local_xposition -= 1;
local_yposition += 1;
break;
default:
return 1;
}

if ((local_xposition >= 0 && local_xposition <= 7) && (local_yposition >= 0 && local_yposition <= 7))
{
xposition = local_xposition;
yposition = local_yposition;
return 0;
}

else
{
return 1;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <QCoreApplication>
#include "checker.h"
#include <iostream>
using namespace std;

//Function declarations
void displayBoard(int (*bb)[8][8]);
void clearBoard(int (*bb)[8][8]);

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Declare the board and fill it with zeros
int board[8][8];
int (*bptr)[8][8];
bptr=&board;

//Declare two objects of class checker
Checker redPiece(0,0);
Checker blackPiece(2,5);

//Clear board, put pieces on it, and display
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);

//Move piece, clear board, and display
blackPiece.move(2);
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);

//Move piece, clear board, and display
redPiece.move(1);
clearBoard(bptr);
board[redPiece.getX()][redPiece.getY()]=1;
board[blackPiece.getX()][blackPiece.getY()]=1;
displayBoard(bptr);


/*Added code*/
FancyChecker f;

f.setmaterial("wood");

f.setantique('n');

FancyChecker g(f);

FancyChecker h;

h=f+g;

cout<<"Fancy Cheker 3 : "<<getmaterial()<<" , "<<getantique();

return a.exec();
}

//Function to display the board
void displayBoard(int (*bb)[8][8])
{ cout<<"Here's the board"<<endl;
for (int ii=0;ii<8;ii++)
{
for (int jj=0;jj<8;jj++)
{
cout<<(*bb)[ii][jj]<<" ";
}
cout<<endl;
}
return;
}


//Function to clear out the board
void clearBoard(int (*bb)[8][8])
{
for(int ii=0;ii<8;ii++)
{
for(int jj=0;jj<8;jj++)
{
(*bb)[ii][jj]=0;
}
}

return;
}


//checker.cpp

#include "Checker.h"

Checker::Checker()
{
xposition = 0;
yposition = 0;
queen = 0;
}

Checker::Checker(int xpos, int ypos)
{
xposition = xpos;
yposition = ypos;
queen = 0;
}

int Checker::getX() const
{
return xposition;
}

int Checker::getY() const
{
return yposition;
}

int Checker::getQueen() const
{
return queen;
}

void Checker::setQueen(int queen_val)
{
queen = queen_val;
}

int Checker::move(int steps)
{
int local_xposition = xposition;
int local_yposition = yposition;

switch (steps)
{
case 0:
local_xposition += 1;
local_yposition -= 1;
break;
case 1:
local_xposition -= 1;
local_yposition -= 1;
break;
case 2:
local_xposition += 1;
local_yposition += 1;
break;
case 3:
local_xposition -= 1;
local_yposition += 1;
break;
default:
return 1;
}

if ((local_xposition >= 0 && local_xposition <= 7) && (local_yposition >= 0 && local_yposition <= 7))
{
xposition = local_xposition;
yposition = local_yposition;
return 0;
}

else
{
return 1;
}

}


//class FancyChecker

class FancyChecker : protected Checker

{

string material;

char antique;

public:

FancyChecker()

{

super();

material="";

antique='\0';

}

FancyChecker (FancyChecker& f)

{

super(f.x,f.y);

if((material=="wood"||material=="plastic")&&(antique=='y'||antique=='n'))

{

this.material=f.material;

this.antique=f.antique;

}

}

void setmaterial(string m)

{

if(material=="wood"||material=="plastic")

material=m;

}

void setantique(char a)

{

if(antique=='y'||antique=='n')

antique=a;

}

string getmaterial()

{

return material;

}

char getantique()

{

return antique;

}

FancyChecker operator= (FancyChecker& f)

{

FancyChecker e;

e.xposition= f.getX();

e.yposition=f.getY();

setmaterial(f.getmaterial());

setantique(f.getantique());

e.setQueen(f.getQueen());

return e;

}

FancyChecker operator+(FancyChecker &f)

{

FancyChecker e;

e.xposition= this.getX()+f.getX();

e.yposition=this.getY()+f.getY();

e.setQueen(this.getQueen()+this.getQueen());

if(this.getmaterial()==f.getmaterial())

e.setmaterial(f.getmaterial());

if(this.getantique()==f.getantique())

e.setantique(f.getantique());

return e;

}
};

Add a comment
Know the answer?
Add Answer to:
Suppose an antiques dealer decides to reuse class Checker, that you wrote last week, for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this assignment is to write a class Checker that can be used as...

    The purpose of this assignment is to write a class Checker that can be used as a part of a checker game program. Open an New Project in Qt Creator, and name it ChGame. Right click on the name of the class, and add a new C++ class named Checker. This will create the new les Checker.h and Checker.cpp. You will need to modify both Checker.h and Checker.cpp for this assignment. (The main function I used for testing my functions...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • Pacman

    For this project, you will be working in on the Pacman game you created for Lab 6. See the solution to Lab 6 for starting files to this project. You should be working in a group of 3 or fewer students. Part 1: InheritanceCreate a new class called GameCharacter. Make GameCharacter a parent class of Pacman and Ghost. Make sure to move all common fields and methods from Pacman and Ghost to GameCharacter.   Part 2: GameplayBegin the game with the...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| Use the Queens class given. In your implementation of the Queens class, complete the body of all methods marked as “To be implemented in Programming Problem 1.” Do not change any of the global variable declarations, constructor or placeQueens methods. Here is what I have so far with notes of what is needed. public class Queens...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It...

    Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It should set the xPos and yPos of the body to the xPos and yPos of the StickFigure. ii.Write a public instance method alignLeg() that takes no argument and returns no value. It should set the xPos and yPos of the leg so it is centred immediately below the body of the StickFigure. public class StickFigure { /*Instance variables*/    private int xPos;//The horizontal position...

  • Complete the program that solves the Eight Queens problem in java only please (pages 318 through...

    Complete the program that solves the Eight Queens problem in java only please (pages 318 through 320). The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| PlaceQueens(in currColumn:integer) //places queens in columns numbered currColumn through 8 If (currColumn>8){ The problem is solved } Else { While(unconsidered squares exist in curr column and the problem is unsolved ){ Determine the next square in column currColumn that is not under attack by a queen in an...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT