使用函数和二维数组的 Tic-Tac-Toe BIG 棋盘

Tic-Tac-Toe BIG board using functions and 2D arrays

我已经尝试解决这个问题一个多星期了。 我的任务是计算(显示)一个看起来像这样的大井字棋盘:

0000 | 1111 | 2222
0000 | 1111 | 2222
0000 | 1111 | 2222
0000 | 1111 | 2222
------------------
3333 | 4444 | 5555
3333 | 4444 | 5555
3333 | 4444 | 5555
3333 | 4444 | 5555
------------------
6666 | 7777 | 8888
6666 | 7777 | 8888
6666 | 7777 | 8888
6666 | 7777 | 8888

说明中建议的一些功能是: "populateBigArray()//fills the 12X12 array with vales 0-8 assignValueToBigArray()// used to populate the 4X4 section of the 12X12 array which corresponds to a specific move by either the human or the computer"

我觉得我理解二维数组并且我能够打印一个 4x4 的数字块。但是,我尝试的所有操作都会导致块直接向下打印,而不是 3x3 结构或所有数字打印在一长行中。

我得到的最接近的是下面的函数,但是,它并没有完全使用二维数组,这是分配的重点,而且我知道一旦玩家选择了一个点就试图操纵它是'这真的不可能。

//Global constants
const int BOARD_ROWS = 3;
const int BOARD_COLS = 3;
int main()
{
    displayBoard();
    return 0;
}
    void displayBoard()
    {
        int spot = 0;
        for (int row = 0; row < BOARD_ROWS; row++)
        {
            for (int col = 0; col < BOARD_COLS; col++)
            {
                cout << spot << spot << spot << spot
                    << " | " << (spot + 1) << (spot + 1) << (spot + 1) << (spot + 1)
                    << " | " << (spot + 2) << (spot + 2) << (spot + 2) << (spot + 2)
                    << endl;
            }
            cout << "------------------" << endl;
            spot = spot + 3;
        }
    }//end displayBoard

我绝对是新手,感谢任何帮助。谢谢!!!

嗯,你没有写建议的函数,也没有使用二维数组,"which is the point of the assignment"正如你所说...

也许你可以从二维数组开始,看看函数的样子:

#include <iostream>
using std::cout;

const int BOARD_SIZE = 3;
const int SECTION_SIZE = 4;
constexpr int BIG_ARRAY_DIM = BOARD_SIZE * SECTION_SIZE;

void populateBigArray( int b[][BIG_ARRAY_DIM] );
void assignValueToBigArray( int b[][BIG_ARRAY_DIM], int r, int c, int val );
void printBigArray( int b[][BIG_ARRAY_DIM] );

int main() {
    int board[BIG_ARRAY_DIM][BIG_ARRAY_DIM];

    populateBigArray(board);

    printBigArray(board);
    return 0;
}

然后你可以实现这些功能,从这样的事情开始:

void populateBigArray( int b[][BIG_ARRAY_DIM] ) {
    int k = 0;
    for ( int i = 0; i < BOARD_SIZE; ++i ) {
        for ( int j = 0; j < BOARD_SIZE; ++j ) {
            assignValueToBigArray(b,i,j,k);
            ++k;
        }
    }
}

void assignValueToBigArray( int b[][BIG_ARRAY_DIM], int r, int c, int val ) {
    int row = r * SECTION_SIZE;
    int col = c * SECTION_SIZE;
    for ( int i = 0; i < SECTION_SIZE; ++i ) {
        for ( int j = 0; j < SECTION_SIZE; ++j ) {
            b[row + i][col + j] = val;
        }
    }
}

void printBigArray( int b[][BIG_ARRAY_DIM] ) {
    for ( int i = 0, l = 1; i < BIG_ARRAY_DIM; ++i, ++l ) {
        for ( int j = 0, k = 1; j < BIG_ARRAY_DIM; ++j, ++k ) {
            cout << b[i][j];
            if ( k == SECTION_SIZE ) {
                if ( j == (BIG_ARRAY_DIM - 1) ) {
                    cout << '\n';
                } else {
                    cout << " | ";
                    k = 0;
                }
            }
        }
        if ( l == SECTION_SIZE  &&  i != (BIG_ARRAY_DIM - 1) ) {
            cout << "------------------\n";
            l = 0;
        }
    }
}