我正在嘗試創建一個sodoku程序,並且我需要找到一種方法來編輯我所有函數中的相同數組。我已經讀過,指針可以用於這個,但我不知道如何很好地使用指針。如何編輯原始數組遍歷所有函數C++
#include <iostream>
#include <fstream>
using namespace std;
char upperCase(char letter)
{
int decVal = (int) letter;
if (decVal >= 97 && decVal <= 122)
decVal -= 32;
letter = (char) decVal;
return letter;
}
void readFile(int board[9][9])
{
char fileName[256];
int row = 0;
int col = 0;
cout << "Where is your board located? ";
cin >> fileName;
ifstream fin(fileName);
while (fin >> board[row][col])
{
col++;
if (col == 9)
{
row++;
col = 0;
}
}
}
void displayOptions()
{
cout << "Options:" << endl;
cout << " ? Show these instructions" << endl;
cout << " D Display the board" << endl;
cout << " E Edit one square" << endl;
cout << " S Show the possible values for a square" << endl;
cout << " Q Save and quit" << endl;
cout << endl;
}
void displayBoard(int board[9][9])
{
int row = 0;
int col = 0;
int rowNum = 1;
bool onSide = true;
cout << " A B C D E F G H I" << endl;
while (row <= 8)
{
if ((row == 3 || row == 6) && col == 0)
cout << " -----+-----+-----" << endl;
if (onSide)
{
cout << rowNum << " ";
onSide = false;
}
if (col == 3 || col == 6)
cout << "|";
else
cout << " ";
if (board[row][col] == 0)
cout << " ";
else
cout << board[row][col];
col++;
if (col == 9)
{
row++;
col = 0;
rowNum++;
onSide = true;
cout << endl;
}
}
}
void editSquare(int board[9][9])
{
char cord[2];
int row;
int col;
bool run = true;
bool fail = false;
while (run)
{
run = false;
fail = false;
cout << "What are the coordinates of the square: ";
cin >> cord;
if (cord[0] == 'A' || cord[0] == 'a')
row = 0;
else if (cord[0] == 'B' || cord[0] == 'b')
row = 1;
else if (cord[0] == 'C' || cord[0] == 'c')
row = 2;
else if (cord[0] == 'D' || cord[0] == 'd')
row = 3;
else if (cord[0] == 'E' || cord[0] == 'e')
row = 4;
else if (cord[0] == 'F' || cord[0] == 'f')
row = 5;
else if (cord[0] == 'G' || cord[0] == 'g')
row = 6;
else if (cord[0] == 'H' || cord[0] == 'h')
row = 7;
else if (cord[0] == 'I' || cord[0] == 'i')
row = 8;
else
{
cout << "ERROR: Square '";
for (int i = 0; i < 2; i++)
cout << cord[i];
cout << "' is invalid" << endl;
run = true;
fail = true;
}
if (!fail)
{
if (cord[1] == '1')
col = 0;
else if (cord[1] == '2')
col = 1;
else if (cord[1] == '3')
col = 2;
else if (cord[1] == '4')
col = 3;
else if (cord[1] == '5')
col = 4;
else if (cord[1] == '6')
col = 5;
else if (cord[1] == '7')
col = 6;
else if (cord[1] == '8')
col = 7;
else if (cord[1] == '9')
col = 8;
else
{
cout << "ERROR: Square '";
for (int i = 0; i < 2; i++)
cout << cord[i];
cout << "' is invalid" << endl;
run = true;
}
}
}
cord[0] = upperCase(cord[0]);
if (board[row][col] == 0)
{
int number;
cout << "What is the value at '";
for (int i = 0; i < 2; i++)
cout << cord[i];
cout << "': ";
cin >> number;
if (number >= 0 && number <= 9)
board[row][col] = number;
else
{
cout << "ERROR: Value '" << number << "' in square '";
for (int i = 0; i < 2; i++)
cout << cord[i];
cout << "' is invalid" << endl;
}
}
else
{
cout << "ERROR: Square '";
for (int i = 0; i < 2; i++)
cout << cord[i];
cout << "' is filled" << endl;
}
}
bool writeFile(int board[9][9])
{
int row = 0;
int col = 0;
char fileName[256];
cout << "What file would you like to write your board to: ";
cin >> fileName;
ofstream fout(fileName);
while (row <= 8)
{
fout << board[row][col] << " ";
col++;
if (col == 9)
{
row++;
col = 0;
fout << endl;
}
}
cout << "Board written successfully" << endl;
return false;
}
bool getCommand(int board[9][9])
{
bool run = true;
char input;
cout << endl << "> ";
cin >> input;
input = upperCase(input);
if (input == '?')
displayOptions();
else if (input == 'D')
displayBoard(board);
else if (input == 'E')
editSquare(board);
else if (input == 'S');
else if (input == 'Q')
{
run = writeFile(board);
}
else
cout << "ERROR: Invalid command" << endl;
return run;
}
int main()
{
bool run = true;
int board[9][9];
readFile(board);
displayOptions();
displayBoard(board);
while (run)
run = getCommand(board);
return 0;
}
您的代碼已經全部在同一塊板上運行的功能。請澄清你在問什麼。如果您的代碼不符合您的預期,請發佈您的預期結果,並解釋這與您觀察到的有何不同。 –