Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Mappings file #37

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions control_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Writes outcomes to a result file
import json
import yaml

from services.grid import Grid
from services.arya_bota import AryaBota
from services.mappings import DIR
from services.singleton_classes import Grid
from services.singleton_classes import AryaBota
from services.utils import check_answer

# Opening config to read grid attributes
Expand Down Expand Up @@ -76,7 +76,7 @@ def get_number_of_coins(row = None, column = None):
row = BOT.my_row()
if column is None:
column = BOT.my_column()
return GRID.get_number_of_coins(row,column)
return GRID.get_number_of_coins_at_pos(row,column)

def obstacle_ahead(row = None, column = None):
"""return if obstacle ahead"""
Expand All @@ -85,7 +85,7 @@ def obstacle_ahead(row = None, column = None):
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
direction = BOT.get(DIR)
if direction == "down":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
Expand Down Expand Up @@ -119,7 +119,7 @@ def obstacle_behind(row = None, column = None):
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
direction = BOT.get(DIR)
if direction == "up":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
Expand Down Expand Up @@ -153,7 +153,7 @@ def obstacle_left(row = None, column = None):
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
direction = BOT.get(DIR)
if direction == "left":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
Expand Down Expand Up @@ -187,7 +187,7 @@ def obstacle_right(row = None, column = None):
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
direction = BOT.get(DIR)
if direction == "right":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
Expand Down
4 changes: 2 additions & 2 deletions languages/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from services.utils import convert_english_pseudocode_to_python
from control_hub import *
from services.grid import Grid
from services.arya_bota import AryaBota
from services.singleton_classes import Grid
from services.singleton_classes import AryaBota

bot = AryaBota.get_instance()
grid = Grid.get_instance()
Expand Down
4 changes: 2 additions & 2 deletions lexer_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import logging

from control_hub import *
from services.grid import Grid
from services.arya_bota import AryaBota
from services.singleton_classes import Grid
from services.singleton_classes import AryaBota
from services.utils import get_custom_error
from languages.english import english_lexer, english_parser

