none
Index out of range question RRS feed

  • Question

  • So I have this really large function for a battleships game I'm creating, and currently it's coming up with an index out of range error for one section, but when I use the same format for assigning a value to another variable it's fine.

    public bool SelectAIMove(string[] hits)
            {
                int randomX = 0;
                int randomY = 0;
                int randomDirection;
    
                for (int index5 = 0; index5 < hits.Length; index5++)
                {
                    Console.Write(hits[index5] + " ");
                }
    
                string move;
    
                if (hits[0] == "" || hits[0] == null)
                {
                    do
                    {
                        randomX = random.Next(0, 9);
                        randomY = random.Next(0, 9);
                    }
                    while (playerBoard[randomX, randomY] == "x");
                }
                else
                {
                    //Direction: 0 = Right; 1 = Left; 2 = Up; 3 = Down;
                    bool flag;
    
                    do
                    {
                        flag = false;
                        int hitsX = 0;
                        int hitsY = 0;
    
                        for (int index3 = 0; index3 < hits.Length; index3++)
                        {
                            if (hits[index3] != null || hits[index3] != "")
                            {
                                for (int index4 = 0; index4 < letters.Length; index4++)
                                {
                                    if (letters[index4] == hits[index3][0].ToString())
                                    {
                                        hitsX = index4;
                                    }
                                }
    
                                if (hits[index3].Length == 3)
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString() + hits[index3][2].ToString())) - 1;
                                }
                                else
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString())) - 1;
                                }
                            }
                        }
    
                        randomDirection = random.Next(0, 4);
    
                        if (randomDirection == 0)
                        {
                            randomX = hitsX;
                            randomY = hitsY + 1;
                        }
                        else if (randomDirection == 1)
                        {
                            randomX = hitsX;
                            randomY = hitsY - 1;
                        }
                        else if (randomDirection == 2)
                        {
                            randomX = hitsX - 1;
                            randomY = hitsY;
                        }
                        else if (randomDirection == 3)
                        {
                            randomX = hitsX + 1;
                            randomY = hitsY;
                        }
    
                        flag = AICheckForOutOfBoundsOrEmptySpace(randomX, randomY);
                    }
                    while (flag == true);
                }
    
                move = (letters[randomX] + (randomY + 1));
    
                for (int x = 0; x < shipsLetters.Length; x++)
                {
                    if (playerBoard[randomX, randomY] == shipsLetters[x])
                    {
                        playerBoard[randomX, randomY] = "x";
                        Console.WriteLine(move + " - HIT");
    
                        for (int y = 0; y < AIHits.Length; y++)
                        {
                            if ((y != 0 && AIHits[y - 1] == null) || (AIHits[y] == "") || (y == 0 && AIHits[y] == null))
                            {
                                AIHits[y] = move;
                                break;
                            }
                        }
    
                        for (int index = 0; index < boardHeight; index++)
                        {
                            for (int index2 = 0; index2 < boardWidth; index2++)
                            {
                                if (playerBoard[index, index2] == shipsLetters[x])
                                {
                                    goto Finish;
                                }
                            }
                        }
    
                        Console.WriteLine("I've sunk your " + shipNames[x]);
                        Array.Clear(AIHits, 0, AIHits.Length);
    
                    Finish:
                        Console.WriteLine();
                        return true;
                    }
                }
                playerBoard[randomX, randomY] = "x";
                Console.WriteLine(move + " - MISS");
                return false;
            }

    That's the whole function, the bit in particular that's frustrating me is:

    for (int index3 = 0; index3 < hits.Length; index3++)
                        {
                            if (hits[index3] != null || hits[index3] != "")
                            {
                                for (int index4 = 0; index4 < letters.Length; index4++)
                                {
                                    if (letters[index4] == hits[index3][0].ToString())
                                    {
                                        hitsX = index4;
                                    }
                                }
    
                                if (hits[index3].Length == 3)
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString() + hits[index3][2].ToString())) - 1;
                                }
                                else
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString())) - 1;
                                }
                            }
                        }


    Even more specifically, these bits:

    for (int index4 = 0; index4 < letters.Length; index4++)
                                {
                                    if (letters[index4] == hits[index3][0].ToString())
                                    {
                                        hitsX = index4;
                                    }
                                }
    
                                if (hits[index3].Length == 3)
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString() + hits[index3][2].ToString())) - 1;
                                }
                                else
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString())) - 1;
                                }

    I've no idea why the bottom two work.

    The type of data inserted into "hits[]" is stuff like "A4", "B10". Stuff like that, yet hits[index3][0] won't work for me, claiming it's out of range, but hits[index3][1] works, I'm really frustrated by it.

    Can anyone help me?


    • Edited by noskiw Friday, January 3, 2014 12:07 AM
    Friday, January 3, 2014 12:06 AM

Answers

  • try printing the length of all arrays you are using in this block

    for (int index4 = 0; index4 < letters.Length; index4++)
                                {
                                    if (letters[index4] == hits[index3][0].ToString())
                                    {
                                        hitsX = index4;
                                    }
                                }
    
                                if (hits[index3].Length == 3)
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString() + hits[index3][2].ToString())) - 1;
                                }
                                else
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString())) - 1;
                                }


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    Friday, January 3, 2014 4:49 AM

All replies

  • Without reading the code in great depth, one obvious thing is

    if (hits[index3] != null  || hits[index3] != "")

    The condition will always be true.  If hits[index3] is not null then the first part is true and if it is null then the second part is true. Result -> always true. I think what you want is

    if ( ! string.IsNullOrEmpty(hits[index3]) )

    if hits[index3] is equal to "" then

    hits[index3][0]

    will be out of range.

    Before you access hits[index3][1] you are checking that the length is 3 characters and so everything is OK.


    Paul Linton

    Friday, January 3, 2014 3:09 AM
  • try printing the length of all arrays you are using in this block

    for (int index4 = 0; index4 < letters.Length; index4++)
                                {
                                    if (letters[index4] == hits[index3][0].ToString())
                                    {
                                        hitsX = index4;
                                    }
                                }
    
                                if (hits[index3].Length == 3)
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString() + hits[index3][2].ToString())) - 1;
                                }
                                else
                                {
                                    hitsY = (int.Parse(hits[index3][1].ToString())) - 1;
                                }


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    Friday, January 3, 2014 4:49 AM