Home>
I'm making a janken program with multiple people in Ruby.
As a function to be realized,
・ Janken with 3 or more players (up to 100 players).
・ The number of people can be decided freely. (Variable length)
・ Janken is performed 100 times with the number of people, and the result (why and how many wins or how many times it is divided) is output.
,
Therefore, the following error message occurred while implementing the second function.
janken.rb: 22: in `block (2 levels) in<main>': undefined method` []' for nil: NilClass (NoMethodError)
Applicable source code
puts "Start janken"
count_rock = 0
count_scissors = 0
count_paper = 0
count_r_w = 0
count_s_w = 0
count_p_w = 0
count_draw = 0
player = [] [3..100]
for i in 0..99 do
puts "Please tell me how many people will play"
num = gets.to_i
for j in 1..num do
player [i] [j] = rand (0..999)% 3
if player [i] [j] == 0
count_rock + = 1
elsif player [i] [j] == 1
count_scissors + = 1
else
count_paper + = 1
end
#puts "# {plyer [i] [j]}"
if j == num
if count_rock == num
count_draw + = 1
elsif count_scissors == num
count_draw + = 1
elsif count_paper == num
count_draw + = 1
elsif count_rock == 0&&count_scissors! = num
count_s_w + = 1
elsif count_scissors == 0&&count_paper! = num
count_p_w + = 1
elsif count_paper == 0&&count_rock! = num
count_r_w + = 1
else
count_draw + = 1
end
count_rock = 0
count_scissors = 0
count_paper = 0
puts ("# {j} people goo choki par draw")
puts ("wins # {count_rock} times # {count_scissors} times # {count_paper} times # {count_draw} times")
puts ("________________________________________________________________________")
end
end
end
I didn't know much about the declaration of a two-dimensional array and I looked into it, but the results remain unclear.
Supplemental information (FW/tool version etc.)ruby 2.6.3p62 (2019-04-16 revision 67580) [x64-mingw32]
-
Answer # 1
Related articles
- daemonizing a ruby program
- ruby - i want to save multiple records in an intermediate table in one process
- ruby sort multiple conditions?
- ruby - rails i want to search by multiple keywords
- ruby - i want to receive and search with multiple params
- ruby - i want to perform standard output with multiple lines and write/read multiple files
- ruby on rails multiple keyword search i don't know how to write
- ruby - when registering multiple images with rails, i would like to know the specific reason for managing the models separately
- ruby - rails json i want to get multiple resources at the same time
- ruby - i want to set multiple processes that are not registered when the conditions are met in validates
- ruby on rails 6 - if you use multiple link_to in rails6 helper, only the last one will be reflected
- ruby - [rails] i want to register data to multiple models with one form_with
- ruby on rails - i want to realize multi-level categories
- ruby - batch action for multiple selected data [rails]
- ruby - i want to write a program that averages a certain range of a certain array and adds it so that it exceeds the average if
- ruby - i want to register multiple records of the same model at the same time
- ruby - i want to execute what is assigned to a variable as a program
- ruby on rails - extract multiple specific columns from one activerecord and make them json
- ruby - i want to know how to break a line in a program that creates a calendar
- ruby - how to implement the edit/update function of multiple tables when using form object in rails
Related questions
- ruby - find out if there are any pairs in the array that add up to 0
- creating a two-dimensional array using malloc in c language
- ruby: update an element in an array that has a hash as an element
- i want to display an associative array as a table in php
- javascript - i want to create a new array by extracting the value from another array using the value of the array with index spe
- ruby on rails uninitialized constant error
- about passing ruby arguments (arrays)
- in python, i want to extract an array from a number reference
- i want to output all ruby on rails flash messages
The 20th line is difficult to understand, but maybe
player [i] [j] = rand (0..999)% 3
? Player initialization is inadequate and player [i] reaches nil, and this error occurs.1. If player [i] = Ary.new within the range of .num, will it pass?