[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Logic...

StarLion

12/10/2005 5:26:00 PM

def MakeBoard
rcount = 0
puts "Push enter, then begin input. (For Input Format Compliance, extra
enter key is necessary)"
gets
board = [[],[],[],[],[],[],[],[],[]]
9.times do
instr = gets
board[rcount] =
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
rcount += 1
puts board[rcount]
end #do
return board
end #MakeBoard

Is my logic flawed here? the puts doesnt actually output anything.

--
Posted via http://www.ruby-....


12 Answers

Marcin Mielzynski

12/10/2005 5:35:00 PM

0

StarLion wrote:
>
> Is my logic flawed here? the puts doesnt actually output anything.
>

It is supposed not to print anything since nothing is called here...

just call the MakeBoard method

lopex

Michael Fellinger

12/10/2005 5:41:00 PM

0

Ok, i made a bit more cleaned up version, but i'm still not sure what you want
to do with your method.
especially, what does this .to_i mean?

However, for everything you write twice or more often, ruby usually has a way
to make it at the same time cleaner and easier to write.


def make_board
puts "Push enter, then begin input."
puts "(For Input Format Compilance, extra enter key is necessary)"
gets
board = Array.new(9){[]}
board.size.times do |i|
instr = gets
board[i] = instr.split(//).map{|c| c.to_i}
puts board[i]
end
return board
end

Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:
> def MakeBoard
> rcount = 0
> puts "Push enter, then begin input. (For Input Format Compliance, extra
> enter key is necessary)"
> gets
> board = [[],[],[],[],[],[],[],[],[]]
> 9.times do
> instr = gets
> board[rcount] =
> [instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,inst
>r[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i] rcount += 1
> puts board[rcount]
> end #do
> return board
> end #MakeBoard
>
> Is my logic flawed here? the puts doesnt actually output anything.

Daniel Schierbeck

12/10/2005 5:41:00 PM

0

StarLion wrote:
> def MakeBoard
> rcount = 0
> puts "Push enter, then begin input. (For Input Format Compliance, extra
> enter key is necessary)"
> gets
> board = [[],[],[],[],[],[],[],[],[]]
> 9.times do
> instr = gets
> board[rcount] =
> [instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
> rcount += 1
> puts board[rcount]
> end #do
> return board
> end #MakeBoard
>
> Is my logic flawed here? the puts doesnt actually output anything.

I think it's because when you write `puts board[rcount]', `rcount' is 1
higher than the highest index number in board. Try this instead.

def make_board
puts "Push enter, then begin input. (For Input Format Compliance,
extra enter key is necessary)"
gets
board = [[],[],[],[],[],[],[],[],[]]
9.times do |i|
# you may want to use gets.chomp instead
instr = gets
board[i] =
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
puts board[i] # the last item in `board'
end
return board
end

Cheers,
Daniel

Gregory Brown

12/10/2005 5:49:00 PM

0

On 12/10/05, StarLion <hmrhouse@hotmail.com> wrote:
> def MakeBoard
> rcount = 0
> puts "Push enter, then begin input. (For Input Format Compliance, extra
> enter key is necessary)"
> gets
> board = [[],[],[],[],[],[],[],[],[]]
> 9.times do
> instr = gets
> board[rcount] =
> [instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
> rcount += 1
> puts board[rcount]
> end #do
> return board
> end #MakeBoard
>
> Is my logic flawed here? the puts doesnt actually output anything.

I think this is a little cleaner:

make_board returns a board split by spaces.

i.e. you enter the rows like

1 0 1 0 2 1 8 2 1
1 2 1 1 9 1 9 1 9
...

The method returns the actual array, and I map and join for pretty
printing in the puts.
Tweak as needed, code below.
------------------------------------------------

def make_board
board = []
puts "input rows, seperated by spaces:"
9.times { board << gets.split(" ")[0..8].map { |e| e.to_i } }
return board
end

puts make_board.map { |r| r.join(" ") }.join("\n")


StarLion

12/10/2005 5:56:00 PM

0

m.fellinger wrote:
> Ok, i made a bit more cleaned up version, but i'm still not sure what
> you want
> to do with your method.
> especially, what does this .to_i mean?
>
> However, for everything you write twice or more often, ruby usually has
> a way
> to make it at the same time cleaner and easier to write.
>
>
> def make_board
> puts "Push enter, then begin input."
> puts "(For Input Format Compilance, extra enter key is necessary)"
> gets
> board = Array.new(9){[]}
> board.size.times do |i|
> instr = gets
> board[i] = instr.split(//).map{|c| c.to_i}
> puts board[i]
> end
> return board
> end
>
> Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:

The idea is to take a string of 9 characters, and transform them into an
array of integers, which are stored in an array of rows (IE: generate a
9x9 array.).

--
Posted via http://www.ruby-....


StarLion

12/10/2005 5:57:00 PM

0

> The idea is to take a string of 9 characters, and transform them into an
> array of integers, which are stored in an array of rows (IE: generate a
> 9x9 array.).

Obviously i meant 9 strings of 9 characters.

--
Posted via http://www.ruby-....


Gregory Brown

12/10/2005 6:06:00 PM

0

On 12/10/05, StarLion <hmrhouse@hotmail.com> <hmrhouse@hotmail.
<hmrhouse@hotmail.com> wrote:
> > The idea is to take a string of 9 characters, and transform them into an
> > array of integers, which are stored in an array of rows (IE: generate a
> > 9x9 array.).
>
> Obviously i meant 9 strings of 9 characters.

That is what my code does, but it seperates by spaces to make it
easier to see as your typing in... replace the split(" ") with
split(//) and it will do exactly that

rows can then be entered like this: 123456789


Michael Fellinger

12/10/2005 6:06:00 PM

0

i see.. it wasn't clear at first what the kind of data is that is the input
for the program.
However, after testing my little suggestion, i found that there are is a bug
in it - it didn't strip the \n from the line it gets. also i made the
iteration independent from the size of the board and made the output each
line a bit prettier ^^

def make_board
  puts "Push enter, then begin input."
  puts "(For Input Format Compilance, extra enter key is necessary)"
  gets
  board = Array.new(9){[]}
  9.times do |i|
    instr = gets.strip
    board[i] = instr.split(//).map{|c| c.to_i}
    puts board[i].inspect
  end
  return board
end

Am Samstag, 10. Dezember 2005 18:56 schrieb StarLion <hmrhouse@hotmail.com>
<hmrhouse@hotmail.:
> > The idea is to take a string of 9 characters, and transform them into an
> > array of integers, which are stored in an array of rows (IE: generate a
> > 9x9 array.).
>
> Obviously i meant 9 strings of 9 characters.

Gregory Brown

12/10/2005 6:11:00 PM

0

On 12/10/05, Michael Fellinger <m.fellinger@gmail.com> wrote:
> i see.. it wasn't clear at first what the kind of data is that is the input
> for the program.
> However, after testing my little suggestion, i found that there are is a bug
> in it - it didn't strip the \n from the line it gets. also i made the
> iteration independent from the size of the board and made the output each
> line a bit prettier ^^

if you use a range, you don't need to worry about extraneous input
(such as the \n)

def make_board
board = []
puts "input rows, seperated by spaces:"
9.times { board << gets.split(//)[0..8].map { |e| e.to_i } }
return board
end

puts make_board.map { |r| r.join(" ") }.join("\n")


StarLion

12/10/2005 9:51:00 PM

0


> def make_board
> puts "Push enter, then begin input."
> puts "(For Input Format Compilance, extra enter key is necessary)"
> gets
> board = Array.new(9){[]}
> 9.times do |i|
> instr = gets.strip
> board[i] = instr.split(//).map{|c| c.to_i}
> puts board[i].inspect
> end
> return board
> end
>

This code returns a 10 element array, with 0 in index 9.

--
Posted via http://www.ruby-....