编辑矩阵中的值将其删除

时间:2019-12-16 19:46:39

标签: c++

很抱歉,如果这没有道理,但我正在为数独游戏编写程序。它获取一个文件,将其转换为矩阵,然后在屏幕上打印电路板。然后,应该使用户能够编辑游戏。 我的问题是我的编辑功能。每当我尝试在板上编辑一个值时,它都将占用该空间。

void edit(char sudoku[][9])
{
   char letter;
   int number;
   //these are the coordinates for the board

   int value = 0;
   //this is the entered value for the choosen square

   cout << "What are the coordinates of the square: ";
   cin >> letter >> number;

   letter = toupper(letter); // makes sure the letter is caps

   if (sudoku[letter - 65][number - 1] != ' ')
      // if the coordinates are off the board or already have a value
   {
      cout << "Error: Square \'" << letter << number
           << "\' is invalid."
           << endl;
  }
   else
   {
      cout << "What is the value at \'" << letter << number
           << "\': ";
      cin >> value;

      if (value > 9 || value < 1)
         //if the value is invalid
      {
         cout << "ERROR: Value \'" << value
              << "\'in square \'" << letter << number
              << "\' is invalid\n";
      }
      cout << endl;

      sudoku[letter - 65][number - 1] = value;
      //set the square = the entered value
   }
   return;

这是编辑之前的木板:

   A B C D E F G H I
1  7 2 3|     |1 5 9
2  6    |3   2|    8
3  8    |  1  |    2
   -----+-----+-----
4    7  |6 5 4|  2
5      4|2   7|3
6    5  |9 3 1|  4
   -----+-----+-----
7  5    |  7  |    3
8  4    |1   3|    6
9  9 3 2|     |7 1 4

及之后:

What are the coordinates of the square: b2
What is the value at 'B2': 3

   A B C D E F G H I
1  7 2 3|     |1 5 9
2  6   |3   2|    8
3  8    |  1  |    2
   -----+-----+-----
4    7  |6 5 4|  2
5      4|2   7|3
6    5  |9 3 1|  4
   -----+-----+-----
7  5    |  7  |    3
8  4    |1   3|    6
9  9 3 2|     |7 1 4

所以唯一的变化是编辑后删除了一个空格。

1 个答案:

答案 0 :(得分:4)

此行:

sudoku[letter - 65][number - 1] = value;

将ASCII值0-9放入由sudoku组成的char数组中。这些ASCII字符通常不可见或具有其他特殊含义,例如蜂鸣或制表。

您需要向其中添加'0'的值,以使其可以正常显示:

sudoku[letter - 'A'][number - 1] = value + '0';

,然后用字符文字(例如'A')替换65等魔术数字。

相关问题