Game Development Community

dev|Pro Game Development Curriculum

Bejeweled Clone.. Can I say that?

by Kareem Glover · 11/16/2009 (10:46 pm) · 3 comments

Okay.. First off I am open to any ideas, criticism, and guidance. Please don't praise.. because I know I'm not that good. :) The main purpose of this blog is to chronicle my path on making a bejeweled clone and hopefully along the way strengthen my C# and come to have a better understanding of TorqueX.

One of the things that I am doing is restricting the builder to limited use. I feel it is better for me to understand the code before I start to skip steps using TGB.

I will be posting my code in this blog so please feel free to correct me on my logic. Since I am a little ways into the project the first few post will be getting everyone caught up to where I am now.

Also to the makers of bejeweled.. I don't plan to publish my knock off and bring down the value of your IP. :) I figure everone knows bejeweled so we are on the same field logically of what I am trying to accomplish in code.

[Day 1: Implementing the Game Objects]

Okay just for the fun of it I made this a 9x9 grid. So I need to create the player and the game pieces. I decided to make the game pieces as objects also.

private void InitGameObjects()
        {
           
            

            // Intializing Player
            playerPiece.Name = "playerPiece";
            playerPiece.Visible = true;
            playerPiece.Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("pink_clearMaterial");
            playerPiece.Size = new Vector2(5, 5);
            playerPiece.Position = new Vector2(0, 0);
            playerPiece.Layer = 0;
            playerPiece.Components.AddComponent(new MovementComponent());
            

            TorqueObjectDatabase.Instance.Register(playerPiece);

            // Intializing GameBoard Pieces
            for (int i = 0; i < gamePiece.Length; i++)
            {

                gamePiece[i] = new T2DStaticSprite();
                gamePiece[i].Name = "gamePiece" + i;
                gamePiece[i].Visible = false;
                gamePiece[i].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("green_jewelMaterial");
                gamePiece[i].Size = new Vector2(5, 5);
                gamePiece[i].Layer = 1;
                gamePiece[i].ObjectType += typeGamePiece;
                
                TorqueObjectDatabase.Instance.Register(gamePiece[i]);
            }

            


        }

One of the things I did notice is that TorqueX can only put around 80? or so objects on a screen at a time. In the forums there is a fix that lets you bypass the limit. At this point I have my player object and game object in memory. The next piece of code will be getting them nice and layed out on the screen.

[Entry 2]

Okay I am still about three steps above where I am at in this blog so bear with me as I try to catch up with my development. In this entry I am working on getting the jewel pieces randomly laid out on the screen.

private void GenerateGameGrid()
        {
            int[] shape;
            int xval, yval;

            shape = new int[7];
            bool _avail  ;
            int _colorChoice = 0;

       


            shape[0] = 12; // Green Shape
            shape[1] = 12; // Blue Shape
            shape[2] = 12; // Orange Shape
            shape[3] = 12; // Pink Shape
            shape[4] = 11; // Red Shape
            shape[5] = 11; // White Shape
            shape[6] = 11; // Yellow Shape

            
            for (int i = 0; i < gamePiece.Length; i++)
            {
                _avail = false;
                while (_avail == false)
                {
                    do
                    {
                        _colorChoice = RandomNumber(0,7);   

                    }
                    while (shape[_colorChoice] < 0);

                    if (shape[_colorChoice] >= 0)
                    {
                        _avail = true;
                        shape[_colorChoice]--;
                        xval = _xpos(i);
                        yval = _ypos(i);
                        gamePiece[i].Position = new Vector2(_xpos(i) * x_tilesize, _ypos(i) * y_tilesize);
                        gamePiece[i].Visible = true;
                        _assignPieceImage(_colorChoice, i);
              
     

                    }

                }

            }
           

           
            // Setup Grid
            gridPosition.gridNumber = 41;
            gridPosition.gridPosition = new Vector2(0, 0);

        }

private void _assignPieceImage(int imageNum, int pieceNum)
        {

            gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("green_jewelMaterial");

            if (imageNum == 0)//green
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("green_jewelMaterial");
            }
            if (imageNum == 1)//blue
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("blue_jewelMaterial");
            }
            if (imageNum == 2)//orange
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("orange_jewelMaterial");
            }
            if (imageNum == 3)//pink
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("purple_jewelMaterial");
            }
            if (imageNum == 4)//red
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("red_jewelMaterial");
            }
            if (imageNum == 5)//white
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("white_jewelMaterial");
            }
            if (imageNum == 6)//yellow
            {
                gamePiece[pieceNum].Material = TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.Materials.RenderMaterial>("yellow_jewelMaterial");
            }
        }

        /// <summary>
        /// Translates Piece Number into Board Coordinate
        /// </summary>
        /// <param name="pieceNum"></param>
        /// <returns></returns>
        private int _xpos(int num)
        {
            int theValue = 0;
            int theSlot;

            theSlot = num % 9;
            theValue = theSlot - 4;

            return theValue;

        }

        private int _ypos(int num)
        {
            int theValue = 0;
            int theRow;

            theRow = (int)Math.Floor((double)num / 9);
            theValue = 4 - theRow;
            
            return theValue;


        }


        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
          Thread.Sleep(random.Next(100));
            return random.Next(min, max);
        }

Okay what I have done is broke out the type of shapes/colors. There are six types in all and I have computed how many times they should appear on the grid. Eventually this will need to be refined but it works for my immediate purposes. Once a shape comes up I assign it its next position on the 9x9 grid with a little bit of simple algebra. I also assign the material layer to match what was assigned to the shape.

When all said in down I have a 9x9 grid with assorted jewels. One of the things that will need to be expanded on is the random code.. it still at time produces to long of string of jewels.

d1vh308ia84qfn.cloudfront.net/images/blogpic_1.jpg

About the author

Recent Blogs


#1
11/17/2009 (1:10 pm)
Good idea starting with something that you know the behavior of. Saves you writing too detailed design docs.

Regardless, it might still be a good idea to write down everything on paper though before writing code. I've found it to be a good way to see what I am forgetting about the features of my game.

Are you going to include neighboring piece info in each piece object, or just scan the table as a whole somehow?

I'm not familiar with TorqueX, but you could probably create only one single object for each piece type. Something like a binary map of some sort. It would leave you with a lot less objects, and neighbor checks would definitely be a lot faster.

Good luck, keep us posted!
#2
11/17/2009 (1:37 pm)
Wow, that's going to be a fun project. I've considered doing one myself but haven't exactly had the proper time to do it just yet. Good luck with this and keep us up to date!

#3
11/22/2009 (9:28 pm)
[bold]@konrad[/bold] After the board resettles (this is after the player makes their move) I was going to have the game evaluate each piece. The objects array numbers on the game board never change show position (x,x) will always be gamepiece[23]. The game will just swap out its images. The gameobject will not switch position. So when it comes to scanning the logic would be if we are at gamepiece[23]

if gamepiece[23] ismatch of gamepiece[24] ismatch of gamepiece[25] trigger three_in_a_row.

So in reality it would do a four way check (up,down,left,right) on everypiece.

As always I'm open to any ideas.