Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用函数和二维阵列的Tic-Tac-Toe bigboard_C++_Arrays_Function_Console Application_Cout - Fatal编程技术网

C++ 使用函数和二维阵列的Tic-Tac-Toe bigboard

C++ 使用函数和二维阵列的Tic-Tac-Toe bigboard,c++,arrays,function,console-application,cout,C++,Arrays,Function,Console Application,Cout,我已经试着解决这个问题一个多星期了。 我的任务是展示一个大的tic-tac趾板,看起来像这样: 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

我已经试着解决这个问题一个多星期了。 我的任务是展示一个大的tic-tac趾板,看起来像这样:

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
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;
        }
    }
}
说明中建议的一些功能包括: “populateBigArray()//使用值0-8 assignValueToBigArray()填充12X12数组//用于填充12X12数组的4X4部分,该部分对应于人或计算机的特定移动”

我感觉我了解2D阵列,并且能够打印一个4x4数字块。然而,我尝试的每一种方法都会直接打印区块,而不是3x3结构或所有数字都打印在一条长线上

我得到的最接近的函数是下面的函数,然而,它并不完全使用2D数组,这是分配的点,我知道一旦玩家选择了一个点,尝试操纵一个点是不可能的

//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
//全局常量
线路板上的常数=3;
电路板常数=3;
int main()
{
显示板();
返回0;
}
无效显示板()
{
int spot=0;
用于(int row=0;rowcout好吧,您没有编写建议的函数,也没有使用2D数组,正如您所说的“这是赋值点”

也许您可以从2D数组开始,看看函数的外观:

#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;
}
#包括
使用std::cout;
const int BOUND_SIZE=3;
const int SECTION_SIZE=4;
constexpr int BIG\u ARRAY\u DIM=电路板大小*截面大小;
void populateBigArray(intb[]大数组大小];
void assignValueToBigArray(int b[]大数组,int r,int c,int val);
void printBigArray(int b[[BIG_ARRAY_DIM]);
int main(){
int板[BIG_ARRAY_DIM][BIG_ARRAY_DIM];
人口部落(委员会);
printBigArray(板);
返回0;
}
然后您可以实现这些功能,从以下内容开始:

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
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;
        }
    }
}
void populateBigArray(int b[[BIG\u ARRAY\u DIM]){
int k=0;
用于(int i=0;i当您使用调试器并一次执行一条语句时,是哪条语句导致了问题?您的问题是什么?您忘记了为
void displayBoard()提供声明
在main之前?谢谢。我编写了多个版本,使用了2D数组,但是,输出结果与此不符。这些块会一个接一个地排列起来,或者我只会有一行,而不是一个4x4数字的3x3块。我发布的代码是唯一正确显示电路板的版本,尽管我不会这样做我能够操纵它。我感谢你的帮助。我对assignValueToBigArray函数感到敬畏。@VanidadVelour不客气,很高兴你喜欢它。如果你认为这回答了你的问题,你可以使用它,如果你觉得有用,你可以投票。