1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
using System; using System.Collections.Generic; using System.Text; namespace NuriProg.Model { public enum SquareType : byte { Wall = 1, Room = 2, Unknown = 4 } public delegate void SquareChanged(Square sender); public sealed class Square { private readonly int hashcode; public override int GetHashCode() { return hashcode; } public event SquareChanged Change; private Model parent; public NuriProg.View.GridSquare View {get; set;} public int RemainingRoomSize { get; set; } public Square Owner{get;set;} private HashSet<Square> groupMembers; public HashSet<Square> GroupMembers { get { return Owner.groupMembers; } set { groupMembers = value; } } public bool IsSharedRoom { get; set; } public bool Guess { get; set; } public int Level { get; set; } public Square[] Neighbors { get; set; } public Square[] FullNeighbors { get; set; } public byte X { get; private set; } public byte Y { get; private set; } public short RoomSize {get;private set;} private SquareType roomType; public SquareType RoomType { get { return roomType; } set { roomType = value; Level = parent.Level; if (Change!=null) Change(this); } } public Square(short size, byte x, byte y, Model parent) { RemainingRoomSize = 0; IsSharedRoom = false; Owner = null; this.parent = parent; Guess = false; Level = 0; X = x; Y = y; this.hashcode = x + y * parent.Width; if (size > 0) { RoomSize = size; RemainingRoomSize = size; roomType = SquareType.Room; Owner = this; groupMembers = new HashSet<Square>(); groupMembers.Add(this); } else { RoomSize = 0; roomType = SquareType.Unknown; } } } }
Refactorings
No refactoring yet !
Ants
May 25, 2009, May 25, 2009 04:13, permalink
You're right. This looks like a collection of random features. Can you give us an overview of the problem that you are trying to solve and how this class is used to solve that problem? Can you tell us the significance of RoomType that it generates a Change event, but the other read/write properties do not? Can you tell us, what changing the Owner does? Is it valid to get GroupMembers, when RoomType is SquareType.Unknown?
emptyiness12z.blogspot.com
May 28, 2009, May 28, 2009 13:40, permalink
It's part of a Nurikabe solver. RoomType generates a change event because A) changing the roomtype may cause the display to change and B) changing the roomtype causes some revalidation to occur (checking if the square has gained/lost association with other squares, checking for a subset of rule violations).
The Neighbors/FullNeighbors array store the neighbors of the square (FullNeighbors includes the square itself). It is initialized elsewhere...which may be inappropriate, itself.
There is a temptation to replace it with a struct just containing X/Y coordinates and hashcode (LayoutKind.Explicit to make it overlap the coordinates), and roomtype, but such a refactoring would be a hassle.
A messy class that has grown random features :/