I'm creating a command line connect four game where the board is created with a 2 dimensional array that represents the columns and rows. I use the following code to create the array
@board = Array.new(6) {Array.new(7, " ")}
I'm creating a visual aspect to the connect four game using interpolation. Each element from each subarray in @board is represented as a "cell" using interpolation like below but it keeps giving me an 'undefined method `[]' for nil:NilClass' error.
puts " #{@board[0][5]} | #{@board[1][5]} | #{@board[2][5]} | #{@board[3][5]} | #{@board[4][5]} | #{@board[5][5]} | #{@board[6][5]}"
That's just one row from the board. How can I access an element from the subarray so that it can display on my board using interpolation? The reason I want to use interpolation is that the values will change as the game goes on and they all start off as blank. If I remove one of the [] from the @board[][] callout it prints out a whole row in the "cell" which is not what I want. Any help is appreciated!
putsstatement. But you have no idea where in this string the error is. Therefore the correct procedure is to start to simplify the string in order to isolate where exactly the error comes from. When you do that, you should find the answer.Boardclass, so you don't have to fiddle with the details in your higher-level game code.