考虑具有组合关系的两个类的以下定义:
class Dex {
private:
Position pos;
int team;
public:
Dex(int m, int n, int t);
Position getPos() const;
int getTeam() const;
void swapPos(Dex& d);
~Dex();
};
class Position {
private:
int m;
int n;
public:
Position(int m, int n);
void newPos(m, n);
int getM() const;
int getN() const;
}
;Dex是用来表示使用六角形网格的电脑游戏中的一块。网格上的位置由一个坐标系统指定,该坐标系使用两个整数(m和n)来识别网格中的任何单元,如下所示:
从任何单元格中,都有六个方向: A、B、C、D、E和F。注意,网格在所有方向无限期扩展。
A或D方向上的移动会引起m坐标的变化。B或E方向上的运动会引起n坐标的变化。C或F方向的运动导致m坐标和n坐标同时发生变化。
注:游戏规则并不重要。你不需要写一个程序来玩游戏。您必须编写一个程序来测试Dex和Position类的实现。
首先,将这些类实现如下:
Dex构造函数应将数据成员初始化为作为参数提供的值。
·getPos()应该返回当前职位的副本,而getTeam()应该返回团队号。
·swapPos()将当前实例的位置与另一个Dex实例的位置互换。
·职位的成员职能是不言而喻的。
请注意,团队数据成员的目的是允许多个Dex实例属于同一个团队,例如,团队编号1。一个团队中有多少个Dexes,有多少个团队,这些都是游戏规则的一部分,对本练习来说并不重要。类似地,Dexes可能被移动或被交换其位置的情况对于此练习并不重要。您必须确保成员函数执行上面指定的操作。
接下来,编写一个简短的控制台应用程序来测试Dex类。它应该创建至少四个Dex实例,然后使用它们测试该类的成员函数。
最后,更改Dex类,以便pos数据成员是指向某个位置的指针。您还需要为Dex类实现三大类(复制构造函数、赋值操作符和析构函数)。不需要更改职位类。
我目前的实现如下:
#ifndef DEX_H
#define DEX_H
#include "position.h"
class Dex
{
private:
Position* pos;
int team;
public:
Dex(int m, int n, int t);
Position getPos() const;
int getTeam() const;
void swapPos(Dex& d);
Dex(const Dex & otherDex);
Dex& operator=(const Dex& otherDex);
~Dex();
};
#endif // DEX_H#ifndef POSITION_H
#define POSITION_H
class Position
{
private:
int m;
int n;
public:
Position(int m, int n);
void newPos(int m, int n);
int getM() const;
int getN() const;
};
#endif // POSITION_H#include <stdexcept>
#include "dex.h"
#include "position.h"
#include <cmath>
Dex::Dex(int m, int n, int t)
: pos(new Position(m, n)),
team(t) {
}
Dex::Dex(const Dex& d)
: pos(d.pos), team(d.team) {
}
Dex & Dex::operator=(const Dex & d)
{
if (this != &d) { /* operator=() should always do nothing in the case of self-assignment. */
pos = d.pos;
team = d.team;
}
return *this;
}
Dex::~Dex()
{
delete pos;
}
Position Dex::getPos() const
{
return *(pos);
}
int Dex::getTeam() const
{
return team;
}
void Dex::swapPos(Dex& d)
{
int tm = d.getPos().getM();
int tn = d.getPos().getN();
d.getPos().newPos(getPos().getM(), getPos().getN());
getPos().newPos(tm, tn);
// Position *temp;
// temp = this->pos;
// this->pos = d->pos;
// d->pos = temp;
}#include "position.h"
using namespace std;
Position::Position(int m, int n)
: m{m}, n{n} {
}
void Position::newPos(int m, int n)
{
this->m = m;
this->n = n;
}
int Position::getM() const
{
return m;
}
int Position::getN() const
{
return n;
}#include <QCoreApplication>
#include <iostream>
#include "dex.h"
#include <QList>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << "Welcome to Dex Game." << endl;
cout << "testing..." << endl;
Dex d1(2, 5, 1);
Dex d2(d1);
Dex d3(0, 0, 2);
Dex d4(0, 0, 2);
d1.swapPos(d3); // positions didn't change
//show status of each dex
cout << "Position of dex player 1: m" << d1.getPos().getM() << " n="
<< d1.getPos().getN() << " team=" << d1.getTeam() << endl;
cout << "Position of dex player 2: m" << d2.getPos().getM() << " n="
<< d2.getPos().getN() << " team=" << d2.getTeam() << endl;
cout << "Position of dex player 3: m" << d3.getPos().getM() << " n="
<< d3.getPos().getN() << " team=" << d3.getTeam() << endl;
cout << "Position of dex player 4: m" << d4.getPos().getM() << " n="
<< d4.getPos().getN() << " team=" << d4.getTeam() << endl;
return a.exec();
}我的书库如下:
我的swapPos函数不能工作。
这里的任何指导都将不胜感激。1:https://i.stack.imgur.com/FTs7D.png 2:https://i.stack.imgur.com/9prok.png
更新
对于下面的C++程序,我只需要使用swapPos,它应该将一个Dex实例的位置(m和n)替换为另一个Dex实例:
#ifndef DEX_H
#define DEX_H
#include "position.h"
class Dex
{
private:
Position* pos;
public:
Dex(int m, int n);
Position getPos() const;
bool move(char, int);
void swapPos(Dex& d);
Dex(const Dex & otherDex);
Dex& operator=(const Dex& otherDex);
~Dex();
};
#endif // DEX_H#ifndef POSITION_H
#define POSITION_H
class Position
{
private:
int m;
int n;
public:
Position(int m, int n);
void newPos(int m, int n);
int getM() const;
int getN() const;
};
#endif // POSITION_H#include <stdexcept>
#include "dex.h"
#include "position.h"
Dex::Dex(int m, int n) {
pos = new Position(m, n);
}
Dex::Dex(const Dex& d) {
pos = new Position(d.getPos().getM(), d.getPos().getN());
}
Dex & Dex::operator=(const Dex & d)
{
if (this != &d) {
pos->newPos(d.getPos().getM(), d.getPos().getN());
}
return *this;
}
Dex::~Dex()
{
delete pos;
}
Position Dex::getPos() const
{
return *(pos);
}
bool Dex::move(char dn, int de)
{
dn = toupper(dn);
//test for invalid values
if (dn != 'A' && dn != 'B' && dn != 'C' && dn != 'D' && dn != 'E' && dn != 'F' || de <= 0)
{
return false;
}
else
{
//horizontal movement
if (dn == 'A')
{
int i = pos->getM() + de;
pos->newPos(i, pos->getN());
}
if (dn == 'D')
{
int i = pos->getM() - de;
pos->newPos(i, pos->getN());
}
//right diagonal movement
if (dn == 'B')
{
int i = pos->getN() + de;
pos->newPos(pos->getM(), i);
}
if (dn == 'E')
{
int i = pos->getN() - de;
pos->newPos(pos->getM(), i);
}
//left diagonal movement
if (dn == 'C')
{
int i = pos->getM() - de;
int j = pos->getN() + de;
pos->newPos(i, j);
}
if (dn == 'F')
{
int i = pos->getM() + de;
int j = pos->getN() - de;
pos->newPos(i , j);
}
return true;
}
}
void Dex::swapPos(Dex& d)
{
Position tp = d.getPos();
d.getPos() = this->getPos();
this->getPos() = tp;
}#include "position.h"
using namespace std;
Position::Position(int m, int n)
: m{m}, n{n} {
}
void Position::newPos(const int m, const int n)
{
this->m = m;
this->n = n;
}
int Position::getM() const
{
return m;
}
int Position::getN() const
{
return n;
}#include <QCoreApplication>
#include <iostream>
#include "dex.h"
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Dex d1(1, 2);
Dex d2(d1);
d2.move('B', 5);
cout << "d1: m=" << d1.getPos().getM() << " n=" << d1.getPos().getN() << endl;
cout << "d2: m=" << d2.getPos().getM() << " n=" << d2.getPos().getN() << endl;
d1.swapPos(d2);
cout << "d1: m=" << d1.getPos().getM() << " n=" << d1.getPos().getN() << endl;
cout << "d2: m=" << d2.getPos().getM() << " n=" << d2.getPos().getN() << endl;
return a.exec();
}是我的构造函数还是swapPos的错误?我该怎么做才好呢?谢谢。
发布于 2020-09-06 21:58:25
我的Dex应用程序开始工作了。这不是游戏本身,而是游戏会调用的功能。总之,这是完整的代码:
#ifndef DEX_H
#define DEX_H
#include "position.h"
class Dex
{
private:
Position* pos;
int team;
public:
Dex(int m, int n, int t);
Position getPos() const;
int getTeam() const;
bool move(char, int);
bool lineOfSight(Dex *d) const;
void swapPos(Dex& d);
Dex(const Dex & otherDex);
Dex& operator=(const Dex& otherDex);
~Dex();
};
#endif // DEX_H#ifndef POSITION_H
#define POSITION_H
class Position
{
private:
int m;
int n;
public:
Position(int m, int n);
void newPos(int m, int n);
int getM() const;
int getN() const;
};
#endif // POSITION_H#include <stdexcept>
#include "dex.h"
#include "position.h"
#include <cmath>
Dex::Dex(int m, int n, int t) {
pos = new Position(m, n);
team = t;
}
Dex::Dex(const Dex& d) {
pos = new Position(d.getPos().getM(), d.getPos().getN());
this->team = d.getTeam();
}
Dex & Dex::operator=(const Dex & d)
{
if (this != &d) {
pos->newPos(d.getPos().getM(), d.getPos().getN());
}
return *this;
}
Dex::~Dex()
{
delete pos;
}
Position Dex::getPos() const
{
return *(pos);
}
int Dex::getTeam() const
{
return team;
}
bool Dex::move(char dn, int de)
{
dn = toupper(dn);
//test for invalid values
if (dn != 'A' && dn != 'B' && dn != 'C' && dn != 'D' && dn != 'E' && dn != 'F' || de <= 0)
{
return false;
}
else
{
//horizontal movement
if (dn == 'A')
{
int i = pos->getM() + de;
pos->newPos(i, pos->getN());
}
if (dn == 'D')
{
int i = pos->getM() - de;
pos->newPos(i, pos->getN());
}
//right diagonal movement
if (dn == 'B')
{
int i = pos->getN() + de;
pos->newPos(pos->getM(), i);
}
if (dn == 'E')
{
int i = pos->getN() - de;
pos->newPos(pos->getM(), i);
}
//left diagonal movement
if (dn == 'C')
{
int i = pos->getM() - de;
int j = pos->getN() + de;
pos->newPos(i, j);
}
if (dn == 'F')
{
int i = pos->getM() + de;
int j = pos->getN() - de;
pos->newPos(i , j);
}
return true;
}
}
bool Dex::lineOfSight(Dex * d) const
{
int tm = pos->getM();
int tn = pos->getN();
int dm = d->getPos().getM();
int dn = d->getPos().getN();
if (tm == dm)
{
return true;
}
else if (tn == dn)
{
return true;
}
else if (abs(tm - dm) == abs(tn - dn))
{
return true;
}
else
{
return false;
}
}
void Dex::swapPos(Dex& d)
{
Dex temp = d;
temp = *this;
*this = d;
d = temp;
}#include "position.h"
using namespace std;
Position::Position(int m, int n)
: m{m}, n{n} {
}
void Position::newPos(int m, int n)
{
this->m = m;
this->n = n;
}
int Position::getM() const
{
return m;
}
int Position::getN() const
{
return n;
}一个简单的主测试实现:
#include <QCoreApplication>
#include <iostream>
#include "dex.h"
#include <QList>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << "Welcome to Dex Game." << endl;
cout << "testing..." << endl;
Dex d1(2, 5, 1);
Dex d2(d1);
Dex d3(0, 0, 2);
Dex d4(0, 0, 2);
//show status of each dex
cout << "Position of dex player 1: m" << d1.getPos().getM() << " n="
<< d1.getPos().getN() << " team=" << d1.getTeam() << endl;
cout << "Position of dex player 2: m" << d2.getPos().getM() << " n="
<< d2.getPos().getN() << " team=" << d2.getTeam() << endl;
cout << "Position of dex player 3: m" << d3.getPos().getM() << " n="
<< d3.getPos().getN() << " team=" << d3.getTeam() << endl;
cout << "Position of dex player 4: m" << d4.getPos().getM() << " n="
<< d4.getPos().getN() << " team=" << d4.getTeam() << endl;
//move players then check status and line of sight
d1.move('A', 3);
d2.move('C', 2);
d3.move('F', 6);
d4.move('E', 8);
//show status of each dex
cout << "Dex player 1 moved A3. Position of dex player 1: m" << d1.getPos().getM() << " n="
<< d1.getPos().getN() << " team=" << d1.getTeam() << endl;
cout << "Dex player 2 moved C2. Position of dex player 2: m" << d2.getPos().getM() << " n="
<< d2.getPos().getN() << " team=" << d2.getTeam() << endl;
cout << "Dex player 3 moved F6. Position of dex player 3: m" << d3.getPos().getM() << " n="
<< d3.getPos().getN() << " team=" << d3.getTeam() << endl;
cout << "Dex player 4 moved E8. Position of dex player 4: m" << d4.getPos().getM() << " n="
<< d4.getPos().getN() << " team=" << d4.getTeam() << endl;
//swap postions
d1.swapPos(d3);
cout << "The position of d1 was swapped with d3." << endl;
//show status of each dex
cout << "Position of dex player 1: m" << d1.getPos().getM() << " n="
<< d1.getPos().getN() << " team=" << d1.getTeam() << endl;
cout << "Position of dex player 3: m" << d3.getPos().getM() << " n="
<< d3.getPos().getN() << " team=" << d3.getTeam() << endl;
//line of sight
if (d1.lineOfSight(&d2))
{
cout << "Dex player 1 is in line of sight with dex player 2." << endl;
}
else
{
cout << "Dex player 1 is not in line of sight with dex player 2." << endl;
}
if (d1.lineOfSight(&d3))
{
cout << "Dex player 1 is in line of sight with dex player 3." << endl;
}
else
{
cout << "Dex player 1 is not in line of sight with dex player 3." << endl;
}
if (d1.lineOfSight(&d4))
{
cout << "Dex player 1 is in line of sight with dex player 4." << endl;
}
else
{
cout << "Dex player 1 is not in line of sight with dex player 4." << endl;
}
if (d2.lineOfSight(&d3))
{
cout << "Dex player 2 is in line of sight with dex player 3." << endl;
}
else
{
cout << "Dex player 3 is not in line of sight with dex player 3." << endl;
}
if (d2.lineOfSight(&d4))
{
cout << "Dex player 2 is in line of sight with dex player 4." << endl;
}
else
{
cout << "Dex player 2 is not in line of sight with dex player 4." << endl;
}
return a.exec();
}https://stackoverflow.com/questions/63672085
复制相似问题