Optimizing code for color generation of chess board

70671-chessboard.png

Hey, another optimazation problem with my chess game. My code works but yeah its just a bunch of repeating if statements that can be shorten down and optimized.

                int indexCount = tiles.Count - 1;
                if (indexCount % 9 == 0)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 2 && indexCount <= 9 * 6)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 3 && indexCount >= 9 * 5)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 4 && indexCount <= 9 * 4)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 5 && indexCount >= 9 * 3)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 6 && indexCount <= 9 * 2)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }
                if (indexCount % 9 == 7 && indexCount >= 9)
                {
                    piece.GetComponent<Renderer>().material.color = Color.black;
                }

indexCount allows me to count each tile as it was an index (so i dont count a grid from 1 to 64, but 0 to 63). The way the code works is that it checks each index and accesses the indexes that belong in the multiplication table of 9 and fills them. Then it repeats but checks for the remainder of 9. The extra stuff in the if-statements are so that it only handles a diagonal row at a time and doesnt alter any indexes outside of this row.

How can i optimize this code? Make sure to note the operators change when handling even and uneven numbers outside of the diagonal row.

Thanks

That 9 there looks so wierd. You only need the row and column of a tile to set it’s color. In your code I guess this should work to get the row and column of a tile:

int row = (tiles.Count - 1) / 8; //integer division, so for the first 8 tiles you'll be in row 0, for the next 8 tiles you'll be in row 1 and so on until row 7 (the 8th row)
int column = (tiles.Count - 1) % 8; //no matter what row you're filling, the column will always go from 0 to 7

Once you have the row and column, do this (it’s what I’m doing in my game):

piece.GetComponent<Renderer>().material.color = row % 2 == column % 2 ? Color.black : Color.white;

EDIT: If all your tiles are already white and want to set the black ones to black, get the row and column like before and then:

if (row % 2 == column % 2)
    piece.GetComponent<Renderer>().material.color = Color.black;

Less code does not mean optimized. You can replace the 7 short if-statements for 1 longer one, less code, but I wouldn’t call it optimized, since it’s doing the same thing:

    int indexCount = tiles.Count - 1;
    if (indexCount % 9 == 0 ||
        indexCount % 9 == 2 && indexCount <= 9 * 6 ||
        indexCount % 9 == 3 && indexCount >= 9 * 5 ||
        indexCount % 9 == 4 && indexCount <= 9 * 4 ||
        indexCount % 9 == 5 && indexCount >= 9 * 3 ||
        indexCount % 9 == 6 && indexCount <= 9 * 2 ||
        indexCount % 9 == 7 && indexCount >= 9)
    {
        piece.GetComponent<Renderer>().material.color = Color.black;
    }

Instead of using GetComponent to fetch the Renderer’s material, you can store the material and access it directly, this could be considered an optimized, a micro one perhaps, probably won’t even be noticeable though.

The logic is a bit funky, not sure how you are generating the board, but you could probably replace the magic numbers for something easier to read, again I wouldn’t consider this an optimization.

    // Tiles are children to the ChessBoard (An empty GameObject here)
    Renderer[] tiles = GetComponentsInChildren<Renderer>();
    for (int i = 0; i < tiles.Length; i++)
    {
        tiles*.material.color = (i % 2 == 0) ? Color.black : Color.white;*

}
If you want to optimize, then the best thing is probably to import the Chessboard as a model, you won’t need to generate the tiles and colors on start every time.

@vittu1994 … The best optimization I can think of here is to simply set the gameObject material colors in the editor. Those settings will persist and you don’t have to mess with them ever again. In short, this is a ‘NO CODE’ solution … a no code solution is going to be better/faster than any optimized code anyone can come up with.

However, if you want to allow the player to set their own colors -or- you want an Inspector Editor feature where you can quickly set & update the colors yourself then @DiegoSLTS answer is the way to go.

try this:(not tested)

private GameObject[] grid; 
 public Gameobject block;
 public float blockSize;
 
 
 void Start(){
 grid = new grid[2,2];
 }
 
 void Update(){
 
 for(int i = 0; i <grid.length; i++){
   for(int j = 0; j <grid.height; j++){
    temp = (GameObject) instantiate(Block, new vector3(i *  blockSize, j * blockSize,0),quternion.identity);
    grid[i,j] = temp;
 
 
   }
 }
 }