// Return the group of jewels, including this one, by searching recursively // A jewel is in the group if it is next to a jewel in this group, and is the same color. public ArrayList GetJewelGroup(Jewel jewel) { ArrayList jewelGroup = new ArrayList(); GetGroup(jewel, jewelGroup); return(jewelGroup); }
public Engine() { jewels = new Jewel[NUM_ROWS, NUM_COLS]; this.timeSinceLastFrame = 0; this.score = 0; this.jewelInBiggestGroup = null; this.biggestGroupSize = 1; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Services.AddService(typeof(SpriteBatch), spriteBatch); menu = new Menu(this); menu.addButton("Start!", Color.Yellow, Color.Red, 100, 100, 200, 100, StartGame); engine = new Engine(); Jewel.LoadJewelImages(Content); engine.LoadScoreFont(Content); menu.LoadButtons(Content, graphics); Components.Add(menu); }
//private void DrawJewels(SpriteBatch spriteBatch) //{ // foreach (Jewel jewel in jewels) // { // jewel.Draw(spriteBatch); // } //} public void MakeJewels(Game game) { for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j < NUM_COLS; j++) { Jewel jewel = new Jewel(game); jewel.x = i; jewel.y = j; jewel.setOffset(); jewels[i, j] = jewel; game.Components.Add(jewel); } } }
public void OnMouseUp(MouseState mouseState) { Jewel clickedJewel = GetClickedJewel(mouseState.X, mouseState.Y); if (clickedJewel != null) { if (clickedJewel.isSpinning) { // Change its color, and if it's now in a group, freeze it clickedJewel.ChangeColor(); ArrayList group = GetJewelGroup(clickedJewel); if (group.Count > 1) { foreach (Jewel jewel in group) { jewel.Freeze(); } } // If this is now the biggest group, update the score if (group.Count > biggestGroupSize) { biggestGroupSize = group.Count; score = biggestGroupSize * 1000; // If there is a previous biggest, make it not solid if (jewelInBiggestGroup != null) { ArrayList previousBiggestGroup = GetJewelGroup(jewelInBiggestGroup); foreach (Jewel jewel in previousBiggestGroup) { jewel.isSolid = false; jewel.isInBiggestGroup = false; } } // Make the new biggest group solid foreach (Jewel jewel in group) { jewel.isSolid = true; jewel.isInBiggestGroup = true; } jewelInBiggestGroup = clickedJewel; } } } }
// Second parameter is an array to store the results of the recursive function in. private void GetGroup(Jewel current, ArrayList jewelGroup) { if (!jewelGroup.Contains(current)) { jewelGroup.Add(current); } // If it's already in the group, we've checked this one already else { return; } if (current.y > 0) { Jewel up = jewels[current.x, current.y - 1]; if (current.color == up.color) { GetGroup(up, jewelGroup); } } if (current.y < (Engine.NUM_ROWS - 1)) { Jewel down = jewels[current.x, current.y + 1]; if (current.color == down.color) { GetGroup(down, jewelGroup); } } if (current.x > 0) { Jewel left = jewels[current.x - 1, current.y]; if (current.color == left.color) { GetGroup(left, jewelGroup); } } if (current.x < (Engine.NUM_COLS - 1)) { Jewel right = jewels[current.x + 1, current.y]; if (current.color == right.color) { GetGroup(right, jewelGroup); } } }