fruit: fixed bug where fruit could generate under tail
This commit is contained in:
+10
-13
@@ -8,22 +8,24 @@ void Point::set_new_point(int row, int col, int dir) {
|
||||
m_direction = dir;
|
||||
}
|
||||
|
||||
Fruit::Fruit() {
|
||||
std::srand(std::time(0));
|
||||
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
|
||||
Fruit::Fruit(char (&board)[ROW][COL]) {
|
||||
int row = (rand() % ROW);
|
||||
int col = (rand() % COL);
|
||||
m_point.set_new_point(row, col, 0);
|
||||
}
|
||||
|
||||
void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head, int &score) {
|
||||
int row, col;
|
||||
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
|
||||
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
|
||||
do {
|
||||
row = (rand() % ROW);
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col] != '.');
|
||||
m_point.set_new_point(row, col, 0);
|
||||
score++;
|
||||
}
|
||||
}
|
||||
|
||||
Head::Head() {
|
||||
m_head.set_new_point(5, 5, 2);
|
||||
}
|
||||
|
||||
void Head::get_new_pos() {
|
||||
if (m_head.m_direction == -1) {
|
||||
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
|
||||
@@ -40,11 +42,6 @@ void Head::get_new_pos() {
|
||||
}
|
||||
}
|
||||
|
||||
Body::Body() {
|
||||
m_score = 0;
|
||||
m_collision = false;
|
||||
}
|
||||
|
||||
void Body::check_collision(Head &head) {
|
||||
for(int i = 0; i < m_body.size(); i++) {
|
||||
if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) {
|
||||
|
||||
Reference in New Issue
Block a user