随机函数方法出错了

时间:2017-05-21 21:36:12

标签: c# ios xamarin.ios

using System;
using System.Collections;
using System.Collections.Generic;

using UIKit;

namespace iOSMemoryGame
{
    public partial class ViewController : UIViewController
    {
        #region vars

    List<String> imgArr = new List<String>();

    float gameViewWidth;
    int gridSize = 6;

    ArrayList tilesArr = new ArrayList();
    ArrayList coordsArr = new ArrayList();

    #endregion

    public ViewController(IntPtr handle) : base(handle)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        // make sure Game View is Laid Out
        gameView.LayoutIfNeeded();
        gameViewWidth = (float)gameView.Frame.Size.Width;

        // let's load all of our images into an array
        for (int i = 1; i <= 18; i++)
        {
            imgArr.Add("img_" + i.ToString() + ".png");
        }

        // let's make a call to tileMaker
        tileMaker();

        // let's call the randomizer
        randomizer();
    }

    private void randomizer()
    {
        // we are gonna go through our tiles in ORDER
        // and we are gonna assign a new center to them RANDOMLY

        foreach (UIView any in tilesArr)
        {
            // UIImageView thisTile = (UIImageView)tilesArr[i];

            Random myRand = new Random();
            int randomIndex = myRand.Next(0, coordsArr.Count);

            CoreGraphics.CGPoint newRandCenter = (CoreGraphics.CGPoint)coordsArr[randomIndex];
            any.Center = newRandCenter;

            coordsArr.RemoveAt(randomIndex);
        }
    }

    private void tileMaker()
    {
        float tileWidth = gameViewWidth / gridSize;

        float xCenter = tileWidth / 2;
        float yCenter = tileWidth / 2;

        int imgCounter = 0;

        for (int h = 0; h < gridSize; h++)
        {
            for (int v = 0; v < gridSize; v++)
            {
                UIImageView tileImgView = new UIImageView();

                CoreGraphics.CGPoint newCenter = new CoreGraphics.CGPoint(xCenter, yCenter);

                tileImgView.Frame = new CoreGraphics.CGRect(0, 0, tileWidth - 5, tileWidth - 5);

                String imgName = imgArr[imgCounter];
                tileImgView.Image = new UIImage(imgName);
                tileImgView.Center = newCenter;

                // user CAN interact with this image view
                tileImgView.UserInteractionEnabled = true;

                // adding the new image view to the array
                tilesArr.Add(tileImgView);

                gameView.AddSubview(tileImgView);

                xCenter = xCenter + tileWidth;
                imgCounter++;

                if (imgCounter == gridSize * gridSize / 2)
                {
                    imgCounter = 0;
                }
            }

            xCenter = tileWidth / 2;
            yCenter = yCenter + tileWidth;
        }
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }

    partial void Rst4Button_TouchUpInside(UIButton sender)
    {
        throw new NotImplementedException();
    }

    partial void Rst6Button_TouchUpInside(UIButton sender)
    {
        throw new NotImplementedException();
    }
}

}

随机化器方法有些问题(它被注释掉它运行正常)但我不知道是什么(我只是这里的初学者(https://www.udemy.com/xamarin-native-ios-memory-game-csharp/learn/v4/content)。

它再次给我一个超出范围的错误。

Error

1 个答案:

答案 0 :(得分:0)

问题是您的阵列尚未填充,因此Count0。但即使max的{​​{1}}值是独占上限,如果您拨打myRand.Next(min, max),也会返回myRand.Next(0, 0)。然后,当代码尝试访问0并且没有要访问的项目时,您会收到异常。

避免异常的一种方法是首先检查数组是否包含任何项目。另请注意,我将coordsArr[0]代码行移到new Random()循环之外:

foreach

然而,仔细观察您的代码,您似乎正在循环遍历一个阵列并使用另一个阵列执行某些操作。原因通常是两件事之一:

  1. 有一个拼写错误,用户在循环体内或条件内键入了错误的数组名称。要解决此问题,只需在if (coordsArr.Count > 0) { Random myRand = new Random(); foreach (UIView any in tilesArr) { // UIImageView thisTile = (UIImageView)tilesArr[i]; int randomIndex = myRand.Next(0, coordsArr.Count); CoreGraphics.CGPoint newRandCenter = (CoreGraphics.CGPoint)coordsArr[randomIndex]; any.Center = newRandCenter; coordsArr.RemoveAt(randomIndex); } } else { // Take some action if the array is empty? } 循环代码中将coordsArr更改为tilesArr(反之亦然)。
  2. 此人正在使用相关项填充两个单独的数组(因此它们都具有相同数量的项目,对于任何索引i,foreach将以某种方式与firstArray[i]相关联。如果这样是这种情况,那么你可能在secondArray[i]循环中遗漏了一些应该向tileMaker数组添加内容的代码。
相关问题