Home > Blockchain >  Method, with a foreach loop in a mini game
Method, with a foreach loop in a mini game

Time:10-22

I'm new to coding and I'm making a mini game that depends on different methods (that is the assignment).

In the game there are 3 piles of sticks with 5 sticks in each. I'm trying to make a method that prints the stacks of sticks.

I have made an array with int[] sticks = new int[] = {5, 5, ,5}; (3 stacks of 5).

I want to make it visual in the console so the player can see amount of sticks etc.

If I use a method like this can I change the value in the array as they play or do I need to make another method and put it in this one?

static void Stickor(int[] array)
{
    foreach (var item in array)
    {
        for (int i = 0; i <= array.Length; i  )
        {
            Console.Write(" | ");
        }
    }
}

CodePudding user response:

If you use Lists you will find it easier.

using System;
using System.Collections;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        var stacks = new List<int>() {5, 5, 5};
        PrintStacks(stacks);
        Console.WriteLine();
        
        stacks[0] = 2;      
        PrintStacks(stacks);
        Console.WriteLine();
                
        stacks[1] = 10;     
        PrintStacks(stacks);
        Console.WriteLine();
    }
    static void PrintStacks(List<int> stacks)
    {
        foreach (var totalSticks in stacks)
        {
            Console.Write(totalSticks   ": ");
            for (int i = 0; i < totalSticks; i  )
            {
                Console.Write(" | ");
            }
            Console.WriteLine();
        }
    }
}

Output:

5:  |  |  |  |  | 
5:  |  |  |  |  | 
5:  |  |  |  |  | 

2:  |  | 
5:  |  |  |  |  | 
5:  |  |  |  |  | 

2:  |  | 
10:  |  |  |  |  |  |  |  |  |  | 
5:  |  |  |  |  |

FYI, I changed the for loop in two ways:

  • <= to < (arrays/lists are 0 indexed, so less than or equal to would end up looping once more than needed, because 0 is counted).
  • array.Length was changed to the value of the enumerated item in the foreach loop (totalSticks). You would need to do this even if using arrays as that is what contains the amount of sticks. array.Length will always return 3 in your example, because that is the total amount of stacks, not sticks.

Lists work pretty much the same way as arrays, in that you can access them by indices as shown above (stacks[0], stacks[1], etc.).

It gives another benefit of being able to add or remove extra items without redefining the array, using stacks.Add(amountOfSticks), etc.

If you want to use arrays, basically the same code will still work. If you want to modify the amount of sticks in the array, just use array[x] = newAmountOfSticks;:

using System;
                    
public class Program
{
    public static void Main()
    {
        var stacks = new int[3] {5, 5, 5};
        PrintStacks(stacks);
        Console.WriteLine();
        
        stacks[0] = 2;      
        PrintStacks(stacks);
        Console.WriteLine();
                
        stacks[1] = 10;     
        PrintStacks(stacks);
        Console.WriteLine();
        
        
    }
    static void PrintStacks(int[] stacks)
    {
        foreach (var totalSticks in stacks)
        {
            Console.Write(totalSticks   ": ");
            for (int i = 0; i < totalSticks; i  )
            {
                Console.Write(" | ");
            }
            Console.WriteLine();
        }
    }
}

CodePudding user response:

Alfie's answer will fix your issue, however he's using a generic list. If you want to keep using an array of integers, all you need to is to change array.Length to item in your for-loop. Also the i variable needs to start at 1 instead of 0, otherwise it will print out 6 times instead of 5 times since you're using LTE operator in your for-loop condition.

  •  Tags:  
  • c#
  • Related