Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created January 23, 2016 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplace/dcfef2e291f4a57c9e12 to your computer and use it in GitHub Desktop.
Save mindplace/dcfef2e291f4a57c9e12 to your computer and use it in GitHub Desktop.
Battleship
class HumanPlayer
attr_accessor :name
def get_play
gets.chomp.split(",").map{|item| item.to_i}
end
def prompt
puts "Where do you want to fire? (Input like this: 2,3)\n>\n"
end
def display(board)
b = board.grid.map do |row|
row.map{|col| col.nil? || col == :s ? col = "_" : col}
end
puts " 0 1 2 3 4 5 6 7 8 9"
puts "0 #{b[0][0]} #{b[0][1]} #{b[0][2]} #{b[0][3]} #{b[0][4]} #{b[0][5]} #{b[0][6]} #{b[0][7]} #{b[0][8]} #{b[0][9]}"
puts "1 #{b[1][0]} #{b[1][1]} #{b[1][2]} #{b[1][3]} #{b[1][4]} #{b[1][5]} #{b[1][6]} #{b[1][7]} #{b[1][8]} #{b[1][9]}"
puts "2 #{b[2][0]} #{b[2][1]} #{b[2][2]} #{b[2][3]} #{b[2][4]} #{b[2][5]} #{b[2][6]} #{b[2][7]} #{b[2][8]} #{b[2][9]}"
puts "3 #{b[3][0]} #{b[3][1]} #{b[3][2]} #{b[3][3]} #{b[3][4]} #{b[3][5]} #{b[3][6]} #{b[3][7]} #{b[3][8]} #{b[3][9]}"
puts "4 #{b[4][0]} #{b[4][1]} #{b[4][2]} #{b[4][3]} #{b[4][4]} #{b[4][5]} #{b[4][6]} #{b[4][7]} #{b[4][8]} #{b[4][9]}"
puts "5 #{b[5][0]} #{b[5][1]} #{b[5][2]} #{b[5][3]} #{b[5][4]} #{b[5][5]} #{b[5][6]} #{b[5][7]} #{b[5][8]} #{b[5][9]}"
puts "6 #{b[6][0]} #{b[6][1]} #{b[6][2]} #{b[6][3]} #{b[6][4]} #{b[6][5]} #{b[6][6]} #{b[6][7]} #{b[6][8]} #{b[6][9]}"
puts "7 #{b[7][0]} #{b[7][1]} #{b[7][2]} #{b[7][3]} #{b[7][4]} #{b[7][5]} #{b[7][6]} #{b[7][7]} #{b[7][8]} #{b[7][9]}"
puts "8 #{b[8][0]} #{b[8][1]} #{b[8][2]} #{b[8][3]} #{b[8][4]} #{b[8][5]} #{b[8][6]} #{b[8][7]} #{b[8][8]} #{b[8][9]}"
puts "9 #{b[9][0]} #{b[9][1]} #{b[9][2]} #{b[9][3]} #{b[9][4]} #{b[9][5]} #{b[9][6]} #{b[9][7]} #{b[9][8]} #{b[9][9]}"
end
end
require_relative 'board'
require_relative 'player'
class BattleshipGame
attr_reader :board, :player
def initialize(player=Player.new, board=Board.new)
@board = board
@player = player
end
def offer_rules
puts "Would you like to see the rules?"
choice = gets.chomp
rules if choice.include?("y")
end
def attack(pos)
@board.grid[pos[0]][pos[1]] = :x
end
def count
@board.count
end
def game_over?
board.won?
end
def play_turn
location = player.get_play
attack(location) if @board.grid[location[0]][location[1]] == :s
end
def endgame_message
puts "You've destroyed all the enemy ships!!"
end
def play
offer_rules
20.times {board.place_random_ship}
fired_board = Board.new(board)
puts "Press enter to play:"
gets
while game_over? == false
player.display(fired_board)
player.prompt
play_turn
end
if game_over?
fired_board.display
endgame_message
end
end
end
class Board
attr_reader :empty
attr_accessor :grid
def initialize(grid=self.class.default_grid)
@grid = grid unless block_given?
@grid = yield if block_given?
end
def [](pos)
grid[pos[0]][pos[1]]
end
def self.default_grid
Array.new(10) {Array.new(10)}
end
def count
grid.flatten.count(:s)
end
def empty?(pos=nil)
if pos != nil
grid[pos[0]][pos[1]] == nil
elsif grid.flatten.compact.select{|x| x == :s}.length == 0
true
else
false
end
end
def full?
grid.flatten.compact.length == grid.flatten.length
end
def place_random_ship
empty_locations = []
grid.each_with_index do |row, i|
row.each_with_index do |column, j|
empty_locations << [i, j] if grid[i][j].nil?
end
end
pos = empty_locations.sample
@grid[pos[0]][pos[1]] = :s
end
def won?
empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment