/// <summary> /// Determines whether the game board is valid or not. Valid is defined as having all of /// the game tiles connected together. NOTE - this method does not check to see whether /// all of the game tiles are on the game board and none are remaining in the tile rack. /// </summary> /// <returns></returns> public bool IsGameBoardValid() { bool isValid = true; int unoccupiedTileCount = 0; int markedTileCount = 0; //ToDo: Does this array need to be initialized to some default value? var connections = new TileValidity[BoardSpaceDimension, BoardSpaceDimension]; // // Start with marking all of the unoccupied spots. // for (int i = 0; i < BoardSpaceDimension; i++) { for (int j = 0; j < BoardSpaceDimension; j++) { if (!_boardMatrix[i, j]._isOccupied) { connections[i, j] = TileValidity.Unoccupied; unoccupiedTileCount++; } else if (connections[i, j] != TileValidity.Valid) { markedTileCount = MarkConnectedTiles(ref connections, i, j); } } } //ToDo: Find some way to mark the smallest groups of unoccupied tiles as Invalid. if ((markedTileCount + unoccupiedTileCount) != BoardSpaceDimension * BoardSpaceDimension) { isValid = false; } return(isValid); }
/// <summary> /// Helper method for IsGameBoardValid(). This method is recursive and finds the number /// of connected tiles. /// </summary> /// <param name="connections"></param> /// <param name="i">X index into the connections array to examine.</param> /// <param name="j">Y index into the connections array to examine.</param> /// <returns>The count of connected tiles.</returns> private int MarkConnectedTiles(ref TileValidity[,] connections, int i, int j) { Debug.Assert(0 < i && i < BoardSpaceDimension); Debug.Assert(0 < j && j < BoardSpaceDimension); int tileCountMarked = 1; // // Mark current tile as valid. // connections[i,j] = TileValidity.Valid; // // Check the tile to the West. // if (i > 0) { if ((_boardMatrix[i - 1, j]._isOccupied) && (connections[i - 1, j] != TileValidity.Valid)) { tileCountMarked += MarkConnectedTiles(ref connections, i - 1, j); } } // // Check the tile to the East. // if (i < BoardSpaceDimension - 1) { if ((_boardMatrix[i + 1, j]._isOccupied) && (connections[i + 1, j] != TileValidity.Valid)) { tileCountMarked += MarkConnectedTiles(ref connections, i + 1, j); } } // // Check the tile to the North // if (j > 0) { if ((_boardMatrix[i, j - 1]._isOccupied) && (connections[i, j - 1] != TileValidity.Valid)) { tileCountMarked += MarkConnectedTiles(ref connections, i, j - 1); } } // // Check the tile to the South // if (j < BoardSpaceDimension - 1) { if ((_boardMatrix[i, j + 1]._isOccupied) && (connections[i, j + 1] != TileValidity.Valid)) { tileCountMarked += MarkConnectedTiles(ref connections, i, j + 1); } } return tileCountMarked; }
/// <summary> /// Determines whether the game board is valid or not. Valid is defined as having all of /// the game tiles connected together. NOTE - this method does not check to see whether /// all of the game tiles are on the game board and none are remaining in the tile rack. /// </summary> /// <returns></returns> public bool IsGameBoardValid() { bool isValid = true; int unoccupiedTileCount = 0; int markedTileCount = 0; //ToDo: Does this array need to be initialized to some default value? var connections = new TileValidity[BoardSpaceDimension,BoardSpaceDimension]; // // Start with marking all of the unoccupied spots. // for (int i = 0; i < BoardSpaceDimension; i++) { for (int j = 0; j < BoardSpaceDimension; j++) { if (!_boardMatrix[i, j]._isOccupied) { connections[i, j] = TileValidity.Unoccupied; unoccupiedTileCount++; } else if (connections[i,j] != TileValidity.Valid) { markedTileCount = MarkConnectedTiles(ref connections, i, j); } } } //ToDo: Find some way to mark the smallest groups of unoccupied tiles as Invalid. if ((markedTileCount + unoccupiedTileCount) != BoardSpaceDimension*BoardSpaceDimension) { isValid = false; } return isValid; }