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
0
Question by edesl · Mar 23, 2016 at 12:59 PM · rangeturn-basedboardgame

Detecting possible movements in a 2 range of cells in a turn based game

Hi, I’m trying to implement a turn based game where player could move in a maximum of two steps in every turn.

I have a small board for my first test that you can see in the next image. Every cell has an ID and every time the player wants to make a move, you can see the possible movements but just in 4 directions, for example:

alt text

You can see that possible paths are painted green with a maximum of 2 cells for every path.

The thing is that when you see it closely, you can note that would also be possible to make more movements in a range of 2 cells by turn as you can see here:

alt text

I’m using an approach with an ID based to detect the possible ways now, so my current code looks like this:

 // This receives a GO (from where touch started)
 public void CheckAdjacentCell(GameObject go) {
 
         string text = go.name;
         int cellId = int.Parse(text.Split(' ')[1]);
 
         // Check UP cells (3 is the id of latest cell in first row)
         if(cellId > 3) {
 
             GameObject upGO = GameObject.Find("Cell " + (cellId - upDownDistance));
 
             Debug.Log("I have UP cell and is: " + upGO.name);
 
             if(upGO.GetComponent<CellInfo>().isCellActive) {
 
                 if(color) {SetGreenCellColor(upGO);}
                 else ResetCellColor(upGO);
 
                 string secondGOText = upGO.name;
                 int secondCellId = int.Parse(secondGOText.Split(' ')[1]);
 
                 if(secondCellId > 3) {
                     GameObject secondUpGO = GameObject.Find("Cell " + (secondCellId - upDownDistance));
                     if(secondUpGO.GetComponent<CellInfo>().isCellActive) {
                         if(color) {SetGreenCellColor(secondUpGO);}
                         else ResetCellColor(secondUpGO);
                     }    
                 }
             }
         }
 
         // Check DOWN cells (12 is the id of first cell in latest row)
         if(cellId < 12) {
 
             GameObject downGO = GameObject.Find("Cell " + (cellId + upDownDistance));
 
             Debug.Log("I have DOWN cell and is: " + downGO.name);
 
             if(downGO.GetComponent<CellInfo>().isCellActive) {
 
                 if(color) {SetGreenCellColor(downGO);}
                 else ResetCellColor(downGO);
 
                 string secondGOText = downGO.name;
                 int secondCellId = int.Parse(secondGOText.Split(' ')[1]);
 
                 if(secondCellId < 12) {
                     GameObject secondDownGO = GameObject.Find("Cell " + (secondCellId + upDownDistance));
                     if(secondDownGO.GetComponent<CellInfo>().isCellActive) {
                         if(color) {SetGreenCellColor(secondDownGO);}
                         else ResetCellColor(secondDownGO);
                     }    
                 }
             }
         }
 
         // Check LEFT cells (4 is the number of cells by row)
         if(cellId % 4 != 0) {
 
             GameObject leftGO = GameObject.Find("Cell " + (cellId - 1));
 
             Debug.Log("I have LEFT cell and is: " + leftGO.name);
 
             if(leftGO.GetComponent<CellInfo>().isCellActive) {
 
                 if(color) {SetGreenCellColor(leftGO);}
                 else ResetCellColor(leftGO);
 
                 string secondGOText = leftGO.name;
                 int secondCellId = int.Parse(secondGOText.Split(' ')[1]);
 
                 if(secondCellId % 4 != 0) {
                     GameObject secondLeftGO = GameObject.Find("Cell " + (secondCellId - 1));
                     if(secondLeftGO.GetComponent<CellInfo>().isCellActive) {
                         if(color) {SetGreenCellColor(secondLeftGO);}
                         else ResetCellColor(secondLeftGO);
                     }    
                 }
             }
         }
 
         // Check RIGHT cells (4 is the number of cells by row, 3 the module between cellId and latest cell of row)
         if(cellId % 4 < 3) {
 
             GameObject rightGO = GameObject.Find("Cell " + (cellId + 1));
 
             Debug.Log("I have RIGHT cell and is: " + rightGO.name);
 
             if(rightGO.GetComponent<CellInfo>().isCellActive) {
 
                 if(color) {SetGreenCellColor(rightGO);}
                 else ResetCellColor(rightGO);
 
                 string secondGOText = rightGO.name;
                 int secondCellId = int.Parse(secondGOText.Split(' ')[1]);
 
                 if(secondCellId % 4 < 3) {
                     GameObject secondRightGO = GameObject.Find("Cell " + (secondCellId + 1));
                     if(secondRightGO.GetComponent<CellInfo>().isCellActive) {
                         if(color) {SetGreenCellColor(secondRightGO);}
                         else ResetCellColor(secondRightGO);
                     }    
                 }
             }
         }
     }

So if I continue with this approach I think there will be necessary a lot of code to check the available cells in every turn. Do you know in your expertise if there could be a better way to get the behavior I want to implement?

Thanks a lot for your help!

captura-de-pantalla-2016-03-21-a-las-65954-pm.png (183.3 kB)
captura-de-pantalla-2016-03-21-a-las-65954-pm-copy.png (145.5 kB)
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 Fredex8 · Mar 23, 2016 at 01:52 PM 0
Share

That code looks a bit messy. I think I would use a grid numbered similar to a a chessboard.

That way if the player were on say D4 you know that D5 and D6 need to be highlighted for forward movement, E4 and F4 highlighted for right movement etc. Store all the cells in an array beforehand rather than constantly running gameObject.Find and then just highlight the appropriate cells from that array.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do you create and control a proper turn manager? 0 Answers

Is this the right program 0 Answers

How can I highlight tiles that are in Range of attack? 1 Answer

Turn-based Multiplayer Board Game Storage 0 Answers

Best way to handle troop positioning on a Risk-like game. 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