3 Replies - 507 Views - Last Post: 09 June 2017 - 11:23 PM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Integrating Do Not Repeat process for my code

Posted 08 June 2017 - 07:05 PM

Hello, I'm still new to Ruby and so I end up doing stuff the very long way and printing out the same code many times, instead of utilizing methods. I have this code that repeats it self a lot, it is a word search algorithm that checks the "boundaries" of the code. I am trying to figure out how to create a method that will store it in an inbounds method but it also changes at certain parts but it is not a drastic change. Here is the first direction.

  def check_word(rand_row, rand_col, word, direction)
    placeable = true
    if direction == 0
      l = 0
      while l < word.length
        if rand_row - l < @size && rand_row - l >= 0 && rand_col < @size && rand_col >= 0
          if @puzzle_array[rand_row - l][rand_col] != word[l] && @puzzle_array[rand_row - l][rand_col] != '.' # North
            placeable = false
          end
        else
          placeable = false
        end
        l += 1
      end



Here is the second direction, I have 8 total directions that check boundaries

 
 elsif direction == 1
      l = 0

      while l < word.length
        if rand_row - l < @size && rand_row - l  >= 0 && rand_col + l < @size && rand_col + l >= 0
          if @puzzle_array[rand_row - l][rand_col + l] != word[l] && @puzzle_array[rand_row - l][rand_col + l] != '.' # NorthEast
            placeable = false
          end
        else
          placeable = false
        end
        l += 1
      end



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Integrating Do Not Repeat process for my code

#2 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Integrating Do Not Repeat process for my code

Posted 08 June 2017 - 08:46 PM

Never mind I got it figure out, it was easy, thanks though
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4083
  • View blog
  • Posts: 18,159
  • Joined: 24-September 08

Re: Integrating Do Not Repeat process for my code

Posted 09 June 2017 - 08:39 AM

Could you post the solution so others can solve their problems too? Sorry I didn't see this in time.
Was This Post Helpful? 0
  • +
  • -

#4 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Integrating Do Not Repeat process for my code

Posted 09 June 2017 - 11:23 PM

Sorry took so long but all i did was created a simple method like:

def inbounds(rand_row, rand_col)
    return (rand_row >= 0 && rand_row < @size) && (rand_col >= 0 && rand_col < @size)
  end



and then I passed it through like....

 if (inbounds(rand_row - l, rand_col - l))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1