Help/hint for print_grid for battleship project

Hi!
I am stuck in print_grid function of battleship project for days. anyone can give me hints/suggestions? no need for solution, any hints or help will do just fine.

Hey, @sh1v! The community would be happy to help you out if you could provide a bit more direction on what you’re stuck with.

1 Like

Here is the code i wrote @bryanray but it’s not passing the test.

   def self.print_grid(arr2d)
    str=""
    arr2d.each do |row|
       str<<row.join(' ')
        p str
        str=""
     end
    end

The failed test description:

Board PART 2 ::print_grid should print each row of @grid so every element in a row is separated with a space
Failure/Error: expect { Board.print_grid([[:S, :N],[:X, :S]]) }.to output(/S N\nX S\n/).to_stdout
expected block to output /S N\nX S\n/ to stdout, but output ““S N”\n"X S”\n"
Diff:
@@ -1,2 +1,3 @@
-/S N\nX S\n/
+“S N”
+“X S”

 # ./spec/0_board_spec.rb:196:in `block (4 levels) in <top (required)>'
1 Like

Excellent!

Would it be possible for you to create a https://repl.it example with your code in it? That would help put some context around the code that you’ve shared and allow me to help guide you towards a solution. :pray:

1 Like

@bryanray

1 Like

Oddly enough my tests pass when I run …

image

Does the failing test exist in the repl you pasted? Could you paste your failing test in a code block in here?

1 Like

@bryanray Here you go:

describe "::print_grid" do
  it "should accept a 2D array representing a grid as an arg" do
    Board.print_grid([[:S, :N],[:X, :S]])
  end

  it "should print each row of @grid so every element in a row is separated with a space" do
    expect { Board.print_grid([[:S, :N],[:X, :S]]) }.to output(/S N\nX S\n/).to_stdout
  end
end

Oh, I see … one solution would be to use something like this, rather than a regex …

expect { Board.print_grid([[:S, :N],[:X, :S]]) }.to output("\"S N\"\n\"X S\"\n").to_stdout

1 Like

@bryanray Thanks for your reply!
But I have some questions:

  1. The code for the test code which i mentioned was from the orginal test case file of the battle_ship project, So is that a bug/mistake?
  2. Is it advisable to alter the test case file with the code you have given?
  1. The code for the test code which i mentioned was from the orginal test case file of the battle_ship project, So is that a bug/mistake?

Great point! My guess is that it is not a bug / mistake. I am not super familiar with this piece of the curriculum … let me pull it up and take a look just to compare a couple things.

Is it advisable to alter the test case file with the code you have given?

Tough answer: It Depends. In this specific case, I believe that the regex and the non-regex tests are pretty equivalent.

However, in order to answer you question more specifically … typically it is not a good idea to alter the case unless you are sure that it makes sense to the code that you are writing.

@sh1v would you mind providing me with a link to the curriculum when you get a chance, please? :pray:

1 Like

Here is the link of the project in curriculum:

In the zip file of the project there is a file named: 0_board_spec.rb
in this file we have the test for the board.rb file

Okay, great! So, yes … it appears that your self.print_grid just needs a minor tweak …

def self.print_grid(arr2d)
    str = ''
    arr2d.each do |row|
      str << row.join(' ')
      # p str #> This outputs "S N"
      puts str #> This outputs S N
      str = ''
    end
  end

Ultimately, you’ll need to use puts instead of p to output your array. There is a minor difference and that is what is causing your test to fail.

Here is a bit of reading that may help you out a bit.

https://www.garethrees.co.uk/2013/05/04/p-vs-puts-vs-print-in-ruby/

1 Like

Thank you so much!!!
It finally worked!!
I sweated a lot on such a tiny thing.
again thanks a lot for your help!!!

1 Like

You had this issue around when I was also struggling through Battleship, so hearing you were at a similar place was motivating :smiley: Hope you’re doing well and pushing through!!

2 Likes

wow! it feels nice to see that there are other learners going through the same curriculum path as me. I hope this thread might be of any help to you and all the community.
It’s also a relieve to see that i was not the only one who was struggling with the battleship project!!! :smile: :smile: :smile: :smile: