public
Last active

RPCFN 7 - incomplete entry

  • Download Gist
rpcfn7.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# 3) add properties for each square, with a default value if one doesn't exist.
 
#TODO:
# 2) start to play with "enemy_presence" (rating) to like squares where we found ships often
# 3) Write modifications to the YAML file
# 4) When a square got hit, then we MUST queue up other nearby squares to find the rest of the ship(s).
require 'yaml'
require 'fileutils'
 
class AGBroadside
attr_reader :my_board, :enemy_board, :board_size, :config, :myconfig
 
def initialize
#$stdout.sync = true
@file = __FILE__.split('/')[-1].split('.')[0]
@file << ".yaml"
@config = nil
@board_size = ""
end
 
def board_size size
h, v = size.split('x').map{ |i| i.to_i }
# Since I am using letters for one direction, I am keeping it simple.
raise(ArgumentError, "Board too big!") if h > 26 || v > 26
@board_size = size
FileUtils.touch @file
@config = YAML.load_file(@file) || {}
@myconfig = @config[size]
if @myconfig.nil?
# Okay, time to generate default information for this board size.
letters = ('A'..'Z').to_a[0...h]
numbers = ('1'..'26').to_a[0...v]
@squares = numbers.map do |y|
letters.map do |x|
"#{x}#{y}"
end
end.flatten
@my_board = {}
@enemy_board = {}
@squares.each do |k|
@my_board[k] = {:safety => 0.0}
@enemy_board[k] = {:enemy_presence => 0.0}
end
@myconfig = {:my_board => @my_board, :enemy_board => @enemy_board }
end
end
 
def set_and_place_ships ship_list
# We receive something like "5,4,3,3,2" and we want [5,4,3,3,2].
ship_list = ship_list.split(',').map{ |x| x.to_i }
@ship_count = ship_list.size
direction = ['H', 'V']
favorite_squares = @my_board.keys.sort_by {|k| -@my_board[k][:safety] }
ship_list.each do
end
end
def save
@squares.each do |x|
@my_board[x] = {:safety => @my_board[x][:safety] }
@enemy_board[x] = {:enemy_presence => @enemy_board[x][:enemy_presence] }
end
@config[@board_size] = {:my_board => @my_board, :enemy_board => @enemy_board }
# The previous line may need to be expanded to actually store changed data.
File.open(@file, 'w') { |f| f << YAML.dump(@config) }
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.