#include "GridNavigatorAutomatic.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

GridNavigatorAutomatic::GridNavigatorAutomatic() : GridNavigator()
{
}

void GridNavigatorAutomatic::Navigate()
{

    const int WIDTH = 20;
    const int HEIGHT = 20;
   
    bool Move(char maze[][WIDTH], bool wall[][WIDTH],
        int X, int Y);
    bool move(char maze[][WIDTH], bool wall[][WIDTH],
        int& X, int& Y, int x, int y);
    bool Move(char maze[][WIDTH], bool wall[][WIDTH],
        int x, int Y)
    {
        // Check for hitting maze walls
        if (x < 0 || x >= WIDTH)
            return false;
        if (Y < 0 || Y >= HEIGHT)
            return false;
        if (wall[Y][x])
            return false;
        return true;
    }


    bool search(char maze[][WIDTH], bool wall[][WIDTH],
        int x, int y)
    {
        bool exit;
        if (maze[y][x] == 'O')
            return true;
        wall[y][x] = true;
        if (Move(maze, wall, x, y - 1))
            Exit = search(maze, wall, x, y - 1);
        if (!Exit && (Move(maze, wall, x, y + 1)))
            Exit = search(maze, wall, x, y + 1);
        if (!Exit && (Move(maze, wall, x - 1, y)))
            Exit = search(maze, wall, x - 1, y);
        if (!Exit && (Move(maze, wall, x + 1, y)))
            Exit = search(maze, wall, x + 1, y);

        if (exit)
        {
            maze(maze, x, y);
            cout << endl;
            return true;
        }
        return false;
        return Exit;
    }
    int main()
    {
        for (int x = 0; x < WIDTH; x++)
            for (int y = 0; y < HEIGHT; y++)
                visited[y][x] = false;
        visited[y][x] = true;
        cout << search(maze, visited, x, y) << endl;

    }

    bool validMove(char maze[][WIDTH], bool visited[][WIDTH],
        int newX, int newY)
    {
        if (X < 0 || X >= WIDTH)
            return false;
        if (Y < 0 || Y >= HEIGHT)
            return false;
        if (maze[Y][X] == 'X')
            return false;
        if (visited[Y][X])
            return false;
        return true;
    }
    bool move(char maze[][WIDTH], bool visited[][WIDTH],
        int& X, int& Y, int x, int y)
    {
        bool Exit = false;
        if (maze[Y][X] == 'O')
            Exit = true;
        x = X; 
        y = Y;
        visited[Y][X] = true;
        return Exit;
}
