Matrix type?

(Jonas Pfenniger) #1

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 :stuck_out_tongue:

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
----
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.. :slight_smile:
/me is refraining to flood ruby-talk

--
Cheers,
聽聽zimba

http://zimba.oree.ch

(Brian Schr枚der) #2

You should start with a board class. Something like this

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

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

聽聽def []=(x,y,v)
聽聽聽聽@fields[y][x] = 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

路路路

On 10/08/05, zimba-tm <zimba.tm@gmail.com> wrote:

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 :stuck_out_tongue:

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
----
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.. :slight_smile:
/me is refraining to flood ruby-talk

--
Cheers,
聽聽zimba

http://zimba.oree.ch

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

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

(Robert) #3

zimba-tm wrote:

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 :stuck_out_tongue:

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.

----
If I "puts board", I'd like to have a 2D view instead.
How does puts work, does it call the to_str method ?

No, it's #to_s:

o=Object.new

=> #<Object:0x10180648>

puts o

#<Object:0x10180648>
=> nil

def o.to_s() "foo" end

=> nil

puts o

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.. :slight_smile:
/me is refraining to flood ruby-talk

Keep coming!

Kind regards

聽聽聽聽robert

(Berger, Daniel) #4

zimba-tm wrote:

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 :stuck_out_tongue:

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

(James Edward Gray II) #5

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

路路路

On Aug 10, 2005, at 9:56 AM, zimba-tm wrote:

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

(Tom Copeland) #6

You might be interested in this Sudoku game:

http://rubyforge.org/projects/sudoku/

Yours,

Tom

路路路

On Wed, 2005-08-10 at 23:56 +0900, zimba-tm wrote:

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 :stuck_out_tongue:

(Josef 'Jupp' SCHUGT) #7

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

路路路

At Wed, 10 Aug 2005 23:56:40 +0900, zimba-tm wrote:
--
Terrorism is the systematic use of violence and brutality as a means of
gaining some political end. So the "War on Terrorism" is a failure if
it increases violence and brutality and even may be terrorism by itself
if some well-known incidents turn out not to be singular ones. :expressionless:

(Leslie Viljoen) #8

Tom Copeland wrote:

路路路

On Wed, 2005-08-10 at 23:56 +0900, zimba-tm wrote:

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 :stuck_out_tongue:
聽聽聽
You might be interested in this Sudoku game:

http://rubyforge.org/projects/sudoku/

Yours,

Tom

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.

--
ruby -e "puts 'Just another fickle programmer'"

Leslie Viljoen [leslie@camary.co.za]
Camary Consulting [http://www.camary.co.za]
Cellphone [083-6186100]
Personal web [http://mobeus.homelinux.org]

(James Edward Gray II) #9

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

James

路路路

On Aug 12, 2005, at 7:44 AM, Leslie Viljoen wrote:

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.