public void Initialize() { selectedHorizontalIndex = -1; selectedVerticalIndex = -1; previouslySelectedHorizontalIndex = -1; previouslySelectedVerticalIndex = -1; firstTap = false; showScore = false; totalScore = 0; selectionScore = 0; isLost = false; //Clear the matrix before reinstatiating it if (ballMatrix != null) for (int i = 0; i < numberOfColumns; ++i) { for (int j = 0; j < numberOfRows; ++j) { ballMatrix[i, j] = null; } } numberOfColumns = 480 / mColorDictionary["blue"].Width; numberOfRows = 660 / mColorDictionary["blue"].Height; ballMatrix = new Ball[numberOfColumns, numberOfRows]; Random random = new Random(); //Load all the balls into the game. for (int i = 0; i < numberOfColumns; ++i) { for (int j = 0; j < numberOfRows; ++j) { Ball ball = new Ball(); int number = random.Next(0, 6); switch (number) { case 0: ball.Initialize(mColorDictionary["blue"], i, j, Ball.BallColor.Blue); ballMatrix[i, j] = ball; break; case 1: ball.Initialize(mColorDictionary["green"], i, j, Ball.BallColor.Green); ballMatrix[i, j] = ball; break; case 2: ball.Initialize(mColorDictionary["purple"], i, j, Ball.BallColor.Purple); ballMatrix[i, j] = ball; break; case 3: ball.Initialize(mColorDictionary["red"], i, j, Ball.BallColor.Red); ballMatrix[i, j] = ball; break; case 4: ball.Initialize(mColorDictionary["teal"], i, j, Ball.BallColor.Teal); ballMatrix[i, j] = ball; break; case 5: ball.Initialize(mColorDictionary["yellow"], i, j, Ball.BallColor.Yellow); ballMatrix[i, j] = ball; break; default: break; } } } }
private void PopulateListOfAdjacentBalls(Ball ball, string previousDirection) { if (ball.IsUsed) { return; } ball.IsUsed = true; int i = ball.X; int j = ball.Y; Ball.BallColor color = ball.color; Ball leftBall = null; Ball upBall = null; Ball rightBall = null; Ball downBall = null; if (i != 0) { leftBall = ballMatrix[i - 1, j]; } if (j != 0) { upBall = ballMatrix[i, j - 1]; } if (i != numberOfColumns - 1) { rightBall = ballMatrix[i + 1, j]; } if (j != numberOfRows - 1) { downBall = ballMatrix[i, j + 1]; } //If not, we will check adjacents and see if they are good to remove if (leftBall != null && leftBall.color == color && previousDirection != "right") { PopulateListOfAdjacentBalls(leftBall, "left"); } if (rightBall != null && rightBall.color == color && previousDirection != "left") { PopulateListOfAdjacentBalls(rightBall, "right"); } if (upBall != null && upBall.color == color && previousDirection != "down") { PopulateListOfAdjacentBalls(upBall, "up"); } if (downBall != null && downBall.color == color && previousDirection != "up") { PopulateListOfAdjacentBalls(downBall, "down"); } lastPopulatedListOfBalls.Add(ball); }
private void RemoveAdjacentBalls(Ball ball, string previousDirection) { lastPopulatedListOfBalls.Clear(); PopulateListOfAdjacentBalls(ball, ""); lastPopulatedListOfBalls.ForEach(b => b.IsAlive = false); }
private int GetScore(Ball ball) { lastPopulatedListOfBalls.Clear(); PopulateListOfAdjacentBalls(ball, ""); lastPopulatedListOfBalls.ForEach(b => b.IsUsed = false); return (int)Math.Pow(lastPopulatedListOfBalls.Count, 2); }