I'm having some trouble trying to move a ball around obstacles towards a goal on a 2x2 array. I have a function that takes two integers as parameters. The function then creates a 2x2 array of size 10, fills it with free space, creates a set obstacle (obstacle stays the same everytime). Then it creates the goal point (assigns it to the same spot everytime) and the ball point (which is in the array location [x][y]).
The goal is to have the ball move towards the goal until it hits an obstacle, then circumvent the obstacle while keeping track of how far away it is from the goal for each space around the obstacle, then return to the closest point and continue towards the goal.
I'm trying to develop a moveToGoal function that moves the ball towards the goal given that there is no obstacle in the way, but I'm having a lot of trouble accessing the grid outside of the printGrid function. If I create the grid outside of the print grid function, I can't access it because it is out of scope. I know it might seem confusing, but I'll try and answer any questions about it. Here's what I have so far:
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
using namespace std;
void printGrid(int x, int y) //Used to clear old grids, and print a
{ //new grid with input location for ball
system("CLS");
string grid [10][10]; //Initialize grid array
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
grid[i][j] = ' '; //Constructs grid with freespace
}
}
for (int i=2; i<8; i++) //Constructs obstacle(s)
{
grid[5][i]='O';
}
grid[2][5] = 'G'; //Sets Goal location
grid[x][y] = 'B'; //Sets current ball location(starts 8,5)
for (int i=0; i<10; i++) //Prints finished grid
{
for (int j=0; j<10; j++)
{
cout<<grid[i][j];
}
cout<<endl;
}
}
void moveToGoal(int x, int y)
{
printGrid(x-1, y);
}
int main()
{
moveToGoal(8,5);
sleep(1);
moveToGoal(7,5);
sleep(1);
moveToGoal(6,5);
sleep(1);
moveToGoal(5,5);
sleep(1);
moveToGoal(4,5);
sleep(1);
moveToGoal(3,5);
}
Any help would be appreciated!