- Home /
Help optimizing my code for chess game
Hey im working on a chess clone and made some code to generate names for each tile in the board grid. In chess each coordinate of tiles are defined by a letter in width and a number in length. I have this code for generating letters for the tiles but as you can see its messy and could be optimized
if (tiles.Count <= width)
{
piece.name = "a";
}
if (tiles.Count <= width * 2 && tiles.Count >= width + 1)
{
piece.name = "b";
}
if (tiles.Count <= width * 3 && tiles.Count >= width * 2 + 1)
{
piece.name = "c";
}
if (tiles.Count <= width * 4 && tiles.Count >= width * 3 + 1)
{
piece.name = "d";
}
if (tiles.Count <= width * 5 && tiles.Count >= width * 4 + 1)
{
piece.name = "e";
}
if (tiles.Count <= width * 6 && tiles.Count >= width * 5 + 1)
{
piece.name = "f";
}
if (tiles.Count <= width * 7 && tiles.Count >= width * 6 + 1)
{
piece.name = "g";
}
if (tiles.Count <= width * 8 && tiles.Count >= width * 7 + 1)
{
piece.name = "h";
}
tiles is the list where i store all tiles. width is the width of the grid with and value of 8. piece is the gameobject that every tile is assigned to in the for-loop and when you access the piece object u access all of them in the grid.
Im going to do one for length aswell where you assign numbers and it works pretty much the same. How can i optimize this code and make it shorter than it is? Thanks
Here is how i did for the numbers along the length of the grid:
if (tiles.Count % length == 1)
{
piece.name = piece.name + 1.ToString();
}
if (tiles.Count % length == 2)
{
piece.name = piece.name + 2.ToString();
}
if (tiles.Count % length == 3)
{
piece.name = piece.name + 3.ToString();
}
if (tiles.Count % length == 4)
{
piece.name = piece.name + 4.ToString();
}
if (tiles.Count % length == 5)
{
piece.name = piece.name + 5.ToString();
}
if (tiles.Count % length == 6)
{
piece.name = piece.name + 6.ToString();
}
if (tiles.Count % length == 7)
{
piece.name = piece.name + 7.ToString();
}
if (tiles.Count % length == 0)
{
piece.name = piece.name + 8.ToString();
}
It also needs optimization, but if anyone wanted to know how to do it here it is ^^
Answer by DiegoSLTS · May 24, 2016 at 04:52 PM
For the numbers (refactoring your code in the comment) I guess you could do:
int rest = tiles.Count % length;
if (rest == 0) rest = 8;
piece.name = piece.name + rest.ToString();
For the letters you could do something like this:
char letter = 'a';
int count = tiles.Count;
while (count > length) {
count -= length;
letter++;
}
piece.name = letter.ToString();
I've not tested this code, but that's the cleanest idea I can think off.
EDIT: It looks like you can just increment a char instead of using an int and avoid using ascii codes and costs, I changed the code a bit.
They both worked! :) and 64 was "@" in ascii and 65 was "A". I understand the second example for generating the letters but can you explain the first example when generating numbers? Otherwise thanks alot!
Oh, right, 65 for 'A', I tried to remember... close enough!
The code for the numbers does almost the same as your 8 ifs. When "tiles.Count % length == 1" returns true it means "tiles.Count % length" is equals to 1, so ins$$anonymous$$d of writing "1.ToString()" in the next line, you could write "(tiles.Count % length).ToString()". If you make that change for the first 7 ifs you'll see that the "if (...)" is useless, since you have the same line inside each if:
piece.name = piece.name + (tiles.Count % length).ToString();
You can make it look cleaner storing the mod operation into a variable like:
int rest = tiles.Count % length;
piece.name = piece.name + rest.ToString();
This doesn't work for the 8th if since you want to write "8" when the rest of the operation is 0, so the "if (rest == 0) rest = 8;" line is there to fix that.
oh so:
if (rest == 0) rest = 8;
is just for the instance when you want the number 8? So from 1-7 is all covered by the first line?
int rest = tiles.Count % length;
But thanks for the explanation! :) interesting to know of stuff i didnt know about, like char and ascii
EDIT: Well its obvious for u maybe about if rest == 0, then rest = 8. Im just not familiar with if statements that have no brackets ^^
Yes, for 1-7 you don't need the if. In case you don't know the "%" operator is mathematically known as "modulo" or "mod". It returns the rest of an integer division, so for your chess game, since "length" is obviously 8, the "rest" variable will always have a value from 0 to 7.
Your answer
Follow this Question
Related Questions
Optimizing code for color generation of chess board 4 Answers
A node in a childnode? 1 Answer
Need help optimizing grid initialization. 1 Answer
Best way to implement a grid system 1 Answer