c# - Bitwise logic on maze walls -


i have enum represents current state of cell within maze, this:

[flags] public enum cellinfo : ushort {     none = 0,     northwall = 1,     eastwall = 2,     southwall = 4,     westwall = 8,     allwalls = northwall | eastwall | southwall | westwall } 

with can track current state of each cells' walls pretty easily, example if want open wall east.

var mycell = cellinfo.allwalls; // initialise cell walls mycell ^= cellinfo.eastwall; // east wall down! 

easy! but, each cell next cell, has own wall, in actual fact know im moving cell a cell b, , arguments sake lets eastwards movement, need take down east wall of a , west wall of b:

var = cellinfo.allwalls; var b = cellinfo.allwalls; // assume a->b eastwards ^= cellinfo.eastwall; b ^= cellinfo.westwall; 

now, have generic way of handling movement, don't it, got code smell! many if statements, , wondering if i've missed obvious - bitwise logic may not have spotted perhaps? have pasted knockdownwall method below , takes grid of cells (cellinfo[,]) along grid position of a , b expressed tuple<int,int> (x,y position)


protected static void knockdownwall(cellinfo[,] cells, tuple<int, int> target, tuple<int, int> neighbour) {     var offsettarget = tuple.create(neighbour.item1 - target.item1, neighbour.item2 - target.item2);     if(offsettarget.item1 == 0)     {         if(offsettarget.item2 == -1)         {             cells[target.item1, target.item2] ^= cellinfo.northwall;             cells[neighbour.item1, neighbour.item2] ^= cellinfo.southwall;         }         else         {             cells[target.item1, target.item2] ^= cellinfo.southwall;             cells[neighbour.item1, neighbour.item2] ^= cellinfo.northwall;         }     }     else     {         if (offsettarget.item1 == -1)         {             cells[target.item1, target.item2] ^= cellinfo.westwall;             cells[neighbour.item1, neighbour.item2] ^= cellinfo.eastwall;         }         else         {             cells[target.item1, target.item2] ^= cellinfo.eastwall;             cells[neighbour.item1, neighbour.item2] ^= cellinfo.westwall;         }     } } 

update

plenty of useful info farin answers - fear oversimplified problem start with. cellinfo has many more values store such things n/s/e/w border (allows me freedom shape maze, , fence off areas), n/s/e/w solution , backtrack info - feeds model draws maze. treating cells rather walls has benefit.

the problem approach modeling maze each inner wall represented twice, requiring significant effort maintaining internal consistency. better approach expand maze row @ south side , column @ east side, , use 2 wall types instead of 4 - i.e. wall on north , wall on east side of cell:

walls removed

this picture shows grid of 2 rows 3 columns, extended 3 rows 4 columns. red lines show walls taken down.

in order deal walls in 4 directions specific cell, make method recognizes south , west walls north , east walls of adjacent cell.

finally, make 2 2d arrays of bools - 1 representing horizontal walls, , 1 representing vertical walls. way individual cells not have store own walls @ all, , maze internally consistent:

private const int rows = 10; private const int cols = 15; private bool northwalls[,] = new bool[rows+1, cols+1]; private bool westwalls[,] = new bool[rows+1, cols+1]; public bool haswall(int cellrow, int cellcol, cellinfo dir) {     switch(dir) {         case cellinfo.northwall:             return northwalls[cellrow,cellcol];         case cellinfo.westwall:             return westwalls[cellrow,cellcol];         case cellinfo.easthwall:             return westwalls[cellrow,cellcol+1];         case cellinfo.southhwall:             return northwalls[cellrow+1,cellcol];     } } 

if must work current design, rid of conditionals in knockdownwall, make 2 3×3 arrays values of cellinfo.xyzwall implementation, , index them offsettarget.item1+1, offsettarget.item2+1:

protected static void knockdownwall(cellinfo[,] cells, tuple<int, int> target, tuple<int, int> neighbour) {     var offsettarget = tuple.create(neighbour.item1 - target.item1, neighbour.item2 - target.item2);     cells[target.item1, target.item2] ^= targetlookup[offsettarget.item1+1, offsettarget.item2+1];     cells[neighbour.item1, neighbour.item2] ^= neighborlookup[offsettarget.item1+1, offsettarget.item2+1]; } 

Comments