Matrix type ?

Z

zimba-tm

Hello ruby fellows,

Maybe you can help me.

I'm currently trying to make a Sudoku solver for my first ruby program.
Sudoku is a nice 9x9 board game with simple rules, but it can be tough
to solve. More infos at http://www.sudoku.com/ if you want to launch a
RubyQuizz :p

What I would do, is use a kind of 2D Matrix that would represent the
board but couldn't find a satisfying method.

This is my current implementation using arrays :=20
----
board =3D Array.new
=20
(1..9).each do |row_num|
board[row_num] =3D Array.new
(1..9).each do |col_num|
board[row_num][col_num] =3D "X"
end
end
----
If I "puts board", I'd like to have a 2D view instead.
How does puts work, does it call the to_str method ?

Also, I'd like to access the console as a 2D Array too,
so that I can write on screen directly like a text-framebuffer.

I have so many questions.. :)
/me is refraining to flood ruby-talk

--=20
Cheers,
zimba

http://zimba.oree.ch
 
R

Robert Klemme

zimba-tm said:
Hello ruby fellows,

Maybe you can help me.

I'm currently trying to make a Sudoku solver for my first ruby
program. Sudoku is a nice 9x9 board game with simple rules, but it
can be tough to solve. More infos at http://www.sudoku.com/ if you
want to launch a RubyQuizz :p

What I would do, is use a kind of 2D Matrix that would represent the
board but couldn't find a satisfying method.

This is my current implementation using arrays :
----
board = Array.new

(1..9).each do |row_num|
board[row_num] = Array.new
(1..9).each do |col_num|
board[row_num][col_num] = "X"
end
end

That can be simplified to

board = Array.new(9) { Array.new(9) { "X" } }

In your case I'd create a customer class that does this internally and
provides access methods to cells. This class then also can check the
validity of moves etc.

class Board
def initialize(size=9)
@board = Array.new(size) { Array.new(size) { "X" } }
end

def move(from_x, from_y, to_x, to_y)
raise "Invalid move" if ...
# do the move
...
end

def to_s
# generate reasonable string representation
...
end
end

You might also need / want classes for figures if they behave differently
etc.

No, it's #to_s:
foo
=> nil
Also, I'd like to access the console as a 2D Array too,
so that I can write on screen directly like a text-framebuffer.

Hm, I think there is (n)curses support somewhere. You can check with the
RAA
http://raa.ruby-lang.org/
I have so many questions.. :)
/me is refraining to flood ruby-talk

Keep coming!

Kind regards

robert
 
B

Brian Schröder

Hello ruby fellows,
=20
Maybe you can help me.
=20
I'm currently trying to make a Sudoku solver for my first ruby program.
Sudoku is a nice 9x9 board game with simple rules, but it can be tough
to solve. More infos at http://www.sudoku.com/ if you want to launch a
RubyQuizz :p
=20
What I would do, is use a kind of 2D Matrix that would represent the
board but couldn't find a satisfying method.
=20
This is my current implementation using arrays :
----
board =3D Array.new
=20
(1..9).each do |row_num|
board[row_num] =3D Array.new
(1..9).each do |col_num|
board[row_num][col_num] =3D "X"
end
end
----
If I "puts board", I'd like to have a 2D view instead.
How does puts work, does it call the to_str method ?
=20
Also, I'd like to access the console as a 2D Array too,
so that I can write on screen directly like a text-framebuffer.
=20
I have so many questions.. :)
/me is refraining to flood ruby-talk
=20
--
Cheers,
zimba
=20
http://zimba.oree.ch
=20
=20

You should start with a board class. Something like this

class Board
def initialize
@fields =3D Array.new(9) { Array.new(9) { :empty } }
end

def [](x,y)
@fields[y][x]
end

def []=3D(x,y,v)
@fields[y][x] =3D v
end

def to_s
@fields.map{|row| row.map{|cell| cell.ljust(10) }.join(' ')}.join("\n")
end
end

and go on from here.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
D

Daniel Berger

zimba-tm said:
Hello ruby fellows,

Maybe you can help me.

I'm currently trying to make a Sudoku solver for my first ruby program.
Sudoku is a nice 9x9 board game with simple rules, but it can be tough
to solve. More infos at http://www.sudoku.com/ if you want to launch a
RubyQuizz :p

What I would do, is use a kind of 2D Matrix that would represent the
board but couldn't find a satisfying method.

require "matrix"

Part of the stdlib.

Regards,

Dan
 
J

James Edward Gray II

More infos at http://www.sudoku.com/ if you want to launch a
RubyQuizz :p

This is the second time I've seen this requested, so I just wrote it
up. I already have a quiz for next week, but it will pop-up after that.

Thanks for the idea.

James Edward Gray II
 
L

Leslie Viljoen

J

Josef 'Jupp' SCHUGT

Hi!
I'm currently trying to make a Sudoku solver for my first ruby
program. Sudoku is a nice 9x9 board game with simple rules, but it
can be tough to solve.

Sudoku seem to be pandemic. I started sudokuing today using a booklet
by German "Zeit" and "Handelsblatt". After spending about two hours I
have solved 25 sudokus with 48 given entries and started to find them
boring. I wonder if I should skip the remaining 25 and continue with
sudokus with 32 given entries :->

Up to now I didn't even bother taking a look at the hints on solving
this kind of puzzle which are provided in the booklet as well.

Josef 'Jupp' SCHUGT
 
J

James Edward Gray II

Interesting - I wrote a program to generate Sodoku boards
as one of my first Ruby programs. Very quick and easy -
which really testifies to the power of Ruby.

Any chance I could talk you into posting that as a reply to the Ruby
Quiz message next week?

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top