Created
September 2, 2013 03:53
-
-
Save SunnyIzr/6409090 to your computer and use it in GitHub Desktop.
Revisions
-
SunnyIzr created this gist
Sep 2, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,218 @@ class Computer_Board attr_reader :board def initialize @ships = [%w[A A A A A], %w[B B B B], %w[C C C], %w[D D], %w[D D], %w[S S], %w[S S]] @board = Array.new(10) {Array.new(10,'/')} @mvmt = [ [-1, 0], #top [0, 1], #right [1, 0], #bttm [0, -1]] #left @mvmt_words = %w[top right bttm left] end def create_board @board.clone end def format_board(board) board.each_with_index do |x,index| puts x.join(' ') end return end def on_board?(coord) coord[0] >= 0 && coord[1] >= 0 && coord[0]<=9 && coord[1]<=9 end def is_water?(coord) @computer_ship_board[coord[0]][coord[1]] == '/' end def determine_direction(coord,ship_size) p "running determine_direction on random point #{coord}" rand_pt_row = coord[0] rand_pt_col = coord[1] mvmt_clone = @mvmt.dup 4.times do |directional| possible_dir = mvmt_clone.delete(mvmt_clone.sample) dir_row = possible_dir[0] dir_col = possible_dir[1] adj_row = dir_row + rand_pt_row adj_col = dir_col + rand_pt_col if on_board?([adj_row,adj_col]) && is_water?([adj_row,adj_col]) && check_successive_adjs([rand_pt_row,rand_pt_col],[dir_row,dir_col],ship_size).length == 2 return check_successive_adjs([rand_pt_row,rand_pt_col],[dir_row,dir_col],ship_size) end end return [] end def check_successive_adjs(orig_coord,mvmt,ship_size) puts ' ' puts "Running check_successive_cells_empty orig_coord=#{orig_coord} mvmt=#{mvmt}, ship_size=#{ship_size}" checking_row = orig_coord[0]+(mvmt[0]*(ship_size-1)) #checking the last point in the ship that HASNT been checked checking_col = orig_coord[1]+(mvmt[1]*(ship_size-1)) if (ship_size-2) == 0 p "We've reached the second point (on the ship) which has already been checked (in previous method, first point was also checked in previous method). END check_successive_cells_empty RECURSION: Successful" return mvmt end if on_board?([checking_row,checking_col]) == false p "coordinate (#{orig_coord[0]}, #{orig_coord[1]}) is out of scope" return [] end next_cell_coord = [checking_row,checking_col] if is_water?([checking_row,checking_col]) p "Cell #{checking_row}, #{checking_col} which has a value of #{@computer_ship_board[checking_row][checking_col]} has been checked and it is EMPTY" ship_size = ship_size-1 check_successive_adjs(orig_coord,mvmt,ship_size) else p "Cell #{next_cell_coord[0]}, #{next_cell_coord[1]} which has a value of #{@computer_ship_board[checking_row][checking_col]} has been checked and it is FILLED" return [] end end def find_rand_ship_coord(ship_size) p "Trying to find random space..." rand_pt_row = rand(10) rand_pt_col = rand(10) directional = [] p "Random Space Found! Checking to see if the random space is water. If it is water will return directional" if is_water?([rand_pt_row,rand_pt_col]) directional = determine_direction([rand_pt_row,rand_pt_col],ship_size) end p "Checking to see if space (#{rand_pt_row},#{rand_pt_col}) works for our ship!" if directional.length == 2 p "It works! Random point #{rand_pt_row},#{rand_pt_col} going direction #{directional} will work. Now go place the ship!" return [[rand_pt_row,rand_pt_col],directional] else find_rand_ship_coord(ship_size) end end def populate_ship(ship, rand_pt, directional) (ship.length).times do |ship_pt| ship_pt_row = rand_pt[0] + directional[0]*ship_pt ship_pt_col = rand_pt[1] + directional[1]*ship_pt @computer_ship_board[ship_pt_row][ship_pt_col] = ship[ship_pt] end end def populate_computer_board p "...starting populate computer board" @computer_ship_board = create_board p @computer_ship_board p "Starting ship code block" @ships.each do |ship| p rand_pt_and_dir = find_rand_ship_coord(ship.length) #returns starting point and directional rand_pt = rand_pt_and_dir[0] directional = rand_pt_and_dir[1] populate_ship(ship, rand_pt, directional) end format_board(@computer_ship_board) end end game = Computer_Board.new game.populate_computer_board #KNOWN BUGS: Fails randomness test - 35% likely it will go left when it should be 25% #---------------------------------------------- # #EMPTY SPACE TEST # #Checks to make sure it finds ONLY the empty space where the ship ship_size can fit based on direction and coord # #DIRECTIONAL RANDOMNESS TEST # #Checks to ensure that the direction the ship goes in is truly random by running 10000 times and seeing percent breakdown # array = [] # 10000.times {array << game.populate_computer_board} # array_of_dirs = [] # p array # p '' # array.each_with_index do |x,index| # array_of_dirs << x[1] # end # p '' # p '' # puts "Top Dir (-1,0): #{array_of_dirs.count([-1,0])} times" # puts "Bottom Dir (+1,0): #{array_of_dirs.count([1,0])} times" # puts "Right Dir (0,+1): #{array_of_dirs.count([0,1])} times" # puts "Left Dir (0,-1): #{array_of_dirs.count([0,-1])} times" # puts "Total Check is #{array_of_dirs.count([-1,0])+array_of_dirs.count([1,0])+array_of_dirs.count([0,1])+array_of_dirs.count([0,-1])}" #---------------------------------------------- # #DIRECTIONAL RANDOMNESS TEST 2 # array_of_dirs = [] # p '' # 10000.times do |x,index| # array_of_dirs << game.determine_direction([rand(9),rand(9)],6) # end # p '' # p '' # puts "Top Dir (-1,0): #{array_of_dirs.count([-1,0])} times" # puts "Bottom Dir (+1,0): #{array_of_dirs.count([1,0])} times" # puts "Right Dir (0,+1): #{array_of_dirs.count([0,1])} times" # puts "Left Dir (0,-1): #{array_of_dirs.count([0,-1])} times" # puts "Total Check is #{array_of_dirs.count([-1,0])+array_of_dirs.count([1,0])+array_of_dirs.count([0,1])+array_of_dirs.count([0,-1])}" # end # game = Battleship.new # p game.populate_computer_board #Known BUGS: Edges, Overlap of ships