Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by vittu1994 · May 24, 2016 at 04:09 PM · listoptimizationgridtilechess

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vittu1994 · May 24, 2016 at 04:28 PM -1
Share

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 ^^

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vittu1994 · May 24, 2016 at 05:09 PM 0
Share

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!

avatar image DiegoSLTS vittu1994 · May 24, 2016 at 05:23 PM 0
Share

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.

avatar image vittu1994 vittu1994 · May 24, 2016 at 05:30 PM -1
Share

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 ^^

avatar image DiegoSLTS vittu1994 · May 24, 2016 at 05:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Instantiate an object next to an object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges