Computer Science Canada 2D Arrays |
Author: | Cervantes [ Fri Aug 05, 2005 7:19 pm ] | ||
Post subject: | 2D Arrays | ||
I'm having difficulty working with 2D arrays in Ruby.
Would someone kindly shed some light on what is going on here, and how to use 2D arrays properly? Thanks |
Author: | wtd [ Fri Aug 05, 2005 7:38 pm ] | ||||
Post subject: | |||||
There's no such thing as a two dimensional array in Ruby. An array can hold any kind of object, including other arrays, so you just have an array of arrays.
I think that does what you want. Then to access, (3, 2):
|
Author: | Cervantes [ Fri Aug 05, 2005 7:44 pm ] |
Post subject: | |
Intriguing Thanks wtd! That works just fine. |
Author: | wtd [ Fri Aug 05, 2005 10:34 pm ] |
Post subject: | |
Embrace the power of blocks. |
Author: | wtd [ Fri Aug 05, 2005 10:40 pm ] | ||
Post subject: | |||
Or in "I hate whitespace" mode...
|
Author: | Cervantes [ Sat Aug 06, 2005 7:01 am ] | ||||||||
Post subject: | |||||||||
Using my newfound knowledge of the make-shift 2D array, I tried to convert the N-Queen's solver into Ruby. I failed:
When run, this program says: Quote: n_queens.rb:52in 'solve': stack level too deep (SystemStackError) Then if goes through "from this method, from that method" a lot of times, then it says 1038 levels later, and lists the last three methods that it traced. I can't understand why this isn't working, especially since I've got @N set to a measily 3! A 3x3 grid should be very simple to solve. I've lifted this code from zylum's NQueen's solver in Turing, which does work. Here's his code:
Also, are there any other ways I can shorten/simplify my program? I was wondering about this bit:
Since if statements can return a value, I was looking to do something like this:
But that is printing "nil" instead of 1's and 0's. Once again, thanks in advance! |
Author: | zylum [ Sat Aug 06, 2005 7:21 am ] | ||||
Post subject: | |||||
one mistake i see:
you should not call solve(col + 1) unless you have successfully placed a peice in the current column. it should be:
also, there are no solutions for 3x3 board, so that may cause some issues as well although i doubt it. |
Author: | Cervantes [ Sat Aug 06, 2005 10:47 am ] | ||||||
Post subject: | |||||||
Thanks for pointing that out zylum. Unfortunately, I've still got some problems, though that fixes the stack overflow. The next error wrote: n_queens.rb:33:in 'clearSpot': undefined method '[]' for nil:NilClass (NoMethodError) Then it lists about 20 levels of methods. I don't understand where the nil is coming in. If I have an array
then calling
yields nil. But I don't see why I should be encountering nil, because my checks should be within the bounds of the array. Right now I've switched from using "and" to "&&". Is "&&" similar to as it is in Java? That is, if an if statement is set up like this
and condition1 fails, will condition2 even be checked? I hope not. Thoughts? Thanks once again. |
Author: | wtd [ Sat Aug 06, 2005 2:30 pm ] |
Post subject: | |
Ruby's arrays are indexed starting at zero, not one. |
Author: | Cervantes [ Sat Aug 06, 2005 3:48 pm ] |
Post subject: | |
I know. That's why you see a bunch of minus one after @N in my code. That's also why I start the solve at column 0, and why I check if x - i or y - i is >= 0, not 1. Aah, I just discovered I forgot to subtract one from @N in my check for victory. That doesn't make any difference though. That just changes the number of solutions found, which, so far, has been none. |
Author: | wtd [ Sat Aug 06, 2005 4:25 pm ] |
Post subject: | |
Can you please post your current code, exactly as you're using it? |
Author: | Cervantes [ Sat Aug 06, 2005 5:04 pm ] | ||
Post subject: | |||
Sure.
|
Author: | wtd [ Sat Aug 06, 2005 5:39 pm ] | ||
Post subject: | |||
Something's happening here. I don't think your bounds checking is sufficient, though my head hurts too much right now to rewrite it. |
Author: | Cervantes [ Sat Aug 06, 2005 7:07 pm ] |
Post subject: | |
I just did a check and the && works as in Java. If the first condition is false then the second condition will not be checked. Since this rules the possibility of checking outside the grid's bounds, I'm stunped as well. |
Author: | wtd [ Sat Aug 06, 2005 7:27 pm ] | ||
Post subject: | |||
Are you certain? In each of those checks it looks like you're only checking one boundary. For instance:
Only checks the lower boundary. |
Author: | Cervantes [ Sun Aug 07, 2005 6:11 am ] | ||||||
Post subject: | |||||||
That shouldn't be a problem, so long as the position I pass in as (x,y), which is (col, i), is on the grid. I think it is. i must be between 0 and 7, and col must also be between 0 and 7 (if it were bigger than 7, the method returns 0 [Actually, that line (49) should be changed to
instead of
This change does not make any difference though, in that I'm still getting the same errors message]). Hooah! I just fixed it. Turns out it I needed to add or subtract one from i inside my clearSpot method. Say I was on the bottom left corner of the board. I need to check the far right corner. The counter, i, goes from 0 to 7. x and y are both 0. The furthest along the board I can get is (N - 1, N - 1). I can understand why a thing like this would prevent the program from giving the correct output, but I'm baffled as to why it would crash the program. Must be something that occurs later on, after a piece has been placed in a spot that it shouldn't be. I've also optimized the clearSpot procedure a little bit by taking out the checks for pieces on the right side of the piece I'm placing. We know everything on that side of the grid is empty, because we haven't got that far yet (or we've recursed back, in which case the grid spot is made false each time we recurse back). I've also discovered that it makes no difference what I do with the following bit of code:
I can comment it out and it will still work fine. It doesn't seem to speed the program any, either. Here's the working code. I'd like to know how I could shorten it, however. Any thoughts there? Thanks in advance. I had to change the extension of the file to .txt because .rb files are not allowed. Tony or Dan, please allow us to upload .rb files. |
Author: | zylum [ Sun Aug 07, 2005 4:31 pm ] | ||
Post subject: | |||
Cervantes wrote: I've also discovered that it makes no difference what I do with the following bit of code:
I can comment it out and it will still work fine. It doesn't seem to speed the program any, either. Here's the working code. I'd like to know how I could shorten it, however. Any thoughts there? Thanks in advance. I had to change the extension of the file to .txt because .rb files are not allowed. Tony or Dan, please allow us to upload .rb files. lol that is strange. that means there is no place in you code preveting it from calling solve(N)... but i guess that doesnt matter because your program prevents any peices from being placed off the boad . im guessing that will make you program a little less efficient with larger Ns. |
Author: | wtd [ Wed Aug 10, 2005 7:59 pm ] | ||
Post subject: | |||
Perhaps my code, that does work could help.
|