Expand Down
4 changes: 2 additions & 2 deletions problem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Importing Grid Module"""
from grid import Grid
from arya_bota import AryaBota
from singleton_classes import Grid
from singleton_classes import AryaBota

class DictCompareWrapper:
"""Comparing dictionaries"""
Expand Down
97 changes: 0 additions & 97 deletions services/grid.py

This file was deleted.

81 changes: 81 additions & 0 deletions services/mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Attribute Constants
PROBLEM_TYPE = "problem_type"
STATEMENT = "statement"
ANSWER = "answer"
ROWS = "rows"
COLUMNS = "columns"
HOMES = "homes"
OBSTACLES = "obstacles"
COINS = "coins"
COLOURED = "coloured"
ROW = "row"
COLUMN = "column"
DIR = "dir"
PEN = "pen"

# Schema Constants
POSITION = "position"
NUMBER = "number"

# Attribute Mappings
attributes = {
"problem.problem_type": {
"class": "Problem",
"attribute_name": PROBLEM_TYPE
},
"problem.statement": {
"class": "Problem",
"attribute_name": STATEMENT
},
"answer": {
"class": "Problem",
"attribute_name": ANSWER,
"default": None
},
"initial_state.grid.dimensions.row": {
"class": "Grid",
"attribute_name": ROWS
},
"initial_state.grid.dimensions.column": {
"class": "Grid",
"attribute_name": COLUMNS
},
"initial_state.grid.homes": {
"class": "Grid",
"attribute_name": HOMES,
"default": []
},
"initial_state.grid.obstacles": {
"class": "Grid",
"attribute_name": OBSTACLES,
"default": []
},
"initial_state.grid.coins": {
"class": "Grid",
"attribute_name": COINS,
"default": []
},
"initial_state.grid.coloured": {
"class": "Grid",
"attribute_name": COLOURED,
"default": []
},
"initial_state.arya_bota.position.row": {
"class": "AryaBota",
"attribute_name": ROW
},
"initial_state.arya_bota.position.column": {
"class": "AryaBota",
"attribute_name": COLUMN
},
"initial_state.arya_bota.dir": {
"class": "AryaBota",
"attribute_name": DIR,
"default": "down"
},
"initial_state.arya_bota.pen": {
"class": "AryaBota",
"attribute_name": PEN,
"default": "down"
}
}
78 changes: 33 additions & 45 deletions services/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,52 @@
import json
import logging

from services.arya_bota import AryaBota
from services.grid import Grid
from services.utils import get_for_every_position
from services.singleton_classes import AryaBota, Problem
from services.singleton_classes import Grid
from services.mappings import attributes

""" Problem Service """

def get_initial_state(problem):
def get_initial_state():
"""Return intial state of problem"""
problem_details = problem["problem"]
grid = Grid.get_instance()
bot = AryaBota.get_instance()
grid_state = grid.get_state()
arya_bota_state = bot.get_state()
grid_state.update(arya_bota_state)
problem_type = problem_details["problem_type"]
grid_state["type"] = problem_type
return grid_state
state = Problem.get_instance().get_state()
state.update(Grid.get_instance().get_state())
state.update(AryaBota.get_instance().get_state())
return state

def setup_grid_and_bot(problem):
"""Initialise the state of the grid"""
problem_details = problem["problem"]
problem_type = problem_details["problem_type"]
statement = problem_details["statement"]
state = problem["initial_state"]
bot = AryaBota.get_instance()
arya_bota_state = state["arya_bota"]
if "pen" in arya_bota_state:
bot.configure(arya_bota_state["position"]["row"], arya_bota_state["position"]["column"], arya_bota_state["dir"], arya_bota_state["pen"])
else:
bot.configure(arya_bota_state["position"]["row"], arya_bota_state["position"]["column"], arya_bota_state["dir"], "down")
answer = problem["answer"]
grid = Grid.get_instance()
grid_state = state["grid"]
rows = grid_state["dimensions"]["row"]
columns = grid_state["dimensions"]["column"]
coins_per_position = obstacles_per_position = None
if "coins" in grid_state:
coins_per_position = get_for_every_position(grid_state["coins"], rows, columns)
else:
grid_state["coins"] = None
if "obstacles" in grid_state:
obstacles_per_position = get_for_every_position(grid_state["obstacles"], rows, columns, False)
else:
grid_state["obstacles"] = None
if not "homes" in grid_state:
grid_state["homes"] = None
grid.configure(rows, columns, grid_state["coins"], coins_per_position, grid_state["obstacles"], obstacles_per_position, grid_state["homes"], statement, problem_type, answer)
def set_up(problem):
"""Initialise the state of the programming environment"""
attributes_per_class = {
"Problem": {},
"Grid": {},
"AryaBota": {}
}
for path in attributes:
value = None
try:
path_string = "problem" + "".join(["['" + key + "']" for key in path.split('.')])
value = eval(path_string)
except KeyError:
if "default" in attributes[path]:
value = attributes[path]["default"]
else:
raise KeyError("Check the Problem Schema, required field(s) missing!")
attributes_per_class[attributes[path]["class"]][attributes[path]["attribute_name"]] = value

Grid.get_instance().configure(attributes_per_class["Grid"])
AryaBota.get_instance().configure(attributes_per_class["AryaBota"])
Problem.get_instance().configure(attributes_per_class["Problem"])

def initialise(problem_file_path):
"""Initialises chosen problem"""
# Read problem
with open(problem_file_path) as problem_file:
problem = json.loads(problem_file.read())
setup_grid_and_bot(problem)
return get_initial_state(problem)
set_up(problem)
return get_initial_state()

def render(level):
""" Renders selected problem grid """
"""Renders selected problem"""
with open('config.yaml') as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
problem_file_path = config['levels'][level]['file']
Expand Down
Loading