Battleship - refactoring

I’m almost done refactoring my Battleship so that it can be a 2-player game (human vs cpu or human vs human) with a set-up phase (w/o yet implementing ships of different sizes). However, I have some questions about the methods I implemented that I was hoping to go over with someone. Normally, with questions as broad as mine, I would simply come in to office hours, but due to a family emergency, it will be impossible for me to go over the next week. Is there any way I can walk through my code with a TA or someone else?

I uploaded a screenshot of the BattleshipGame methods display_status, play_turn, and play. I wanted to know if it seems like I am doing too much within these methods and I should refactor into smaller methods. (For example, within the play_turn method, should I invoke another method that prints whether the attack was a hit or miss?) Additionally, from the original specs, the .display method was native to the Board class, but I changed the .display method to be native to the HumanPlayer/ComputerPlayer classes. In my mind, this made the most sense, but I wanted to see whether someone else would structure their code differently.

Additionally, I had struggles with making sure the console clears at the appropriate times, and in order to make sure the hit/miss message was not immediately cleared when switching player turns, I implemented sleep(5). Is there a more elegant solution to this?

Lastly, as my code increases in complexity, it no longer satisfies the specs (e.g. :x and :o for hit vs miss), but would this be a problem when I submit it? I didn’t save previous basic versions that satisfied the specs.

Whoops, didn’t upload the screenshot. I’m going to go ahead and implement a hit_or_miss method to clean up the play_turn method, but in a similar vein, I was wondering if I do too much in my HumanPlayer.setup method. I also don’t know how to clear the console in between ship placements for an individual player. (I.E. My BattleshipGame .setup method has player1.setup(board1), system(“clear”), player2.setup(board2) but as player 1 sets up his board, between iterations, how can I have the console clear?)13%20PM

Actually, last issue. It seems like system(“clear”) is not actually clearing my console between turns or when passing the setup phase between players. How can I resolve this? I tried looking it up to no avail.

edit: never mind, system(“clear”) is working fine and works between ship placements.

Hi James,

Great questions! I’ll try to address them in turn.

Looks good to me!

You could ask the user to press enter to continue. A simple way of checking whether the user pressed enter is to gets.chomp and then check whether the value of that is the empty string.

It would be good if you submitted both the version that satisfies the specs as well as your refactored version.

Let me know if I missed any of your questions!

Noted; thanks for the help Matthias! Clicking enter to continue/clear the console seems like a brilliantly simple solution to my problem and I’ll work to implement that.

1 Like