-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFood.cpp
More file actions
69 lines (58 loc) · 1.86 KB
/
Copy pathFood.cpp
File metadata and controls
69 lines (58 loc) · 1.86 KB
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
65
66
67
68
69
#include "Food.h"
Food::Food()
{
foodBin = new objPosArrayList(); //food storage list
}
Food::~Food() //memory clean up
{
delete foodBin;
foodBin = nullptr;
}
void Food::generateFood(const objPosArrayList* blockOff) { //checks where to spawn food
int xCoord, yCoord;
bool validCoords;
// Clear the existing food list
while (foodBin->getSize() > 0)
{
foodBin->removeTail();
}
// Generate 3 food items
for (int i = 0; i < FOOD_NUMBER; i++)
{
validCoords = false;
// Decide if this is a special food (20% chance for one special food)
bool isSpecialFood = (i == 0 && rand() % 5 == 0);
// Keep trying until valid coordinates are found
while (!validCoords)
{
validCoords = true;
xCoord = rand() % (BOARD_X - 2) + 1; // Within board bounds
yCoord = rand() % (BOARD_Y - 2) + 1;
// Check against blocked positions (snake body)
for (int j = 0; j < blockOff->getSize(); j++)
{
if (xCoord == blockOff->getElement(j).pos->x && yCoord == blockOff->getElement(j).pos->y)
{
validCoords = false;
break;
}
}
// Check against existing food positions
for (int j = 0; j < foodBin->getSize(); j++)
{
if (xCoord == foodBin->getElement(j).pos->x && yCoord == foodBin->getElement(j).pos->y)
{
validCoords = false;
break;
}
}
}
// Add the new food to the food list
char foodSymbol = isSpecialFood ? specialFoodSymbol : foodSymbols[i % 3];
foodBin->insertTail(objPos(xCoord, yCoord, foodSymbol));
}
}
objPosArrayList* Food::getFood() const
{
return foodBin;
}