How to make a multidimensional matrix read data entered by the user?

Asked

Viewed 884 times

2

F1=[['1','2','3'],['4','5','6'],['7','8','9']]
print "\nJogador 1: Escolha uma linha e uma coluna no tabuleiro:\n"
L=gets.chomp
C=gets.chomp
F1[L][C]='X'

The idea here is to build an old game.

1 answer

0


Hello, I tested your code, only one to_i was missing to convert the entire input, I do not know if this was your difficulty. I made some modifications to your code.

tabuleiro = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

def show(tabuleiro)
  puts "\nTABULEIRO\n\n"
  tabuleiro.each do |r|
    puts r.each { |p| p }.join(' ')
  end
end

loop do
  puts "\nJogador 1: Escolha uma linha do tabuleiro:"
  linha = gets.chomp.to_i
  puts "\nJogador 1: Escolha uma coluna do tabuleiro:"
  coluna = gets.chomp.to_i
  tabuleiro[linha][coluna] = 'X'

  show(tabuleiro)
end

Upshot:

Player 1: Pick a Line from the Board: 0

Player 1: Choose a Column from the Board: 0

TRAY

X 2 3

4 X 6

7 8 X

  • 1

    Yes it was was the difficulty, I ended up finding the solution on an English site, but thank you for your reply!

  • @Wandaymo if the answer has been satisfactory, please accept as correct answer. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.