vendredi 6 décembre 2019

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

I have a board class that makes N*N board of chars.

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;
        }
    }

now when I use in the main this

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

It is changing this place in the matrix to 'X' and any other char.

I need to limit this matrix to only 'X' 'O' or '.'

I"m not allowed to chain the main! I may only change the classes.

My goal I can not achieve right not is to make the program print "error" when I"m trying to do

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

And I have wasted hours on hours trying to achieve it and I really need your help here.

This is the whole class I wrote:

#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;
    }
};

and this is the whole main I"m not allowed to change:

#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;
}

THANKS!

Aucun commentaire:

Enregistrer un commentaire