0

Hey I was wondering how I could have settings in-game which would allow the user to set the size of the 'game-board' by changing the array values. Here is the code. I know the code is messy and over the place but it is my first program.

#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';
            }
        }
    }

}

Thank you in advance.

Danturr
  • 11
  • 3

3 Answers3

3

you can't do that with ordinary arrays. you should use dynamic arrays, for example std::vector http://www.cplusplus.com/reference/vector/vector/

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
1

Actually, what you want to do can be done in C, not in C++: C++ requires array dimensions to be compile time constants, C can use any runtime value.

If you stay in C++, you should take a look at vector<>. If, however, you choose to use C you can simply remove the const from the declaration of row and col.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
0

You may find this answer useful. It lists several methods to create dynamic arrays.

Quoting the answer :

In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic about following the C++ standard)

Based on the suggestions, here are some ways you could initialize it (ignoring how you take the input value) :-

vector<vector<char>> Array(row, vector<char>(col));

or

char **Array = new char*[row];
for(int i = 0; i < row; i++)
{
  Array[i] = new char[col];
}

UPDATE

Based on the comments, I am adding how to use the vector method and use it with the function 'fill'. fill uses reference while fill_with_ptr makes use of pointer. Although I list both the methods, I strongly recommend the one using reference.

void fill(vector<vector<char> >& array);
void fill_with_ptr(vector<vector<char> >* array);
int main()
{
   ...
   cin >> row;
   cin >> col;
   vector<vector<char> > Array(row, vector<char>(col));
   ...
   fill (Array); // or fill_with_ptr(&Array);
}
void fill(vector<vector<char> >& array)
{
   ... // access elements as array[i][j]
}
void fill_with_ptr(vector<vector<char> >* array)
{
   ... // access elements as (*array)[i][j]
}
Community
  • 1
  • 1
rajatkhanduja
  • 974
  • 1
  • 9
  • 28
  • So later on in the code when I print out the Array, would I keep as is? Also, would this allow me to enter characters in the Array when printed out? – Danturr Jan 08 '14 at 22:21
  • Sorry, I used `int` instead of `char` by mistake. I have edited that. Now, Array can hold characters. And yes, using either of the above methods, the code to access the elements in the Array would remain the same, i.e. Array[i][j] refers to the element in the i-th row and j-th column. – rajatkhanduja Jan 09 '14 at 02:06
  • And were do I introduce the code? When I put it in int_main the prototype of int_fill and int_fill both have errors. It still wants the expression to have a constant value. – Danturr Jan 09 '14 at 04:12
  • If you want to pass the array to another function, you could use `void fill(char **Array);` However, since this might make things prone to error and you would also need to pass other parameters such as row and column (unless they remain global, which by itself might not be the best decision), I would suggest using the vector of vector method. So, fill would change as `void fill(vector > Array)` or `void fill(vector > &Array)` if you want to pass the reference instead of a copy of the Array. – rajatkhanduja Jan 09 '14 at 07:38
  • I have updated the answer to include the contents of the previous comment. Since you need the operation to be performed on the array itself (and not its copy), you need to make use of references (or you could use pointers). – rajatkhanduja Jan 09 '14 at 07:48