如何将矩阵类限制为仅获取"XO"或""。

How to limit a matrix class to get only 'X' 'O' or '.'

本文关键字:获取 XO      更新时间:2023-10-16

我有一个板类,可以制作 N*N 个字符板。

class Cell
{ 
public:
int row; int col;
};

class Board {
private:
int size;
char** matrix = nullptr;
//many other class functions.
char & operator[](const Cell& cellToChange) {
if (cellToChange.row < size && cellToChange.col < size) {
return matrix[cellToChange.row][cellToChange.col];
}
else {
cout << "ERROR!" << endl;
}
}

现在当我主要使用这个

"board1[{1, 4}] = 'X';"

它将矩阵中的这个地方更改为"X"和任何其他字符。

我需要将此矩阵限制为仅"X"O"或"。

我不允许链主!我只能改变类。

我无法实现的目标不是在我尝试时使程序打印"错误">

"board1[{1, 4}] = 'z'".

我浪费了几个小时试图实现它,我真的需要你的帮助。

这是我写的整个班级:

#include <iostream>
using namespace std;

class Cell
{ 
public:
int row; int col;
};

class Board {
private:
int size;
char** matrix = nullptr;
public: 
Board(int sizeToSet) {                       //constructor with size
size = sizeToSet;
matrix = new char*[size];                 //creates a matrix
for (int i = 0; i < size; i++)
matrix[i] = new char[size];
for (int i = 0; i < size; i++) {          //makes every cell in matix '.'
for (int j = 0; j < size; j++) {
matrix[i][j] = '.';
}
}
}

void printSize() {                            //matrix size print
cout << size << endl;
}
~Board() {                                    //destructor
for (int i = 0; i < size; i++)
delete[] matrix[i];
delete[] matrix;
}
Board(const Board& other) {                   //copy constructor
if (this != &other) {
size = other.size;
matrix = new char*[size];
for (int i = 0; i < size; i++)
matrix[i] = new char[size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = other.matrix[i][j];
}
}
}
}
Board(Board&& other) {                   //move constructor
size = other.size;
matrix = other.matrix;
other.matrix = nullptr;
}

friend ostream& operator<<(ostream& os, const Board& boardToPrint) {       //prints matrix
for (int i = 0; i < boardToPrint.size; i++) {
for (int j = 0; j < boardToPrint.size; j++) {
os << boardToPrint.matrix[i][j] << "  ";
}
os << endl;
}
os << endl;
return os;
}

char & operator[](const Cell& cellToChange) {
if (cellToChange.row < size && cellToChange.col < size) {
return matrix[cellToChange.row][cellToChange.col];
}
else {
cout << "ERROR!" << endl;
}
}
void operator=(char charToAdd) {
if (charToAdd == 'X' || charToAdd == 'O' || charToAdd == '.') {
for (int i = 0; i < size; i++) {         
for (int j = 0; j < size; j++) {
matrix[i][j] = charToAdd;
}
}
}
else {
cout << "ERROR!" << endl;
}
}
const Board& operator=(const Board& other) {
if (this != &other) {
size = other.size;
matrix = new char*[size];
for (int i = 0; i < size; i++)
matrix[i] = new char[size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = other.matrix[i][j];
}
}
}
return *this;
}
};

这是不允许更改的全部主要内容:

#include "Board.h"
#include <iostream>
using namespace std;
int main() {
Board board1{4};  // Initializes a 4x4 board
cout << board1 << endl;   /* Shows an empty board:
....
....
....
....
*/
cout << board1[{1,2}] << endl; // .
board1[{1,1}]='X';
board1[{1,2}]='O';
char c = board1[{1,2}]; cout << c << endl; // O
cout << board1 << endl;  /* Shows the following board:
....
.XO.
....
....
*/
// This should raise an exception
//  "Illegal"
board1 = '.';     // Fill the entire board with "."
cout << board1 << endl;  /* Shows an empty board, as above */
board1 = 'a';        // This should raise exception
//  "Illegal"
board1[{0,1}] = 'x';  // This should raise an exception
//   "Illegal"
Board board2 = board1;
board2[{0,0}] = 'X';
cout << board1 << endl;  /* Shows an empty board, as above */
cout << board2 << endl;  /* Shows a board with an X at top-left */
board1 = board2;
board1[{3,3}] = 'O';
cout << board2 << endl;  /* Shows a board with an X at top-left */
cout << board1 << endl;  /* Shows a board with an X at top-left and O at bottom-right */
cout << "Good bye!" << endl;
return 0;
}

谢谢!

你需要的是另一层抽象。 由于您无法控制分配的右侧,因此需要控制分配运算符。 为此,您需要一个代理对象。 您将从operator[]返回该对象,然后在其赋值运算符中执行逻辑。 那看起来像

class Proxy
{
char& val;
Proxy(char& val) : val(val) {}
Proxy& operator=(char new_val)
{
if (new_val == 'X' || new_val == 'O' || new_val == '.')
{
val = new_val;
return *this;
}
throw std::invalid_argument("Invalid Assignment.  Use X, O, or .");
}
// allows this class to implicitly convertible to a char
operator char() { return val; }
};

Proxy operator[](const Cell& cellToChange) {
if (cellToChange.row < size && cellToChange.col < size) {
return {matrix[cellToChange.row][cellToChange.col]};
}
throw std::out_of_range("invalid index");
}