0

I am creating a ASP.Net Web API service for a maze game.

  1. Users should get all cells details in the maze to visualize the maze in 2D plane. I am achieving this using the get method public List<Cell> Get()
  2. User should get details of a cell by giving CellID as input. I am achieving this using the get method public CellRepresentation Get(string id). Note that the CellRepresentation is used to return hypermedia links using WebApi.Hal. These links represent target cells for the possible movements like up, down, left and right.

Now, I need a way to communicate which is the starting point cell to the users to start the game. [In the following maze , cell “10” is the starting point]. What is the REST based way to send this message?

Controller

public class CellsController : ApiController
    {
        public CellsController()
        {

        }

        //Get all cells in the maze
        public List<Cell> Get()
        {
            Maze m = new Maze();
            return m.GetCells();
        }

        //Get specific cell
        public CellRepresentation Get(string id)
        {

            Maze m = new Maze();

            Cell c = m.GetCell(id);
            List<Cell> possibleLists = m.GetPossibleRelatedCells(c);
            CellRepresentation beerRep = new CellRepresentation(c, possibleLists);
            return beerRep;
        }
    }

HAL related Classes

public class CellRepresentation : WebApi.Hal.Representation
        {
            //CellID

            Cell theCell;
            List<Cell> possibleMoveCells;

            public CellRepresentation(Cell c, List<Cell> possibleMoveCells )
            {
                theCell = c;
                this.possibleMoveCells = possibleMoveCells;
            }

            public override string Rel
            {
                get { return LinkTemplates.CellLinks.CellEntry.Rel; }
                set { }
            }

            public override string Href
            {
                get { return LinkTemplates.CellLinks.CellEntry.CreateLink(new { id = theCell.CellID }).Href; }
                set { }
            }

            protected override void CreateHypermedia()
            {
                foreach (Cell relatedCell in possibleMoveCells)
                {
                    if (relatedCell.RelativeName == "Up")
                    {
                        Links.Add(LinkTemplates.CellLinks.UpLink.CreateLink(new { id = relatedCell.CellID }));
                    }
                    if (relatedCell.RelativeName == "Right")
                    {
                        Links.Add(LinkTemplates.CellLinks.RightLink.CreateLink(new { id = relatedCell.CellID }));
                    }
                    if (relatedCell.RelativeName == "Down")
                    {
                        Links.Add(LinkTemplates.CellLinks.DownLink.CreateLink(new { id = relatedCell.CellID }));
                    }
                    if (relatedCell.RelativeName == "Left")
                    {
                        Links.Add(LinkTemplates.CellLinks.LeftLink.CreateLink(new { id = relatedCell.CellID }));
                    }
                }
            }
        }

    public static class LinkTemplates
    {

        public static class CellLinks
        {
            public static Link CellEntry { get { return new Link("self", "~/api/cells/{id}"); } }
            public static Link UpLink { get { return new Link("up", "~/api/cells/{id}"); } }
            public static Link RightLink { get { return new Link("right", "~/api/cells/{id}"); } }
            public static Link DownLink { get { return new Link("down", "~/api/cells/{id}"); } }
            public static Link LeftLink { get { return new Link("left", "~/api/cells/{id}"); } }
        }
    }

Business Classes

