10 Replies
      Latest reply on Jul 14, 2018 12:30 AM by Claude31
      Squirrel Shi Level 1 Level 1 (0 points)

        Board: GKGameModel{                                         GameScene: SKNode{

                  var cells: [ [ SKSpriteNode ?] ] = [ ]                          var board = Board( )

                                                                                                  override func didMove( to view: SKView){

                 isWin(for player: GKGameModelPlayer) {                                 self.setCells( )

                              for i in 0...m{                                                        }

                                  for j in 0...n{                                                      func setCells( ) {

                                      if let node = self.cells[ i ][ j ] {                               self.board.cells = Array(repeating: ( repeating: nil, count: n ) , count: m)

                           / /    fatal error: index out of range                                          }

                                      }

        i don`t know why appear"fatal error index out of range" after i have initialized the variable cells in the GameScene. before i make initializer for variable "cells" under func didMove( to view: SKView) in the GameScene is fine, no appear "fatal error index out of range". func isWin( for player: ) is called by GKMinMaxStrategist object in the GameScene.

        • Re: why index out of 2D array`s range which set capacity
          Claude31 Level 7 Level 7 (3,725 points)

          Could you edit your code in a readable format ?

           

          And give more complete code.

           

          If you fear so much someone steals you code, you'd better off not ask for support !

            • Re: why index out of 2D array`s range which set capacity
              QuinceyMorris Level 8 Level 8 (5,635 points)

              I agree with what you said, but I just noticed that the bug is obvious — or would have been, if the code was formatted better.

               

              If 'cells' is an array of 'm' arrays of 'n' elements, then the 'for' loops need to be like this:

               

                   for i in 0..<m {

                        for j in 0..<n {

               

              That is, ..< rather than ... .

              • Re: why index out of 2D array`s range which set capacity
                Squirrel Shi Level 1 Level 1 (0 points)

                GameScene: SKScene{

                       var board =  Board( )

                    override func didMove( to view: SKView){

                             self.setCells ()  }

                  func setCells( ){

                         self.board.cells = Array(repeating: Array(repeating: nil, counts: n), counts: m) }   }

                Board: GKGameModel{

                   var cells: [ [ SKSpriteNode? ] ] = [ ]

                func isWin(for player: GKGameModelPlayer) {

                       for i in 0...m{

                           for j in 0...n{

                                 if let node = self.cells[ i ] [ j ] {    }  }} }

                      /. / appear "fatal error: index out of range.       

                why appear fatalerror after i have set 2D array`s capability.

                  • Re: why index out of 2D array`s range which set capacity
                    Claude31 Level 7 Level 7 (3,725 points)

                    Quincey gave you the solution.

                     

                    For formatting, use the <> tool: And take care of your identation, it is hardly readable.

                     

                    GameScene: SKScene{
                           var board =  Board( )
                    
                        override func didMove( to view: SKView){
                                 self.setCells ()  }
                    
                                func setCells( ){
                                  self.board.cells = Array(repeating: Array(repeating: nil, counts: n), counts: m) }  
                                 }     

                     

                    class Board: GKGameModel {
                       var cells: [ [ SKSpriteNode? ] ] = [ ]
                    
                         func isWin(for player: GKGameModelPlayer) {
                           for i in 0..           for j in 0..                 if let node = self.cells[ i ] [ j ] {    }  
                                   }
                              } 
                         }
                          // appear "fatal error: index out of range.      
                      • Re: why index out of 2D array`s range which set capacity
                        Squirrel Shi Level 1 Level 1 (0 points)

                        the fatalerror appear  in for loop:

                             for i in 0...(m - 1){

                                 for j in 0...(n - 1){

                                        if let node = self.cells[ i ][ j ] {  }    }

                             /. / appear "fatal error: index out of range"

                        so i don` t know how it come out and how adjust. thanks for your appriciation.

                          • Re: why index out of 2D array`s range which set capacity
                            Claude31 Level 7 Level 7 (3,725 points)

                            what is the value of m ? of n ?

                             

                            Add a print statement to trace exactly when it occurs

                                 for i in 0...(m - 1) {
                                     for j in 0...(n - 1) {
                                           print(i, j)
                                            if let node = self.cells[ i ][ j ] { 
                                                print("This one OK", i, j) 
                                      }    
                            
                            }

                             

                             

                            I am surprised that this statement works:

                            self.board.cells = Array(repeating: Array(repeating: nil, counts: n), counts: m) }

                             

                            because the syntax is count, not counts

                             

                            Did you copy the actual code or did you retype it ?

                              • Re: why index out of 2D array`s range which set capacity
                                Squirrel Shi Level 1 Level 1 (0 points)

                                in the for loop the "fatal error: index out of range" not disappear and crash process,  i don`t know how amend to remove "fatal error".

                                ( Int m, n ) can set 9 and 10, or other.

                                GameScene : SKScene {

                                      var board = Board ( )

                                      func setCells ( ) {

                                                self.board.cells = Array(repeating : Array ( repeating : nil , count : 10  ) , count : 9 )

                                                   }

                                 

                                Board : GKGameModel {

                                          var cells : [ [ SKSpriteNode ? ] ]  =  [  ]

                                         func  isWin( GKGameModelPlayer ) {

                                                for i in 0 . . . 8 {

                                                   for j in 0 . . . 9 {

                                                        if let node = self . cells [ i ] [ j ] {        / / appear " fatal error: index out of range"

                                                         }

                                                   }

                                              }       }

                                why appear "index out of range ",  the 2D array` s capability has been set, why out of range.

                                  • Re: why index out of 2D array`s range which set capacity
                                    Claude31 Level 7 Level 7 (3,725 points)

                                    Could you add the print I asked you :

                                     

                                         for i in 0...(m - 1) {
                                             for j in 0...(n - 1) {
                                                   print(i, j)
                                                    if let node = self.cells[ i ][ j ] {  
                                                        print("This one OK", i, j)  
                                              }     
                                    } 

                                     

                                     

                                    And report exactly what you get on console (copy and paste).

                                     

                                    As you don't show more code, it is impossible to know where you call the different func.

                                    When do you call isWin ?

                                    Is it after didMove ?

                                     

                                    Please show the code, do not only paraphrase your code, it is useless.

                                     

                                    Unless I get some code to understand the context, I cannot continue to investigate and spend my time on this.

                                      • Re: why index out of 2D array`s range which set capacity
                                        Squirrel Shi Level 1 Level 1 (0 points)

                                        as your idea, i have tryed, the first function print ( ) can be fine, but process to" if let node = self.cells [ i ][ j ]" appear fatalerror.

                                        when implement on xcode`s simulator ipad pro 12.9 , process crash showing below:

                                          for i in 0...(m - 1){

                                              for j in 0...(n - 1) {

                                                  print( i, j )

                                                if let node = self . cells [ i ][ j ] {    /. /. appear "fatal error: index out of range "  why appear fatal error, i can not explain.

                                                       print("ok", i ,  j )

                                                 }

                                           }

                                        }

                                        GameScene: SKScene{

                                              var board = Board ( )

                                               var strategy: GKMonteCarloStrategist?    / / this object calls func isWin( ) automatically.

                                               override didMove( ) {

                                                   self. setCells ( )    / / self.board.cells = Array( repeating: ( repeating: nil, count : n ), count: m )

                                                  }

                                             }

                                        other code has nothing with variable cells of Board, the property capability of cells has been set ( Int m, n ).