0

I'm writing a mastermind game and I need to update the value of an array size using a variable inside a while loop which increments on each loop is there any way i can do this?

bool game = false;
        do
        {
            int codeSize;
            int colourSize;
            int guessNumber = 1;
            int userGuess;
            int black = 0;
            int white = 0;
            int count = 1;

            Console.WriteLine("Welcome to Mastermind coded by ****");
            Console.Write("How many positions  > ");
            codeSize = Convert.ToInt32(Console.ReadLine());
            Console.Write("How many colours    > ");
            colourSize = Convert.ToInt32(Console.ReadLine());
            Random rand = new Random();
            int[] code = new int[codeSize];
            int[] guess = new int[codeSize];

            for (int i = 0; i < codeSize; i++)
            {
                code[i] = rand.Next(1, colourSize + 1);//filling the secret code array
            }
            Console.WriteLine("O.k. - I've generated a code -- guess it!");

            while (black < codeSize)
            {
                int[,] history = new int[count, codeSize + 2];

                Console.WriteLine("Next guess please.");
                for (int n = 0; n < codeSize; n++)
                {
                    Console.Write("Position " + guessNumber + " >");
                    userGuess = Convert.ToInt32(Console.ReadLine());

                    guess[n] = userGuess;

                    history[count - 1, n] = guess[n];

                    guessNumber++;
                }
                for (int x = 0; x < codeSize; x++)
                {
                    int caseSwitch = 1;
                    switch (caseSwitch)
                    {
                        case 1:
                            {
                                if (guess[x] == code[x])
                                {
                                    black++;
                                    break;
                                }
                                goto case 2;
                            }
                        case 2:
                            {
                                if (guess[x] == code[x])
                                {
                                    break;
                                }

                                int i = 0;
                                while (i < codeSize)
                                {
                                    if ((guess[x] == code[i]) && (guess[i] != code[i]))
                                    {
                                        white++;
                                        break;
                                    }
                                    i++;
                                }
                                break;
                            }
                    }
                }
                guessNumber = 1;

                if (black == codeSize)
                {
                    white = 0;
                }
                history[count - 1, codeSize + 1] = white;
                history[count - 1, codeSize] = black;
                count++;
                Debug.WriteLine("-----------\nSecret code\n-----------");
                for (int x = 0; x < codeSize; x++)
                {
                    Debug.WriteLine(code[x]);
                }
                Console.WriteLine("Correct positions : {0}", black);
                Console.WriteLine("Correct colours   : {0}\n", white);
                Console.WriteLine("History");

                for (int t = 1; t < codeSize + 1; t++)
                {
                    Console.Write(t + " ");
                }
                Console.WriteLine("B W");
                for (int g = 0; g < codeSize + 3; g++)
                {
                    Console.Write("--");
                }
                Console.Write("\n");
                for (int t = 0; t < count - 1; t++)
                {
                    for (int g = 0; g < codeSize + 2; g++)
                    {
                        Console.Write("{0} ", history[t, g]);
                    }
                    Console.WriteLine("\n");
                }
                if (codeSize > black)//reseting values for next turn
                {
                    black = 0;
                    white = 0;
                }
            }
            int play;
            Console.WriteLine("\nYou Win!\n\nPress 1 to play again or any other number to quit");
            play = Convert.ToInt32(Console.ReadLine());
            if (play == 1)
                game = true;

        } while (game == true);
6
  • 1
    Please remove the noise in your post next time. Hundreds of dashes that obscure your actual question won't get you the help you require. Commented Feb 26, 2014 at 0:13
  • its the count variable that i need to update. the above code has the array inside the while loop where i need it outside so that a new array isnt created each loop. is there any way to not create a new one each loop or to update the value? Commented Feb 26, 2014 at 0:19
  • I'd also work on your naming. If you can't include "the game is false" in a sentence, it's probably named badly. Same goes for the play variable which apparently means whatever the user typed in at the end. A variable should be named in a way that makes sense. Don't even get me started on 'if code size is greater than black'. Commented Feb 26, 2014 at 0:20
  • @user3253363 when you say update the value of an array, are you talking about the length of the array? Are you trying to expand it? Commented Feb 26, 2014 at 0:21
  • i want the rows to increase by 1 after each time the while loop goes round Commented Feb 26, 2014 at 0:23

4 Answers 4

1

Arrays have a fixed size when you declare them and you cannot change the size afterwards without creating a new array. Try using a strongly typed List instead.

List<int> MyList = new List<int>();
// Add the value "1"
MyList.Add(1); 

or the following for a table:

List<List<int>> MyTable = new List<List<int>>();
// Add a new row
MyTable.Add(new List<int>());
// Add the value "1" to the 1st row
MyTable[0].Add(1);
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you are asking whether you can change the length property of an array from within a loop, extending it as required.

Directly, no. There are helpers and classes which provide for such functionality, allocating more memory as needed, but I doubt this is what you really need. You could try using an array of fixed dimensions (the maximum codeSize your program will tolerate or expect), and then an integer next to it to record the length/position.

Alternatively, if you really need to expand to arbitrary sizes and store all codes, just use a List.

List<int[]> theList = new List<int[]>();
theList.Add(code);

Creates a list of integer arrays (your codes) that you can keep adding onto, and index just like any simple array.

1 Comment

the List<int[]> was the method i used. worked perfectly thanks :)
0

There is a way to resize array size:

Array.Resize<T>

method. Details there: http://msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx

But it's usually a pretty bad idea to resize the arrays loop based. You need to select another data structure to save your data or i.e. create an array of bigger size filled i.e. with zeroes.

Comments

0

You also need to add the items to the list as so. The list will dynamically grow based on it's size.

int myInt = 6;
List<int> myList = new List<int>();
myList.Add(myInt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.