public class Cell
    {
        public int XVal { get; set; }
        public int YVal { get; set; }
        public bool TopIsWall { get; set; }
        public bool RightIsWall { get; set; }
        public bool BottomIsWall { get; set; }
        public bool LeftIsWall { get; set; }

        public bool IsStartCell { get; set; }
        public bool IsExtCell { get; set; }

        public string RelativeName { get; set; }  //Top, Right, Etc.

        public string CellID
        {
            get
            {
                string characterID = XVal.ToString() + YVal.ToString();
                return characterID; //Example 10
            }

         }  
    }


    public class Maze
    {
        List<Cell> cells;
        public Maze()
        {
            cells = CreateCells();
        }


        public Cell GetFirtCell()
        {
            Cell firstCell = null;

            foreach (Cell c in cells)
            {
                if(c.IsStartCell )
                {
                    firstCell = c;
                    break;
                }
            }
            return firstCell;
        }

        public Cell GetCell(string cellID)
        {
            Cell theCell = null;

            foreach (Cell c in cells)
            {
                if (c.CellID == cellID)
                {
                    theCell = c;
                    break;
                }
            }
            return theCell;
        }

        public List<Cell> GetCells()
        {
            return cells;
        }

        public List<Cell> GetPossibleRelatedCells(Cell inputCell)
        {
            List<Cell> possibleCells = new List<Cell>();

            foreach (Cell c in cells)
            {

                if (c.XVal == inputCell.XVal-1 && c.RightIsWall == false  && c.YVal== inputCell.YVal  )
                {
                    //Go left from the input cell
                    c.RelativeName = "Left";
                    possibleCells.Add(c);

                }
                else if (c.XVal == inputCell.XVal + 1 && c.LeftIsWall == false && c.YVal == inputCell.YVal )
                {
                    //Go right from the input cell
                    c.RelativeName = "Right";
                    possibleCells.Add(c);
                }
                else if (c.YVal == inputCell.YVal - 1 && c.TopIsWall == false && c.XVal == inputCell.XVal )
                {
                    //Go down from the input cell
                    c.RelativeName = "Down";
                    possibleCells.Add(c);
                }
                else if (c.YVal == inputCell.YVal + 1 && c.BottomIsWall == false && c.XVal == inputCell.XVal)
                {
                    //Go up from the input cell
                    c.RelativeName = "Up";
                    possibleCells.Add(c);
                }

            }
            return possibleCells;
        }

        public List<Cell> CreateCells()
        {
            List<Cell> cells = new List<Cell>();
            //cells = 

            Cell cell1 = new Cell
            {
                XVal = 0,YVal = 0,TopIsWall = false,RightIsWall = false,BottomIsWall = true,LeftIsWall = true,
                RelativeName="Self"
            };

            Cell cell2 = new Cell
            {
                XVal = 1,YVal = 0,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = false,
                IsStartCell = true, //--Start
                RelativeName="Self"
            };

            Cell cell3 = new Cell
            {
                XVal = 2,YVal = 0,TopIsWall = false,RightIsWall = false,BottomIsWall = true,LeftIsWall = false,
                RelativeName="Self"
            };

            Cell cell4 = new Cell
            {
                XVal = 3,YVal = 0,TopIsWall = false,RightIsWall = true,BottomIsWall = true,LeftIsWall = false,
                RelativeName = "Self"
            };


            Cell cell5 = new Cell
            {
                XVal = 0,YVal = 1,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = true,
                RelativeName = "Self"
            };


            Cell cell6 = new Cell
            {
                XVal = 0,YVal = 0,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = false,
                RelativeName = "Self"
            };


            Cell cell7 = new Cell
            {
                XVal = 1,YVal = 1,TopIsWall = true,RightIsWall = false,BottomIsWall = true,LeftIsWall = false,
                RelativeName = "Self"
            };


            Cell cell8 = new Cell
            {
                XVal = 2,YVal = 1,TopIsWall = false,RightIsWall = true,BottomIsWall = false,LeftIsWall = false,
                RelativeName = "Self"
            };


            Cell cell9 = new Cell
            {
                XVal = 3,YVal = 1,TopIsWall = false,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                RelativeName = "Self"
            };


            Cell cell10 = new Cell
            {
                XVal = 2,YVal = 2,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                RelativeName = "Self"
            };

            Cell cell11 = new Cell
            {
                XVal = 3,YVal = 2,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                RelativeName = "Self"
            };

            Cell cell12 = new Cell
            {
                XVal = 3,YVal = 3,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = false,
                IsExtCell = true, //--Exit
                RelativeName = "Self"


            };

            cells.Add(cell1);
            cells.Add(cell2);
            cells.Add(cell3);
            cells.Add(cell4);
            cells.Add(cell5);
            cells.Add(cell6);

            cells.Add(cell7);
            cells.Add(cell8);
            cells.Add(cell9);
            cells.Add(cell10);
            cells.Add(cell11);
            cells.Add(cell12);


            return cells;

        }

    }

Maze

enter image description here

LCJ
  • 22,196
  • 67
  • 260
  • 418
  • what does this have to do with rest? can't you just enhance one of your models (say CellRepresentation) with a new property (say IsStartingPoint)? – Alexandru Marculescu Aug 09 '16 at 07:37
  • @AlexandruMarculescu Is the client expected to find out the cell from a list of cells to see which one has IsStartingPoint set to true? Is it the REST way? I don't think so - business logic should be on the server ; not client. – LCJ Aug 09 '16 at 11:59

0 Answers0