编辑矩阵中的值将其删除

问题描述 投票:0回答:1

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

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

因此唯一的更改是在编辑后删除了一个空格。

c++
1个回答
1
投票

此行:

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

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

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

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

并用字符文字,例如'A'替换像65这样的魔术数字。

© www.soinside.com 2019 - 2024. All rights reserved.