namespace Tickypip { using System; public class Board : Gnome.Canvas { /* private static uint sColumns = 10; private static uint sRows = 10; */ private uint mColumns; private uint mRows; private bool mWrapped = false; private uint mTotalComputers = 0; private uint mActiveComputers = 0; private uint mLevelNumber; private Cell[,] mCells; private CellBorder[,] mCellBorders; private LevelLoader mLevelLoader; private GameEngine mGameEngine; public delegate void LevelFinishedEventHandler(Board board); public delegate void LevelLoaderLoadedEventHandler(Board board); public delegate void LevelChangedEventHandler(Board board); public event LevelFinishedEventHandler LevelFinished; public event LevelLoaderLoadedEventHandler LevelLoaderLoaded; public event LevelChangedEventHandler LevelChanged; public Board(GameEngine engine) { mGameEngine = engine; PixelsPerUnit = 1; Setup(10, 10, false); } public GameEngine GameEngine { get { return mGameEngine; } } public uint Columns { get { return mColumns; } set { mColumns = value; } } public uint Rows { get { return mRows; } set { mRows = value; } } public bool Wrapped { get { return mWrapped; } set { mWrapped = value; } } public uint TotalComputers { get { return mTotalComputers; } set { mTotalComputers = value; } } public bool LevelCompleted { get { return TotalComputers == ActiveComputers; } } public void Setup(uint rows, uint cols, bool wrapped) { for (uint row = 0; row < Rows; row++) { for (uint col = 0; col < Columns; col++) { if (mCells[row, col] != null) mCells[row, col].Destroy(); if (mCellBorders[row, col] != null) mCellBorders[row, col].Destroy(); } } Rows = rows; Columns = cols; Wrapped = wrapped; ActiveComputers = 0; TotalComputers = 0; int width = (int)(Columns * (Cell.WIDTH - 1)) + 2; int height = (int)(Rows * (Cell.HEIGHT - 1)) + 2; SetScrollRegion(0, 0, width, height); WidthRequest = width; HeightRequest = height; mCells = new Cell[Rows, Columns]; mCellBorders = new CellBorder[Rows, Columns]; for (uint row = 0; row < Rows; row++) { for (uint col = 0; col < Columns; col++) { mCells[row, col] = null; mCellBorders[row, col] = null; } } } public void SetCell(uint row, uint col, Cell cell) { uint newRow = row * (Cell.HEIGHT - 1); uint newCol = col * (Cell.WIDTH - 1); if (!(cell is BlankCell)) { CellBorder border = new CellBorder(this); mCellBorders[row, col] = border; border.X = newCol; border.Y = newRow; } mCells[row, col] = cell; cell.X = newCol + 1; cell.Y = newRow + 1; cell.Row = row; cell.Column = col; cell.Clicked += new Cell.ClickedEventHandler(OnCellClicked); } public Cell GetCell(uint row, uint col) { return mCells[row, col]; } public Cell GetNearCell(Cell baseCell, Direction dir) { int col, row; if (baseCell == null) return null; col = (int)baseCell.Column; row = (int)baseCell.Row; switch (dir) { case Tickypip.Direction.North: row--; break; case Tickypip.Direction.East: col++; break; case Tickypip.Direction.South: row++; break; case Tickypip.Direction.West: col--; break; default: return null; } /* Bounds checking */ if (col == -1) { if (!Wrapped) return null; col = (int)Columns - 1; } else if (col == Columns) { if (!Wrapped) return null; col = 0; } if (row == -1) { if (!Wrapped) return null; row = (int)Rows - 1; } else if (row == Rows) { if (!Wrapped) return null; row = 0; } return GetCell((uint)row, (uint)col); } public void LoadLevel(string filename, uint num) { mLevelLoader = new TPLevelLoader(this); if (!LevelLoader.LoadLevel(filename)) { mLevelLoader = new NetWalkLevelLoader(this); if (!LevelLoader.LoadLevel(filename)) { Console.WriteLine("Unable to load {0}", filename); mLevelLoader = null; return; } } if (LevelLoaderLoaded != null) LevelLoaderLoaded(this); LevelNumber = num; } public LevelLoader LevelLoader { get { return mLevelLoader; } } public uint LevelNumber { get { return mLevelNumber; } set { if (value < 0 || value > LevelLoader.NumLevels) { /* TODO: Do something more here. */ return; } mLevelNumber = value; LevelLoader.SetupLevel(mLevelNumber); GameEngine.MovesCount = 0; if (LevelChanged != null) LevelChanged(this); } } public void Randomize() { Random rand = new Random(); for (uint row = 0; row < Rows; row++) { for (uint col = 0; col < Columns; col++) { Cell cell = GetCell(row, col); if (cell != null) { cell.Direction = (Tickypip.Direction) rand.Next((int)Tickypip.Direction.Count); } } } } public uint ActiveComputers { get { return mActiveComputers; } set { mActiveComputers = value; } } private void OnCellClicked(Cell cell) { if (cell is BlankCell) return; cell.RotateLeft(); GameEngine.MovesCount++; if (TotalComputers == ActiveComputers && LevelFinished != null) LevelFinished(this); } } }