Logic


#1

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.


#2

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


#3

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:


#4

StarLion wrote:

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


#5

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.).


#6

On 12/10/05, StarLion removed_email_address@domain.invalid wrote:

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")


#7

On 12/10/05, StarLion removed_email_address@domain.invalid <hmrhouse@hotmail.
removed_email_address@domain.invalid 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


#8

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.


#9

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
removed_email_address@domain.invalid
<hmrhouse@hotmail.:


#10

On 12/10/05, Michael F. removed_email_address@domain.invalid 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")


#11

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

Nevermind… I just cant count how many characters i put on one line.
-_-


#12

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.


#13

On Dec 10, 2005, at 3:52 PM, StarLion wrote:

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

Nevermind… I just cant count how many characters i put on one line.

Which is probably a sign that input error checking is needed:

input =~ /^\d{9}$/

James Edward G. II