Chapter 9: Arrays

See, using normal variables is useful, but sometimes, you can't just have a variable for every single thing, but instead, we use something called arrays.

Arrays can have a varied amount of data inside of it, and you can change it on the fly, without creating a variable for each element.

Imagine this scenario. You want to create a guess the number where the user can set how many players can play, and each player gets a turn, but you encounter a problem, you can't know how many players the user wants to create a variable for each one, and even if you could, that will make your code look very ugly.

The solution is an array.

Using arrays.

An array is a variable that contain multiples of a specific type, arrays need you to tell them the size, how much data will go in.

Sounds scary? Nope, it's not.

What all that meant is basically, in our scenario, when the user is asked how many players are there, you're supposed to take the amount the user gives you and make an array with that size. So you could contain every player's guess to compare against later.

Let's look at how it works.

Random random = new();
int number = random.Next(1, 101);
Console.WriteLine("How many players will play?");
int amount = int.Parse(Console.ReadLine());
int[] players = new int[amount];

See, not scary, you just know the amount then make a container, array in this scenario, and tell it you want it to contain this much.

The brackets are necessary so the language can know this is an array. And then when creating, we put the amount(size) in brackets. You could even play with it and give it a number directly, try it, put 1, or 2 inside.

But still, we do nothing with it, yet, so, let's fix that.

First, we need to give everyone their turns, let them venture a guess, and when all is said and done, we find out who guessed correctly, and who did not.

Random random = new();
int number = random.Next(1, 101);
Console.WriteLine("How many players will play?");
int amount = int.Parse(Console.ReadLine());
int[] players = new int[amount];
for (int i = 0; i < players.Length; i++)
{
    Console.WriteLine($"Player {i + 1}'s turn");
    int guess = int.Parse(Console.ReadLine());
    players[i] = guess;
}
  • we use a for loop to move through every player's variable inside the array, the logic we used is, for (int i = 0; i < players.Length; i++) IF you're lost you can return to the loops chapter, but we tell it the starting counter is 0, we tell it keep going until we encounter the end of the array, then we tell it to increment by one each time.
  • inside the loop, we display a message for the specific player, telling them which player is it using the for loopp's increment variable, and tell them their turn, some of you will be confused why does it have a + 1 in there, but that will be explain below.
  • we then take the guess from the player as usual, and then, we put it inside of the array, we use i, the increment variable for the for loop to tell the array which player is this for.

But there are a few confusing things in here.

  1. We use i, the increment variable, both for the array and player identification, this works because, we always try to match the player position in the array, if you remember how our for loop works, and remember how for loops worked, our condition insures that we always walk through the array step by step.
  2. Using i + 1 in the Console.WriteLine, we do this because, unlike what you'd expect, arrays count their containing elements from 0, not from 1, which is also why we did start the for loop counter with the value 0.

Ok, that's all nice and good, but it just asks for turns then does nothing. Yes, that's because we didn't check who guessed right and who guessed wrong.

So, here's what we should do.

Random random = new();
int number = random.Next(1, 101);
Console.WriteLine("How many players will play?");
int amount = int.Parse(Console.ReadLine());
int[] players = new int[amount];
for (int i = 0; i < players.Length; i++)
{
    Console.WriteLine($"Player {i + 1}'s turn");
    int guess = int.Parse(Console.ReadLine());
    players[i] = guess;
}
for (int i = 0; i < players.Length; i ++)
{
    if (players[i] == number)
    {
        Console.WriteLine($"Player {i + 1} guessed correctly!");
    }
    else
    {
        Console.WriteLine($"Player {i + 1} guessed incorrectly!");
    }
}

so, what we did here basically is, as a whole.

  1. We made the first for loop, where we give everyone their turns, and let them guess.
  2. Then, after we're done letting everyone guess, we made yet another for loop, where we checked all the array elements and if a player guessed right, we say so, and if a player guessed wrong, we also say so.
  3. The first loop shows you how to store something in an array, you put the array variable's name, then you put 2 brackets, inside them you specify which element in the array you want to set a value for, then you specify the value itself after an equals, like normal variables, The second shows you how to get a value out of an element in an array, doing the same as setting, but the difference is there is no need for an equals then the value to set.

And that's done. Congratulations, you made your game playable for multiple people.

next: Inheritence.