c++ - 如何在程序中改变多维数组的大小?

标签 c++ arrays multidimensional-array size

嘿,我想知道如何在游戏中设置允许用户通过更改数组值来设置“游戏板”的大小。这是代码。我知道代码很乱,但这是我的第一个程序。

#include "stdafx.h"
#include "iostream"
#include "string"
#include "cstdlib"
#include "ctime"

int xRan;
int choicei = 17;
int choicej = 17;
const int row = 15;
const int col = 16;
int play = 0;


void fill(char Array[row][col]);

int main()
{
    int play = 0;
    char Array[row][col];

    srand((unsigned int)time(0));

    xRan = rand() % 15 + 1;

    if (play == 0)
    {
        std::cout << "1. To Play Treasure Hunt!" << std::endl;
        std::cout << "2. How To Play Treaure Hunt!" << std::endl;
        std::cout << "3. Treaure Hunt Settings! (Comming Soon)\n" << std::endl;
        std::cin >> play;
        std::cout << "-----------------------------------------------------------------------" << std::endl;
    }

    if (play == 2)
    {
        std::cout << "1. Select a row number. Be sure to make it less than or equal to " << row << "!" << std::endl;
        std::cout << "2. Select a column number. Be sure to make it less than or equal to " << col << "!" << std::endl;
        std::cout << "3. If you see the 'X' you have won! If you see the 'O' you lose!" << std::endl;
        std::cout << "-----------------------------------------------------------------------\n" << std::endl;
        std::cin >> play;
    }

    if (play == 3)
    {
        std::cout << "\nComming Soon!" << std::endl;
        std::cout << "-----------------------------------------------------------------------\n" << std::endl;
        std::cin >> play;
    }

    while (choicei > row || choicej > col || choicei < 1 || choicej < 1)
    {
        std::cout << "\nEnter The Row Number Less Than Or Equal To " << row << "!" << std::endl;
        std::cin >> choicei;
        std::cout << std::endl;
        std::cout << "Enter The Column Number Less Than Or Equal To " << col << "!" << std::endl;
        std::cin >> choicej;
        std::cout << "\n-----------------------------------------------------------------------" << std::endl;
        if (choicei > row || choicej > row)
        {
            std::cout << "Make Sure The Row And Column Numbers Are Less Than Or Equal To " << row << "and" << col << "!\n" "---------------------------------------------------------------------- - " << std::endl;
        }
        if (choicei < 1 || choicej < 1)
        {
            std::cout << "Make Sure The Row And Column Numbers Are More Than Or Equal To 1" << "!\n" "-----------------------------------------------------------------------" << std::endl;
        }

    }

    fill(Array);

    std::cout << std::endl;

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            std::cout << Array[i][j] << " ";
        }
        std::cout << std::endl;
    }

    if (xRan > 11)
    {
        std::cout << "\nCongratulations! You Won!\n" << std::endl;
    }
    else
    {
        std::cout << "\nBetter Luck Next Time!\n" << std::endl;
    }

}


void fill(char Array[row][col])
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Array[i][j] = '*';
        }
    }

    if (xRan > 11)
    {
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < col; j++)
            {
                Array[choicei - 1][choicej - 1] = 'X';
            }
        }
    }
    else
    {
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < col; j++)
            {
                Array[choicei - 1][choicej - 1] = 'O';
            }
        }
    }

}

提前谢谢你。

最佳答案

普通数组无法做到这一点。你应该使用动态数组,例如 std::vector http://www.cplusplus.com/reference/vector/vector/

关于c++ - 如何在程序中改变多维数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20996616/

相关文章:

c++ - 如何在代码中引用资源文件夹

java - 输入验证后如何显示菜单?

c# - 使用 LINQ 比较两个数组

multithreading - C++ AMP 中 1D 和 2D 数组上的运算符 [] 的行为。

php - 如何将多维数组转换为PHP中的对象?

c - 如何为指向 char * 数组的指针分配内存?

c++ - Boost 单元测试因 sigabrt 而失败

c++ - 如何使用 SDL 修改像素?

c++ - 与 MKL 一起使用的数组类型

c - 将 C 数组作为单独的值传递到函数中