简体   繁体   English

我可以在 C++ 中使数组的某些值不可更改吗?

[英]Can I make some values of an array unchangeable in c++?

I am designing a console-based Sudoku.我正在设计一个基于控制台的数独。 According to the game, you can change entries as you move along the board.根据游戏,您可以在棋盘上移动时更改条目。 I've used a 2d array to store the values but the problem here is that I cannot make some of the entries constant as in the sudoku game where some values on the board cannot be changed so the user must reach a solution following those entries.我使用了一个二维数组来存储这些值,但这里的问题是我不能像在数独游戏中那样使某些条目保持不变,其中棋盘上的某些值无法更改,因此用户必须在这些条目之后找到解决方案。 So my question is:所以我的问题是:

- How can I make some of the entries constant here? - 我怎样才能在这里使一些条目保持不变?

I have attached the code below我附上了下面的代码

#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

class Sudoku
{
    int wholeArray[9][9];
    int number;

public:
    void setArray(void);
    void displayArray(int& x, int& y);
    void readNumber(void);
    void setPositions(int& x, int& y);
    void changeEntry(int &x, int &y);
};

void setConsoleColor(int textColor, int bgColor) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
}

void Sudoku::setArray(void)
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            wholeArray[i][j] = 0;
        }
    }
}

void Sudoku::displayArray(int& x, int& y)
{
    setConsoleColor(11, 0);
    cout << "\t\b\b\b1 2 3   4 5 6   7 8 9\n";
    cout << "   -------------------------\n";
    for (int i = 0; i < 9; i++)
    {
        cout << i + 1 << "  | ";
        for (int j = 0; j < 9; j++)
        {
            if (x == i && y == j)
                setConsoleColor(0, 11);
            cout << wholeArray[i][j];
            setConsoleColor(11, 0);
            cout << " ";

            if ((j + 1) % 3 == 0)
                cout << "| ";
        }
        cout << endl;
        if ((i + 1) % 3 == 0)
            cout << "   -------------------------\n";
    }
}

void Sudoku::readNumber(void)
{
    cout << "Enter number you want to enter (from 1-9): ";
    cin >> number;
}

void Sudoku::setPositions(int& x, int& y)
{
        char key = _getch();
        if (key == KEY_UP)
            x--;
        if (key == KEY_DOWN)
            x++;
        if (key == KEY_LEFT)
            y--;
        if (key == KEY_RIGHT)
            y++;

        if (x < 0)
            x = 8;
        else if (x > 8)
            x = 0;

        if (y < 0)
            y = 8;
        else if (y > 8)
            y = 0;

        if (key == 'x')
            exit(0);
        if (key == 'v')
            changeEntry(x, y);
        system("cls");
}

void Sudoku:: changeEntry(int& x, int& y)
{
    int entry;
    do
    {
        cout << "Enter value:\t";
        cin >> entry;
    } while (entry < 1 || entry > 9);

    wholeArray[x][y] = entry;
}

int main()
{
    int x = 0, y = 0, entry;

    Sudoku s1;
    s1.setArray();

    s1.displayArray(x, y);
    while (1)
    {
        s1.setPositions(x, y);
        s1.displayArray(x, y);
    }
    return 0;
}

Can I make some values of an array unchangeable in c++?我可以在 C++ 中使数组的某些值不可更改吗?

You can make an entire array unchangeable by making it an array of const objects.您可以通过将其设置为 const 对象数组来使整个数组不可更改。 You can make an array entirely changeable by making it an array of non-const objects.您可以通过将数组设为非常量对象数组来使数组完全可变。 C++ arrays are homogeneous, so you cannot have mix of differently qualified types. C++ 数组是同质的,所以你不能混合不同的限定类型。

What you could have is an array of std::variant<T, const T> .您可能拥有的是std::variant<T, const T>数组。 Or, you could simply write a class that wraps the array that allows modification only through member functions.或者,您可以简单地编写一个包装数组的类,该数组只允许通过成员函数进行修改。 Those member functions can refuse to change elements that you deem to be unchangeable.这些成员函数可以拒绝更改您认为不可更改的元素。

You can create another array forbidden[9][9] and the position that you don't want the user to change mark them for example您可以创建另一个数组禁止 [9] [9] 和您不希望用户更改的位置标记它们例如

forbidden[1][0] = 1;
forbidden[2][5] = 1;
forbidden[3][7] = 1;

and when you want to change the value of your original array check the forbidden array first当您想更改原始数组的值时,请先检查禁止的数组

if (key == 'v' && forbidden[x][y] != 1)
    changeEntry(x, y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM