"Ant with food on a board" Program Issue

So I've been working on this program in class for some time, adding to it little by little for every week's assignment (we'll be adding linked lists soon enough). Before I work on that part though, I would like to know how to fix an issue I am having with my code.
When I compile and output the program, it runs through the motions as it should (the "ant" symbol is supposed to autonomously move around the board and collect "food " (F)), but when the ant moves below the top row, it disappears from the board.
Can anyone help me solve this issue? Code is as follows:
Main Document:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
#include "ant.h"

using namespace std;

/*
 This program is designed to run a board approximately 20 by 20 spaces.  A symbol
 * is placed to represent the ant on the board (seen as "^", ">", "<", "v" depending
 * upon the directional orientation.
 * The ant moves according to various key inputs and will gather food pieces
 * randomly scattered across the board (seen as "F").  This food will add to its
 * overall energy level by five.
 */



//This void is designed to find the closest piece of food on the board by comparing
//the position of the ant to the location of every piece of food, designated by the 
//letter "F," and print the exact coordinates of the closest one.
void find_closest(int b[20][20], ant a, int &ax, int &ay){
    float closest_distance = 1000;
    float distance;
    //int ax,ay;
    for(int y = 0; y < 20; y++){
        for(int x = 0; x < 20; x++){
            if(b[x][y] == 1){
                distance = abs(x-a.GetX()) + abs(y-a.GetY());
                //cout << x << " " << y << " " << distance << endl;
                if(distance < closest_distance){
                    closest_distance = distance;
                    ax = x;
                    ay = y;
                }
            }
        }
    }
}


int main(int argc, char** argv) {
    srand(time(NULL));
    int x, y;
    int b, c;
    int food = 10;
    int board[20][20];
    ant a;
    //char action;

    /*This first pair of for loops is designed to initially set up the board
     as a 20x20 board with food scattered in random positions while other areas
     are displayed otherwise (as a ".").*/

    for (int y = 0; y < 20; y++) {
        for (int x = 0; x < 20; x++) {
                int food_appear = (rand() % food) + 1;
                if (food_appear > 1) {
            board[x][y] = 0;
        }
                else {
                    board[x][y] = 1;
                }
    }
    }
    
    /*The following is for entry of the string for movement as well as reprinting the board for each
     consecutive movement in the string.  Movement is done by entering "a" for left, "s" for right,
     and "w" to go forward.
     All movement occurs on the board, and after each movement the board is repeated so as to show
     the new position of the ant, as well as if food has disappeared (should the ant have passed
     over the top of a food piece during the previous turn).
     At the end of every string of movements, the program will print the final energy of the ant.*/

   /*    
        cout << "Move left(a), right(s), or forward(w)?" << endl;
        cin >> action;
    
    switch (action) {
        case 'a':
            a.Left();
            break;
        case 's':
            a.Right();
            break;
        case 'w':
            a.Forward();
            break;
    }
   */   
            while(a.GetEnergy() > 0){

    for (int x = 0; x < 20; x++) {
            cout << "-";
        }
    cout << endl;    
    for (int y = 0; y < 20; y++) {
        cout << "|";
        for (int x = 0; x < 20; x++) {
            if (a.GetX() == x && a.GetY() == y) {
                a.Draw();
            }
            else {
                if (board[x][y] == 1) {
                    cout << "F";
                }
                else {
                    cout << ".";
                }
            }
            if (board[x][y] == 1 && a.GetX() == x && a.GetY() == y) {
                a.ChangeEnergy();
                board[x][y] = 0;
            }
        }
        cout << "|";
        cout << endl;
}
    for (int x = 0; x < 20; x++) {
        cout << "-";
    }
    cout << endl;
        find_closest(board,a,x,y);
        cout << "Closest Location:  " << x << ", " << y << endl; //The exact coordinates of the closest food.
        b = x;
        c = y;
        a.Move(b, c);
        cout << endl;
        
            usleep(100000);
    }
    return 0;
    }


Header Function Document:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "ant.h"
#include <iostream>
#include <cstdlib>
using namespace std;
ant a;

//The following booleans are designed to change the position of the ant based upon
//math functions which change its orientation.  Additionally, energy is subtracted
//from the current energy level of the ant at the start of each boolean.
bool ant::Forward() {
    if (e_level >= 2) {
        e_level = e_level - 2;
        cout << e_level << endl;
    if (direction == 1) {
        y++;
    }
    else if (direction == 2) {
        x--;
    }
    else if (direction == 3) {
        y--;
    }
    else if (direction == 4) {
        x++;
    }
    return true;
}
    return false;
}

bool ant::Left() {
if (e_level >= 1) {
    e_level = e_level - 1;
    cout << e_level << endl;
    if (direction == 1) {
        direction = 2;
    }
    else if (direction == 2) {
        direction = 3;
    }
    else if (direction == 3) {
        direction = 4;
    }
    else if (direction == 4) {
        direction = 1;
    }
    return true;
}
return false;
}

bool ant::Right() {
    if (e_level >= 1) {
        e_level = e_level - 1;
        cout << e_level << endl;
    if (direction == 1) {
        direction = 4;
    }
    else if (direction == 4) {
        direction = 3;
    }
    else if (direction == 3) {
        direction = 2;
    }
    else if (direction == 2) {
        direction = 1;
    }
    return true;
}
    return false;
}

//ChangeEnergy works to change the energy level of the ant by adding five to it after
//it runs across a food character "F".
void ant::ChangeEnergy() {
    n = 5;
    e_level = e_level + n;
}

void ant::Draw() {
    // should return either '^' for north, '>' for East, 'v' for south, or '<' for west
    //in order to represent the ant on the board.
    switch (direction) {
        case 1:
            cout << '^';
            break;
        case 2:
            cout << '<';
            break;
        case 3:
            cout << 'v';
            break;
        case 4:
            cout << '>';
            break;
    }
}
//Reveals the energy level
int ant::GetEnergy() {
    return e_level;
}
//Reveals the current x value
int ant::GetX() {
    return x;
}
//Reveals the current y value
int ant::GetY() {
    return y;
}
bool ant::Move(int bx, int cy /*, int b[20][20]*/) {
   
    //bx is equal to the nearest food's x value and cy is the nearest food's y value.
    if (bx != GetX()) {
        if(bx > GetX()) {//East
            switch(direction){
        case 1:
            return Right();
            break;
        case 2:
            return Right();
            break;
        case 3:
            return Left();
            break;
        case 4:
            return Forward();
            break;
    }
        }
        else {//West
            switch(direction){
        case 1:
            return Left();
            break;
        case 2:
            return Forward();
            break;
        case 3:
            return Right();
            break;
        case 4:
            return Left();
            break;
    }
    }
    }
    else if (cy < GetY()){//North
    switch(direction){
        case 1:
            return Forward();
            break;
        case 2:
            return Right();
            break;
        case 3:
            return Right();
            break;
        case 4:
            return Left();
            break;
    }
    }
    else {//South
        switch(direction){
        case 1:
            return Left();
            break;
        case 2:
            return Left();
            break;
        case 3:
            return Forward();
            break;
        case 4:
            return Right();
            break;
    }
    }
}

//Initial position and conditions of the ant
ant::ant() {
    x = 0;
    y = 0;
    direction = 1;
    e_level = 50;
}
Character limit was reached, so this is to add on:


Header Document:
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
#ifndef ANT_H
#define	ANT_H
using namespace std;

class ant {
public:
    ant();
    int GetX();
    int GetY();
    int GetEnergy();

    bool Left();
    bool Right();
    bool Forward();
    void ChangeEnergy();
    void Draw();
    bool Move(int, int);

private:
    int x, y; //x and y values
    int direction; //a, w, and d for direction
    int e_level; //value for energy level
    int n; //energy intake value/food value
};

#endif	/* ANT_H */ 


Any help will be greatly appreciated!
Topic archived. No new replies allowed.