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;
}
CodePudding user response:
Can I make some values of an array unchangeable in c ?
You can make an entire array unchangeable by making it an array of const objects. 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.
What you could have is an array of 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.
CodePudding user response:
You can create another array forbidden[9][9] and the position that you don't want the user to change mark them for example
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);