简体   繁体   English

清除2D阵列中的值

[英]Clearing values in a 2D array

I've been doing some programming for a naughts and crosses program and the board is a 2D array. 我一直在为一个naughts和crosses程序做一些编程,而且这个板是一个2D数组。 I have been trying to make the program repeat if the user wants to replay however I noticed that all of the values stay in the array when it is repeated. 我一直试图让程序重复,如果用户想要重播但是我注意到重复时所有值都保留在数组中。 So I was wondering if there was a way to clear all of the values in my array. 所以我想知道是否有办法清除我的数组中的所有值。

I did try some previous questions on forums however some of the solutions I found didn't seem to work. 我确实在论坛上尝试了一些先前的问题,但是我找到的一些解决方案似乎没有用。

If anyone would like to see the code just comment and I will add it here, I just wasn't sure if it would be necessary. 如果有人想看到代码只是评论,我会在这里添加,我只是不确定是否有必要。

Any help would be much appreciated. 任何帮助将非常感激。

    const int Rows = 4;
    const int Columns = 4;
    char Board[Rows][Columns] = { {' ', ' ', ' ', ' ' },
                                  {' ', '_', '_', '_' },
                                  {' ', '_', '_', '_' },
                                  {' ', '_', '_', '_' } };


    for (int i = 0; i < Rows; ++i)
    {
        for (int j = 0; j < Columns; ++j)
            cout << Board [i][j];
        cout << endl;
    }

    cout << endl << endl;



    int row;
    int column;


    do
    {
        cout << "Please enter the value of the row you would like to take ";
        cin >> row;
        }while (row != 0 && row != 1 && row != 2 && row != 3);


    do
    {
        cout << "Please enter the value of the column you would like to take ";
        cin >> column;
        }while (column != 0 && column != 1 && column != 2 && column != 3);


    Board [row][column] = Player1.GetNorX();

            for (int i = 0; i < Rows; ++i)
    {
        for (int j = 0; j < Columns; ++j)
            cout << Board [i][j];
        cout << endl;
    }

Assuming you want Board to be reset to its original state, you need: 假设您希望Board重置为其原始状态,您需要:

for (int i = 0; i < Rows; i++) {
  for (int j = 0; j < Columns; j++) {
    if (i == 0 || j == 0) {
      Board[i][j] = ' ';
    } else {
      Board[i][j] = '_';
    }
  }
}

This will loop through every element of the array and, if the column or row number is 0, fill it with a ' ' , or otherwise fill it with a '_' . 这将循环遍历数组的每个元素,如果列或行号为0,则用' '填充它,或者用'_'填充它。

If you only care about the bottom right 3x3 grid then you could do: 如果您只关心右下方的3x3网格,那么您可以这样做:

for (int i = 1; i < 4; i++) {
  for (int j = 1; j < 4; j++) {
    Board[i][j] = '_';
  }
}

But then I recommend declaring Rows and Columns as 3 instead. 但后来我建议将RowsColumns声明为3 If you want your user to enter row and column numbers starting from 1, just translate from {1, 2, 3} to {0, 1, 2} when you access the array. 如果您希望用户输入从1开始的行号和列号,则只需在访问阵列时从{1,2,3}转换为{0,1,2}。

Put the code into separate function 将代码放入单独的函数中

void game()
{
   const int Rows = 4;
   // ...
}

and invoke it from the game controller 并从游戏控制器调用它

bool replay;
do
{
    game();
    cout << "Replay? (0 - no, 1 - yes)";
    cin >> replay;
} while(replay);

This method restores the whole gave environment. 这种方法恢复了整个给定的环境。

Use List class instead of traditional multidimensional arrays. 使用List类而不是传统的多维数组。 The elements of the object of this class can be easily cleared and removed. 可以轻松清除和删除此类对象的元素。 Moreover, the size of the list is dynamical and changeable. 此外,列表的大小是动态的和可变的。 It is not necessary to specify the size when creating an object. 创建对象时无需指定大小。 Try to define a two dimensional list. 尝试定义二维列表。

List<List<char>> Board = new List<List<char>>;

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

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