CodeBlanks Logo Icon CodeBlanks

Matching and stacking

Learn to match and stack identical items.

The stack idea

Items of the same type are gathered in one slot as a stack. For example 5 apples sit in one slot; the slot count does not rise. Every slot has a stack limit and this limit is not exceeded.

int yiginAdedi = 5;
int yiginLimiti = 10;

This way inventory space is not wasted.

Matching

Understanding whether two items are the same is called matching. If the item type and the name are the same, they can be merged. Items of different types never enter the same stack.

bool eslesme = (birAd == ikiAd);

Matching is the first step of the merge operation.

Practice Merge the stacks

In a class named YiginBirlestir, define an int birAdet field with the value 3 and an int ikiAdet field with the value 5. In the Start method, add these two values and assign the result to an integer named toplam. Print the text "Toplam yigin: " joined with toplam using Debug.Log.

Starter code

Player.cs
using UnityEngine;

public class YiginBirlestir : MonoBehaviour
{
    // int birAdet alanini 3, int ikiAdet alanini 5 degeriyle tanimla

    void Start()
    {
        // birAdet ile ikiAdet degerlerini toplayip toplam degiskenine ata
        // "Toplam yigin: " metnini toplam ile birlestirip yazdir
    }
}

Sample solution

Player.cs
using UnityEngine;

public class YiginBirlestir : MonoBehaviour
{
    int birAdet = 3;
    int ikiAdet = 5;

    void Start()
    {
        int toplam = birAdet + ikiAdet;
        Debug.Log("Toplam yigin: " + toplam);
    }
}

Expected output

Toplam yigin: 8

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens when 5 apples are added to the inventory?

Player.cs
int yiginAdedi = 5;
  • The 5 apples stack in a single slot Correct answer
  • The slot count rises automatically
  • 5 separate slots are used
  • The items do not fit in the inventory

Explanation: Items of the same type stack in one slot; the slot count rises with the number of types, not with the item count.

2 Complete the merge operation

Question
    int toplamYigin = 3 ____ 5;
Answer
    int toplamYigin = 3 + 5;

Explanation: When two stacks are merged their counts are added, so the plus operation is used.

3 Merge terms

Term Meaning
Stack A pile of the same items
Merge Combining two stacks
Match Finding that items are the same
Yığın limiti The maximum count a slot carries

All lessons in Advanced