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 masonb21 · Sep 09, 2016 at 07:57 AM · c#errorerror messageindexoutofrangeexception

how to fix Index Out Of Range Exception

I made this code for the king in a chess game. Whenever the king is on the very right or left side of the board, I get the "IndexOutOfRangeException" error. Anyone know how to fix?

 using UnityEngine;
 using System.Collections;
 
 public class King : Chessman
 {
     public override bool[,] PossibleMove()
     {
         bool[,] r = new bool[8, 8];
 
         Chessman c;
         int i, j;
 
         //Top Side
         i = CurrentX - 1;
         j = CurrentY + 1;
         if (CurrentY != 7)
         {
             for (int k = 0; k < 3; k++)
             {
                 if (i >= 0 || i < 8)
                 {
                     c = BoardManager.Instance.Chessmans[i, j];
                     if (c == null)
                         r[i, j] = true;
                     else if (isWhite != c.isWhite)
                         r[i, j] = true;
                 }
 
                 i++;
             }
         }
 
         //Down Side
         i = CurrentX - 1;
         j = CurrentY - 1;
         if (CurrentY != 0)
         {
             for (int k = 0; k < 3; k++)
             {
                 if (i >= 0 || i < 8)
                 {
                     c = BoardManager.Instance.Chessmans[i, j];
                     if (c == null)
                         r[i, j] = true;
                     else if (isWhite != c.isWhite)
                         r[i, j] = true;
                 }
 
                 i++;
             }
         }
 
         //Midlle Left
         if (CurrentX != 0)
         {
             c = BoardManager.Instance.Chessmans[CurrentX - 1, CurrentY];
             if (c == null)
                 r[CurrentX - 1, CurrentY] = true;
             else if (isWhite != c.isWhite)
                 r[CurrentX - 1, CurrentY] = true;
         }
 
         //Midlle Right
         if (CurrentX != 7)
         {
             c = BoardManager.Instance.Chessmans[CurrentX + 1, CurrentY];
             if (c == null)
                 r[CurrentX + 1, CurrentY] = true;
             else if (isWhite != c.isWhite)
                 r[CurrentX + 1, CurrentY] = true;
         }
 
         return r;
     }
 }
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 Bunny83 · Sep 09, 2016 at 08:57 AM 0
Share

doublemax is right about your error.

$$anonymous$$eep in $$anonymous$$d that a king is not allowed to move himself into check so he would be checkmate. Same is true for any other piece. You are not allowed to move away a piece that would put your king in check. This complicates the calculations significantly ^^. Also don't forget the requirements for "castling" ^^.

ps: using a boolean array with 64 values is a bit "strange". I would prefer to return a simple list of a custom class / struct that holds the coordinates of possible end points.

 public struct ChessPos
 {
     public int x;
     public int y;
 }

It also would be useful to be able to create a temporary board for the various check you have to perform. So using a singleton can be a problem here.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by doublemax · Sep 09, 2016 at 08:13 AM

 if (i >= 0 || i < 8)

Should be:

 if (i >= 0 && i < 8)

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Help with basic AI script 1 Answer

Why is this error coming up in my code ? 1 Answer

Distribute terrain in zones 3 Answers

Help please guys thank you so much! 1 Answer

Error opening project.,Error opening project 0 Answers


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