Logic...

S

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,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.
 
M

Marcin Mielżyński

StarLion said:
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
 
M

Michael Fellinger

--nextPart2406237.PquL2S5S0o
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Ok, i made a bit more cleaned up version, but i'm still not sure what you w=
ant=20
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 w=
ay=20
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 =3D Array.new(9){[]}
board.size.times do |i|
instr =3D gets
board =3D instr.split(//).map{|c| c.to_i}
puts board
end
return board
end

Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:
def MakeBoard
rcount =3D 0
puts "Push enter, then begin input. (For Input Format Compliance, extra
enter key is necessary)"
gets
board =3D [[],[],[],[],[],[],[],[],[]]
9.times do
instr =3D gets
board[rcount] =3D
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,in= st
r[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i] rcount +=3D 1
puts board[rcount]
end #do
return board
end #MakeBoard

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

--nextPart2406237.PquL2S5S0o
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDmxMSMdQeL6eBxhIRAsPxAKDd4Nq6TLXdiLYFsCIMQ/JRZ07tjQCfdjuw
XCuSMweM6nq8V9jnSoX/4hU=
=cQkd
-----END PGP SIGNATURE-----

--nextPart2406237.PquL2S5S0o--
 
D

Daniel Schierbeck

StarLion said:
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 =
[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 # the last item in `board'
end
return board
end

Cheers,
Daniel
 
G

Gregory Brown

def MakeBoard
rcount =3D 0
puts "Push enter, then begin input. (For Input Format Compliance, extra
enter key is necessary)"
gets
board =3D [[],[],[],[],[],[],[],[],[]]
9.times do
instr =3D gets
board[rcount] =3D
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,in= str[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
rcount +=3D 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 =3D []
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")
 
S

StarLion

m.fellinger said:
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 = instr.split(//).map{|c| c.to_i}
puts board
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.).
 
S

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

Obviously i meant 9 strings of 9 characters.
 
G

Gregory Brown

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
 
M

Michael Fellinger

--nextPart1683663.D7JR0Hymbj
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

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

Am Samstag, 10. Dezember 2005 18:56 schrieb StarLion <[email protected]>=
=20
<hmrhouse@hotmail.:
Obviously i meant 9 strings of 9 characters.

--nextPart1683663.D7JR0Hymbj
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDmxjvMdQeL6eBxhIRAoYgAJ43n1oksn03Yjlg5y5ZXdp1hR293QCcC5q/
PMLVy09NJ2zPErgQ6GEs4Z8=
=JPbe
-----END PGP SIGNATURE-----

--nextPart1683663.D7JR0Hymbj--
 
G

Gregory Brown

i see.. it wasn't clear at first what the kind of data is that is the inp= ut
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 =3D []
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")
 
S

StarLion

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 = instr.split(//).map{|c| c.to_i}
puts board.inspect
end
return board
end


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

James Edward Gray II

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 Gray II
 

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

Similar Threads

Unspecified Syntax Error 6
Why do I keep getting "nil"? 2
Roman Numerals (Arrgh!) 21
Method error 0
help with code, new to programming 12
Help in this program. 2
[QUIZ SOLUTION] #88 Chip-8 4
regexp+hash problem 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top