-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fruit.cpp
64 lines (53 loc) · 1.41 KB
/
Fruit.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// MIT License
//
// Copyright (c) 2024 Tomas Mark
//
#include "Fruit.hpp"
#include <cstdlib>
Fruit::Fruit(double fruitEmptiness, int totalW, int totalH)
: totalW(totalW), totalH(totalH),
fruitXArr(std::make_unique<int[]>(totalW * totalH)), // contain coords in array of int which mean ammount of fruits
fruitYArr(std::make_unique<int[]>(totalW * totalH)), // contain coords in array of int which mean ammount of fruits
totalFruits(0)
{
int smartAlocMemory = totalW * totalH / fruitEmptiness;
// seed fruit
for (int i = 0; i < smartAlocMemory; i++)
this->addFruitItem();
}
Fruit::~Fruit()
{
fruitXArr.reset();
fruitYArr.reset();
}
const int *Fruit::getFruitX() const
{
return fruitXArr.get();
}
const int *Fruit::getFruitY() const
{
return fruitYArr.get();
}
int Fruit::getTotalFruit() const
{
return this->totalFruits;
}
int Fruit::getRandomFruitX() const
{
return (rand() % (totalW - 2)) + 1;
}
int Fruit::getRandomFruitY() const
{
return (rand() % (totalH - 2)) + 1;
}
void Fruit::addFruitItem()
{
this->fruitXArr[this->totalFruits] = getRandomFruitX();
this->fruitYArr[this->totalFruits] = getRandomFruitY();
this->totalFruits++;
}
void Fruit::refreshFruit(int refreshIndex)
{
this->fruitXArr[refreshIndex] = getRandomFruitX();
this->fruitYArr[refreshIndex] = getRandomFruitY();
}