find empty inventory slots

I made two dimensional array to represent my (game) inventory.
Slot is empty if its value is 0 and 1 if not.
Now i need to find if i can put new item in my inventory that can take up 4x2 slots. I tried this but it doesn't work as i expected:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

int main()
{
	const int inventorySlotRows    = 5;
	const int inventorySlotColumns = 5;

	int arr[inventorySlotRows][inventorySlotColumns] = 
	{ 
		{ 1, 1, 1, 1, 1 },
		{ 0, 1, 1, 0, 1 },
		{ 1, 0, 1, 0, 0 },
		{ 1, 0, 0, 0, 0 },
		{ 1, 0, 0, 0, 0 }
	};

    // needed empty slots for new item
	int emptySlotsRows    = 4;
	int emptySlotsColumns = 2;

	bool have = false;

	for(int i = 0; i < inventorySlotRows; ++i)
	{
		for(int j = 0; j < inventorySlotColumns; ++j)
		{
			if(0 == arr[i][j] && (i + emptySlotsRows) <= inventorySlotRows && (j + emptySlotsColumns) <= inventorySlotColumns)
			{
				int sum = 0;
				for(int k = 0; k < emptySlotsRows; ++k)
				{
					for(int l = 0; l < emptySlotsColumns; ++l)
					{
						sum += arr[i+k][j+l];
					}
				}

				have = (sum == 0) ? true : false;
			}
		}
	}

	std::cout << std::boolalpha << have << std::endl;

	return 0;
};


How would you do this?

Thank you for your time.
Last edited on
There is no space in the inventory for an item that takes 4 rows and 2 columns, but there is space for an item that takes 2 rows and 4 columns. Your algorithm seems to work fine except that it will not stop when have becomes true so what it really does at the moment is to print if the item can be placed in the bottom right corner of the inventory.
Yea, i got all wrong with rows and columns, should be 2x4 to find.
Looks like i need a goto there to break out of loops.
Topic archived. No new replies allowed.