function grid_world = read_grid(rows, columns, obstacles_file, terminals_file, rewards_file, moves_file)
% Description: this function is to read in the description of a grid world
% Arguments taken:
%       rows: number of rows in the grid world
%       columns: number of columns in the grid world
%       obstacles_file: file containing row, column coordinates for the
%                       obstacles in the grid
%       terminals_file: file containing row, column coordinates for the
%                       terminal squares in the grid
%       rewards_file: the reward for each square (state) of the grid world
%       moves_file: file specifies the probability of moving in each of the
%                   four directions, given that the agent has chosen to go
%                   in a given direction.
% Return:
%       grid_world: a structure that contains all information about the
%                   grid world.

grid_world.rows = rows;
grid_world.columns = columns;

%read in obstacle coordinates
grid_world.obstacles = str2num(char(textread(obstacles_file, '%s', 'delimiter', '\n')));

%read in terminal square coordinates
grid_world.terminals = str2num(char(textread(terminals_file, '%s', 'delimiter', '\n')));

%read in rewards for each state
grid_world.rewards = str2num(char(textread(rewards_file, '%s', 'delimiter', '\n')));

%read in the "move" action probabilities
grid_world.moves = zeros(4,4);
moves = char(textread(moves_file, '%s', 'delimiter', '\n'));
[num, junk] = size(moves);
for (i = 1:num)
    line = moves(i,:);
    pos = 0; index = 0;
    if (isempty(findstr('N', line)) == 0)
        pos = findstr('N', line);
        index = 1;
    elseif (isempty(findstr('S', line)) == 0)
        pos = findstr('S', line);        
        index = 2;
    elseif (isempty(findstr('E', line)) == 0)
        pos = findstr('E', line);                
        index = 3;
    elseif (isempty(findstr('W', line)) == 0)
        pos = findstr('W', line);            
        index = 4;
    end
    grid_world.moves(index,:) = str2num(line(pos+1:length(line)));
end
