-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.cpp
46 lines (38 loc) · 1.28 KB
/
food.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
#include <iostream>
#include <ncurses.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <cstdlib>
#include <ctime>
#include "snake.h"
#include "food.h"
Food::Food(){ //default constructor
xLocation = 0;
yLocation = 0;
}
Food::Food (int x, int y){ //constructor
xLocation = x;
yLocation = y;
}
Food::Food (const Food& iFood){ //copy constructor
xLocation = iFood.xLocation;
yLocation = iFood.yLocation;
}
Food::~Food(){
std::cout << "Food destroyed" << std::endl;
}
void Food::drawFood(Board& gBoard){ //draws a food piece on the board array
xLocation = rand() % (gBoard.width-2); //xrand is a random number between 0 and xMax
yLocation = rand () % (gBoard.height-2); //yrand is a random number between 0 and yMax
xLocation += 1; //prevents food from spawning on the border
yLocation += 1;
while (gBoard.board [xLocation][yLocation] != ' '){ //food cannot spawn on taken pieces
xLocation = rand() % (gBoard.width-2); //xrand is a random number between 0 and xMax
yLocation = rand () % (gBoard.height-2); //yrand is a random number between 0 and yMax
xLocation += 1; //prevents food from spawning on the border
yLocation += 1;
}
gBoard.editBoard(xLocation,yLocation,'F');
gBoard.drawBoard();
}